Data logging and graphing using RRDtool

hourlyWhen it comes to logging some data over a long period of time, RRDtool is a convenient solution. Creating and updating the RRD database is simple, graphing is fairly easy as well. To make the graphs available over the web, I chose to install the lighttpd web server.

Getting the packages

opkg install rrdtool lighttpd

After installing lighttpd, I changed its port to 8090 to avoid interference with the service running on port 80. In /etc/lighttpd.conf:

## bind to port (default: 80)
server.port                = 8090

In the same file you can change the document-root if you wish, I left it unchanged:

## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
server.document-root        = "/www/pages/"

The scripts presented below are available in my GitHub repository: https://github.com/donfuge/ADS1248/.

Building the database

First of all, edit the LOG_CONFIG file:

#!/bin/bash

RRD_DB="/home/root/rrd_db/temperature.rrd"
GRAPH_DIR="/www/pages/"
GET_TEMP="/home/root/ADS1248/getTemperature.py"

The variable RRD_DB contains the path of the RRD database file, it will be created by create_db.sh. The create_graphs.sh script will create the graph images in the directory defined by GRAPH_DIR. I set it to be the default root directory of lighttpd. GET_TEMP is the path of the executable which supplies the raw data. It must have an output format compatible with the rrdtool update syntax. In my case it is a sligthly modified version of the ADS1248_minimal.py presented in the previous post.

create_db.sh:

#!/bin/bash
source $(dirname $0)/LOG_CONFIG

rrdtool create $RRD_DB --start N --step 60 \
DS:temperature:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:60 \
RRA:AVERAGE:0.5:10:144 \

We create the RRD database starting from now (N), it will accept a new data point every 60 seconds. The name of the data source (DS) is “temperature”, it is of GUAGE type, RRD will consider it unkown if we don’t supply new data for 600 seconds. We store every the raw data of the past hour (averaging of 1, 60 samples) and the past day with averaging of 10 (144 samples).

Updating the database and graphing
rrd_update_temp.sh:

#!/bin/bash
source $(dirname $0)/LOG_CONFIG

TEMP=$($GET_TEMP)
rrdtool update $RRD_DB N:$TEMP

This code simply invokes our executable supplying the data point (a number) and adds it to the RRD database.

create_graphs.sh:

#!/bin/bash
source $(dirname $0)/LOG_CONFIG

HOUR=3600
DAY=$((24*$HOUR))
YEAR=$((365*$DAY))

COLOR="#CC0000"

# past hour
rrdtool graph $GRAPH_DIR/hourly.png  \
--width 800 --height 200 \
--start -$HOUR  --end -1 \
--vertical-label "Temperature" \
--title "Inside temperature" \
DEF:temperature=$RRD_DB:temperature:AVERAGE \
LINE1:temperature$COLOR:"Temperature"

# past day
rrdtool graph $GRAPH_DIR/daily.png \
--width 800 --height 200 \
--start -$DAY  --end -1 \
--vertical-label "Temperature" \
--title "Inside temperature" \
DEF:temperature=$RRD_DB:temperature:AVERAGE \
LINE1:temperature$COLOR:"Temperature"

This script creates two graphs for us, one about the past hour and another one about the past day:

hourly

daily

Simple HTML code to show both graphs on a single web page:

<html><body><h1>Temperature</h1>

<img src="hourly.png">
<p>
<img src="daily.png">
</body></html>

Save it as index.html in the document-root of the web server, then check the web page on http://beagleboneIP:PORT. If your BeagleBone is behind a router and you want to access the webpage you have just created from anywhere, you have to set up port forwarding on your router. Furthermore, you may register for a dynamic DNS service to make yourself able using a domain name instead of the WAN IP of your router.

Making it automatic

To make the logging and graph publishing run every minute, add the following two lines to /etc/crontab with the proper paths:

*/1 * * * * root /home/root/ADS1248/rrd_update_temp.sh
*/1 * * * * root /home/root/ADS1248/create_graphs.sh

References

http://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html
http://blog.turningdigital.com/2012/09/raspberry-pi-ds18b20-temperature-sensor-rrdtool/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s