From cdb2122257681f2cf33ca95505532d3926116297 Mon Sep 17 00:00:00 2001 From: Martin Langhoff Date: Tue, 24 Mar 2015 15:02:32 -0400 Subject: [PATCH] watch_log: Improve option handling, add --pidfile and --keep parameters, add help --- watch_log.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) mode change 100755 => 100644 watch_log.py diff --git a/watch_log.py b/watch_log.py old mode 100755 new mode 100644 index 438b887..21e94fe --- a/watch_log.py +++ b/watch_log.py @@ -196,8 +196,9 @@ class LogWatcher(object): class Parser: - def __init__(self, fpath): + def __init__(self, fpath, keepdays): self.datestamp = self._getdatestamp() + self.keepdays = keepdays self.conn = sqlite3.connect(fpath) self.cur = self.conn.cursor() self.cur.execute("""CREATE TABLE IF NOT EXISTS logentries @@ -245,7 +246,7 @@ class Parser: # and GC # this will trigger soon after midnight # so we want to preserve the last 24hs - gcepoch = int(time.time()) - (24 * 60 * 60) + gcepoch = int(time.time()) - (24 * 60 * 60 * self.keepdays) self.cur.execute('DELETE FROM logentries WHERE timestamp < ?', list([gcepoch]) ) self.conn.commit() self.cur.execute('VACUUM') @@ -255,17 +256,26 @@ class Parser: import sqlite3 import json +import sys +from optparse import OptionParser if __name__ == '__main__': - import sys - - logdir = sys.argv[1] - sqlfname = sys.argv[2] + parser = OptionParser("usage: %prog [options] /path/to/logdir /path/to/db.sqlite") + parser.add_option('-k', '--keep', dest='keep', action='store', type='int', default=2, + help='Days of data to keep, defaults to 2') + parser.add_option('-p', '--pidfile', dest='pidfile', action='store', type='string', + help='Pidfile, to help service scripts.') + (opts, args) = parser.parse_args() + logdir = args[0] + sqlfname = args[1] if not os.path.isdir(logdir): sys.stderr.write('First argument should be log directory\n') sys.exit(1) - parser = Parser(sqlfname) + if opts.pidfile: + open(opts.pidfile, 'w').write(str(os.getpid())) + + parser = Parser(sqlfname, int(opts.keep)) def callback(filename, lines): for line in lines: parser.parseline(line) -- 2.11.4.GIT