#! /usr/bin/perl -w # check_scmxx_info Copyright (C) 2006 Stephan Lauffer # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # you should have received a copy of the GNU General Public License # along with this program (or with Nagios); if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA use strict; use Getopt::Long; use vars qw($PROGNAME); use lib "/usr/lib/nagios/plugins"; use utils qw ($TIMEOUT %ERRORS &print_revision &support); sub print_help (); sub print_usage (); my ( # nagios plugin default options... $opt_h, $opt_V, $opt_t, $verbose, # check_scmxx_info options... $opt_d, $opt_s, $opt_S, $opt_b, $opt_B, $DEFAULT_opt_s, $DEFAULT_opt_S, $DEFAULT_opt_b, $DEFAULT_opt_B, # others... $result, $myresult, $message, $version, $author, # scmxx related... $scmxx_bin, $scmxx_exec, $signal, $battery, $status, ); ########### # # configuration # PLEASE CHECK $tw_cli_bin AND $tw_cli_exec for your requirements! Any other variables should not be changed. # $scmxx_bin = '/usr/bin/scmxx'; $scmxx_exec = "/usr/bin/sudo"; $PROGNAME = "check_scmxx_info"; $version = "0.1"; $author = 'Copyright (C) 2006 Stephan Lauffer '; $opt_d = ""; $DEFAULT_opt_s = 90; $DEFAULT_opt_S = 110; $DEFAULT_opt_b = 60; $DEFAULT_opt_B = 30; $message = ""; $result = ""; $myresult = ""; $battery = ""; $signal = ""; $status = ""; # ############ Getopt::Long::Configure('bundling'); GetOptions( "V" => \$opt_V, "version" => \$opt_V, "v=i" => \$verbose, "verbose=i" => \$verbose, "h" => \$opt_h, "help" => \$opt_h, "d=s" => \$opt_d, "device=i" => \$opt_d, "s=s" => \$opt_s, "signal-warning=i" => \$opt_s, "S=s" => \$opt_S, "signal-critical=i" => \$opt_S, "b=s" => \$opt_b, "battery-warning=i" => \$opt_b, "B=s" => \$opt_B, "battery-critical=i" => \$opt_B, ); ############ # some checks... # # note: # first "bad" return code wins although further test will follow if ($opt_V) { print_revision($PROGNAME, "$version $author"); exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } if ($opt_d eq "") { $message .= "No device given\n"; $result or $result = 'UNKNOWN'; } elsif ($opt_d !~ /^\/dev\/[^\s]+$/) { $message .= "$opt_d ist not a valid device\n"; $result or $result = 'UNKNOWN'; } if ($scmxx_exec and ! -f $scmxx_exec) { $message .= "$scmxx_exec missing!\n"; $result or $result = 'UNKNOWN'; } if (! -f $scmxx_bin) { $message .= "$scmxx_exec missing!\n"; $result or $result = 'UNKNOWN'; } $message and print $message; $result and exit $ERRORS{$result}; # ############ ############ # main # start timeoutcheck $SIG{'ALRM'} = sub { print ("ERROR: No response from $PROGNAME (alarm)\n"); exit $ERRORS{"UNKNOWN"}; }; alarm($TIMEOUT); $result = "UNKNOWN"; # set state to unknow until we know a bit more... !$opt_s and $opt_s = $DEFAULT_opt_s; !$opt_S and $opt_S = $DEFAULT_opt_S; !$opt_b and $opt_b = $DEFAULT_opt_b; !$opt_B and $opt_B = $DEFAULT_opt_B; open (SCMXX, "export LANG=eng; $scmxx_exec $scmxx_bin --device $opt_d --info --quit 2>&1 |") or exit $ERRORS{"UNKNOWN"}; while () { chomp; !$_ and next; $verbose and $verbose > 1 and print "$_\n"; /^Status:\s+(.+)$/ and $status = $1; /^Signal:\s+-(\d+)\s+dBm$/ and $signal = $1; /^Battery:\s+(\d+)%$/ and $battery = $1; } close SCMXX; if ($signal and $status and $battery) { # critical states... # critical status (not checked in) if ($status ne "checked in") { $result = "CRITICAL"; } else { $myresult = "OK"; $result = $myresult; $message .= "Net-Status $myresult: $status, "; } # critical battery status if ($battery <= $opt_B) { $result = "CRITICAL"; $message .= "Battery $result: $battery%, "; } # critical Signal strength if ($signal >= $opt_S) { $result = "CRITICAL"; $message .= "Signal $result: -$signal dBm, "; } # warning states... only change $result until its OK|UNKNOWN # battery warning if ($battery > $opt_B and $battery <= $opt_b) { $myresult = "WARNING"; $result =~ /(UNKNOWN)|(OK)|(WARNING)/ and $result = $myresult; $message .= "Battery $myresult: $battery%, "; } # signal warning... # note: lower value is worse... if ($signal < $opt_S and $signal >= $opt_s) { $myresult = "WARNING"; $result =~ /(UNKNOWN)|(OK)|(WARNING)/ and $result = $myresult; $message .= "Signal $myresult: -$signal dBm, "; } # ok states... # signal ok... if ($signal < $opt_s) { $myresult = "OK"; $result =~ /(UNKNOWN)|(OK)/ and $result = $myresult; $message .= "Signal $myresult: -$signal dBm, "; } # battery ok... if ($battery > $opt_b) { $myresult = "OK"; $result =~ /(UNKNOWN)|(OK)/ and $result = $myresult; $message .= "Battery $myresult: $battery%, "; } } $message =~ s/, $//; # if the check of scmxx results didn't work, return a UNKNOWN stat, if not, # add the result state before the message: if (!$message) { $message = $result; } else { $message = "$result - $message"; } # $verbose unset or zero... short message: !$verbose and $message =~ s/ \-.*//; # disable alarm alarm(0); # return message and exit with returncode print "$PROGNAME: $message\n"; exit $ERRORS{$result}; # ############ ############ # subs sub print_usage () { print "Usage:\n"; print " $PROGNAME\t-d [-s ] [-S ]\n"; print "\t\t[-b ] [-B ] [-v <0-2>]\n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n\n"; print "NOTE:\tAlthough the real signal strenght is lower then 0, you must enter a positiv value\n"; print "\tA higher value describes a worse signal strength.\n\n"; print "\n"; print " -d | --device=STRING\n\tFor example: \"-d /dev/ttyUSB0\"\n"; print " -s | --signal-warning=INTEGER\n\tSignal strength warning. The higher the value, the worse the signal! (Default $DEFAULT_opt_s)\n"; print " -S | --signal-critical=INTEGER\n\tSignal strength critical. The higher the value, the worse the signal! (Default $DEFAULT_opt_S)\n"; print " -b | --battery-warning=INTEGER\n\tBattery load in percent (Default $DEFAULT_opt_b)\n"; print " -B | --battery-critical=INTEGER\n\tBattery load in percent (Default $DEFAULT_opt_B)\n"; print " -v | --verbose=INTEGER\n\t0 or unset: only report status (UNKNOWN|OK|WARNIG|CRITICAL).\n\t1: informations with network status, signal strength and battery load\n\t2: all smcxx infos to and others...\n"; print "\n"; } sub print_help () { print_revision($PROGNAME, "$version"); print "$author\n\n"; print "$PROGNAME was written to read informations from a Siemens Handy with scmxx on localhost.\n\n"; print_usage(); print "\n"; print " Example: ./check_scmxx_info -d /dev/ttyUSB0\n"; print "\n"; support(); } # ############ # eof