From 6b34e849bedd62deb8ef4964c399f3e6ebd87bb7 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Tue, 6 Oct 2020 22:50:30 +0200 Subject: [PATCH] major refactoring --- src/logalert.py | 56 +++++++++++++++++++++++++++++++++---------------- src/misc.py | 8 +++++++ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/logalert.py b/src/logalert.py index c8eed19..73e5660 100755 --- a/src/logalert.py +++ b/src/logalert.py @@ -2,13 +2,26 @@ import argparse import logging +import os +import signal import time import misc -MAX_LINES = 10 +LINES_TO_KEEP = 10 ALERT_INTERVAL = 86400 +def handleInterrupt(sig, frame): + global stop + if os.getpid() == mainPid: + logging.info( "Got stop signal." ) + stop = True + +mainPid = os.getpid() +signal.signal(signal.SIGINT, handleInterrupt) + +stop = False + 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, @@ -26,35 +39,39 @@ logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', level=logging.getLevelName(args.log_lvl), datefmt='%m/%d/%Y %H:%M:%S') -kept_times = [] +num_intervals = (args.num_intervals + 1) +intervals = [False] * num_intervals lines = [] -last_slot_time = None +last_time_slot = 0 +interval_ptr = 0 error_state = False last_alert_time = 0 for line in misc.follow_file(args.logfile): time_now = time.time() - slot_now = int(time_now) // args.interval_size + time_slot = int(time_now) // args.interval_size + time_slot_diff = time_slot - last_time_slot + last_time_slot = time_slot - if line != None: - if not last_slot_time or slot_now > last_slot_time: - kept_times.append(slot_now) - last_slot_time = slot_now + interval_ptr = (interval_ptr + time_slot_diff) % num_intervals + for i in range(0, min(num_intervals, time_slot_diff)): + intervals[(interval_ptr + i ) % num_intervals] = False + + if line: + intervals[interval_ptr] = True lines.append(line) - if len(lines) > MAX_LINES: + if len(lines) > LINES_TO_KEEP: lines.pop(0) - while len(kept_times) > 0 and \ - kept_times[0] <= slot_now - (args.num_intervals + 1): - kept_times.pop(0) - - intervals = [False] * (args.num_intervals + 1) - for kept_time in kept_times: - intervals[slot_now - kept_time] = True + logging.debug( misc.print_list(intervals, interval_ptr) ) - logging.debug(intervals) + intervals_error_state = True + for i in range(1, num_intervals): + if not intervals[(interval_ptr + i) % num_intervals]: + intervals_error_state = False + break - if not False in intervals[1:]: + if intervals_error_state: if not error_state or time_now - last_alert_time > ALERT_INTERVAL: last_alert_time = time_now misc.feed_handler( args.handler, @@ -71,3 +88,6 @@ for line in misc.follow_file(args.logfile): if error_state: logging.info("Leaving error state.") error_state = False + + if stop: + break diff --git a/src/misc.py b/src/misc.py index 8d8253d..821159f 100644 --- a/src/misc.py +++ b/src/misc.py @@ -48,3 +48,11 @@ def create_msg(title, icon, logfile, text, lines): for line in lines: msg += line + "\n" msg += "" return msg + +def print_list(lst, ptr): + txt = '[' + for i in range(0, len(lst)): + txt += str( lst[(ptr + i) % len(lst)] ) + if i < len(lst) - 1: txt += ', ' + txt += ']' + return txt -- 2.39.5