public
Created

Integrating Alerts via the OpsCenter REST API

  • Download Gist
fetch-opscenter-alerts.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import urllib, json, time
 
def getUrl(path):
""" Makes an OpsCenter API call to the given path """
 
url = 'http://localhost:8888/%s' % path
contents = urllib.urlopen(url).read()
return json.loads(contents)
 
def pollCluster(cluster_id):
""" Processes all fired alerts in a given cluster """
fired_alerts = getUrl('%s/alerts/fired' % cluster_id)
 
# Fetch configured alert rules
alert_rules = getUrl('%s/alert-rules' % cluster_id)
rules_map = dict((r['id'], r) for r in alert_rules) #create map for easy lookup
 
for alert in fired_alerts:
rule = rules_map.get(alert['alert_rule_id'])
doSomething(alert, rule)
 
def doSomething(alert, rule):
""" Function to process the fired alert however you'd like """
 
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']
else:
msg = "Unknown alert type"
 
datetime_str = time.strftime("%m/%d/%Y %H:%M:%S %Z", time.localtime(alert['first_fired']))
msg += " (since %s)" % datetime_str
print msg
 
def pollAllClusters():
""" Process all clusters managed by OpsCenter """
 
cluster_configs = getUrl('cluster-configs')
 
for cluster_id in cluster_configs.keys():
pollCluster(cluster_id)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.