Creating a Graphdat Plugin, step-by-step
Graphdat has recently opened up its API to provide great looking dashboards for anything you wish to graph. With this new API you can now graph any measurable metric in realtime, however, there is a small amount of work involved in getting the metrics you are measuring up to Graphdat for display. To help solve this problem we introduce the Graphdat Plugin.
A plugin is a tiny application that can be written in any language or script which simply reads the metric you are after and reports its value. The plugin is launched using a tool called graphdat-relay which manages configuration, polling, and communication with Graphdat.
Plugins not only make it very easy to add arbitrary metric monitoring to Graphdat but because plugins are kept in a central, public, repository, they can be shared by all. So if there is some metric in your system that needs to be tracked, chances are there is a plugin already written for it.
Before starting we will make a few assumptions regarding the following:
A Graphdat account - it is assumed you've already created your Graphdat account.
NodeJS - Although plugins can be written in any language you desire (you specify a command line to run the plugin which can call any program or script engine), for the purposes of this step-by-step we will use the NodeJS language so it is assumed you have NodeJS installed.
A GitHub account - Because all Graphdat plugins use github as a repository you should make sure you have a github account (there is no charge to signup and create public repositories).
SCM tool - Github usually works in conjunction with a source code management tool installed on your system. For the purposes of this step-by-step we will assume this tool is git but the commands are so simple that you should not have any problem translating these steps for whichever tool you are using.
Step 1 - Create the metrics
The first step in creating any plugin is deciding what metrics you want to track. In Graphdat the idea is that when a type of metric is created, it is given a universal name and is typically associated with a single plugin.
For the purposes of this step-by-step let's create a plugin that will track CPU usage of individual CPU cores. The default CPU metric that the graphdat agent collects is only the system wide average CPU so with this plugin we can now dive into individual CPU core usage information.
Creating the metric is simple. Log in to Graphdat then open your Settings dialog..
..then go to the Metrics tab..
..and click on Create Metric. On this screen you are prompted to enter some information. Now for a quick word on metic identifiers. Metric identifiers must be globally unique so for the purpose of this step-by-step you will need to come up with a unique metric identifier. Try just appending your user name to CPU_CORE (ex: CPU_CORE_BOB99). If the name exists you will be told and you can change it.
Go ahead and enter the following:
Name: Per core CPU utilization
Identifier: CPU_CORE_... (the name you created above)
Description: Per core CPU utilization
Default Aggregate: Average
Click Save and your new metric should show up in the list.
Note that a plugin can provide several metrics if desired.
Step 2 - Create the plugin
Now the easy part, creating the actual plugin. As noted above all plugins are simply github repositories so to start off we need to create a new repository. Log in to github and click on New Repository
On the next screen you are asked for some information. Enter the following:
Repository name: graphdat-plugin-cpu-core
Initialize this repository with a README:
Add .gitignore: Node
Add a license: Apache v2 License
Note that we chose Node for this step-by-step but you plugins can be any language you like. We also chose the Apache v2 open source license as this is the default license Graphdat uses for all its open source.
Now click Create repository, all good.
At this point we have a new repository and github has been kind enough to initialize it with some files. Let's clone the repository locally. The easiest way to do this is to first copy the github URL to the clipboard by clicking the clipboard icon on right side of your new repository page.
(Note that we have chosen SSH for this step-by-step.)
Now we just use git from the command line to bring it down:
$ cd graphdat-plugin-cpu-core
..and create a new file called index.js. In this file paste the following code:
https://gist.github.com/pdiemert/6979264
In the code above we used CPU_CORE as the metric name. You should replace this with the metric name you created from above.
Note: Although not applicable in this step-by-step, you may choose to pass configuration information to your plugin for testing purposes. To do this, simply create a file called param.json in this directory that contains a JSON object with the parameters/value pairs you require. During normal operation the relay will overwrite this file with the settings that are chosen in the Settings / Relays / Plugins screen. See the section on paramSchema in the plugin documentation on how to define configuration parameters.
Now give your plugin a try from the command line..
Now, assuming all is working up to this point, you should now be seeing some output of your CPU core percentages, reported once a second. That is all the coding you will need to do for this plugin! Press CTRL+C to stop the plugin.
Next, we need to create a description of this plugin. Create another file in the same directory called plugin.json. In this file paste the following:
{ "description" : "Provides per core CPU utilization", "command" : "node index.js $(pollInterval)", "metrics" : ["CPU_CORE"] }
As before, you should replace CPU_CORE with the name of your metric.
That's it! You have created a minimal plugin. Before we can deploy it make sure to check it into github:
git add .
git commit -m "added source files"
git push
Step 3 - Testing the deployment
Before submitting your plugin to Graphdat for inclusion in the list of available plugins you will want to first test installing the plugin into your own account. At the command line:
$ curl https://api.graphdat.com/v1/plugins/private/cpu_core/<your github username>/graphdat-plugin-cpu-core -X PUT -u <your email>:<your API key>
You will now see the plugin listed when browsing all available plugins from Settings / Plugins / Get Plugins.
Click Install to add this plugin to your account. Next you will need to have installed the Graphdat Relay on the machine you wish to monitor. The relay will host the plugin and communicate the measurements back to Graphdat. Go to the machine you wish to monitor and install the relay (note: the Graphdat Relay is written in NodeJS so it is assume you have NodeJS already installed).
$ npm install graphdat-relay -g
Let's now create a directory where we want to run the relay. This is the directory where we will create the configuration file and also where the relay will put any plugins you select.
$ mkdir .graphdat-relay
$ cd .graphdat-relay
There is one small piece of configuration required to get the relay running. We will create the initial configuration with the following:
$ graphdat-relay -e <your email> -t <your api token>
This will create a new file in this directory called config.json. The email and API token values can be found in your Settings / Account tab. You can browse the complete relay reference for more background on how it works. Now that the relay is configured, from the same directory as the config.json, launch:
You should now be able to see the relay in your Settings / Relays tab. Click on the relay to see the plugins it is running. Next click to add the cpu-core plugin:
Once you select the plugin you will be taken to the plugin configuration screen. We don't need to change any settings so just click Save. As soon as you save the plugin configuration you will next be shown the relay console. If we wait a few moments we will see the relay install the plugin:
The plugin is now running! For this step-by-step we have chosen not to add a new dashboard (see plugin documentation on how to do this). Instead, we need to add the metric graph to our dashboard using the dashboard editor. Open the dashboard editor now:
Notice that in the toolbox we can see our new metric:
Just drag + drop this metric onto the dashboard and click Save.
Done. On your dashboard you should now be seeing the per-core CPU performance being graphed!
If you were building your own plugin you'd probably be making further changes to the plugin before release. You can simply repeat the steps above (from the point of pushing your changes to github) and you will notice an 'Apply update' link next to the plugin from the Settings / Relays / (Relay) tab.
Step 4 - Submit to Graphdat
When you are happy with your plugin and are ready to submit it to Graphdat for inclusion in the public repository just follow the steps outlined in the plugin documentation to create a github pull request. The folks at Graphdat will review your plugin quickly and soon everyone will be able to enjoy your shiny new Graphdat plugin!