#!/usr/bin/perl -w # # Check humidity of 1Wire device # Requires use of Fuse and owfs # # # By Peter Andersson # peter@it-slav.net # http://www.it-slav.net/blogs/?p=802 # 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 $humidity; 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-wirehumid [-h] -c lowhumidcritical -w lowhumidwarning -W highhumidwarning -C highhumidcritical -i id [-o owserver:port]\n"; print "-h Help, this text\n-c num Critical threshold for low humidity\n-w num Warning threshold for low humidity\n"; print "-W num Warning threshold for high humidity\n-C num Critical threshold for high humidity\n"; print "-o :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\n"; exit $status; } #sanity check if (!$opt_c||!$opt_w||!$opt_W||!$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; } $humidity = OW::get("$opt_i/humidity"); $humidity =~ s/^\s*(.*?)\s*$/$1/; #remove whitespaces unless (($humidity =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) || ($humidity =~ /^[+-]?\d+$/)) #check that it is an integer or decimal returned { $message="Did not got an integer or a decimal from humidity probe"; $status=$ERRORS{CRITICAL}; } $humidity = sprintf("%.0f", $humidity); 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 Humidity:$humidity\n"; } if ($humidity <= $opt_c) { $status=$ERRORS{CRITICAL}; $message="CRITICAL"; } elsif ($humidity > $opt_c && $humidity <= $opt_w) { $status=$ERRORS{WARNING}; $message="WARNING"; } elsif ($humidity > $opt_w && $humidity < $opt_W) { $status=$ERRORS{OK}; $message="OK"; } elsif ($humidity >= $opt_W && $humidity < $opt_C) { $status=$ERRORS{WARNING}; $message="WARNING"; } elsif ($humidity >= $opt_C) { $status=$ERRORS{CRITICAL}; $message="CRITICAL"; } else { #This should never happend $status=$ERRORS{UNKNOWN}; $message="UNKNOW"; } print "$message: $humidity \%\|humidity=$humidity\%\;$opt_c;$opt_w;$opt_W;$opt_C\n"; exit $status;