We’ve been doing a lot of work on our Resin administration application over the last few releases, adding features like statistic graphs, postmortem reports, and REST. The framework we’re building for these features is also quite extensible and easy to use. One of the in-house extensions we developed combines the REST and graph features so that you can export and embed user-defined graphs. We’ve added this extension to the main application for the upcoming 4.0.4 release so it’s available without any coding. In this blog post, I’ll show you how to embed Resin statistics graphs in your own monitoring tools, how the underlying graph API works, and give some tips on how to write your own similar extensions.
Introducing Resin statistics graphs
We’ve recently added graphs to the Resin administration application using the HTML5 <canvas> tag. Firefox 3.5 and Google Chrome support this feature, but many (most?) other browsers don’t, so you’ll need to use one of them to see the graphs. The graph feature works by selecting a set of metrics (gathered from JMX) to plot, then saving that set of metrics. For example, you could select the JVM’s heap metrics (total size and free memory), then plot those on a graph you called “JVM Heap”. In fact, that’s exactly what I’ve done below:
|Creating a new graph||JVM Heap Graph|
Notice that when I saved the graph, I selected servers ‘00′ and ‘01′. I’m actually running a cluster of two servers in this example, but I’m collecting statistics for both of them and plotting them on the same chart.
Embedding the graph in other pages
If you notice on the graph screenshot above, you can see a link at the bottom, “Embedded graph URL”. If you click on it, you should get a raw graph directly in your browser and see the URL. You can copy that URL and embed it into any page you like, such as a custom monitoring page or even as a plugin to a framework like WordPress.
|Raw REST Graph|
For example, I’ve created 4 graphs and put them into a static HTML page with <iframe>s as such:
|Custom Graph Layout|
Behind the scenes
The graphs in this example are generated through our administration REST interface, which gives us access to JMX data including these statistics. Resin 4.0 has a new statistics gathering infrastructure which is constantly keeping track of server data for these graphs and other purposes. These data are available through a StatService MBean in Resin. The REST resources we’ve implemented in this case just graphs these statistics in an HTML5 <canvas>. In REST-speak, you’re actually creating resources each time you create a new graph and this script just retrieves them.
If you’d like to do something similar, we actually have a PHP graphing API that takes the statistics and plots them. The basic call is:
The $canvas argument is the id of the <canvas> element, the width and height should be obvious, $start and $end are the start and end sample boundaries (in epoch time), and the $names (an array) describe which statistics to plot. The names of the statistics need a little more description. They are string-based descriptors with a basic format:
For example, Server 00’s physical memory free statistic would be encoded:
This encoding lets you do interesting things like pass in regular expressions for the names of the statistics you want to graph using this function:
The REST graph interface uses the saved graphs (which are essentially a set of statistics names) from the Resin administration application, but you can generate graphs from any set of names you like.
There are a lot of possibilities with the new features in the administration application, especially integration with other tools via REST. You can integrate with traditional monitoring frameworks like Nagios or even some unconventional frameworks like WordPress or Drupal. Let us know if you develop any useful patterns!