Skip to content

Instantly share code, notes, and snippets.

@tupton
Created November 6, 2012 22:01
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 tupton/4027889 to your computer and use it in GitHub Desktop.
Save tupton/4027889 to your computer and use it in GitHub Desktop.
Concise Opscenter Alerts
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DataStax OpsCenter Alerts</title>
<script src="js/dojotoolkit/dojo/dojo.js"></script>
<script>
require([
'dojo/_base/lang',
'dojo/_base/xhr',
'dojo/dom-construct',
'dojo/dom',
'dojo/domReady!'
], function(lang, xhr, dom_construct, dom) {
var cluster_id = 'Test_Cluster';
// Make an OpsCenter API call
function call(url) {
return xhr('GET', {
url: '/' + cluster_id + url,
timeout: 10000,
handleAs: 'json',
failOk: true,
error: function(error) {
var message = error.message || 'Unknown error';
throw new Error('Error calling ' + url + ': ' + message);
}
});
}
// Display a fired alert and its rule's parameters
function displayAlert(alert, rule) {
var description = rule.metric + ' ' + rule.type;
if (rule.comparator) {
description += ': ' + alert.current_value + ' ' + rule.comparator + ' ' + rule.threshold;
}
if (rule.duration > 0) {
description += pluralize(' for ' + rule.duration + ' minute', rule.duration);
}
dom_construct.create('div', {
innerHTML: 'Node ' + alert.node + ': ' + description
}, dom.byId('main'));
}
// Return a string that pluralizes a phrase based on a given amount
function pluralize(phrase, amount) {
return amount === 1 ? phrase : phrase + 's'
}
// Get alert rules and fired alerts and display them
call('/alert-rules')
.then(function(rules) {
call('/alerts/fired')
.then(function(alerts) {
dom_construct.create('h1', {
innerHTML: pluralize(alerts.length + ' fired alert', alerts.length)
}, dom.byId('header'));
var rule_map = {};
rules.forEach(function(r) {
rule_map[r.id] = r;
});
alerts.forEach(function(alert) {
if (alert.alert_rule_id in rule_map) {
var rule = rule_map[alert.alert_rule_id];
// Use `alert` and `rule` here to format alerts however you see fit
displayAlert(alert, rule);
}
});
});
});
});
</script>
</head>
<body>
<a href="/opscenter">View in OpsCenter &raquo;</a>
<div id="header"></div>
<div id="main"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment