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

After got the 1-wire stuff working it is time to use the temperature probes to get some use of measuring your environment. This article describe how you can create an op5 Monitor or Nagios plugin to monitor your temperatures.

Read my previous articles to get the 1-wire bus up and running:

I found a 1-wire temperature Nagios plugin on internet, but it had some disadvantages:

  • Used Fahrenheit instead of Celsius
  • Did not accept thresholds below zero degrees, in Sweden that is quite common temperature during winter.
  • Strange License, it just said Copyright and a name and email address that bounced.

The Nagios plugin was written in Perl so it was straight forward to modify and fix the obstacles I found. But the copyright bothered me because I wanted to publish the changes, I tried to email the email address I found in the script but it bounced, so I decided to write my own plugin.

I have published the script at nagiosexchange.

The files

The plugin check_1-wiretemp version 1.0 can be found here or below.

#!/usr/bin/perl -w
#
# Check temperature of 1Wire device
# Requires use of owfs
#
# By Peter Andersson
# peter@it-slav.net
# http://www.it-slav.net/blogs/?p=115
# Licence GPLv2
# Version 1.0

use strict;
use Getopt::Std;
use OW;

my $owserver = "127.0.0.1:3001";
my(%ERRORS) = ( OK=>0, WARNING=>1, CRITICAL=>2, UNKNOWN=>3 );

my $temperature;
my $status=$ERRORS{OK};;
my $message;

my $debug_flag=0;

our($opt_c, $opt_w, $opt_W, $opt_C, $opt_h, $opt_o, $opt_i);

getopts("w:W:c:C:ho:i:");

sub printhelp () {
        print "Usage: check_1-wiretemp [-h] -c lowtempcritical -w lowtempwarning -W higtempwarning -C higtempcritical -i id [-o owserver:port]\n";
        print "-h Help, this text\n-c num Critical threshold for low temp\n-w num Warning threshold for low temp\n";
        print "-W num Warning threshold for hightemp\n-C num Critical threshold for hightemp\n";
        print "-o <name|ip>:port, Servername of 1-wire server and portnumber the owserver use, default 127.0.0.1:3001\n";
        print "-i 1-wire ID, i.e. 10.DEF05F01080015\n";
        print "\n\t\tby Peter Andersson\n\t\tpeter\@it-slav.net\n\t\thttp://www.it-slav.net/blogs/?p=115\n";
        if ($debug_flag) {
                print "opt_c:$opt_c opt_w:$opt_w opt_W:$opt_W opt_C:$opt_C opt_h:$opt_h owserver:$owserver opt_i:$opt_i Temperture:$temperature\n";
        }

        exit $status;
}

#sanity check
if (!defined $opt_c||!defined $opt_w||!defined $opt_W||!defined $opt_C||$opt_h) {
        $status= $ERRORS{UNKNOWN};
        &printhelp;
} elsif ($opt_c > $opt_w) {
        print "Critical low threshold must be higher or equal to warning low threshold\n";
        $status= $ERRORS{UNKNOWN};
        &printhelp;
} elsif ($opt_w > $opt_W || $opt_c > $opt_C) {
        print "Lower tresholds must be lower then higher thresholds\n";
        $status= $ERRORS{UNKNOWN};
        &printhelp;
} elsif ($opt_C < $opt_W) {
        print "Higher critical threshold must be higher or equal to higher warning threshold\n";
        $status= $ERRORS{UNKNOWN};
        &printhelp;
}

if ($opt_o) {
        $owserver = $opt_o;
}

unless(OW::init($owserver)) {
        $status = $ERRORS{CRIT};
        $message = "OWServer not running at $owserver\n";
        exit $status;
}
$temperature = OW::get("$opt_i/temperature");
$temperature =~ s/^\s*(.*?)\s*$/$1/; #remove whitespaces
unless (($temperature =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) || ($temperature =~ /^[+-]?\d+$/)) #check that it is an integer or decimal returned
{
        $message="Did not got an integer or a decimal from temperature probe";
        $status=$ERRORS{CRITICAL};
}
$temperature = sprintf("%.2f", $temperature);

if ($debug_flag) {
        print "opt_c:$opt_c opt_w:$opt_w opt_W:$opt_W opt_C:$opt_C opt_h:$opt_h owserver:$owserver opt_i:$opt_i Temperture:$temperature\n";
}

if ($temperature <= $opt_c) {
        $status=$ERRORS{CRITICAL};
        $message="CRITICAL";
} elsif ($temperature > $opt_c && $temperature <= $opt_w) {
        $status=$ERRORS{WARNING};
        $message="WARNING";
} elsif ($temperature >  $opt_w && $temperature <  $opt_W) {
        $status=$ERRORS{OK};
        $message="OK";
} elsif ($temperature >= $opt_W && $temperature < $opt_C) {
        $status=$ERRORS{WARNING};
        $message="WARNING";
} elsif ($temperature >= $opt_C) {
        $status=$ERRORS{CRITICAL};
        $message="CRITICAL";
} else { #This should never happend
        $status=$ERRORS{UNKNOWN};
        $message="UNKNOW";
}

print "$message: $temperature C\|temperature=$temperature;$opt_c;$opt_w;$opt_W;$opt_C\n";
exit $status;

 

The Metadata in op5 Monitor /opt/monitor/etc/checkcommands.cfg or Nagios checkcommands.cfg:

# command 'check_1-wiretemp'
define command{
    command_name                   check_1-wiretemp
    command_line                   $USER1$/custom/check_1-wiretemp -c $ARG1$ -w $ARG2$ -W$ARG3$ -C$ARG4$ -i $ARG5$ -o $ARG6$
    }


The service command definitions /opt/monitor/etc/services.cfg:

# service 'Temperature Outdoor'
define service{
    use                            default-service
    host_name                      monitor
    service_description            Temperature Outdoor
    check_command                  check_1-wiretemp!-20!-15!25!30!10.DEF05F01080015!127.0.0.1:3001
    contact_groups                 call_it-slav,it-slav_mail
    }

The graph map /opt/monitor/op5/nagiosgraph/map_custom/1-wiretemp.map:

# peter@it-slav.net
# http://www.it-slav.net/blogs/?p=115
# Service type: 1-wire-temperature
#   output: OK: 22.88 C
#   perfdata: temperature=2.56;-20;-15;25;30
/perfdata:temperature=(-?\d+.\d+);(-?\d+);(-?\d+);(-?\d+);(-?\d+)/
and push @s, [ temperature,
               [ "current", GAUGE, $1 ],
               [ "critical_low", GAUGE, $2 ],
               [ "warning_low", GAUGE, $3 ],
               [ "warning_high", GAUGE, $4 ],
               [ "critical_high", GAUGE, $5 ] ];
Hint: If you do not want to graph the thresholds remove $2, $3 ,$4, $5 gauges

Step by step guide

Step by step guide for op5 Monitor users, Nagios (or op5 Monitor) users can copy and paste the information above to the configuration files:

1. Copy the plugin to the correct path, i.e. /opt/plugins/custom/

2. Test the plugin

[root@op5 custom]# ./check_1-wiretemp -c 0 -w 1 -W 20 -C 23 -i 10.DEF05F01080015
OK: 3.56 C|temperature=3.56;0;1;20;23

Yes it works!

3. Define the command in op5 Monitor (in Nagios edit the

configure->commands

Choose "Add new command"

Enter

command name: check_1-wiretemp

command line: $USER1$/custom/check_1-wiretemp -c $ARG1$ -w $ARG2$ -W$ARG3$ -C$ARG4$ -i $ARG5$ -o $ARG6$

Press Apply button

To test:

Press Test this command

Change $ARG1$ $ARG2$ and so on to:

/opt/plugins/custom/check_1-wiretemp -c 0 -w 1 -W20 -C23 -i 10.DEF05F01080015 -o 127.0.0.1:3001

Press continue

And the test works

Press "Save" to update the configuration files.

4. Configure the check_1-wiretemp

Press "Configure"

Pick the host, i.e. monitor

Press "Go"

Click "Services for monitor"

Choose "Add new service"

Service description i.e: Temperature Outdoor

check_command: check_1-wiretemp

check_command_args: -20!-15!25!30!10.DEF05F01080015!127.0.0.1:3001

(Critical low -20 C, Warning low -15 C, Warning high 25 C, Critical High 30 C, ID #10.DEF05F01080015, Host 127.0.0.1, port 3001)

Choose contact groups according to your preferences

To test, press "Test this service"

If everything looks fine press "Save" and your temperature service check is defined.

5. Create graphs

Copy and paste 1-wiretemp.map from above and put it in /opt/monitor/op5/nagiosgraph/map_custom/1-wiretemp.map

 

Congratulations, now you monitor your temperature and get alarms if it falls aout och thresholds.

 

Links

 


6 Responses to “op5 Monitor or Nagios plugin for 1-wire temperature measurement”

  1. Pages tagged "plugin" Says:

    […] bookmarks tagged plugin op5 Monitor or Nagios plugin for 1-wire temperatur… saved by 3 others     hannahmontana64 bookmarked on 11/23/08 | […]

  2. Nagios or op5 Monitor plugin for 1-wire humidity measurement | An It-Slave in the digital saltmine Says:

    […] In an earlier article I have described a 1-wire temperature plugin. […]

  3. carsten Says:

    hi peter,

    cool script … but

    ran into problems with the embedded perl in nagios

    # nagios: -epn

    in your script resolved the problem

    http://nagios.sourceforge.net/docs/3_0/embeddedperl.html

    have fun
    carsten

  4. Viola Says:

    Hello,
    got this message:

    root@raspberrypi:/opt/fhem# /usr/lib/nagios/plugins/check_1-wiretemp -o 127.0.0.1:4304 -i 10.67C6697351FF
    Can’t locate OW.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at /usr/lib/nagios/plugins/check_1-wiretemp line 13.
    BEGIN failed–compilation aborted at /usr/lib/nagios/plugins/check_1-wiretemp line 13.
    root@raspberrypi:/opt/fhem# mcedit /usr/lib/nagios/plugins/check_1-wiretemp

    Can you help me?

  5. peter Says:

    Have you installed OWFS including perlmodules?

  6. Viola Says:

    Okay 😀
    I’ve install:
    libow-perl and owfs-common, python-ow
    now it works. Don’t know which package was needet.
    Thank you!

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!