November 20th, 2012

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, whereindex.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.


Next, we want to actually load our alerts. Each section of the script has been annotated below.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.


This is the AMD dependency loader that dojo uses. We’ll need some basic functions offered bylang, 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.


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.

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.


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.