#!/usr/bin/perl # # copyright Martin Pot 2006-2009 # http://martybugs.net/electronics/tempsensor/ # # rrd_tempsensors.pl use RRDs; # define location of rrdtool databases my $rrd = '/var/lib/rrd'; # define location of images my $img = '/var/www/rrdtool'; # process data for each interface (add/delete as required) &ProcessSensor(0, "sensor 1"); &ProcessSensor(1, "sensor 2"); &ProcessSensor(2, "sensor 3"); sub ProcessSensor { # process sensor # inputs: $_[0]: sensor number (ie, 0/1/2/etc) # $_[1]: sensor description # get temperature from sensor my $temp = `/usr/bin/digitemp -t $_[0] -q -c /etc/digitemp.conf -o%C`; # remove eol chars chomp($temp); print "sensor $_[0]: $temp degrees C ($_[1])\n"; # if rrdtool database doesn't exist, create it if (! -e "$rrd/temp$_[0].rrd") { print "creating rrd database for temp sensor $_[0]...\n"; RRDs::create "$rrd/temp$_[0].rrd", "-s", "60", "DS:temp:GAUGE:240:U:U", "RRA:AVERAGE:0.5:1:2016", "RRA:AVERAGE:0.5:6:1344", "RRA:AVERAGE:0.5:24:2190", "RRA:AVERAGE:0.5:144:3650"; } if ($ERROR = RRDs::error) { print "$0: failed to create $_[0] database file: $ERROR\n"; } # check for error code from temp sensor if (int $temp eq 85) { print "failed to read value from sensor $_[0]\n"; $temp = "U"; } # insert values into rrd RRDs::update "$rrd/temp$_[0].rrd", "-t", "temp", "N:$temp"; if ($ERROR = RRDs::error) { print "$0: failed to insert $_[0] data into rrd: $ERROR\n"; } } &CreateGraph($_[0], "day", $_[1]); &CreateGraph($_[0], "week", $_[1]); &CreateGraph($_[0], "month", $_[1]); &CreateGraph($_[0], "year", $_[1]); sub CreateGraph { # creates graph # inputs: $_[0]: n/a # $_[1]: interval (ie, day, week, month, year) # $_[2]: graph description RRDs::graph "$img/temps-$_[1].png", "-s -1$_[1]", "-t DS18S20 temperature sensors :: last $_[1] (1 minute samples)", "--lazy", "-h", "200", "-w", "800", "-l", "0", "-a", "PNG", "-v degrees Celsius", "--slope-mode", "--color", "SHADEA#ffffff", "--color", "SHADEB#ffffff", "--color", "BACK#ffffff", "--color", "CANVAS#ffffff", "DEF:temp0=$rrd/temp0.rrd:temp:AVERAGE", "DEF:temp1=$rrd/temp1.rrd:temp:AVERAGE", "DEF:temp2=$rrd/temp2.rrd:temp:AVERAGE", "LINE2:temp0#33CC00:sensor 1\\: ", "GPRINT:temp0:MIN: Min\\: %5.2lf", "GPRINT:temp0:MAX: Max\\: %5.2lf", "GPRINT:temp0:AVERAGE: Average\\: %5.2lf", "GPRINT:temp0:LAST: Current\\: %5.2lf degrees Celsius\\n", "LINE2:temp1#990000:sensor 2\\: ", "GPRINT:temp1:MIN: Min\\: %5.2lf", "GPRINT:temp1:MAX: Max\\: %5.2lf", "GPRINT:temp1:AVERAGE: Average\\: %5.2lf", "GPRINT:temp1:LAST: Current\\: %5.2lf degrees Celsius\\n", "LINE2:temp2#00FF00:sensor 3\\: ", "GPRINT:temp2:MIN: Min\\: %5.2lf", "GPRINT:temp2:MAX: Max\\: %5.2lf", "GPRINT:temp2:AVERAGE: Average\\: %5.2lf", "GPRINT:temp2:LAST: Current\\: %5.2lf degrees Celsius\\n"; if ($ERROR = RRDs::error) { print "$0: unable to generate sensor $_[0] $_[1] graph: $ERROR\n"; } }