Hubot Servergrid Scripts

Hubot is a wonderfully versatile piece of software developed by the engineers at Github to automate tasks and become,in essence, your digital butler. At the heart of the software is a collection of scripts that actively listen to either your command line interface to hubot or (if you integrate your Hubot into the plethora of products out there) other systems you utilise daily.

One of the main integration areas is to plug Hubot into chat or messaging systems such as Hipchat, Slack or Basecamp. With this sort of integration you can ask Hubot to undertake tasks just as if you were asking a co-worker to find your information or fire up services.

This leads us to a great possibility of writing our own Coffee Scripts to integrate our own software and systems - infinite methods we could call by simply firing up a command to Hubot.

So this got me thinking - how easy would it be to write something that integrates with my simple server monitoring system, ServerGrid?

Installing Hubot (including Node)

I could replicate a plethora of information about methods for installing Hubot on your current system (which could be Mac, Linux or Windows) but I would like to point you directly to great documentation on The Github Hubot site and the tutsplus site just in case methods change between my version and yours.

Creating the API

The first thing I had to do was to think about the commands I wanted to fire into ServerGrid and the responses I wanted to get back out of it. I didn't want to start adding servers to the system at this stage, mainly just to figure out how things were operating by asking the status. I came up with the following list of things I wanted to find out:

  1. Get a list of servers
  2. Find out how much disk space is left on a server
  3. Ping a server
  4. Find out how much load is on a server
  5. Describe a server, load, ip address, disk space.

ServerGrid Hubot PHP API

So to dive into the ServerGrid system and pull out the information we need to create a fairly simple service to call on the functions already available in the ServerGrid system.

* File: hubot.php
* Description:
* HUBOT Response System - provides output from ServerGrid when Hubot requests it
$ObjFramework = new MicroFramework();
$ObjSG = new serverGrid();

$urlVars = explode("/", $_SERVER['REQUEST_URI']);
$a = db::escapechars(trim($urlVars[3])); // key
$b = db::escapechars(trim($urlVars[4])); // function
$c = db::escapechars(trim($urlVars[5])); // server
$d = db::escapechars(trim($urlVars[6])); // var1
$e = db::escapechars(trim($urlVars[7])); // var2

// Modify this key to your own private auth key if($a == "1010101011010101010110"){

  $returnResponse = "";

  if($b == "ping"){
    $serverList = $ObjSG->getServerList($ObjFramework->usernametoid('sysadmin'));
    foreach($serverList as $server){
      if(($c !="")&&($c== $server['serverName'])){
        $myserverid = $server['serverid'];
        $myipaddress = $ObjSG->getmyipaddress($myserverid);
        $pingtime = $ObjSG->pingServer($myipaddress);
        $returnResponse .= "[ Port 80 for ". $server['serverName']." - ".$pingtime . "ms ] ";

  if($b == "info"){
    $serverid = $ObjSG->getServerID($c);
    $serverInfo = $ObjSG->getServerStatus($serverid);
    $loadAverage = $ObjSG->getLoadAverage($serverid);
    $loadAverageArray = explode(" ",$loadAverage);
    $diskuse = $ObjSG->getUsedSpace($serverid);
    $returnResponse = "Server " . $c . " is on IP Address " . $serverInfo['ipaddress'] . " , has a load average of ".$loadAverageArray[0]." ".$loadAverageArray[1]." ".$loadAverageArray[2]." and is using ".$diskuse."% disk";

  if($b == "load"){
    $serverid = $ObjSG->getServerID($c);
    $loadAverage = $ObjSG->getLoadAverage($serverid);
    $loadAverageArray = explode(" ",$loadAverage);
    $returnResponse = "Server " . db::escapechars($c) . " Load Average: ".$loadAverageArray[0]." ".$loadAverageArray[1]." ".$loadAverageArray[2];

  if($b == "disk"){
    $serverid = $ObjSG->getServerID($c);
    $diskuse= $ObjSG->getUsedSpace($serverid);
    $diskfree = 100 - $diskuse;
    $returnResponse = "Server " . db::escapechars($c) . " has " . $diskfree . "% disk space free";

  if($b == "list"){
    $servers = $ObjSG->getServerList($ObjSG->usernametoid('sysadmin'));
    foreach($servers as $server){
      $returnResponse .= "[ " . $server['serverName']." ] ";

  echo $returnResponse;
  die('Could not connect, sorry');

Once this file has been created and placed in the api folder on my ServerGrid system I have an end-point to call for the information. We can test this works by calling your server directly from a browser like this:


If you get a response message with your servers listed - you've got this stage right.

Hubot Coffee Script for ServerGrid

Hubot runs from Coffee Scripts so we need to create a script that sits listening to us in some way and then responds with what we ask it. Coffee script is described as "a little language that compiles into JavaScript". In essence, it is simply JavaScript without all the curly braces, semi-colons and parentheses. It takes a little getting used to, but you will quickly find yourself getting into the groove of it.

For this script set I've chosen to have the system respond only when I precede my question with the word "servergrid". That way I can try to stave off any false launches of the scripts in general tech discussion in the chat system.

In the following script - you will need to replace servergrid.address with your ServerGrid installation address and 1010101011010101010110 with the auth key you write into your php API.

# File:
# Description:
# ServerGrid Response Mechanisms
# Allows checking of server state from ServerGrid Installation

module.exports = (robot) ->

robot.hear /servergrid ping (\w+)/i, (msg) ->
server = msg.match[1]
.get() (error, response, body) ->
msg.send body

robot.hear /servergrid load (\w+)/i, (msg) ->
server = msg.match[1]
.get() (error, response, body) ->
msg.send body

robot.hear /servergrid how much disk is left on (\w+)/i, (msg) ->
server = msg.match[1]
.get() (error, response, body) ->
msg.send body

robot.hear /servergrid describe (\w+)/i, (msg) ->
server = msg.match[1]
.get() (error, response, body) ->
msg.send body

robot.hear /servergrid list servers/i, (msg) ->
.get() (error, response, body) ->
msg.send body

robot.hear /servergrid help/i, (msg) ->
msg.send "\nServerGrid Commands:\nping servername\nload servername\nhow much disk is left on servername\ndescribe servername\nlist servers\nhelp\ngraph load on servername\ngraph free memory on servername"

Putting this together

Now firing up our system we can start to throw commands at the system and start getting results back - Huzzah!

Hubot> servergrid list servers
Hubot> [ Saturn ]
Hubot> servergrid describe Saturn
Hubot> Server Saturn is on IP Address , has a load average of 0.00 0.01 0.05 and is using 53.39% disk

Now you have the basis for your project you can start to add more features - perhaps the ability to stop and restart services on your servers at your request. I've extended the scripts to utilise the PHPGraphLib library to output graphs of load and disk usage which in my chat client automatically displays the image.

This hack took me a few hours to achieve and reduces my administration overhead for my own network significantly. Hopefully it will come in useful for someone else too!

Happy hacking!

Next Item.. ServerGrid Door Entry Integration with Paxton Net2 and HipChat

Previous Item.. Automated Text Summarisation