Skip to content

Instantly share code, notes, and snippets.

@gmcquillan
Created January 11, 2011 18:00
Show Gist options
  • Save gmcquillan/774803 to your computer and use it in GitHub Desktop.
Save gmcquillan/774803 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import io
import logging
import simplejson as json
import time
import beanstalkc
"""
Alerts are pulled from the 'default' tube on beanstalk on the localhost.
Each node gets its own file for writing the alert summary and body.
These logfiles, in turn are copy truncated by logrotate each day. So, each of
the logs are representative for that day.
"""
BPORT = 8989
FILE_PREFIX = '/mnt/alerts'
ALERT_FILES = {} # contains names of files and the file objs.
ALERT_COUNTERS = {} # contains counters for minute, half hour, and hour.
FIRETOWER_LOG = '%s/firetower.log' % (FILE_PREFIX,)
def pull_alerts(qconn, buf_lines=5):
alerts = []
for lines in range(buf_lines):
job = qconn.reserve()
alerts.append(job.body)
job.delete()
return alerts
def parse_alerts(alerts):
return [json.loads(alert) for alert in alerts]
def create_or_append_alertfile(parsed_alerts):
for pa in parsed_alerts:
if pa['host']:
print ALERT_FILES
if pa['host'] in ALERT_FILES:
print 'Writing to file'
line = '%s\n' % (json.dumps(pa))
ALERT_FILES[pa['host']].write(line)
else:
print 'Creating file'
host_file = '%s/%s' % (FILE_PREFIX, pa['host'])
file_obj = io.FileIO(host_file, mode='a')
ALERT_FILES[pa['host']]= file_obj
def handle_alerts():
conn = beanstalkc.Connection(host='localhost', port=BPORT)
# TODO:
# Figure out timing logic, record number of alerts per node within some
# time interval.
while 1:
alerts = pull_alerts(conn)
parsed_alerts = parse_alerts(alerts)
create_or_append_alertfile(parsed_alerts)
def main():
handle_alerts()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment