10 ALERT_INTERVAL = 86400
12 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
14 datefmt='%m/%d/%Y %H:%M:%S')
16 parser = argparse.ArgumentParser(description='Alert on excessive number of error log lines.')
17 parser.add_argument('logfile', type=str, help='logfile to be watched')
18 parser.add_argument('handler', type=str,
19 help='alert will be delivered to standard input of handler')
20 parser.add_argument('-s', '--interval-size', type=int, default=600, dest='interval_size',
21 help='sample interval size in seconds (default: 600)')
22 parser.add_argument('-n', '--num-intervals', type=int, default=6, dest='num_intervals',
23 help='number of intervals to keep in history (default: 6)')
25 args = parser.parse_args()
33 for line in misc.follow_file(args.logfile):
34 time_now = time.time()
35 slot_now = int(time_now) // args.interval_size
38 if not last_slot_time or slot_now > last_slot_time:
39 kept_times.append(slot_now)
40 last_slot_time = slot_now
42 if len(lines) > MAX_LINES:
45 while len(kept_times) > 0 and \
46 kept_times[0] <= slot_now - (args.num_intervals + 1):
49 intervals = [False] * (args.num_intervals + 1)
50 for kept_time in kept_times:
51 intervals[slot_now - kept_time] = True
53 logging.debug(intervals)
55 if not False in intervals[1:]:
56 if not error_state or time_now - last_alert_time > ALERT_INTERVAL:
57 last_alert_time = time_now
58 misc.feed_handler( args.handler,
59 misc.create_msg("Log Alert",
62 "Number of errors exceeded!",
65 logging.warning("Entering error state!")
70 logging.info("Leaving error state.")