check_maxwanstate.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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
  26. use strict;
  27. use Net::SNMP;
  28. use Getopt::Long;
  29. &Getopt::Long::config('auto_abbrev');
  30. my $status;
  31. my $TIMEOUT = 1500;
  32. my %ERRORS = ('UNKNOWN' , '-1',
  33. 'OK' , '0',
  34. 'WARNING', '1',
  35. 'CRITICAL', '2');
  36. my %wanLineState = (
  37. 1,'ls-unknown',
  38. 2,'ls-does-not-exist',
  39. 3,'ls-disabled',
  40. 4,'ls-no-physical',
  41. 5,'ls-no-logical',
  42. 6,'ls-point-to-point',
  43. 7,'ls-multipoint-1',
  44. 8,'ls-multipoint-2',
  45. 9,'ls-loss-of-sync',
  46. 10,'ls-yellow-alarm',
  47. 11,'ls-ais-receive',
  48. 12,'ls-no-d-channel',
  49. 13,'ls-active',
  50. 14,'ls-maintenance');
  51. my %wanLineType = (
  52. '1.3.6.1.4.1.529.4.1','Any',
  53. '1.3.6.1.4.1.529.4.2','T1',
  54. '1.3.6.1.4.1.529.4.3','E1',
  55. '1.3.6.1.4.1.529.4.4','Dpnss',
  56. '1.3.6.1.4.1.529.4.5','Bri',
  57. '1.3.6.1.4.1.529.4.6','S562',
  58. '1.3.6.1.4.1.529.4.7','S564',
  59. '1.3.6.1.4.1.529.4.8','Sdsl',
  60. '1.3.6.1.4.1.529.4.9','AdslCap');
  61. my $state = "UNKNOWN";
  62. my $answer = "";
  63. my $snmpkey;
  64. my $snmpoid;
  65. my $key;
  66. my $community = "public";
  67. my $port = 161;
  68. my @snmpoids;
  69. my $snmpWanLineName = '1.3.6.1.4.1.529.4.21.1.2';
  70. my $snmpWanLineType = '1.3.6.1.4.1.529.4.21.1.3';
  71. my $snmpWanLineState = '1.3.6.1.4.1.529.4.21.1.5';
  72. my $snmpWanLineUsage = '1.3.6.1.4.1.529.4.21.1.8';
  73. my $hostname;
  74. my $session;
  75. my $error;
  76. my $response;
  77. my %wanStatus;
  78. my $ifup =0 ;
  79. my $ifdown =0;
  80. my $ifdormant = 0;
  81. my $ifmessage;
  82. sub usage {
  83. printf "\nMissing arguments!\n";
  84. printf "\n";
  85. printf "Perl Check maxwanstate plugin for Nagios\n";
  86. printf "monitors E1/T1 interface status\n";
  87. printf "usage: \n";
  88. printf "check_maxwanstate.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>";
  89. printf "Copyright (C) 2000 Christoph Kron\n";
  90. printf "check_maxwanstate.pl comes with ABSOLUTELY NO WARRANTY\n";
  91. printf "This programm is licensed under the terms of the ";
  92. printf "GNU General Public License\n(check source code for details)\n";
  93. printf "\n\n";
  94. exit $ERRORS{"UNKNOWN"};
  95. }
  96. # Just in case of problems, let's not hang Nagios
  97. $SIG{'ALRM'} = sub {
  98. print ("ERROR: No snmp response from $hostname (alarm)\n");
  99. exit $ERRORS{"UNKNOWN"};
  100. };
  101. alarm($TIMEOUT);
  102. $status = GetOptions("community=s",\$community,
  103. "port=i",\$port);
  104. if ($status == 0)
  105. {
  106. &usage;
  107. }
  108. #shift;
  109. $hostname = shift || &usage;
  110. push(@snmpoids,$snmpWanLineUsage);
  111. push(@snmpoids,$snmpWanLineState);
  112. push(@snmpoids,$snmpWanLineName);
  113. push(@snmpoids,$snmpWanLineType);
  114. foreach $snmpoid (@snmpoids) {
  115. ($session, $error) = Net::SNMP->session(
  116. -hostname => $hostname,
  117. -community => $community,
  118. -port => $port
  119. );
  120. if (!defined($session)) {
  121. $state='UNKNOWN';
  122. $answer=$error;
  123. print ("$state: $answer");
  124. exit $ERRORS{$state};
  125. }
  126. if (!defined($response = $session->get_table($snmpoid))) {
  127. $answer=$session->error;
  128. $session->close;
  129. $state = 'CRITICAL';
  130. print ("$state: $answer,$community,$snmpkey");
  131. exit $ERRORS{$state};
  132. }
  133. foreach $snmpkey (keys %{$response}) {
  134. $snmpkey =~ /.*\.(\d+)$/;
  135. $key = $1;
  136. $wanStatus{$key}{$snmpoid} = $response->{$snmpkey};
  137. }
  138. $session->close;
  139. }
  140. foreach $key (keys %wanStatus) {
  141. # look only at active Interfaces lu-trunk(5)
  142. if ($wanStatus{$key}{$snmpWanLineUsage} == 5 ) {
  143. # 13 -> active
  144. if ($wanStatus{$key}{$snmpWanLineState} == 13 ) {
  145. $ifup++;
  146. }
  147. else {
  148. $ifdown++ ;
  149. $ifmessage .= sprintf("%s interface status : %s (%s)<BR>",
  150. $wanLineType{$wanStatus{$key}{$snmpWanLineType}},
  151. $wanLineState{$wanStatus{$key}{$snmpWanLineState}},
  152. $wanStatus{$key}{$snmpWanLineName});
  153. }
  154. }
  155. }
  156. if ($ifdown > 0) {
  157. $state = 'CRITICAL';
  158. $answer = sprintf("host '%s', interfaces up: %d, down: %d<BR>",
  159. $hostname,
  160. $ifup,
  161. $ifdown);
  162. $answer = $answer . $ifmessage . "\n";
  163. }
  164. else {
  165. $state = 'OK';
  166. $answer = sprintf("host '%s', interfaces up: %d, down: %d\n",
  167. $hostname,
  168. $ifup,
  169. $ifdown);
  170. }
  171. print ("$state: $answer");
  172. exit $ERRORS{$state};