From 6e4649c5bae35dc883f50642d81f0b57c6df565b Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Tue, 6 Oct 2020 10:05:10 +0200 Subject: [PATCH] re-send alerts; fix early un-alert --- src/logalert.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/logalert.py b/src/logalert.py index d240f44..509b246 100755 --- a/src/logalert.py +++ b/src/logalert.py @@ -10,6 +10,7 @@ import time from line import Line MAX_LINES = 10 +ALERT_INTERVAL = 86400 def follow(filename): while True: @@ -70,39 +71,44 @@ args = parser.parse_args() kept_times = [] lines = [] -last_time = None +last_slot_time = None error_state = False +last_alert_time = 0 for line in follow(args.logfile): - now = int(time.time()) // args.interval_size + time_now = time.time() + slot_now = int(time_now) // args.interval_size if line != None: - if not last_time or now > last_time: - kept_times.append(now) - last_time = now + if not last_slot_time or slot_now > last_slot_time: + kept_times.append(slot_now) + last_slot_time = slot_now lines.append(line) if len(lines) > MAX_LINES: lines.pop(0) while len(kept_times) > 0 and \ - kept_times[0] <= now - args.num_intervals: + kept_times[0] <= slot_now - (args.num_intervals + 1): kept_times.pop(0) - intervals = [False] * args.num_intervals + intervals = [False] * (args.num_intervals + 1) for kept_time in kept_times: - intervals[now - kept_time] = True + intervals[slot_now - kept_time] = True logging.debug(intervals) - if not False in intervals: - if not error_state: - logging.warning("Entering error state!") - error_state = True + 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) ) + if not error_state: + logging.warning("Entering error state!") + error_state = True + else: if error_state: logging.info("Leaving error state.") -- 2.39.5