Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_temp_cpq
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
13 # Note: CPQ starts numbering sensors etc. with "1"
15 BEGIN {
16         if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
17                 $runtimedir = $1;
18                 $PROGNAME = $2;
19         }
20 }
24 require 5.004;
25 use POSIX;
26 #use strict;
27 use Getopt::Std ;
28 use BER;
29 require 'SNMP_Session.pm';
30 use vars qw($opt_H $opt_p $opt_C $opt_s $opt_w $opt_c $opt_h $PROGNAME);
31 use lib $main::runtimedir;
32 use utils qw($TIMEOUT %ERRORS &print_revision &usage &support );
34 use snmputil qw(%CPQ_LOCALE %CPQ_FAN_PRESENT %CPQ_FAN_OVERALL_COND %CPQ_FAN_SPEED);
36 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};   # Make %ENV safer
39 getopts('H:p:C:s:w:c:hV') ;
41 my $ip_address=undef ;
43 if ($opt_h)  {&help();}
45 if ($opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]*(\.[a-zA-Z][-a-zA
46 -Z0-9]*)*)$/) {
47         $ip_address = $opt_H ;
48         }
49 else {
50         usage();
51         print "IP-Address format wrong\n";
52         exit $ERRORS{'UNKNOWN'};
53         }       
55 #if ($opt_p =~ m/^[0-9]
57 my $port = $opt_p;
59 my $community = $opt_C;
61 my $sensor  = $opt_s ;
63 my $warning = $opt_w;
65 my $critical = $opt_c;
68         my $temperature_locale_oid    = encode_oid (1,3,6,1,4,1,232,6,2,6,8,1,3,0,$sensor );
69 # not used for the moment - gives no usable output
70 # if reused, enter at end of list to avoid renumbering !
71         my $temperature_celsius_oid   = encode_oid (1,3,6,1,4,1,232,6,2,6,8,1,4,0,$sensor );
72         my $temperature_threshold_oid = encode_oid (1,3,6,1,4,1,232,6,2,6,8,1,5,0,$sensor );
73         my $temperature_condition_oid = encode_oid (1,3,6,1,4,1,232,6,2,6,8,1,6,0,$sensor );
74         my $count=1 ;
75         my $label ;
76         my @r_array=();
77         my $q ;
78         my $diff ;
79         $warning=$warning/100 ;
80         $crititcal=$critical/100 ;
83 # get temperature, temperature_threshold bfore shutdown 
84         my $session=SNMP_Session->open ($ip_address, $community, $port) || die "couldn't open SNMP-session to host" ;
86         if ($session->get_request_response ($temperature_celsius_oid, $temperature_threshold_oid, $temperature_condition_oid, $temperature_locale_oid )) {
87                 (my $bindings) = $session->decode_get_response ($session->{pdu_buffer});
88                 while ($bindings ne '') {
89                         ($binding, $bindings) = &decode_sequence ($bindings) ;
90                         ($oid,$value) = &decode_by_template ($binding,"%O%@");
91                         $r_array[$count]=&pretty_print($value);
92                         $count++;
93                         }
94         } else {
95                 print "No response from agent\n";
96                 exit $ERRORS{'CRITICAL'};
97         }
98         $result_celsius=$r_array[1];
99         $result_threshold=$r_array[2];
100         $result_condition=$r_array[3];
101         $result_locale=$r_array[4];
103         if ($result_celsius < 0) {
104                 print "Result is negative - Sensor unavailable ?\n";
105                 exit $ERRORS{'UNKNOWN'};
106                 }
107         if ($result_threshold==0) {
108                 print "Division by zero   \n";
109                 exit $ERRORS{'CRITICAL'};
110                 }
112         if ($result_condition=="2") {
113                 $result_condition="OK";
114                 }
115         else {
116                 $result_condition=$result_condition." (other)";
117                 }
118         $q=$result_celsius/$result_threshold ;
119         $diff=$result_threshold-$result_celsius ;
120         
122         if ( $q > $critical ) {
123                 print "Sensor ". $sensor . " (".$CPQ_LOCALE{$result_locale}.") - Critical: ".$result_celsius." °C  - Threshold: ".$result_threshold." °C - Left before shutdown: ".$diff."°C - Overall condition: ". $result_condition ."\n" ; 
124                 exit $ERRORS{'CRITICAL'}  ; 
125         }
126         elsif ( $q > $warning ) {
127                 print "Sensor ". $sensor . " (".$CPQ_LOCALE{$result_locale}.") - Warning: ".$result_celsius." °C  - Threshold: ".$result_threshold." °C - Left before shutdown: ".$diff."°C - Overall condition: ". $result_condition ."\n" ; 
128                 exit $ERRORS{'WARNING'} ;
129         }
130         else {
131                 print "Sensor " .$sensor. " (".$CPQ_LOCALE{$result_locale}.") - OK: ".$result_celsius." °C  - Threshold: ".$result_threshold." °C - Left before shutdown: ".$diff."°C - Overall 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> [-w <warn>] [-c <crit>]\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 "<sensornumber>   = Sensornumber (1, 2, 3 etc.)\n";
149         print "<warn>           = report warning when more than <warn> % of the temperature is reached  defaults to 80\n";
150         print "<crit>           = report critical when more than <crit> % of the temperature is reached  defaults to 90\n";
151         }
153 sub version () {
154         print_revision($PROGNAME,'$Revision: 1113 $ ');
155         exit $ERRORS{'OK'};
158 sub help () {
159         print_help();
160         exit $ERRORS{'OK'};