Skip to content

Instantly share code, notes, and snippets.

@samuel
Created November 30, 2010 00:48
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save samuel/720925 to your computer and use it in GitHub Desktop.
PagerDuty script for Munin
#!/usr/bin/env python
try:
import json
except ImportError:
import simplejson as json
import sys
from pagerduty import PagerDuty
def parse(txt):
events = {}
lines = [x.strip() for x in txt.split('\n') if x]
it = iter(lines)
line = it.next()
while line:
group, host, graph = line.split(' :: ')
statuses = {}
while True:
try:
line = it.next()
if '::' in line:
break
except StopIteration:
line = None
break
status, rest = line.split(': ', 1)
values = dict(
x.split(' is ')
for x in rest.rstrip('.').split(', ')
)
statuses[status.rstrip("s")] = values
events[(group, host, graph)] = statuses
return events
def main():
if len(sys.argv) < 2:
sys.stderr.write("Service key required as first argument\n")
sys.exit(1)
service_key = sys.argv[1]
alert = sys.stdin.read()
hosts = parse(alert)
pg = PagerDuty(service_key)
for key, statuses in hosts.items():
incident_key = "/".join(key)
group, host, graph = key
for status, values in statuses.items():
for vname, value in values.items():
description = "%(status)s [%(group)s/%(host)s :: %(graph)s] %(name)s is %(value)s" % \
dict(
status = status,
group = group,
host = host,
graph = graph,
name = vname,
value = value)
if status == "OK":
pg.resolve(incident_key=incident_key, description=description)
elif status == "WARNING":
pg.trigger(incident_key=incident_key, description=description)
elif status == "CRITICAL":
pg.trigger(incident_key=incident_key, description=description)
elif status == "UNKNOWN":
pg.trigger(incident_key=incident_key, description=description)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment