10 ALERT_INTERVAL = 86400
12 parser = argparse.ArgumentParser(description='Alert on excessive number of error log lines.')
13 parser.add_argument('logfile', type=str, help='logfile to be watched')
14 parser.add_argument('handler', type=str,
15 help='alert will be delivered to standard input of handler')
16 parser.add_argument('-s', '--interval-size', type=int, default=600, dest='interval_size',
17 help='sample interval size in seconds (default: 600)')
18 parser.add_argument('-n', '--num-intervals', type=int, default=6, dest='num_intervals',
19 help='number of intervals to keep in history (default: 6)')
20 parser.add_argument('-l', '--log-level', type=str, default='INFO', dest='log_lvl',
21 choices=['DEBUG', 'INFO', 'WARNING'], help='select log level')
23 args = parser.parse_args()
25 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
26 level=logging.getLevelName(args.log_lvl),
27 datefmt='%m/%d/%Y %H:%M:%S')
35 for line in misc.follow_file(args.logfile):
36 time_now = time.time()
37 slot_now = int(time_now) // args.interval_size
40 if not last_slot_time or slot_now > last_slot_time:
41 kept_times.append(slot_now)
42 last_slot_time = slot_now
44 if len(lines) > MAX_LINES:
47 while len(kept_times) > 0 and \
48 kept_times[0] <= slot_now - (args.num_intervals + 1):
51 intervals = [False] * (args.num_intervals + 1)
52 for kept_time in kept_times:
53 intervals[slot_now - kept_time] = True
55 logging.debug(intervals)
57 if not False in intervals[1:]:
58 if not error_state or time_now - last_alert_time > ALERT_INTERVAL:
59 last_alert_time = time_now
60 misc.feed_handler( args.handler,
61 misc.create_msg("Log Alert",
64 "Number of errors exceeded!",
67 logging.warning("Entering error state!")
72 logging.info("Leaving error state.")