Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_maxwanstate.pl
1 #!/usr/bin/perl -w
2 #
3 # check_maxwanstate.pl - nagios plugin 
4
5 #
6 # Copyright (C) 2000 Christoph Kron
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 #
22 #
23 # Report bugs to: ck@zet.net
24 #
25 # 11.01.2000 Version 1.0
27 use strict;
29 use Net::SNMP;
30 use Getopt::Long;
31 &Getopt::Long::config('auto_abbrev');
34 my $status;
35 my $TIMEOUT = 1500;
37 my %ERRORS = ('UNKNOWN' , '-1',
38               'OK' , '0',
39               'WARNING', '1',
40               'CRITICAL', '2');
42 my %wanLineState = (
43             1,'ls-unknown',
44             2,'ls-does-not-exist',
45             3,'ls-disabled',
46             4,'ls-no-physical',
47             5,'ls-no-logical',
48             6,'ls-point-to-point',
49             7,'ls-multipoint-1',
50             8,'ls-multipoint-2',
51             9,'ls-loss-of-sync',
52             10,'ls-yellow-alarm',
53             11,'ls-ais-receive',
54             12,'ls-no-d-channel',
55             13,'ls-active',
56             14,'ls-maintenance');
58 my %wanLineType = (
59         '1.3.6.1.4.1.529.4.1','Any',
60         '1.3.6.1.4.1.529.4.2','T1',
61         '1.3.6.1.4.1.529.4.3','E1',
62         '1.3.6.1.4.1.529.4.4','Dpnss',
63         '1.3.6.1.4.1.529.4.5','Bri',
64         '1.3.6.1.4.1.529.4.6','S562',
65         '1.3.6.1.4.1.529.4.7','S564',
66         '1.3.6.1.4.1.529.4.8','Sdsl',
67         '1.3.6.1.4.1.529.4.9','AdslCap');
68         
69 my $state = "UNKNOWN";
70 my $answer = "";
71 my $snmpkey;
72 my $snmpoid;
73 my $key;
74 my $community = "public";
75 my $port = 161;
76 my @snmpoids;
77 my $snmpWanLineName = '1.3.6.1.4.1.529.4.21.1.2';
78 my $snmpWanLineType = '1.3.6.1.4.1.529.4.21.1.3';
79 my $snmpWanLineState = '1.3.6.1.4.1.529.4.21.1.5';
80 my $snmpWanLineUsage = '1.3.6.1.4.1.529.4.21.1.8';
82 my $hostname;
83 my $session;
84 my $error;
85 my $response;
86 my %wanStatus;
87 my $ifup =0 ;
88 my $ifdown =0;
89 my $ifdormant = 0;
90 my $ifmessage;
92 sub usage {
93   printf "\nMissing arguments!\n";
94   printf "\n";
95   printf "Perl Check maxwanstate plugin for Nagios\n";
96   printf "monitors E1/T1 interface status\n";
97   printf "usage: \n";
98   printf "check_maxwanstate.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>";
99   printf "Copyright (C) 2000 Christoph Kron\n";
100   printf "check_maxwanstate.pl comes with ABSOLUTELY NO WARRANTY\n";
101   printf "This programm is licensed under the terms of the ";
102   printf "GNU General Public License\n(check source code for details)\n";
103   printf "\n\n";
104   exit $ERRORS{"UNKNOWN"};
107 # Just in case of problems, let's not hang Nagios
108 $SIG{'ALRM'} = sub {
109      print ("ERROR: No snmp response from $hostname (alarm)\n");
110      exit $ERRORS{"UNKNOWN"};
111 };
112 alarm($TIMEOUT);
115 $status = GetOptions("community=s",\$community,
116                      "port=i",\$port);
117 if ($status == 0)
119         &usage;
121   
122    #shift;
123    $hostname  = shift || &usage;
127 push(@snmpoids,$snmpWanLineUsage);
128 push(@snmpoids,$snmpWanLineState);
129 push(@snmpoids,$snmpWanLineName);
130 push(@snmpoids,$snmpWanLineType);
132 foreach $snmpoid (@snmpoids) {
134    ($session, $error) = Net::SNMP->session(
135       -hostname  => $hostname,
136       -community => $community,
137       -port      => $port
138    );
140    if (!defined($session)) {
141       $state='UNKNOWN';
142       $answer=$error;
143       print ("$state: $answer");
144       exit $ERRORS{$state};
145    }
147    if (!defined($response = $session->get_table($snmpoid))) {
148       $answer=$session->error;
149       $session->close;
150       $state = 'CRITICAL';
151       print ("$state: $answer,$community,$snmpkey");
152       exit $ERRORS{$state};
153    }
155    foreach $snmpkey (keys %{$response}) {
156       $snmpkey =~ /.*\.(\d+)$/;
157       $key = $1;
158       $wanStatus{$key}{$snmpoid} = $response->{$snmpkey};
159    }
160    $session->close;
163    foreach $key (keys %wanStatus) {
164       # look only at active Interfaces lu-trunk(5)
165       if ($wanStatus{$key}{$snmpWanLineUsage} == 5 ) {
167          # 13 -> active
168          if ($wanStatus{$key}{$snmpWanLineState} == 13 ) {
169              $ifup++;
170          }
171          else {
172              $ifdown++ ;
173              $ifmessage .= sprintf("%s interface status : %s (%s)<BR>",
174                          $wanLineType{$wanStatus{$key}{$snmpWanLineType}},
175                          $wanLineState{$wanStatus{$key}{$snmpWanLineState}},
176                          $wanStatus{$key}{$snmpWanLineName});
178          }
179       }
180    }
181    
183    if ($ifdown > 0) {
184       $state = 'CRITICAL';
185       $answer = sprintf("host '%s', interfaces up: %d, down: %d<BR>",
186                         $hostname,
187                         $ifup,
188                         $ifdown);
189       $answer = $answer . $ifmessage . "\n";
190    }
191    else {
192       $state = 'OK';
193       $answer = sprintf("host '%s', interfaces up: %d, down: %d\n",
194                         $hostname,
195                         $ifup,
196                         $ifdown);
197    }
199 print ("$state: $answer");
200 exit $ERRORS{$state};