How to Use RRDtool on Debian NSLU2 to Capture Temperature Data

I have a 1-Wire temperature sensor IC connected to my small Debian NSLU2 computer and in order to analyze the temperature data generated I’m using a program called RRDtool:

RRDtool is the OpenSource industry standard, high performance data logging and graphing system for time series data.

– Tobi Oetiker

RRDtool is able to make all sorts of graphs for different kinds of data but for now I just need to use it for a single temperature sensor measuring in degrees Celsius.

I start by logging into the NSLU2 from my laptop computer (replace .xx with the address of the NSLU2 on the intranet):

$ ssh thomas@192.168.1.xx

and make a directory for rrdtool, and write a script that will create a basic rrdtool database:

(Note that $ in front of command means I’m working as a normal user, and # means I’m working as super user)

$ cd /home/thomas
$ mkdir rrdtool
$ cd /home/thomas/rrdtool
$ nano create_database.sh

This is what goes into the script:

#!/bin/bash
rrdtool create database.rrd --start N --step 300 \
DS:temp:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:12 \
RRA:AVERAGE:0.5:1:288 \
RRA:AVERAGE:0.5:12:168 \
RRA:AVERAGE:0.5:12:720 \
RRA:AVERAGE:0.5:288:365

After the script has been saved you’ll need to change the permissions setting of the file to make it executable:

$ chmod +x create_database.sh

rrdtool is already available in the Debian package system – you just need to install it with apt-get:

$ su
# apt-get install rrdtool
# exit

When you run the script it will create a file called database.rrd which contains the rrdtool database:

$ ./create_database.sh

A directory listing shows that the database file was created:

$ ls -la
total 28
drwxr-xr-x 2 thomas thomas  4096 Sep 15 18:50 .
drwxr-xr-x 6 thomas thomas  4096 Sep 15 17:49 ..
-rwxr-xr-x 1 thomas thomas   208 Sep 15 17:52 create_database.sh
-rw-r--r-- 1 thomas thomas 13764 Sep 15 18:50 database.rrd

The database has to be updated regularly and the commands for that can be collected in another script that I choose to name update_database.sh:

$ nano update_database.sh

The update script takes care of three things:

  1. Reading the temperature value from the sensor, and formatting the reading
  2. Updating the database file
  3. Creating, or updating, image files containing temperature graphs

This is the code for the script:

#!/bin/bash
cd /home/thomas/rrdtool

# Read temperature from sensor
tempread=`cat /home/thomas/owfs/10.4F7494010800/temperature` 

temp=`echo $tempread | cut -c -4`

# Update database
rrdtool update database.rrd N:$temp

# Create graphs
rrdtool graph temp_h.png --start -1h DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_d.png --start -1d DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_w.png --start -1w DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_m.png --start -1m DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_y.png --start -1y DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
#0000FF means blue trace color in the graphs.

(You can find more information about owfs in this post: How to Read 1-Wire Temperature Using a NSLU2 With Debian )

echo and cut is used to discard some of the excessive decimals, so that the stored value in the database is for example 23.8 instead of 23.875.

The 5 rrdtool graph commands will create graphs, where the timespan is h = hour, d = day, w = week, m = month and y = year.

A directory listing shows that the update script was created:

$ ls -la
total 32
drwxr-xr-x 2 thomas thomas  4096 Sep 15 19:04 .
drwxr-xr-x 6 thomas thomas  4096 Sep 15 17:49 ..
-rwxr-xr-x 1 thomas thomas   208 Sep 15 17:52 create_database.sh
-rw-r--r-- 1 thomas thomas 13764 Sep 15 18:50 database.rrd
-rw-r--r-- 1 thomas thomas   843 Sep 15 19:05 update_database.sh

You’ll have to modify the permissions of the update script to be able to run it:

$ chmod +x update_database.sh

If everything goes well when you run the script the only output is the dimensions of the 5 graphs:

$ ./update_database.sh
481x163
481x163
481x163
481x163
481x163

and a directory listing now shows that images files have been generated (.png stands for Portable Network Graphics):

$ ls -la
total 76
drwxr-xr-x 2 thomas thomas  4096 Sep 15 19:05 .
drwxr-xr-x 6 thomas thomas  4096 Sep 15 17:49 ..
-rwxr-xr-x 1 thomas thomas   208 Sep 15 17:52 create_database.sh
-rw-r--r-- 1 thomas thomas 13764 Sep 15 19:05 database.rrd
-rw-r--r-- 1 thomas thomas  7206 Sep 15 19:05 temp_d.png
-rw-r--r-- 1 thomas thomas  7966 Sep 15 19:05 temp_h.png
-rw-r--r-- 1 thomas thomas  7951 Sep 15 19:05 temp_m.png
-rw-r--r-- 1 thomas thomas  8040 Sep 15 19:05 temp_w.png
-rw-r--r-- 1 thomas thomas  8630 Sep 15 19:05 temp_y.png
-rwxr-xr-x 1 thomas thomas   843 Sep 15 19:05 update_database.sh

You could probably ask rrdtool to generate .jpg files instead if you can find a way to do so in the manual.

Now the database has been updated with a single snapshot of the temperature but we would like to continuously capture temperature data and keep the graphs updated. For this purpose we can use Linux crontab:

Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.

– Wikipedia

The -e option is for editing:

$ crontab -e
no crontab for thomas - using an empty one
crontab: installing new crontab

Every 5 minutes the update script is run, and any output messages are discarded:

# m h  dom mon dow   command
*/5 * * * * /home/thomas/rrdtool/update_database.sh &> /dev/null

If you want to check what jobs are installed you can use the -l option:

$ crontab -l

Since the NSLU2 doesn’t have any monitor connection on it I need to get the graphs copied to a computer with a screen to be able to see the results. For that I use FTP:

$ cd /home/thomas/rrdtool
$ ftp

Replace SERVER and USERNAME below with your own details:

ftp> open
(to) SERVER
Connected to SERVER.
Name (SERVER:thomas): USERNAME
ftp> mkdir temp
ftp> cd temp
ftp> put temp_d.png
ftp> put temp_h.png
ftp> put temp_m.png
ftp> put temp_w.png
ftp> put temp_y.png
ftp> exit

You could also copy the image files to a USB memory stick and from there onto your PC.

From my server on the Internet I can now download and view the generated graphs.

The first one (temp_h.png) covers only 1 hour in time and the air temperature is very stable over the hour so this particular graph is not very informative:

temp_d.png is more interesting as you can begin to see changes in the temperature:

temp_w.png needs a lot more time before it has been filled up with data, but if the system is stable the graph will be completed in about a week:

temp_m.png and temp_y.png are long term graphs but quite interesting to look at when they have been drawn, especially if you mounted the temperature sensor outdoors. There’s nothing to see yet though, because the system is new:

I’m going to add more and different kinds of sensors to the NSLU2 logging system in the coming weeks.