Welcome to It-Slav.Net blog
Peter Andersson
peter@it-slav.net

I've already got a female to worry about. Her name is the Enterprise.
-- Kirk, "The Corbomite Maneuver", stardate 1514.0
05
Nov

After succefully get your 1-wire bus working with OWFS and been able to read the temperatures, it is time to create some nice graphs. The red line is the temperature and the blue is the sliding avarage temperature the last 24 hours. I’m using RRDTools to store the data and create the graphs. If you want to use RRDTools and RRDGraph for your own purposes I recommend you to read the manual pages several times and start easy and build more and more complex solutions.

Here is the script that I’m using to read the temperatures, store the data in a round robin database and finally create the graphs.

#!/usr/bin/perl
# By Peter Andersson peter@it-slav.net
# rrd_tempsensor.pl
use RRDs;
use OW;
# define location of rrdtool databases
my $rrd = '/root/owfs/script/rrd_db';
# define location of images
my $img = '/home/peter/public_html/temps';
# process data for each interface (add/delete as required)
&ProcessSensor(0, "Temperatur i garaget", "127.0.0.1" , "3001" , "10.87507C010800");
&ProcessSensor(1, "Temperatur i PDCn", "op5.mynet",3001, "10.04E060010800");
&ProcessSensor(2, "Temperatur ute", "op5.mynet",3001,"10.DEF05F010800");
sub ProcessSensor
{
# process sensor
# inputs: $_[0]: sensor number (ie, 0/1/2/etc)
#         $_[1]: sensor description
        print "number:$_[0] desc:$_[1], server:$_[2] port:$_[3], id: $_[4]\n";
        my $owserver = "$_[2]:$_[3]";
        unless(OW::init($owserver)) {
            $status = $ERRORS{CRIT};
            $message = "OWServer not running at $owserver\n";
            exit $status;
        }
        # get temperature from sensor
        my $handle = OW::get("$_[4]/temperature");
#       print "handle=$handle\n";
        $handle =~ s/^\s*(.*?)\s*$/$1/;
        ## Check if input is an integer or decimal
        unless (($handle =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) || ($handle =~ /^[+-]?\d+$/))
        {
                print "Not an integer or a decimal\n";
                return($ERRORS{CRITICAL});
        }
        $temp=$handle;
        # remove eol chars
        chomp($temp);
        print "sensor $_[0]: $temp degrees C\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 300",
                        "DS:temp:GAUGE:600: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";
        }
        else
        {
                # 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"; }
        }
        # create graphs for current sensor
        &CreateGraph($_[0], "day", $_[1]);
        &CreateGraph($_[0], "week", $_[1]);
        &CreateGraph($_[0], "month", $_[1]);
        &CreateGraph($_[0], "year", $_[1]);
}
sub CreateGraph
{
# creates graph
# inputs: $_[0]: sensor number (ie, 0/1/2/etc)
#         $_[1]: interval (ie, day, week, month, year)
#         $_[2]: sensor description
        RRDs::graph "$img/temp$_[0]-$_[1].png",
                "-s -1$_[1]",
                "-t $_[2]",
                "-h", "150", "-w", "475",
                "-a", "PNG",
                "-v WWW.It-Slav.Net",
                "--slope-mode",
                "DEF:temp=$rrd/temp$_[0].rrd:temp:AVERAGE",
                "LINE2:temp#FF0000::",
                "CDEF:tempavarage=temp,86400,TREND",
                "LINE1:tempavarage#000088::",
                "GPRINT:temp:MIN:  Min\\: %6.1lf",
                "GPRINT:temp:MAX:  Max\\: %6.1lf",
                "GPRINT:temp:AVERAGE: Snitt\\: %6.1lf",
                "GPRINT:temp:LAST: Nuvarande\\: %6.1lf grader C\\n";
        if ($ERROR = RRDs::error) { print "$0: unable to generate sensor $_[0] $_[1] graph: $ERROR\n"; }
}

Use crontab to run it every 5 minutes:

*/5     *       *       *       *       /root/owfs/script/tempgraph.pl > /dev/null

5 Responses to “Temperature Graphs”

  1. Eduard Says:

    Using this script i get an error: Can’t locate OW.pm in @INC

    I installed perl and owfs. What i’m i missing?

  2. peter Says:

    Have you installed the owfs perl package?
    If yes, which version of owfs are you running?
    In the the script I’m using 2.7, newer versions have another syntax.

    /Peter

  3. 1wire attic cooling at Raging Computer Says:

    […] The graphing is just a modified version of http://www.it-slav.net/blogs/?p=75 […]

  4. temp master Says:

    hello,
    what are the syntah for 2.8.x version ?

  5. peter Says:

    Have you looked in the manual?

Leave a Reply

You must be logged in to post a comment.





Book reviews
FreePBX 2.5
Powerful Telephony Solutions






Asterisk 1.6
Build a feature rich telephony system with Asterisk






Learning NAGIOS 3.0





Cacti 0.8 Network Monitoring,
Monitor your network with ease!