Skip to content

Instantly share code, notes, and snippets.

@mbulman
Created August 22, 2012 16:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbulman/3427108 to your computer and use it in GitHub Desktop.
Save mbulman/3427108 to your computer and use it in GitHub Desktop.
Integrating Alerts via the OpsCenter REST API
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment