Книга: Automation and Monitoring with Hubot: For DevOps and Developers
Назад: Monitoring With Hubot
Дальше: GitHub Integration

Graphing With Hubot

Hubot is notorious for littering your chat with fun animated GIFs and random pictures of pugs and squirrels. However, you can exploit this feature to actually do something useful - show graphs from various graphing systems, like Graphite or Cacti.

Graphite

can visualize a ton of metrics that you can collect via UDP using , and Hubot has a for querying and drawing all those wonderful graphs. It ships with hubot-scripts package that is included in your Hubot.

This script was recently broken, so if you happen to have an older version, simply download the latest version from and put it in your Hubot’s scripts/. You can tell if you have the old version by examining the list of commands your graphite.coffee supports. Latest version should have graphite list command available.

In case you will be using the version that ships with your Hubot, don’t forget to add "graphite.coffee" to hubot-scripts.json.

Configuring Hubot Graphite Script

You will have to set a couple of environmental variables to get this script working. Place them into hubot.conf.

hubot.conf


# Graphite URL export GRAPHITE_URL="https://graphite.your.org" # Graphite Port (optional unless non standard) # export GRAPHITE_PORT=1234 # Graphite username:password for Basic Auth export GRAPHITE_AUTH="hubot:supersecret" 

Trying It Out

To use this script, you have to have some graphs saved at Graphite first. To do that, you have to be logged into Graphite and when you build your graph, save it using the Save icon on top-left. The graph should then be listed under User Graphs in the tree on the left.

To get the list of graphs you have, just say graphite list:

Tomas V.  graphite list Hubot     metrics.deployments           metrics.portal.logins           metrics.portal.signups           metrics.portal.errors 

If the list is too big, you can search for graphs matching a query:

Tomas V.  graphite search portal Hubot     metrics.portal.logins           metrics.portal.signups           metrics.portal.errors 

Finally, request the graph you want to see using graphite show <graph.name>. You can provide only a part of graph name, just make sure it’s unique because Hubot will always show the first graph that matches the query, so if you say graphite show portal, in our example Hubot will show metrics.portal.logins - you will have to use graphite show signups to see metrics.portal.signups.

Tomas V.  graphite show portal.errors Hubot     https://graphite.your.org/render/... 

You will see graph images if you use Campfire, Hipchat, or some other chat that supports displaying inline images. With other adapters, like IRC, you will only get a link that you will have to click to see the graph. Still, pretty useful.

Extending The Script

This script has some limitations. For instance, you graph will have fixed time window that you have defined when saving it. Wouldn’t it be cool if you could say hubot show deployments -1year and get the graph with last year of deployments?

Let’s modify the original script, so it will do just that. If you are using graphite.coffee that ships with Hubot, first remove it from hubot-scripts.json, then copy it to scripts/ directory.

Now, all we want to do is to extend graphite show <graph.name> with optional extra parameter - graphite show <graph.name> [offset]. To do that, we will make a couple of small changes.

Find this code:

graphite.coffee


robot.hear /graphite show (\S+)/i, (msg) ->   treeversal msg, (data) ->     construct_url msg, data[0].graphUrl, (url) ->       msg.send url 

And change it to this:

graphite.coffee


1 robot.hear /graphite show (\S+)( (.+))?/i, (msg) -> 2   offset = msg.match[3] 3   treeversal msg, (data) -> 4     construct_url msg, data[0].graphUrl, (url) -> 5       if offset 6         if url.match /&from=[-+0-9a-z]+/i 7           url = url.replace /&from=[-+0-9a-z]+/i, '' 8         url = url.replace '#', "&from=#{offset}#" 9       msg.send url 

What have we done?

graphite.coffee


1 robot.hear /graphite show (\S+)( (.+))?/i, (msg) -> 2   offset = msg.match[3] 

This new regexp matches extra word, which will appear in msg.match[3]. If you wondering why not msg.match[2] - it would contain extra space in the beginning.

graphite.coffee


5       if offset 6         if url.match /&from=[-+0-9a-z]+/i 7           url = url.replace /&from=[-+0-9a-z]+/i, '' 8         url = url.replace '#', "&from=#{offset}#" 

Here we check if offset was provided, and if it was, we want to remove the original &from=<something> from graphite URL before enhancing it with new offset.

Since this URL always ends with #<timestamp>&png, it’s a good place to hook in and add our custom &from=<offset>.

It should work:

Tomas V.  graphite show deployments -1year Hubot     https://graphite.your.org/...&from=-1year#1393476771285&png 

Never give up if a script doesn’t do everything you want it to - it’s called open source for a reason. And if your changes are significant enough, consider contributing them back.

Назад: Monitoring With Hubot
Дальше: GitHub Integration