Xively Data Logging the Unix Way

Motivation

I wanted to log my Raspberry Pi’s CPU load and temperature into xively for easy monitoring. Later maybe add a few more Arduino sensors spread around my home.

There are multiple libraries in several programming languages/platforms supplied by xively itself. I believe they are all good, well tested and well maintained pieces of code. However I just wanted to easily log the two values, not spend time learning how to install Java on Raspberry Pi, install the libraries etc.

I wanted as few dependencies as possible. The result needs just standard unix command line tools: bash, xargs, curl and uses the xively REST API.

Following the Unix Philosophy, I separated the problem into several smaller ones:

  1. general script for logging any one value to any one channel on xively
  2. script to log load average using 1)
  3. script to log temperature using 1)
  4. scheduling of periodic execution of 2) and 3)

The general logging script

This script logs one value to one xively channel. There is no possibility to change the timestamp, neither log several values at the same time, nor log into several channels at the same time. It is very limited compared to the other libraries, but IMHO does 90% of what most people need when it comes to real-time sensor logging.



Don’t forget to change the API key and API endpoint as you have it set in your xively account.

The specific scripts

Once the general xively script above is on system path and executable, logging individual values is as easy as typing

xively temperature 23.4

So the only challenge left is where to find the load average and board temperature. In both my xubuntu/x86 and raspbian/arm distributions these values can be obtained form system files – so the following two scripts upon execution log the current values to xively.

cat /proc/loadavg | xargs xively loadavg
awk '{printf "%2.3f", $1/1000}' /sys/class/thermal/thermal_zone0/temp |\
xargs xively thermal

The scripts assume that the channels “loadavg” and “thermal” had already been created in xively web interface.

Scheduling periodic logging

I didn’t want to reinvent the wheel, so I’m using cron for periodic execution of the scripts above. Just put them to /etc/cron.hourly, make them executable and it all works like a charm.

Later I wanted to log every minute instead of every hour. Some distributions come with /etc/cron.minutely preconfigured, some don’t. If your system does not include this folder by default, then create one and add following line to /etc/crontab to make cron actually execute all files in the folder.

* * * * *   root    cd / && run-parts --regex='.*' /etc/cron.minutely