A Concise View of OpsCenter Alerts

By Thomas Upton -  November 5, 2012 | 0 Comments

You can gain quick access to a wealth of data about your cluster using the OpsCenter REST API. Last time we visited the API, we talked about how to retrieve alerts in a Python script. This post will show how an interaction with the API might look like using Javascript.

Sometimes it could be beneficial to have a quick overview of your fired alerts available at a moment’s notice. The API, among many other things, allows you to retrieve alert rules and alerts that have been fired. Using this data, you can set up a simple webpage to quickly view any fired alerts that would be useful when displayed on a big monitor that’s visible to everyone on your team or integrated into a third party dashboard.

The OpsCenter UI is built using the Dojo Javascript framework, so that’s what we’ll use to set up our alerts page. First, we want to create a simple skeleton HTML file, alerts.html. Since browsers are limited by the same-origin policy, we’ll need to locate this particular page in the OpsCenter directory. For tarball installations, this would mean it’s located in /content, where index.html is located. For package installations, this would be located in /usr/share/opscenter/content. In order to view this file in a browser, you would go to http://<your-opscenter-machine-host>:8888/opscenter/alerts.html.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
    <script src="js/dojotoolkit/dojo/dojo.js"></script>
    <a href="/opscenter">View in OpsCenter &raquo;</a>
    <div id="header"></div>
    <div id="main"></div>

We can use OpsCenter’s version of dojo for this simple page. Note that we have a link to OpsCenter here so you can quickly check any graphs that are related to your fired alerts or if you want to view the event log.

Next, we want to actually load our alerts. Each section of the script has been annotated below.

], function(lang, xhr, dom_construct, dom) {

This is the AMD dependency loader that dojo uses. We’ll need some basic functions offered by lang, we need xhr to make our API calls, and we want to actually display alert information on our page, so we’ve loaded some DOM-related dependencies.

    var cluster_id = 'Test_Cluster';

Change your cluster ID here. This could easily be changed to an array of multiple cluster IDs that you could then iterate over and get fired alerts for.

    // 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);

This function actually makes our API calls, and logs any errors that may have occurred. Note that the cluster ID is automatically prepended to the URL.

    // Display a fired alert and its rule 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 &gt; 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'

Once we have a fired alert and its corresponding rule, we want to display some basic information. This function just appends the metric and type to a div, along with some additional parameters if they’re available. You could display this information however you want to.

    // Get alert rules and fired alerts and display them
        .then(function(rules) {
                .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);

This is the actual call to the API. First we need to get alert rules, then the fired alerts. We can iterate over the alerts and check their ids for matching rules, then pass that information on to our displayAlert function above.

Simply put this Javascript in a <script> tag in the HTML page from before, and load it up in your browser. You’ll see a concise view of your fired alerts that could easily be loaded on a phone or other mobile device, with a quick link to OpsCenter for when you need an in-depth look at what’s going on in your cluster.

You can see the complete code sample in this gist.


Your email address will not be published. Required fields are marked *

Subscribe for newsletter: