check_bgpstate.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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
  25. use strict;
  26. use Net::SNMP;
  27. use Getopt::Long;
  28. &Getopt::Long::config('auto_abbrev');
  29. # whois programm for RIPE database queries
  30. my $whois = '/usr/bin/whois';
  31. my $status;
  32. my $TIMEOUT = 30;
  33. # critical bgp sessions
  34. my %uplinks = ( 1273, 'Uplink ECRC',
  35. 1755, 'Uplink EBONE',
  36. 3300, 'Uplink AUCS'
  37. );
  38. my %ERRORS = ('UNKNOWN' , '-1',
  39. 'OK' , '0',
  40. 'WARNING', '1',
  41. 'CRITICAL', '2');
  42. my %bgpPeerState = (
  43. '1',"idle",
  44. '2',"connect",
  45. '3',"active",
  46. '4',"opensent",
  47. '5',"openconfirm",
  48. '6',"established"
  49. );
  50. my $state = "UNKNOWN";
  51. my $answer = "";
  52. my $snmpkey;
  53. my $snmpoid;
  54. my $key;
  55. my $community = "public";
  56. my $port = 161;
  57. my @snmpoids;
  58. my $snmpbgpPeerState = '1.3.6.1.2.1.15.3.1.2';
  59. my $snmpbgpPeerLocalAddr = '1.3.6.1.2.1.15.3.1.5';
  60. my $snmpbgpPeerRemoteAddr = '1.3.6.1.2.1.15.3.1.7';
  61. my $snmpbgpPeerRemoteAs = '1.3.6.1.2.1.15.3.1.9';
  62. my $hostname;
  63. my $session;
  64. my $error;
  65. my $response;
  66. my %bgpStatus;
  67. my $bgpestablished =0 ;
  68. my $bgpcritical =0;
  69. my $bgpdown =0;
  70. my $bgpidle =0;
  71. my $bgpmessage;
  72. my $asname;
  73. my $remoteas;
  74. my @output;
  75. sub usage {
  76. printf "\nMissing arguments!\n";
  77. printf "\n";
  78. printf "Perl bgpstate plugin for Nagios\n";
  79. printf "monitors all BGP sessions\n";
  80. printf "usage: \n";
  81. printf "check_bgpstate.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>\n";
  82. printf "Copyright (C) 2000 Christoph Kron\n";
  83. printf "check_bgpstate.pl comes with ABSOLUTELY NO WARRANTY\n";
  84. printf "This programm is licensed under the terms of the ";
  85. printf "GNU General Public License\n(check source code for details)\n";
  86. printf "\n\n";
  87. exit $ERRORS{"UNKNOWN"};
  88. }
  89. # Just in case of problems, let's not hang Nagios
  90. $SIG{'ALRM'} = sub {
  91. print ("ERROR: No snmp response from $hostname (alarm)\n");
  92. exit $ERRORS{"UNKNOWN"};
  93. };
  94. alarm($TIMEOUT);
  95. $status = GetOptions("community=s",\$community,
  96. "port=i",\$port);
  97. if ($status == 0)
  98. {
  99. &usage;
  100. }
  101. #shift;
  102. $hostname = shift || &usage;
  103. push(@snmpoids, $snmpbgpPeerState);
  104. push(@snmpoids, $snmpbgpPeerLocalAddr);
  105. push(@snmpoids, $snmpbgpPeerRemoteAddr);
  106. push(@snmpoids, $snmpbgpPeerRemoteAs);
  107. foreach $snmpoid (@snmpoids) {
  108. ($session, $error) = Net::SNMP->session(
  109. -hostname => $hostname,
  110. -community => $community,
  111. -port => $port
  112. );
  113. if (!defined($session)) {
  114. $state='UNKNOWN';
  115. $answer=$error;
  116. print ("$state: $answer");
  117. exit $ERRORS{$state};
  118. }
  119. if (!defined($response = $session->get_table($snmpoid))) {
  120. $answer=$session->error;
  121. $session->close;
  122. $state = 'CRITICAL';
  123. print ("$state: $answer,$community,$snmpkey");
  124. exit $ERRORS{$state};
  125. }
  126. foreach $snmpkey (keys %{$response}) {
  127. $snmpkey =~ m/.*\.(\d+\.\d+\.\d+\.\d+$)/;
  128. $key = $1;
  129. # printf "debug: $snmpkey: $key -> $response->{$snmpkey}\n";
  130. $bgpStatus{$key}{$snmpoid} = $response->{$snmpkey};
  131. }
  132. $session->close;
  133. }
  134. foreach $key (keys %bgpStatus) {
  135. if ($bgpStatus{$key}{$snmpbgpPeerState} == 6 ) {
  136. $bgpestablished++;
  137. }
  138. elsif ($bgpStatus{$key}{$snmpbgpPeerState} == 1 ) {
  139. $bgpidle++;
  140. }
  141. else {
  142. $bgpdown++ ;
  143. if (exists($uplinks{$bgpStatus{$key}{$snmpbgpPeerRemoteAs}}) ) {
  144. $bgpcritical++;
  145. }
  146. @output = `$whois -T aut-num AS$bgpStatus{$key}{$snmpbgpPeerRemoteAs}`;
  147. $asname = "";
  148. foreach (@output) {
  149. if (m/as-name/) {
  150. $asname = $_;
  151. $asname =~ s/as-name://;
  152. last;
  153. }
  154. if ( $asname =~ "" && m/descr/ ) {
  155. $asname = $_;
  156. $asname =~ s/descr://;
  157. }
  158. }
  159. $asname =~ s/^\s*//;
  160. $asname =~ s/\s*$//;
  161. $bgpmessage .= sprintf("Peering with AS%s not established -> %s<BR>",
  162. $bgpStatus{$key}{$snmpbgpPeerRemoteAs},
  163. $asname);
  164. }
  165. }
  166. if ($bgpdown > 0) {
  167. if ($bgpcritical > 0) {
  168. $state = 'CRITICAL';
  169. }
  170. else {
  171. $state = 'WARNING';
  172. }
  173. $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d<BR>",
  174. $hostname,
  175. $bgpestablished,
  176. $bgpdown, $bgpidle);
  177. $answer = $answer . $bgpmessage . "\n";
  178. }
  179. else {
  180. $state = 'OK';
  181. $answer = sprintf("host '%s', sessions up: %d, down: %d, shutdown: %d\n",
  182. $hostname,
  183. $bgpestablished,
  184. $bgpdown,$bgpidle);
  185. }
  186. print ("$state: $answer");
  187. exit $ERRORS{$state};