X-Git-Url: http://git.treefish.org/~alex/logalert.git/blobdiff_plain/6e4649c5bae35dc883f50642d81f0b57c6df565b..082f53238d44c1b52dc6a8110ec7dc4c56aa41df:/src/logalert.py?ds=inline diff --git a/src/logalert.py b/src/logalert.py index 509b246..c8eed19 100755 --- a/src/logalert.py +++ b/src/logalert.py @@ -2,62 +2,13 @@ import argparse import logging -import os -import shlex -import subprocess import time -from line import Line +import misc MAX_LINES = 10 ALERT_INTERVAL = 86400 -def follow(filename): - while True: - try: - with open(filename, "r") as f: - logging.info("Re-attached to log file.") - for line in f: pass - while True: - line = f.readline() - if not line: - if not os.path.exists(filename): - break - else: - time.sleep(1.0) - yield None - else: - yield line.rstrip("\n") - except FileNotFoundError: - time.sleep(1.0) - yield None - -def feed_handler(data): - try: - handler = subprocess.Popen(shlex.split(args.handler), - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - encoding='UTF-8') - out_data, err_data = handler.communicate("%s\n" % data) - if handler.returncode != 0: - logging.warning("Handler exited with non-zero return code %d! (%s)" % - (handler.returncode, err_data)) - except Exception as e: - logging.error("Error feeding handler: %s" % str(e)) - -def create_msg(title, icon, logfile, text, lines): - msg = "%s %s %s" % (title, logfile, icon) - msg += "
%s" % text - msg += "
"
-    for line in lines: msg += line + "\n"
-    msg += "
" - return msg - -logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', - level=logging.INFO, - datefmt='%m/%d/%Y %H:%M:%S') - parser = argparse.ArgumentParser(description='Alert on excessive number of error log lines.') parser.add_argument('logfile', type=str, help='logfile to be watched') parser.add_argument('handler', type=str, @@ -66,16 +17,22 @@ parser.add_argument('-s', '--interval-size', type=int, default=600, dest='interv help='sample interval size in seconds (default: 600)') parser.add_argument('-n', '--num-intervals', type=int, default=6, dest='num_intervals', help='number of intervals to keep in history (default: 6)') +parser.add_argument('-l', '--log-level', type=str, default='INFO', dest='log_lvl', + choices=['DEBUG', 'INFO', 'WARNING'], help='select log level') args = parser.parse_args() +logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', + level=logging.getLevelName(args.log_lvl), + datefmt='%m/%d/%Y %H:%M:%S') + kept_times = [] lines = [] last_slot_time = None error_state = False last_alert_time = 0 -for line in follow(args.logfile): +for line in misc.follow_file(args.logfile): time_now = time.time() slot_now = int(time_now) // args.interval_size @@ -100,11 +57,12 @@ for line in follow(args.logfile): if not False in intervals[1:]: if not error_state or time_now - last_alert_time > ALERT_INTERVAL: last_alert_time = time_now - feed_handler( create_msg("Log Alert", - "☠", - args.logfile, - "Number of errors exceeded!", - lines) ) + misc.feed_handler( args.handler, + misc.create_msg("Log Alert", + "☠", + args.logfile, + "Number of errors exceeded!", + lines) ) if not error_state: logging.warning("Entering error state!") error_state = True @@ -113,8 +71,3 @@ for line in follow(args.logfile): if error_state: logging.info("Leaving error state.") error_state = False - feed_handler( create_msg("Log Un-Alert", - "🌞", - args.logfile, - "Log is back to normal.", - lines) )