Technology•August 22, 2012
Integrating Alerts via the OpsCenter REST API
import urllib, json
def getUrl(path):
url = 'http://localhost:8888/%s' % path
contents = urllib.urlopen(url).read()
return json.loads(contents)
load = getUrl('MyCluster/nodes/127.0.0.1/load')
print load
# Prints: 0.32
fired_alerts = getUrl('MyCluster/alerts/fired')
{
"alert_rule_id": "80fc5e0a-355a-4f8f-86ef-5de8fd8fb5bb",
"node": "127.0.0.1",
"current_value": 1234,
"first_fired": 1345575248
}
alert_rules = getUrl('MyCluster/alert-rules')
rules_map = dict((rule['id'], rule) for rule in alert_rules)
for alert in fired_alerts:
rule = rules_map.get(alert['alert_rule_id'])
doSomething(alert, rule)
def doSomething(alert, rule):
if rule['type'] == 'rolling-avg':
msg = "%s on node %s is at %.2f" %
(rule['metric'], alert['node'], alert['current_value'])
elif rule['type'] == 'node-down':
msg = "Node %s is down" % alert['node']
datetime_str = time.strftime("%m/%d/%Y %H:%M:%S %Z",
time.localtime(alert['first_fired']))
msg += " (since %s)" % datetime_str
print msg