Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[nagiosplug.git] / contrib / check_bgpstate.pl
1 #!/usr/bin/perl -w
2 #
3 # check_bgpstate.pl - nagios plugin 
4 #
5 # Copyright (C) 2000 Christoph Kron
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #
21 #
22 # Report bugs to: ck@zet.net
23 #
24 # 11.01.2000 Version 1.0
28 use strict;
30 use Net::SNMP;
31 use Getopt::Long;
32 &Getopt::Long::config('auto_abbrev');
35 # whois programm for RIPE database queries
36 my $whois = '/usr/bin/whois';
37 my $status;
38 my $TIMEOUT = 30;
40 # critical bgp sessions
41 my %uplinks = ( 1273, 'Uplink ECRC',
42                 1755, 'Uplink EBONE',
43                 3300, 'Uplink AUCS'
44               );
46 my %ERRORS = ('UNKNOWN' , '-1',
47               'OK' , '0',
48               'WARNING', '1',
49               'CRITICAL', '2');
52 my %bgpPeerState = (  
53            '1',"idle",
54            '2',"connect",
55            '3',"active",
56            '4',"opensent",
57            '5',"openconfirm",
58            '6',"established"
59         );
60 my $state = "UNKNOWN";
61 my $answer = "";
62 my $snmpkey;
63 my $snmpoid;
64 my $key;
65 my $community = "public";
66 my $port = 161;
67 my @snmpoids;
68 my $snmpbgpPeerState = '1.3.6.1.2.1.15.3.1.2';
69 my $snmpbgpPeerLocalAddr = '1.3.6.1.2.1.15.3.1.5';
70 my $snmpbgpPeerRemoteAddr = '1.3.6.1.2.1.15.3.1.7';
71 my $snmpbgpPeerRemoteAs = '1.3.6.1.2.1.15.3.1.9';
72 my $hostname;
73 my $session;
74 my $error;
75 my $response;
76 my %bgpStatus;
77 my $bgpestablished =0 ;
78 my $bgpcritical =0;
79 my $bgpdown =0;
80 my $bgpidle =0;
81 my $bgpmessage;
82 my $asname;
83 my $remoteas;
84 my @output;
86 sub usage {
87   printf "\nMissing arguments!\n";
88   printf "\n";
89   printf "Perl bgpstate plugin for Nagios\n";
90   printf "monitors all BGP sessions\n";
91   printf "usage: \n";
92   printf "check_bgpstate.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>\n";
93   printf "Copyright (C) 2000 Christoph Kron\n";
94   printf "check_bgpstate.pl comes with ABSOLUTELY NO WARRANTY\n";
95   printf "This programm is licensed under the terms of the ";
96   printf "GNU General Public License\n(check source code for details)\n";
97   printf "\n\n";
98   exit $ERRORS{"UNKNOWN"};
99 }
101 # Just in case of problems, let's not hang Nagios
102 $SIG{'ALRM'} = sub {
103      print ("ERROR: No snmp response from $hostname (alarm)\n");
104      exit $ERRORS{"UNKNOWN"};
105 };
106 alarm($TIMEOUT);
109 $status = GetOptions("community=s",\$community,
110                      "port=i",\$port);
111 if ($status == 0)
113         &usage;
115   
116    #shift;
117    $hostname  = shift || &usage;
120 push(@snmpoids, $snmpbgpPeerState);
121 push(@snmpoids, $snmpbgpPeerLocalAddr);
122 push(@snmpoids, $snmpbgpPeerRemoteAddr);
123 push(@snmpoids, $snmpbgpPeerRemoteAs);
125 foreach $snmpoid (@snmpoids) {
127    ($session, $error) = Net::SNMP->session(
128       -hostname  => $hostname,
129       -community => $community,
130       -port      => $port
131    );
133    if (!defined($session)) {
134       $state='UNKNOWN';
135       $answer=$error;
136       print ("$state: $answer");
137       exit $ERRORS{$state};
138    }
140    if (!defined($response = $session->get_table($snmpoid))) {
141       $answer=$session->error;
142       $session->close;
143       $state = 'CRITICAL';
144       print ("$state: $answer,$snmpkey");
145       exit $ERRORS{$state};
146    }
148    foreach $snmpkey (keys %{$response}) {
149       $snmpkey =~ m/.*\.(\d+\.\d+\.\d+\.\d+$)/;
150       $key =  $1;
151 #      printf "debug: $snmpkey: $key -> $response->{$snmpkey}\n";
152       $bgpStatus{$key}{$snmpoid} = $response->{$snmpkey};
153    }
154    $session->close;
157 foreach $key (keys %bgpStatus) {
158    if ($bgpStatus{$key}{$snmpbgpPeerState} == 6 ) { 
159       $bgpestablished++;
160    }
161    elsif ($bgpStatus{$key}{$snmpbgpPeerState} == 1 ) {
162       $bgpidle++;
163    }
164    else { 
165        $bgpdown++ ;
166        if (exists($uplinks{$bgpStatus{$key}{$snmpbgpPeerRemoteAs}}) ) {
167           $bgpcritical++;
168        }
169        @output = `$whois -T aut-num AS$bgpStatus{$key}{$snmpbgpPeerRemoteAs}`;
171    $asname = "";
172    foreach (@output) {
173      if (m/as-name/) {
174         $asname = $_;
175         $asname =~ s/as-name://;
176         last;
177      }
178      if ( $asname =~ "" && m/descr/ ) {
179         $asname = $_;
180         $asname =~ s/descr://;
181      }
182    }
183    $asname =~ s/^\s*//;
184    $asname =~ s/\s*$//;
185        $bgpmessage .= sprintf("Peering with AS%s not established -> %s<BR>",
186                            $bgpStatus{$key}{$snmpbgpPeerRemoteAs},
187                            $asname);
188    }
190    
192    if ($bgpdown > 0) {
193       if ($bgpcritical > 0) {
194          $state = 'CRITICAL';
195       }
196       else {
197          $state = 'WARNING';
198       }
199       $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d<BR>",
200                         $hostname,
201                         $bgpestablished,
202                         $bgpdown, $bgpidle);
203       $answer = $answer . $bgpmessage . "\n";
204    }
205    else {
206       $state = 'OK';
207       $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d\n",
208                         $hostname,
209                         $bgpestablished,
210                         $bgpdown,$bgpidle);
211    }
213 print ("$state: $answer");
214 exit $ERRORS{$state};