Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_temp_fsc
1 #!/usr/bin/perl 
2 #
3 #
4 # check_most.pl -i <ip address> -p <port> -c community -o <oid> [warn] [critical]
5 #
6 # NetSaint host script to get the disk usage from NT snmp
7 #
8 # Changes and Modifications
9 # =========================
10 # 3-Aug-2000 - Xavier Dusart
11 #                       Created
12 # 2003          - Rainer Duffner
14 BEGIN {
15         if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
16                 $runtimedir = $1;
17                 $PROGNAME = $2;
18         }
19 }
23 require 5.004;
24 use POSIX;
25 #use strict;
26 use Getopt::Std ;
27 use BER;
28 require 'SNMP_Session.pm';
29 use vars qw($opt_H $opt_p $opt_C $opt_s $opt_w $opt_c $opt_h $PROGNAME);
30 use lib $main::runtimedir;
31 use utils qw($TIMEOUT %ERRORS &print_revision &usage &support);
32 use snmputil qw(%FSC_LOCALE %FSC_TEMP_CONDITION);
34 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};   # Make %ENV safer
37 getopts('H:p:C:s:w:c:hV') ;
39 my $ip_address=undef ;
41 if ($opt_h)  {&help();}
43 if ($opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]*(\.[a-zA-Z][-a-zA
44 -Z0-9]*)*)$/) {
45         $ip_address = $opt_H ;
46         }
47 else {
48         usage();
49         print "IP-Address format wrong\n";
50         exit $ERRORS{'UNKNOWN'};
51         }       
53 #if ($opt_p =~ m/^[0-9]
55 my $port = $opt_p;
57 my $community = $opt_C;
59 my $sensor  = $opt_s ;
61 #my $warning = $opt_w;
63 #my $critical = $opt_c;
66         my $temperature_locale_oid    = encode_oid (1,3,6,1,4,1,231,2,10,2,2,5,2,1,1,4,0,$sensor-1 );
67 # not used for the moment - gives no usable output
68 # if reused, enter at end of list to avoid renumbering !
69         my $temperature_celsius_oid   = encode_oid (1,3,6,1,4,1,231,2,10,2,2,5,2,1,1,11,0,$sensor-1 );
70         my $temperature_warning_oid = encode_oid (1,3,6,1,4,1,231,2,10,2,2,5,2,1,1,6,0,$sensor-1 );
71         my $temperature_critical_oid =encode_oid (1,3,6,1,4,1,231,2,10,2,2,5,2,1,1,8,0,$sensor-1 );
72         my $temperature_condition_oid = encode_oid (1,3,6,1,4,1,231,2,10,2,2,5,2,1,1,3,0,$sensor-1 );
73         my $count=1 ;
74         my $label ;
75         my @r_array=();
76         my $q ;
77         my $diff ;
78         $warning=$warning/100 ;
79         $crititcal=$critical/100 ;
82 # get temperature, temperature_threshold bfore shutdown 
83         my $session=SNMP_Session->open ($ip_address, $community, $port) || die "couldn't open SNMP-session to host" ;
85         if ($session->get_request_response ($temperature_celsius_oid, $temperature_warning_oid, $temperature_critical_oid, $temperature_condition_oid, $temperature_locale_oid )) {
86                 (my $bindings) = $session->decode_get_response ($session->{pdu_buffer});
87                 while ($bindings ne '') {
88                         ($binding, $bindings) = &decode_sequence ($bindings) ;
89                         ($oid,$value) = &decode_by_template ($binding,"%O%@");
90                         $r_array[$count]=&pretty_print($value);
91                         $count++;
92                         }
93         } else {
94                 print "No response from agent\n";
95                 exit $ERRORS{'CRITICAL'};
96         }
97         $result_celsius=$r_array[1];
98         $result_warning=$r_array[2];
99         $result_critical=$r_array[3];
100         $result_condition=$r_array[4];
101         $result_locale=$r_array[5];
103         if ($result_celsius < 0) {
104                 print "Result is negative - Sensor unavailable ?\n";
105                 exit $ERRORS{'UNKNOWN'};
106                 }
107         if ($result_warning==0) {
108                 print "Division by zero   \n";
109                 exit $ERRORS{'CRITICAL'};
110                 }
112         if ($result_critical==0) {
113                 print "Division by zero   \n";
114                 exit $ERRORS{'CRITICAL'};
115                 }
116         
118 #       $q=$result_celsius/$result_threshold ;
119         $diff=$result_critical-$result_celsius ;
120         
122         if ( $result_celsius > $result_critical ) {
123                 print "Sensor ". $sensor . " (".$FSC_LOCALE{$result_locale}.") - Critical: ".$result_celsius." °C  - Crit-Threshold:".$result_critical." °C - Left before shutdown:".$diff."°C - Overall condition: ". $FSC_TEMP_CONDITION{$result_condition} ."\n" ; 
124                 exit $ERRORS{'CRITICAL'}  ; 
125         }
126         elsif ( $result_celsius > $result_warning ) {
127                 print "Sensor ". $sensor . " (".$FSC_LOCALE{$result_locale}.") - Warning: ".$result_celsius." °C  - Crit-Threshold:".$result_warning." °C - Left before shutdown:".$diff."°C - Overall condition: ". $FSC_TEMP_CONDITION{$result_condition}."\n" ; 
128                 exit $ERRORS{'WARNING'} ;
129         }
130         else {
131                 print "Sensor " .$sensor. " (".$FSC_LOCALE{$result_locale}.") - OK: ".$result_celsius." °C  - Warn-Threshold:".$result_warning." °C - Left before shutdown:".$diff."°C - Overall condition: ". $FSC_TEMP_CONDITION{$result_condition} ."\n" ; 
132                 exit $ERRORS{'OK'} ;
133         }       
136 sub print_usage () {
137         print "Usage: $PROGNAME -H <host> -p <port> -C <community> -s <sensornumber> \n";
138         }
140 sub print_help () {
141         print_revision($PROGNAME,'$Revision: 1113 $\n ');
142         print "Copyright (c) 2003 Rainer Duffner\n ";
143         print_usage();
144         print "\n";
145         print "<host>           = IP-Address or DNS-Name of the W2K-Server\n";
146         print "<port>           = SNMP-Port (normaly 161)\n";
147         print "<community>      = SNMP v1 community\n";
148         print "<drvnumber>      = Sensornumber (1, 2, 3 etc.)\n";
149         }
151 sub version () {
152         print_revision($PROGNAME,'$Revision: 1113 $ ');
153         exit $ERRORS{'OK'};
156 sub help () {
157         print_help();
158         exit $ERRORS{'OK'};