check_ifstatus.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/perl -w
  2. #
  3. # check_ifstatus.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 %ifOperStatus = ('1','up',
  37. '2','down',
  38. '3','testing',
  39. '4','unknown',
  40. '5','dormant',
  41. '6','notPresent');
  42. my $state = "UNKNOWN";
  43. my $answer = "";
  44. my $snmpkey;
  45. my $snmpoid;
  46. my $key;
  47. my $community = "public";
  48. my $port = 161;
  49. my @snmpoids;
  50. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  51. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  52. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  53. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  54. my $hostname;
  55. my $session;
  56. my $error;
  57. my $response;
  58. my %ifStatus;
  59. my $ifup =0 ;
  60. my $ifdown =0;
  61. my $ifdormant = 0;
  62. my $ifmessage;
  63. sub usage {
  64. printf "\nMissing arguments!\n";
  65. printf "\n";
  66. printf "Perl Check IfStatus plugin for Nagios\n";
  67. printf "monitors operational status of each interface\n";
  68. printf "usage: \n";
  69. printf "check_ifstatus.pl -c <READCOMMUNITY> -p <PORT> <HOSTNAME>\n";
  70. printf "Copyright (C) 2000 Christoph Kron\n";
  71. printf "check_ifstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
  72. printf "This programm is licensed under the terms of the ";
  73. printf "GNU General Public License\n(check source code for details)\n";
  74. printf "\n\n";
  75. exit $ERRORS{"UNKNOWN"};
  76. }
  77. # Just in case of problems, let's not hang Nagios
  78. $SIG{'ALRM'} = sub {
  79. print ("ERROR: No snmp response from $hostname (alarm)\n");
  80. exit $ERRORS{"UNKNOWN"};
  81. };
  82. alarm($TIMEOUT);
  83. $status = GetOptions("community=s",\$community,
  84. "port=i",\$port);
  85. if ($status == 0)
  86. {
  87. &usage;
  88. }
  89. #shift;
  90. $hostname = shift || &usage;
  91. push(@snmpoids,$snmpIfOperStatus);
  92. push(@snmpoids,$snmpLocIfDescr);
  93. push(@snmpoids,$snmpIfAdminStatus);
  94. push(@snmpoids,$snmpIfDescr);
  95. foreach $snmpoid (@snmpoids) {
  96. ($session, $error) = Net::SNMP->session(
  97. -hostname => $hostname,
  98. -community => $community,
  99. -port => $port
  100. );
  101. if (!defined($session)) {
  102. $state='UNKNOWN';
  103. $answer=$error;
  104. print ("$state: $answer");
  105. exit $ERRORS{$state};
  106. }
  107. if (!defined($response = $session->get_table($snmpoid))) {
  108. $answer=$session->error;
  109. $session->close;
  110. $state = 'CRITICAL';
  111. print ("$state: $answer,$community,$snmpkey");
  112. exit $ERRORS{$state};
  113. }
  114. foreach $snmpkey (keys %{$response}) {
  115. $snmpkey =~ /.*\.(\d+)$/;
  116. $key = $1;
  117. $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
  118. }
  119. $session->close;
  120. }
  121. foreach $key (keys %ifStatus) {
  122. # check only if interface is administratively up
  123. if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
  124. if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
  125. if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
  126. $ifdown++ ;
  127. $ifmessage .= sprintf("%s: down -> %s<BR>",
  128. $ifStatus{$key}{$snmpIfDescr},
  129. $ifStatus{$key}{$snmpLocIfDescr});
  130. }
  131. if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
  132. }
  133. }
  134. if ($ifdown > 0) {
  135. $state = 'CRITICAL';
  136. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d<BR>",
  137. $hostname,
  138. $ifup,
  139. $ifdown,
  140. $ifdormant);
  141. $answer = $answer . $ifmessage . "\n";
  142. }
  143. else {
  144. $state = 'OK';
  145. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d\n",
  146. $hostname,
  147. $ifup,
  148. $ifdown,
  149. $ifdormant);
  150. }
  151. print ("$state: $answer");
  152. exit $ERRORS{$state};