Creating Ninja Blocks Drivers for Fun and Profit

As the Ninja Blocks Unix weenie, I wanted a way to monitor CPU on the Ninja Blocks. With the new Ninja Blocks client, extending our platform is easier than ever. In this tutorial, I will lead you through the process I went thru to create a NB driver that reports your machine's CPU load to the Ninja Blocks Platform.

For starters, I should mention that I used the walkthrough outlined here heavily when developing this driver.

I should also mention that this is an early release preview of drivers. Drivers will be ready for the big time very soon so stay tuned.



Ok, let's get started.



Step 1: Setup Ninja Blocks environment

Check out our handy instructions here for a step by step guide on how to get the environment setup. In this case, I used a Raspberry Pi to build this driver, but all the following steps should be the same no matter the platform.

In this case, I simply cloned all of the client code into a directory called ninjaClient in my home directory.  I will reference this as ninjaClient from here on out, but you can choose whatever name you like.

Step 2: Create a blank driver

Run the following commands on your machine.

sudo npm install -g ninja-toolbelt  
cd ninjaClient/drivers
ninja create cpu_driver

The previous commands will install our handy toolbelt scripts, move into your client's drivers directory and creates a fresh driver for you with a name of your choosing. After the script runs, it will ask you to run a few commands.

Run:

cd cpu_driver && npm init

This will ask a few questions regarding your driver. Answer them appropriately.



Step 3: Edit default driver to monitor CPU usage

If you look inside the driver's directory, you will see three files: 

  • index.js - this is the main entry point into your driver
  • package.json - contains the info you supplied when you initialized the driver.
  • lib/device.js - this defines the device your driver will query and/or actuate

Open up device.js using your favorite editor and edit to look like this:



There are surprisingly few edits I needed to do in order to get this working. The edits I performed are listed below:

Line 4 - Added exec to the project which will allow us to execute arbitrary commands

Line 5 - Added a child variable which will hold the exec instance. 

Line 36 - Changed "writable" to false since this is just a "sensor" and I won't be sending commands to the driver.

Line 43 - Wrapped the following code in "setInterval" so the CPU will be queried periodically.

Line 45 - This is the heart of the driver. This executes the command top -b -n1| grep Cpu | awk '{print $2}' in the shell. This simply parses the output from "top" to pull out the current CPU usage. The result of this parsing is assigned sent out to the variables "error", "stdout", and "stderr" which we will use later. This line can be changed to any command you like! 

Line 47 - Remove newlines in output.

Line 48 - Send CPU usage to the internet



Step 4: Test

Now, run:

cd ../..
node client.js

If everything goes well, you should see the following:



Now, login to http://a.ninja.is and pair your client. Your client's randomly generated serial is in the output of client.js (highlighted in yellow). To pair, simply click the pair button in the upper right.



If everything is looking good, you should see a different looking widget on your dashboard. 



The data coming from your CPU is highlighted in yellow! 




Step 5: Marvel at your awesomeness

If you're up to it, play with that command on line 45, you can now send any data you like to the platform!

If you'd like to cheat, you can also run ninja_install -g https://github.com/wandergeek/linux-cpu which will install the driver straight from my github.

Happy Hacking!


Nick Clark
Nick Clark

Author