check_ifoperstatus.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/usr/local/bin/perl -w
  2. #
  3. # check_ifoperstatus.pl - nagios plugin
  4. #
  5. # Copyright (C) 2000 Christoph Kron
  6. # Modified 5/2002 to conform to updated Nagios Plugin Guidelines
  7. # Added support for named interfaces per Valdimir Ivaschenko (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. # $Id$
  28. use POSIX;
  29. use strict;
  30. use lib utils.pm ;
  31. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  32. use Net::SNMP;
  33. use Getopt::Long;
  34. &Getopt::Long::config('bundling');
  35. my $PROGNAME = "check_ifoperstatus";
  36. my $status;
  37. my %ifOperStatus = ('1','up',
  38. '2','down',
  39. '3','testing',
  40. '4','unknown',
  41. '5','dormant',
  42. '6','notPresent');
  43. my $state = "UNKNOWN";
  44. my $answer = "";
  45. my $snmpkey = 0;
  46. my $community = "public";
  47. my $port = 161;
  48. my @snmpoids;
  49. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  50. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  51. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  52. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  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 $snmp_version = 1 ;
  59. my $ifXTable;
  60. my $opt_h ;
  61. my $opt_V ;
  62. my $ifdescr;
  63. my $key;
  64. # Just in case of problems, let's not hang Nagios
  65. $SIG{'ALRM'} = sub {
  66. print ("ERROR: No snmp response from $hostname (alarm)\n");
  67. exit $ERRORS{"UNKNOWN"};
  68. };
  69. #alarm($TIMEOUT);
  70. $status = GetOptions(
  71. "V" => \$opt_V, "version" => \$opt_V,
  72. "h" => \$opt_h, "help" => \$opt_h,
  73. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  74. "C=s" =>\$community, "community=s" => \$community,
  75. "k=i" =>\$snmpkey, "key=i",\$snmpkey,
  76. "d=s" =>\$ifdescr, "descr=s" => \$ifdescr,
  77. "p=i" =>\$port, "port=i",\$port,
  78. "H=s" => \$hostname, "hostname=s" => \$hostname,
  79. "I" => \$ifXTable, "ifmib" => \$ifXTable);
  80. if ($status == 0)
  81. {
  82. print_help();
  83. exit $ERRORS{'OK'};
  84. }
  85. if ($opt_V) {
  86. print_revision($PROGNAME,'$Revision$ ');
  87. exit $ERRORS{'OK'};
  88. }
  89. if ($opt_h) {
  90. print_help();
  91. exit $ERRORS{'OK'};
  92. }
  93. if (! utils::is_hostname($hostname)){
  94. usage();
  95. exit $ERRORS{"UNKNOWN"};
  96. }
  97. if ( $snmp_version =~ /[12]/ ) {
  98. ($session, $error) = Net::SNMP->session(
  99. -hostname => $hostname,
  100. -community => $community,
  101. -port => $port,
  102. -version => $snmp_version
  103. );
  104. if (!defined($session)) {
  105. $state='UNKNOWN';
  106. $answer=$error;
  107. print ("$state: $answer");
  108. exit $ERRORS{$state};
  109. }
  110. }elsif ( $snmp_version =~ /3/ ) {
  111. $state='UNKNOWN';
  112. print ("$state: No support for SNMP v3 yet\n");
  113. exit $ERRORS{$state};
  114. }else{
  115. $state='UNKNOWN';
  116. print ("$state: No support for SNMP v$snmp_version yet\n");
  117. exit $ERRORS{$state};
  118. }
  119. if (defined $ifdescr) {
  120. # escape "/" in ifdescr - very common in the Cisco world
  121. $ifdescr =~ s/\//\\\//g;
  122. $status=fetch_ifdescr(); # if using on device with large number of interfaces
  123. # recommend use of SNMP v2 (get-bulk)
  124. if ($status==0) {
  125. $state = "UNKNOWN";
  126. printf "$state: could not retrive ifIndex - $status-$snmpkey\n";
  127. $session->close;
  128. exit $ERRORS{$state};
  129. }
  130. }
  131. if ( $snmpkey == 0 ) {
  132. printf "ifIndex key cannot be 0\n";
  133. usage();
  134. exit $ERRORS{'UNKNOWN'};
  135. }
  136. $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8' . "." . $snmpkey;
  137. $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2' . "." . $snmpkey;
  138. $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18' . "." . $snmpkey ;
  139. push(@snmpoids,$snmpIfOperStatus);
  140. push(@snmpoids,$snmpIfDescr);
  141. push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
  142. if (!defined($response = $session->get_request(@snmpoids))) {
  143. $answer=$session->error;
  144. $session->close;
  145. $state = 'CRITICAL';
  146. print ("$state: $answer for ifIndex $snmpkey\n");
  147. exit $ERRORS{$state};
  148. }
  149. $answer = sprintf("host '%s', %s(%s) is %s\n",
  150. $hostname,
  151. $response->{$snmpIfDescr},
  152. $snmpkey,
  153. $ifOperStatus{$response->{$snmpIfOperStatus}}
  154. );
  155. $session->close;
  156. if ( $response->{$snmpIfOperStatus} == 1 ) {
  157. $state = 'OK';
  158. }
  159. else {
  160. $state = 'CRITICAL';
  161. }
  162. print ("$state: $answer");
  163. exit $ERRORS{$state};
  164. sub fetch_ifdescr {
  165. if (!defined ($response = $session->get_table($snmpIfDescr))) {
  166. $answer=$session->error;
  167. $session->close;
  168. $state = 'CRITICAL';
  169. printf ("$state: $answer for $snmpIfDescr with snmp version $snmp_version\n");
  170. $session->close;
  171. exit $ERRORS{$state};
  172. }
  173. foreach $key ( keys %{$response}) {
  174. if ($response->{$key} =~ /$ifdescr/) {
  175. $key =~ /.*\.(\d+)$/;
  176. $snmpkey = $1;
  177. #print "$ifdescr = $key / $snmpkey \n"; #debug
  178. }
  179. }
  180. unless (defined $snmpkey) {
  181. $session->close;
  182. $state = 'CRITICAL';
  183. printf "$state: Could not match $ifdescr on $hostname\n";
  184. exit $ERRORS{$state};
  185. }
  186. return $snmpkey;
  187. }
  188. sub usage {
  189. printf "\nMissing arguments!\n";
  190. printf "\n";
  191. printf "usage: \n";
  192. printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
  193. printf "Copyright (C) 2000 Christoph Kron\n";
  194. printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
  195. printf "This programm is licensed under the terms of the ";
  196. printf "GNU General Public License\n(check source code for details)\n";
  197. printf "\n\n";
  198. exit $ERRORS{"UNKNOWN"};
  199. }
  200. sub print_help {
  201. printf "check_ifoperstatus plugin for Nagios monitors operational \n";
  202. printf "status of a particular network interface on the target host\n";
  203. printf "\nUsage:\n";
  204. printf " -H (--hostname) Hostname to query - (required)\n";
  205. printf " -C (--community) SNMP read community (defaults to public,\n";
  206. printf " used with SNMP v1 and v2c\n";
  207. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  208. printf " 2 for SNMP v2c\n";
  209. printf " SNMP v2c will use get_bulk for less overhead\n";
  210. printf " if monitoring with -d\n";
  211. printf " -k (--key) SNMP ifIndex value\n";
  212. printf " -d (--descr) SNMP ifDescr value\n";
  213. printf " -p (--port) SNMP port (default 161)\n";
  214. printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
  215. printf " you don't know what this is.\n";
  216. printf " -V (--version) Plugin version\n";
  217. printf " -h (--help) usage help \n\n";
  218. printf " -k or -d must be specified\n\n";
  219. print_revision($PROGNAME, '$Revision$');
  220. }