#!/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

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;