check_ifstatus.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/usr/local/bin/perl -w
  2. #
  3. # check_ifstatus.pl - nagios plugin
  4. #
  5. #
  6. # Copyright (C) 2000 Christoph Kron
  7. # Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh)
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. #
  23. #
  24. # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
  25. #
  26. # 11.01.2000 Version 1.0
  27. #
  28. # $Id$
  29. use POSIX;
  30. use strict;
  31. use lib utils.pm ;
  32. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  33. use Net::SNMP;
  34. use Getopt::Long;
  35. Getopt::Long::Configure('bundling');
  36. my $PROGNAME = "check_ifstatus";
  37. my $status;
  38. my %ifOperStatus = ('1','up',
  39. '2','down',
  40. '3','testing',
  41. '4','unknown',
  42. '5','dormant',
  43. '6','notPresent');
  44. my $state = "UNKNOWN";
  45. my $answer = "";
  46. my $snmpkey=0;
  47. my $snmpoid=0;
  48. my $key=0;
  49. my $community = "public";
  50. my $port = 161;
  51. my @snmpoids;
  52. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  53. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  54. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  55. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  56. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  57. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  58. my $hostname;
  59. my $session;
  60. my $error;
  61. my $response;
  62. my %ifStatus;
  63. my $ifup =0 ;
  64. my $ifdown =0;
  65. my $ifdormant = 0;
  66. my $ifmessage = "";
  67. my $snmp_version = 1;
  68. my $ifXTable;
  69. my $opt_h ;
  70. my $opt_V ;
  71. # Just in case of problems, let's not hang Nagios
  72. $SIG{'ALRM'} = sub {
  73. print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
  74. exit $ERRORS{"UNKNOWN"};
  75. };
  76. alarm($TIMEOUT);
  77. #Option checking
  78. $status = GetOptions(
  79. "V" => \$opt_V, "version" => \$opt_V,
  80. "h" => \$opt_h, "help" => \$opt_h,
  81. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  82. "C=s" =>\$community,"community=s" => \$community,
  83. "p=i" =>\$port, "port=i" => \$port,
  84. "H=s" => \$hostname, "hostname=s" => \$hostname,
  85. "I" => \$ifXTable, "ifmib" => \$ifXTable );
  86. if ($status == 0)
  87. {
  88. print_help() ;
  89. exit $ERRORS{'OK'};
  90. }
  91. if ($opt_V) {
  92. print_revision($PROGNAME,'$Revision$ ');
  93. exit $ERRORS{'OK'};
  94. }
  95. if ($opt_h) {
  96. print_help();
  97. exit $ERRORS{'OK'};
  98. }
  99. if (! utils::is_hostname($hostname)){
  100. usage();
  101. exit $ERRORS{"UNKNOWN"};
  102. }
  103. if ( ! $snmp_version ) {
  104. $snmp_version =1 ;
  105. }else{
  106. if ( $snmp_version =~ /[12]/ ) {
  107. ($session, $error) = Net::SNMP->session(
  108. -hostname => $hostname,
  109. -community => $community,
  110. -port => $port,
  111. -version => $snmp_version
  112. );
  113. if (!defined($session)) {
  114. $state='UNKNOWN';
  115. $answer=$error;
  116. print ("$state: $answer");
  117. exit $ERRORS{$state};
  118. }
  119. }elsif ( $snmp_version =~ /3/ ) {
  120. $state='UNKNOWN';
  121. print ("$state: No support for SNMP v3 yet\n");
  122. exit $ERRORS{$state};
  123. }else{
  124. $state='UNKNOWN';
  125. print ("$state: No support for SNMP v$snmp_version yet\n");
  126. exit $ERRORS{$state};
  127. }
  128. }
  129. push(@snmpoids,$snmpIfOperStatus);
  130. push(@snmpoids,$snmpIfAdminStatus);
  131. push(@snmpoids,$snmpIfDescr);
  132. push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
  133. foreach $snmpoid (@snmpoids) {
  134. if (!defined($response = $session->get_table($snmpoid))) {
  135. $answer=$session->error;
  136. $session->close;
  137. $state = 'CRITICAL';
  138. print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
  139. exit $ERRORS{$state};
  140. }
  141. foreach $snmpkey (keys %{$response}) {
  142. $snmpkey =~ /.*\.(\d+)$/;
  143. $key = $1;
  144. $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
  145. }
  146. }
  147. $session->close;
  148. foreach $key (keys %ifStatus) {
  149. # check only if interface is administratively up
  150. if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
  151. if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
  152. if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
  153. $ifdown++ ;
  154. $ifmessage .= sprintf("%s: down -> %s<BR>",
  155. $ifStatus{$key}{$snmpIfDescr},
  156. $ifStatus{$key}{$snmpIfName});
  157. }
  158. if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
  159. }
  160. }
  161. if ($ifdown > 0) {
  162. $state = 'CRITICAL';
  163. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d<BR>",
  164. $hostname,
  165. $ifup,
  166. $ifdown,
  167. $ifdormant);
  168. $answer = $answer . $ifmessage . "\n";
  169. }
  170. else {
  171. $state = 'OK';
  172. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d\n",
  173. $hostname,
  174. $ifup,
  175. $ifdown,
  176. $ifdormant);
  177. }
  178. print ("$state: $answer");
  179. exit $ERRORS{$state};
  180. sub usage {
  181. printf "\nMissing arguments!\n";
  182. printf "\n";
  183. printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
  184. printf "Copyright (C) 2000 Christoph Kron\n";
  185. printf "Updates 5/2002 Subhendu Ghosh\n";
  186. printf "\n\n";
  187. support();
  188. exit $ERRORS{"UNKNOWN"};
  189. }
  190. sub print_help {
  191. printf "check_ifstatus plugin for Nagios monitors operational \n";
  192. printf "status of each network interface on the target host\n";
  193. printf "\nUsage:\n";
  194. printf " -H (--hostname) Hostname to query - (required)\n";
  195. printf " -C (--community) SNMP read community (defaults to public,\n";
  196. printf " used with SNMP v1 and v2c\n";
  197. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  198. printf " 2 for SNMP v2c\n";
  199. printf " SNMP v2c will use get_bulk for less overhead\n";
  200. printf " -p (--port) SNMP port (default 161)\n";
  201. printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
  202. printf " you don't know what this is.\n";
  203. printf " -V (--version) Plugin version\n";
  204. printf " -h (--help) usage help \n\n";
  205. print_revision($PROGNAME, '$Revision$');
  206. }