check_ifoperstatus.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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: nagiosplug-help@lists.sourceforge.net
  25. #
  26. # 11.01.2000 Version 1.0
  27. # $Id$
  28. #
  29. # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
  30. # instead of critical.
  31. #
  32. # Primary MIB reference - RFC 2863
  33. use POSIX;
  34. use strict;
  35. use lib utils.pm ;
  36. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  37. use Net::SNMP;
  38. use Getopt::Long;
  39. &Getopt::Long::config('bundling');
  40. my $PROGNAME = "check_ifoperstatus";
  41. my $status;
  42. my %ifOperStatus = ('1','up',
  43. '2','down',
  44. '3','testing',
  45. '4','unknown',
  46. '5','dormant',
  47. '6','notPresent',
  48. '7','lowerLayerDown'); # down due to the state of lower layer interface(s)
  49. my $state = "UNKNOWN";
  50. my $answer = "";
  51. my $snmpkey = 0;
  52. my $community = "public";
  53. my $port = 161;
  54. my @snmpoids;
  55. my $sysUptime = '1.3.6.1.2.1.1.3.0';
  56. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  57. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  58. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  59. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  60. my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
  61. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  62. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  63. my $hostname;
  64. my $ifName;
  65. my $session;
  66. my $error;
  67. my $response;
  68. my $snmp_version = 1 ;
  69. my $ifXTable;
  70. my $opt_h ;
  71. my $opt_V ;
  72. my $ifdescr;
  73. my $key;
  74. my $lastc;
  75. my $dormantWarn;
  76. my $name;
  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. ### Validate Arguments
  84. $status = GetOptions(
  85. "V" => \$opt_V, "version" => \$opt_V,
  86. "h" => \$opt_h, "help" => \$opt_h,
  87. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  88. "C=s" =>\$community, "community=s" => \$community,
  89. "k=i" =>\$snmpkey, "key=i",\$snmpkey,
  90. "d=s" =>\$ifdescr, "descr=s" => \$ifdescr,
  91. "l=s" => \$lastc, "lastchange=s" => \$lastc,
  92. "p=i" =>\$port, "port=i",\$port,
  93. "H=s" => \$hostname, "hostname=s" => \$hostname,
  94. "I" => \$ifXTable, "ifmib" => \$ifXTable,
  95. "n=s" => \$ifName, "name=s" => \$ifName,
  96. "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn );
  97. if ($status == 0)
  98. {
  99. print_help();
  100. exit $ERRORS{'OK'};
  101. }
  102. if ($opt_V) {
  103. print_revision($PROGNAME,'$Revision$ ');
  104. exit $ERRORS{'OK'};
  105. }
  106. if ($opt_h) {
  107. print_help();
  108. exit $ERRORS{'OK'};
  109. }
  110. if (! utils::is_hostname($hostname)){
  111. usage();
  112. exit $ERRORS{"UNKNOWN"};
  113. }
  114. unless ($snmpkey > 0 || defined $ifdescr){
  115. printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
  116. usage();
  117. exit $ERRORS{"UNKNOWN"};
  118. }
  119. if (defined $name) {
  120. $ifXTable=1;
  121. }
  122. if ( $snmp_version =~ /[12]/ ) {
  123. ($session, $error) = Net::SNMP->session(
  124. -hostname => $hostname,
  125. -community => $community,
  126. -port => $port,
  127. -version => $snmp_version
  128. );
  129. if (!defined($session)) {
  130. $state='UNKNOWN';
  131. $answer=$error;
  132. print ("$state: $answer");
  133. exit $ERRORS{$state};
  134. }
  135. }elsif ( $snmp_version =~ /3/ ) {
  136. $state='UNKNOWN';
  137. print ("$state: No support for SNMP v3 yet\n");
  138. exit $ERRORS{$state};
  139. }else{
  140. $state='UNKNOWN';
  141. print ("$state: No support for SNMP v$snmp_version yet\n");
  142. exit $ERRORS{$state};
  143. }
  144. ## End validation
  145. ## map ifdescr to ifindex - should look at being able to cache this value
  146. if (defined $ifdescr) {
  147. # escape "/" in ifdescr - very common in the Cisco world
  148. $ifdescr =~ s/\//\\\//g;
  149. $status=fetch_ifdescr(); # if using on device with large number of interfaces
  150. # recommend use of SNMP v2 (get-bulk)
  151. if ($status==0) {
  152. $state = "UNKNOWN";
  153. printf "$state: could not retrive snmpkey - $status-$snmpkey\n";
  154. $session->close;
  155. exit $ERRORS{$state};
  156. }
  157. }
  158. ## Main function
  159. $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
  160. $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
  161. $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
  162. $snmpIfName = $snmpIfName . "." . $snmpkey ;
  163. $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
  164. push(@snmpoids,$snmpIfAdminStatus);
  165. push(@snmpoids,$snmpIfOperStatus);
  166. push(@snmpoids,$snmpIfDescr);
  167. push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
  168. push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
  169. if (!defined($response = $session->get_request(@snmpoids))) {
  170. $answer=$session->error;
  171. $session->close;
  172. $state = 'WARNING';
  173. print ("$state: SNMP error: $answer\n");
  174. exit $ERRORS{$state};
  175. }
  176. $answer = sprintf("host '%s', %s(%s) is %s\n",
  177. $hostname,
  178. $response->{$snmpIfDescr},
  179. $snmpkey,
  180. $ifOperStatus{$response->{$snmpIfOperStatus}}
  181. );
  182. ## Check to see if ifName match is requested and it matches - exit if no match
  183. ## not the interface we want to monitor
  184. if ( defined $name && not ($response->{$snmpIfName} eq $name) ) {
  185. $state = 'UNKNOWN';
  186. $answer = "Interface name ($name) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
  187. print ("$state: $answer");
  188. exit $ERRORS{$state};
  189. }
  190. ## define the interface name
  191. if (defined $ifXTable) {
  192. $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
  193. }else{
  194. $name = $response->{$snmpIfDescr} ;
  195. }
  196. ## if AdminStatus is down - some one made a consious effort to change config
  197. ##
  198. if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
  199. $state = 'WARNING';
  200. $answer = "Interface $name (index $snmpkey) is administratively down.";
  201. }
  202. ## Check operational status
  203. elsif ( $response->{$snmpIfOperStatus} == 2 ) {
  204. $state = 'CRITICAL';
  205. $answer = "Interface $name (index $snmpkey) is down.";
  206. } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
  207. if (defined $dormantWarn ) {
  208. if ($dormantWarn eq "w") {
  209. $state = 'WARNNG';
  210. $answer = "Interface $name (index $snmpkey) is dormant.";
  211. }elsif($dormantWarn eq "c") {
  212. $state = 'CRITICAL';
  213. $answer = "Interface $name (index $snmpkey) is dormant.";
  214. }elsif($dormantWarn eq "i") {
  215. $state = 'OK';
  216. $answer = "Interface $name (index $snmpkey) is dormant.";
  217. }
  218. }else{
  219. # dormant interface - but warning/critical/ignore not requested
  220. $state = 'CRITICAL';
  221. $answer = "Interface $name (index $snmpkey) is dormant.";
  222. }
  223. } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
  224. $state = 'CRITICAL';
  225. $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
  226. } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
  227. $state = 'CRITICAL';
  228. $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
  229. } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
  230. $state = 'CRITICAL';
  231. $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
  232. } else {
  233. $state = 'OK';
  234. $answer = "Interface $name (index $snmpkey) is up.";
  235. }
  236. print ("$state: $answer");
  237. exit $ERRORS{$state};
  238. ### subroutines
  239. sub fetch_ifdescr {
  240. if (!defined ($response = $session->get_table($snmpIfDescr))) {
  241. $answer=$session->error;
  242. $session->close;
  243. $state = 'CRITICAL';
  244. printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
  245. $session->close;
  246. exit $ERRORS{$state};
  247. }
  248. foreach $key ( keys %{$response}) {
  249. if ($response->{$key} =~ /^$ifdescr$/) {
  250. $key =~ /.*\.(\d+)$/;
  251. $snmpkey = $1;
  252. #print "$ifdescr = $key / $snmpkey \n"; #debug
  253. }
  254. }
  255. unless (defined $snmpkey) {
  256. $session->close;
  257. $state = 'CRITICAL';
  258. printf "$state: Could not match $ifdescr on $hostname\n";
  259. exit $ERRORS{$state};
  260. }
  261. return $snmpkey;
  262. }
  263. sub usage {
  264. printf "\nMissing arguments!\n";
  265. printf "\n";
  266. printf "usage: \n";
  267. printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
  268. printf "Copyright (C) 2000 Christoph Kron\n";
  269. printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
  270. printf "This programm is licensed under the terms of the ";
  271. printf "GNU General Public License\n(check source code for details)\n";
  272. printf "\n\n";
  273. exit $ERRORS{"UNKNOWN"};
  274. }
  275. sub print_help {
  276. printf "check_ifoperstatus plugin for Nagios monitors operational \n";
  277. printf "status of a particular network interface on the target host\n";
  278. printf "\nUsage:\n";
  279. printf " -H (--hostname) Hostname to query - (required)\n";
  280. printf " -C (--community) SNMP read community (defaults to public,\n";
  281. printf " used with SNMP v1 and v2c\n";
  282. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  283. printf " 2 for SNMP v2c\n";
  284. printf " SNMP v2c will use get_bulk for less overhead\n";
  285. printf " if monitoring with -d\n";
  286. printf " -k (--key) SNMP IfIndex value\n";
  287. printf " -d (--descr) SNMP ifDescr value\n";
  288. printf " -p (--port) SNMP port (default 161)\n";
  289. printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
  290. printf " you don't know what this is. \n";
  291. printf " -n (--name) the value should match the returned ifName\n";
  292. printf " (Implies the use of -I)\n";
  293. printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
  294. printf " -V (--version) Plugin version\n";
  295. printf " -h (--help) usage help \n\n";
  296. printf " -k or -d must be specified\n\n";
  297. printf "Note: either -k or -d must be specified and -d is much more network \n";
  298. printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
  299. printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
  300. printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
  301. print_revision($PROGNAME, '$Revision$');
  302. }