public
Last active

Concise Opscenter Alerts

  • Download Gist
gistfile1.html
HTML
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<!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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.