Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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']
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():
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment