check_ifoperstatus.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. # Added SNMPv3 support (10/2003)
  9. #
  10. # This program is free software; you can redistribute it and/or
  11. # modify it under the terms of the GNU General Public License
  12. # as published by the Free Software Foundation; either version 2
  13. # of the License, or (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. #
  24. #
  25. # Report bugs to: nagiosplug-help@lists.sourceforge.net
  26. #
  27. # 11.01.2000 Version 1.0
  28. # $Id$
  29. #
  30. # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
  31. # instead of critical.
  32. #
  33. # Primary MIB reference - RFC 2863
  34. use POSIX;
  35. use strict;
  36. use lib utils.pm ;
  37. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  38. use Net::SNMP;
  39. use Getopt::Long;
  40. &Getopt::Long::config('bundling');
  41. my $PROGNAME = "check_ifoperstatus";
  42. sub print_help ();
  43. sub usage ();
  44. sub process_arguments ();
  45. my $status;
  46. my %ifOperStatus = ('1','up',
  47. '2','down',
  48. '3','testing',
  49. '4','unknown',
  50. '5','dormant',
  51. '6','notPresent',
  52. '7','lowerLayerDown'); # down due to the state of lower layer interface(s)
  53. my $state = "UNKNOWN";
  54. my $answer = "";
  55. my $snmpkey = 0;
  56. my $community = "public";
  57. my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
  58. my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
  59. my $port = 161;
  60. my @snmpoids;
  61. my $sysUptime = '1.3.6.1.2.1.1.3.0';
  62. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  63. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  64. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  65. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  66. my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
  67. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  68. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  69. my $hostname;
  70. my $ifName;
  71. my $session;
  72. my $error;
  73. my $response;
  74. my $snmp_version = 1 ;
  75. my $ifXTable;
  76. my $opt_h ;
  77. my $opt_V ;
  78. my $ifdescr;
  79. my $key;
  80. my $lastc;
  81. my $dormantWarn;
  82. my $name;
  83. # Just in case of problems, let's not hang Nagios
  84. $SIG{'ALRM'} = sub {
  85. print ("ERROR: No snmp response from $hostname (alarm)\n");
  86. exit $ERRORS{"UNKNOWN"};
  87. };
  88. alarm($TIMEOUT);
  89. ### Validate Arguments
  90. $status = process_arguments();
  91. ## map ifdescr to ifindex - should look at being able to cache this value
  92. if (defined $ifdescr) {
  93. # escape "/" in ifdescr - very common in the Cisco world
  94. $ifdescr =~ s/\//\\\//g;
  95. $status=fetch_ifdescr(); # if using on device with large number of interfaces
  96. # recommend use of SNMP v2 (get-bulk)
  97. if ($status==0) {
  98. $state = "UNKNOWN";
  99. printf "$state: could not retrive ifdescr snmpkey - $status-$snmpkey\n";
  100. $session->close;
  101. exit $ERRORS{$state};
  102. }
  103. }
  104. ## Main function
  105. $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
  106. $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
  107. $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
  108. $snmpIfName = $snmpIfName . "." . $snmpkey ;
  109. $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
  110. push(@snmpoids,$snmpIfAdminStatus);
  111. push(@snmpoids,$snmpIfOperStatus);
  112. push(@snmpoids,$snmpIfDescr);
  113. push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
  114. push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
  115. if (!defined($response = $session->get_request(@snmpoids))) {
  116. $answer=$session->error;
  117. $session->close;
  118. $state = 'WARNING';
  119. print ("$state: SNMP error: $answer\n");
  120. exit $ERRORS{$state};
  121. }
  122. $answer = sprintf("host '%s', %s(%s) is %s\n",
  123. $hostname,
  124. $response->{$snmpIfDescr},
  125. $snmpkey,
  126. $ifOperStatus{$response->{$snmpIfOperStatus}}
  127. );
  128. ## Check to see if ifName match is requested and it matches - exit if no match
  129. ## not the interface we want to monitor
  130. if ( defined $name && not ($response->{$snmpIfName} eq $name) ) {
  131. $state = 'UNKNOWN';
  132. $answer = "Interface name ($name) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
  133. print ("$state: $answer");
  134. exit $ERRORS{$state};
  135. }
  136. ## define the interface name
  137. if (defined $ifXTable) {
  138. $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
  139. }else{
  140. $name = $response->{$snmpIfDescr} ;
  141. }
  142. ## if AdminStatus is down - some one made a consious effort to change config
  143. ##
  144. if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
  145. $state = 'WARNING';
  146. $answer = "Interface $name (index $snmpkey) is administratively down.";
  147. }
  148. ## Check operational status
  149. elsif ( $response->{$snmpIfOperStatus} == 2 ) {
  150. $state = 'CRITICAL';
  151. $answer = "Interface $name (index $snmpkey) is down.";
  152. } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
  153. if (defined $dormantWarn ) {
  154. if ($dormantWarn eq "w") {
  155. $state = 'WARNNG';
  156. $answer = "Interface $name (index $snmpkey) is dormant.";
  157. }elsif($dormantWarn eq "c") {
  158. $state = 'CRITICAL';
  159. $answer = "Interface $name (index $snmpkey) is dormant.";
  160. }elsif($dormantWarn eq "i") {
  161. $state = 'OK';
  162. $answer = "Interface $name (index $snmpkey) is dormant.";
  163. }
  164. }else{
  165. # dormant interface - but warning/critical/ignore not requested
  166. $state = 'CRITICAL';
  167. $answer = "Interface $name (index $snmpkey) is dormant.";
  168. }
  169. } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
  170. $state = 'CRITICAL';
  171. $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
  172. } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
  173. $state = 'CRITICAL';
  174. $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
  175. } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
  176. $state = 'CRITICAL';
  177. $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
  178. } else {
  179. $state = 'OK';
  180. $answer = "Interface $name (index $snmpkey) is up.";
  181. }
  182. print ("$state: $answer");
  183. exit $ERRORS{$state};
  184. ### subroutines
  185. sub fetch_ifdescr {
  186. if (!defined ($response = $session->get_table($snmpIfDescr))) {
  187. $answer=$session->error;
  188. $session->close;
  189. $state = 'CRITICAL';
  190. printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
  191. $session->close;
  192. exit $ERRORS{$state};
  193. }
  194. foreach $key ( keys %{$response}) {
  195. if ($response->{$key} =~ /^$ifdescr$/) {
  196. $key =~ /.*\.(\d+)$/;
  197. $snmpkey = $1;
  198. #print "$ifdescr = $key / $snmpkey \n"; #debug
  199. }
  200. }
  201. unless (defined $snmpkey) {
  202. $session->close;
  203. $state = 'CRITICAL';
  204. printf "$state: Could not match $ifdescr on $hostname\n";
  205. exit $ERRORS{$state};
  206. }
  207. return $snmpkey;
  208. }
  209. sub usage() {
  210. printf "\nMissing arguments!\n";
  211. printf "\n";
  212. printf "usage: \n";
  213. printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
  214. printf "Copyright (C) 2000 Christoph Kron\n";
  215. printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
  216. printf "This programm is licensed under the terms of the ";
  217. printf "GNU General Public License\n(check source code for details)\n";
  218. printf "\n\n";
  219. exit $ERRORS{"UNKNOWN"};
  220. }
  221. sub print_help() {
  222. printf "check_ifoperstatus plugin for Nagios monitors operational \n";
  223. printf "status of a particular network interface on the target host\n";
  224. printf "\nUsage:\n";
  225. printf " -H (--hostname) Hostname to query - (required)\n";
  226. printf " -C (--community) SNMP read community (defaults to public,\n";
  227. printf " used with SNMP v1 and v2c\n";
  228. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  229. printf " 2 for SNMP v2c\n";
  230. printf " SNMP v2c will use get_bulk for less overhead\n";
  231. printf " if monitoring with -d\n";
  232. printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
  233. printf " -U (--secname) username for SNMPv3 context\n";
  234. printf " -c (--context) SNMPv3 context name (default is empty string)";
  235. printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
  236. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  237. printf " auth password and authEngineID\n";
  238. printf " -a (--authproto) Authentication protocol ( MD5 or SHA1)\n";
  239. printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
  240. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  241. printf " privacy password and authEngineID\n";
  242. printf " -k (--key) SNMP IfIndex value\n";
  243. printf " -d (--descr) SNMP ifDescr value\n";
  244. printf " -p (--port) SNMP port (default 161)\n";
  245. printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
  246. printf " you don't know what this is. \n";
  247. printf " -n (--name) the value should match the returned ifName\n";
  248. printf " (Implies the use of -I)\n";
  249. printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
  250. printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
  251. printf " -V (--version) Plugin version\n";
  252. printf " -h (--help) usage help \n\n";
  253. printf " -k or -d must be specified\n\n";
  254. printf "Note: either -k or -d must be specified and -d is much more network \n";
  255. printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
  256. printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
  257. printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
  258. print_revision($PROGNAME, '$Revision$');
  259. }
  260. sub process_arguments() {
  261. $status = GetOptions(
  262. "V" => \$opt_V, "version" => \$opt_V,
  263. "h" => \$opt_h, "help" => \$opt_h,
  264. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  265. "C=s" => \$community, "community=s" => \$community,
  266. "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
  267. "a=s" => \$authproto, "authproto=s" => \$authproto,
  268. "U=s" => \$secname, "secname=s" => \$secname,
  269. "A=s" => \$authpass, "authpass=s" => \$authpass,
  270. "X=s" => \$privpass, "privpass=s" => \$privpass,
  271. "c=s" => \$context, "context=s" => \$context,
  272. "k=i" => \$snmpkey, "key=i",\$snmpkey,
  273. "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
  274. "l=s" => \$lastc, "lastchange=s" => \$lastc,
  275. "p=i" = >\$port, "port=i" =>\$port,
  276. "H=s" => \$hostname, "hostname=s" => \$hostname,
  277. "I" => \$ifXTable, "ifmib" => \$ifXTable,
  278. "n=s" => \$ifName, "name=s" => \$ifName,
  279. "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
  280. "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize);
  281. if ($status == 0){
  282. print_help();
  283. exit $ERRORS{'OK'};
  284. }
  285. if ($opt_V) {
  286. print_revision($PROGNAME,'$Revision$ ');
  287. exit $ERRORS{'OK'};
  288. }
  289. if ($opt_h) {
  290. print_help();
  291. exit $ERRORS{'OK'};
  292. }
  293. if (! utils::is_hostname($hostname)){
  294. usage();
  295. exit $ERRORS{"UNKNOWN"};
  296. }
  297. unless ($snmpkey > 0 || defined $ifdescr){
  298. printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
  299. usage();
  300. exit $ERRORS{"UNKNOWN"};
  301. }
  302. if (defined $name) {
  303. $ifXTable=1;
  304. }
  305. if (defined $dormantWarn) {
  306. unless ($dormantWarn =~ /^(w|c|i)$/ ) {
  307. printf "Dormant alerts must be one of w|c|i \n";
  308. exit $ERRORS{'UNKNOWN'};
  309. }
  310. }
  311. if ($snmp_version =~ /3/ ) {
  312. # Must define a security level even though default is noAuthNoPriv
  313. # v3 requires a security username
  314. if (defined $seclevel && defined $secname) {
  315. # Must define a security level even though defualt is noAuthNoPriv
  316. unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
  317. usage();
  318. exit $ERRORS{"UNKNOWN"};
  319. }
  320. # Authentication wanted
  321. if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
  322. unless ($authproto eq ('MD5' || 'SHA1') ) {
  323. usage();
  324. exit $ERRORS{"UNKNOWN"};
  325. }
  326. if ( !defined $authpass) {
  327. usage();
  328. exit $ERRORS{"UNKNOWN"};
  329. }else{
  330. if ($authpass =~ /^0x/ ) {
  331. $auth = "-authkey => $authpass" ;
  332. }else{
  333. $auth = "-authpassword => $authpass";
  334. }
  335. }
  336. }
  337. # Privacy (DES encryption) wanted
  338. if ($seclevel eq 'authPriv' ) {
  339. if (! defined $privpass) {
  340. usage();
  341. exit $ERRORS{"UNKNOWN"};
  342. }else{
  343. if ($privpass =~ /^0x/){
  344. $priv = "-privkey => $privpass";
  345. }else{
  346. $priv = "-privpassword => $privpass";
  347. }
  348. }
  349. }
  350. # Context name defined or default
  351. unless ( defined $context) {
  352. $context = "";
  353. }
  354. }else {
  355. usage();
  356. exit $ERRORS{'UNKNOWN'}; ;
  357. }
  358. } # end snmpv3
  359. if ( $snmp_version =~ /[12]/ ) {
  360. ($session, $error) = Net::SNMP->session(
  361. -hostname => $hostname,
  362. -community => $community,
  363. -port => $port,
  364. -version => $snmp_version,
  365. -maxmsgsize => $maxmsgsize
  366. );
  367. if (!defined($session)) {
  368. $state='UNKNOWN';
  369. $answer=$error;
  370. print ("$state: $answer");
  371. exit $ERRORS{$state};
  372. }
  373. }elsif ( $snmp_version =~ /3/ ) {
  374. if ($seclevel eq 'noAuthNoPriv') {
  375. ($session, $error) = Net::SNMP->session(
  376. -hostname => $hostname,
  377. -port => $port,
  378. -version => $snmp_version,
  379. -username => $secname,
  380. );
  381. }elsif ( $seclevel eq 'authNoPriv' ) {
  382. ($session, $error) = Net::SNMP->session(
  383. -hostname => $hostname,
  384. -port => $port,
  385. -version => $snmp_version,
  386. -username => $secname,
  387. $auth,
  388. -authprotocol => $authproto,
  389. );
  390. }elsif ($seclevel eq 'authPriv' ) {
  391. ($session, $error) = Net::SNMP->session(
  392. -hostname => $hostname,
  393. -port => $port,
  394. -version => $snmp_version,
  395. -username => $secname,
  396. $auth,
  397. -authprotocol => $authproto,
  398. $priv
  399. );
  400. }
  401. if (!defined($session)) {
  402. $state='UNKNOWN';
  403. $answer=$error;
  404. print ("$state: $answer");
  405. exit $ERRORS{$state};
  406. }
  407. }else{
  408. $state='UNKNOWN';
  409. print ("$state: No support for SNMP v$snmp_version yet\n");
  410. exit $ERRORS{$state};
  411. }
  412. }
  413. ## End validation