check_ifstatus.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
  59. my $hostname;
  60. my $session;
  61. my $error;
  62. my $response;
  63. my %ifStatus;
  64. my $ifup =0 ;
  65. my $ifdown =0;
  66. my $ifdormant = 0;
  67. my $ifmessage = "";
  68. my $snmp_version = 1;
  69. my $ifXTable;
  70. my $opt_h ;
  71. my $opt_V ;
  72. # Just in case of problems, let's not hang Nagios
  73. $SIG{'ALRM'} = sub {
  74. print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
  75. exit $ERRORS{"UNKNOWN"};
  76. };
  77. alarm($TIMEOUT);
  78. #Option checking
  79. $status = GetOptions(
  80. "V" => \$opt_V, "version" => \$opt_V,
  81. "h" => \$opt_h, "help" => \$opt_h,
  82. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  83. "C=s" =>\$community,"community=s" => \$community,
  84. "p=i" =>\$port, "port=i" => \$port,
  85. "H=s" => \$hostname, "hostname=s" => \$hostname,
  86. "I" => \$ifXTable, "ifmib" => \$ifXTable );
  87. if ($status == 0)
  88. {
  89. print_help() ;
  90. exit $ERRORS{'OK'};
  91. }
  92. if ($opt_V) {
  93. print_revision($PROGNAME,'$Revision$ ');
  94. exit $ERRORS{'OK'};
  95. }
  96. if ($opt_h) {
  97. print_help();
  98. exit $ERRORS{'OK'};
  99. }
  100. if (! utils::is_hostname($hostname)){
  101. usage();
  102. exit $ERRORS{"UNKNOWN"};
  103. }
  104. if ( ! $snmp_version ) {
  105. $snmp_version =1 ;
  106. }else{
  107. if ( $snmp_version =~ /[12]/ ) {
  108. ($session, $error) = Net::SNMP->session(
  109. -hostname => $hostname,
  110. -community => $community,
  111. -port => $port,
  112. -version => $snmp_version
  113. );
  114. if (!defined($session)) {
  115. $state='UNKNOWN';
  116. $answer=$error;
  117. print ("$state: $answer");
  118. exit $ERRORS{$state};
  119. }
  120. }elsif ( $snmp_version =~ /3/ ) {
  121. $state='UNKNOWN';
  122. print ("$state: No support for SNMP v3 yet\n");
  123. exit $ERRORS{$state};
  124. }else{
  125. $state='UNKNOWN';
  126. print ("$state: No support for SNMP v$snmp_version yet\n");
  127. exit $ERRORS{$state};
  128. }
  129. }
  130. push(@snmpoids,$snmpIfOperStatus);
  131. push(@snmpoids,$snmpIfAdminStatus);
  132. push(@snmpoids,$snmpIfDescr);
  133. push(@snmpoids,$snmpIfType);
  134. push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
  135. foreach $snmpoid (@snmpoids) {
  136. if (!defined($response = $session->get_table($snmpoid))) {
  137. $answer=$session->error;
  138. $session->close;
  139. $state = 'CRITICAL';
  140. print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
  141. exit $ERRORS{$state};
  142. }
  143. foreach $snmpkey (keys %{$response}) {
  144. $snmpkey =~ /.*\.(\d+)$/;
  145. $key = $1;
  146. $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
  147. }
  148. }
  149. $session->close;
  150. foreach $key (keys %ifStatus) {
  151. # check only if interface is administratively up
  152. if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
  153. # check only if interface is not of type 23 aka PPP interface
  154. if ($ifStatus{$key}{$snmpIfType} != 23 ) {
  155. if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
  156. if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
  157. $ifdown++ ;
  158. $ifmessage .= sprintf("%s: down -> %s<BR>",
  159. $ifStatus{$key}{$snmpIfDescr},
  160. $ifStatus{$key}{$snmpIfName});
  161. }
  162. if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
  163. }
  164. }
  165. }
  166. if ($ifdown > 0) {
  167. $state = 'CRITICAL';
  168. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d<BR>",
  169. $hostname,
  170. $ifup,
  171. $ifdown,
  172. $ifdormant);
  173. $answer = $answer . $ifmessage . "\n";
  174. }
  175. else {
  176. $state = 'OK';
  177. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d\n",
  178. $hostname,
  179. $ifup,
  180. $ifdown,
  181. $ifdormant);
  182. }
  183. print ("$state: $answer");
  184. exit $ERRORS{$state};
  185. sub usage {
  186. printf "\nMissing arguments!\n";
  187. printf "\n";
  188. printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
  189. printf "Copyright (C) 2000 Christoph Kron\n";
  190. printf "Updates 5/2002 Subhendu Ghosh\n";
  191. printf "\n\n";
  192. support();
  193. exit $ERRORS{"UNKNOWN"};
  194. }
  195. sub print_help {
  196. printf "check_ifstatus plugin for Nagios monitors operational \n";
  197. printf "status of each network interface (except PPP interfaces) on the target host\n";
  198. printf "\nUsage:\n";
  199. printf " -H (--hostname) Hostname to query - (required)\n";
  200. printf " -C (--community) SNMP read community (defaults to public,\n";
  201. printf " used with SNMP v1 and v2c\n";
  202. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  203. printf " 2 for SNMP v2c\n";
  204. printf " SNMP v2c will use get_bulk for less overhead\n";
  205. printf " -p (--port) SNMP port (default 161)\n";
  206. printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
  207. printf " you don't know what this is.\n";
  208. printf " -V (--version) Plugin version\n";
  209. printf " -h (--help) usage help \n\n";
  210. print_revision($PROGNAME, '$Revision$');
  211. }