#!/usr/bin/perl use strict; use Net::SNMP qw (ticks_to_time); use Getopt::Long; # Nagios specific use lib "/usr/local/nagios/libexec"; use utils qw(%ERRORS $TIMEOUT); # SNMP Datas #my $sysUpTime = '1.3.6.1.2.1.1.3.0'; my $temperature = '1.3.6.1.4.1.22626.1.2.1.1.0'; # Globals my $Name='check_snmp_uptime'; my $Version='1.2'; my $o_host = undef; # hostname my $o_community = undef; # community my $o_port = 161; # port my $o_warnl = undef; # warning limit my $o_critl = undef; # critical limit my $o_warnh = undef; # warning limit my $o_crith = undef; # critical limit my $o_help= undef; # wan't some help ? my $o_verb= undef; # verbose mode my $o_version= undef; # print version my $o_timeout= 5; # Default 5s Timeout my $o_perf= undef; # Output performance data # SNMP V3 specific my $o_login= undef; # snmp v3 login my $o_passwd= undef; # snmp v3 passwd # functions sub p_version { print "$Name version : $Version\n"; } sub print_usage { print "Usage: $Name -H -C | (-l login -x passwd) [-p ] -m -lw -lc -hw -hc [-t ] [-r] [-s] [-i] [-e]\n"; } sub round ($$) { sprintf "%.$_[1]f", $_[0]; } # Get the alarm signal (just in case snmp timout screws up) $SIG{'ALRM'} = sub { print ("ERROR: General time-out (Alarm signal)\n"); exit $ERRORS{"UNKNOWN"}; }; sub help { print "\nSNMP Temperature Monitor for Nagios version ",$Version,"\n"; print "(c)2008 - Author : Seth Mos\n\n"; print_usage(); print < \$o_verb, 'verbose' => \$o_verb, 'h' => \$o_help, 'help' => \$o_help, 'H:s' => \$o_host, 'hostname:s' => \$o_host, 'p:i' => \$o_port, 'port:i' => \$o_port, 'C:s' => \$o_community, 'community:s' => \$o_community, 'l:s' => \$o_login, 'login:s' => \$o_login, 'x:s' => \$o_passwd, 'passwd:s' => \$o_passwd, 's:s' => \$o_critl, 'critlow:s' => \$o_critl, 'd:s' => \$o_warnl, 'warnlow:s' => \$o_warnl, 'f:s' => \$o_warnh, 'warnhigh:s' => \$o_warnh, 'g:s' => \$o_crith, 'crithigh:s' => \$o_crith, 't:i' => \$o_timeout, 'timeout:i' => \$o_timeout, 'V' => \$o_version, 'version' => \$o_version, 'f' => \$o_perf, 'perfparse' => \$o_perf ); if (defined($o_help) ) { help(); exit $ERRORS{"UNKNOWN"}}; if (defined($o_version) ) { p_version(); exit $ERRORS{"UNKNOWN"}}; # check snmp information if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) ) { print "Put snmp login info!\n"; print_usage(); exit $ERRORS{"UNKNOWN"}} # Check compulsory attributes if ( ! defined($o_host) || !defined($o_warnl) || !defined($o_warnh) || !defined($o_critl) || !defined($o_warnh)) { print_usage(); exit $ERRORS{"UNKNOWN"}}; # Check for positive numbers if (($o_warnl < 0) || ($o_critl < 0)) { print " warn and critical < 0 \n";print_usage(); exit $ERRORS{"UNKNOWN"}}; # Check warning and critical values if ($o_warnl <= $o_critl) { print " warn low < crit low\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; if ($o_warnh >= $o_crith) { print " warn high > crit high\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; if ( ($o_warnl < 0 ) || ($o_critl < 0 )) { print "warn low and crit low must be > 0\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; if ( ($o_warnh < 0 ) || ($o_crith < 0 )) { print "warn high and crit high must be > 0\n";print_usage(); exit $ERRORS{"UNKNOWN"}}; } ########## MAIN ####### check_options(); # Check gobal timeout if (defined($TIMEOUT)) { verb("Alarm at $TIMEOUT"); alarm($TIMEOUT); } else { verb("no timeout defined : $o_timeout + 10"); alarm ($o_timeout+10); } # Connect to host my ($session,$error); if ( defined($o_login) && defined($o_passwd)) { # SNMPv3 login verb("SNMPv3 login"); ($session, $error) = Net::SNMP->session( -hostname => $o_host, -version => '3', -username => $o_login, -authpassword => $o_passwd, -authprotocol => 'md5', -privpassword => $o_passwd, -timeout => $o_timeout, -translate => [ -timeticks => 0x0 # Turn off so sysUpTime is numeric ] ); } else { # SNMPV1 login ($session, $error) = Net::SNMP->session( -hostname => $o_host, -community => $o_community, -port => $o_port, -timeout => $o_timeout, -translate => [ -timeticks => 0x0 # Turn off so sysUpTime is numeric ] ); } if (!defined($session)) { printf("ERROR: %s.\n", $error); exit $ERRORS{"UNKNOWN"}; } if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $result = $session->get_request( Varbindlist => [$temperature] ); if (!defined($result)) { printf("ERROR: Size table :%s.\n", $session->error); $session->close; exit $ERRORS{"UNKNOWN"}; } $session->close; # Only a few ms left... alarm(0); my $temp = $result->{$temperature}; my $warn_state_low=0; my $crit_state_low=0; my $warn_state_high=0; my $crit_state_high=0; my $perf_out= undef; my ($p_warnl,$p_critl,$p_warnh,$p_crith); printf("Temperature for host '%s' is %s\n", $session->hostname, $temp ); $p_warnl=$o_warnl; $p_critl=$o_critl; $p_warnh=$o_warnh; $p_crith=$o_crith; ( ($temp <= $o_critl) && ($crit_state_low=1) ) || ( ($temp <= $o_warnl) && ($warn_state_low=1) ); ( ($temp >= $o_crith) && ($crit_state_high=1) ) || ( ($temp >= $o_warnh) && ($warn_state_high=1) ); # add a ',' if some data exists in $perf_out $perf_out .= " " if (defined ($perf_out)) ; ##### Ouputs ##### TODO : subs "," with something $perf_out .= "'temperature'=" . round($temp,1) .";". round($p_warnl,1) . ";" . round($p_warnh,1) . ";" . round($p_critl,1) . ";" . round($p_crith,1); verb ("Perf data : $perf_out"); my $comp_oper=undef; if ( $crit_state_low == 1) { $comp_oper ="<"; (defined($o_perf)) ? print " (",$comp_oper," ",$o_critl,") : CRITICAL | ",$perf_out,"\n" : print " (",$comp_oper," ",$o_critl,") : CRITICAL\n"; exit $ERRORS{"CRITICAL"}; } if ( $crit_state_high == 1) { $comp_oper =">"; (defined($o_perf)) ? print " (",$comp_oper," ",$o_critl,") : CRITICAL | ",$perf_out,"\n" : print " (",$comp_oper," ",$o_critl,") : CRITICAL\n"; exit $ERRORS{"CRITICAL"}; } if ( $warn_state_low == 1) { $comp_oper ="<"; (defined($o_perf)) ? print " (",$comp_oper," ",$o_warnl,") : WARNING | ",$perf_out,"\n" : print " (",$comp_oper," ",$o_warnl,") : WARNING\n"; exit $ERRORS{"WARNING"}; } if ( $warn_state_high == 1) { $comp_oper =">"; (defined($o_perf)) ? print " (",$comp_oper," ",$o_warnh,") : WARNING | ",$perf_out,"\n" : print " (",$comp_oper," ",$o_warnh,") : WARNING\n"; exit $ERRORS{"WARNING"}; } $comp_oper =">"; (defined($o_perf)) ? print " (",$comp_oper," ",$o_warnl,") : OK | ",$perf_out,"\n" : print " (",$comp_oper," ",$o_warnl,") : OK\n"; exit $ERRORS{"OK"};