4
0

check_ifoperstatus.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. #
  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. sub print_help ();
  42. sub usage ();
  43. sub process_arguments ();
  44. my $timeout;
  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 $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
  64. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  65. my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
  66. my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
  67. my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
  68. my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
  69. my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
  70. my $hostname;
  71. my $ifName;
  72. my $session;
  73. my $error;
  74. my $response;
  75. my $snmp_version = 1 ;
  76. my $ifXTable;
  77. my $opt_h ;
  78. my $opt_V ;
  79. my $ifdescr;
  80. my $iftype;
  81. my $key;
  82. my $lastc;
  83. my $dormantWarn;
  84. my $adminWarn;
  85. my $name;
  86. ### Validate Arguments
  87. $status = process_arguments();
  88. # Just in case of problems, let's not hang Nagios
  89. $SIG{'ALRM'} = sub {
  90. print ("ERROR: No snmp response from $hostname (alarm)\n");
  91. exit $ERRORS{"UNKNOWN"};
  92. };
  93. alarm($timeout);
  94. ## map ifdescr to ifindex - should look at being able to cache this value
  95. if (defined $ifdescr || defined $iftype) {
  96. # escape "/" in ifdescr - very common in the Cisco world
  97. if (defined $iftype) {
  98. $status=fetch_ifindex($snmpIfType, $iftype);
  99. } else {
  100. $ifdescr =~ s/\//\\\//g;
  101. $status=fetch_ifindex($snmpIfDescr, $ifdescr); # if using on device with large number of interfaces
  102. # recommend use of SNMP v2 (get-bulk)
  103. }
  104. if ($status==0) {
  105. $state = "UNKNOWN";
  106. printf "$state: could not retrive ifdescr/iftype snmpkey - $status-$snmpkey\n";
  107. $session->close;
  108. exit $ERRORS{$state};
  109. }
  110. }
  111. ## Main function
  112. $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
  113. $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
  114. $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
  115. $snmpIfName = $snmpIfName . "." . $snmpkey ;
  116. $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
  117. push(@snmpoids,$snmpIfAdminStatus);
  118. push(@snmpoids,$snmpIfOperStatus);
  119. push(@snmpoids,$snmpIfDescr);
  120. push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
  121. push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
  122. if (!defined($response = $session->get_request(@snmpoids))) {
  123. $answer=$session->error;
  124. $session->close;
  125. $state = 'WARNING';
  126. print ("$state: SNMP error: $answer\n");
  127. exit $ERRORS{$state};
  128. }
  129. $answer = sprintf("host '%s', %s(%s) is %s\n",
  130. $hostname,
  131. $response->{$snmpIfDescr},
  132. $snmpkey,
  133. $ifOperStatus{$response->{$snmpIfOperStatus}}
  134. );
  135. ## Check to see if ifName match is requested and it matches - exit if no match
  136. ## not the interface we want to monitor
  137. if ( defined $ifName && not ($response->{$snmpIfName} eq $ifName) ) {
  138. $state = 'UNKNOWN';
  139. $answer = "Interface name ($ifName) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
  140. print ("$state: $answer\n");
  141. exit $ERRORS{$state};
  142. }
  143. ## define the interface name
  144. if (defined $ifXTable) {
  145. $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
  146. }else{
  147. $name = $response->{$snmpIfDescr} ;
  148. }
  149. ## if AdminStatus is down - some one made a consious effort to change config
  150. ##
  151. if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
  152. $answer = "Interface $name (index $snmpkey) is administratively down.";
  153. if ( not defined $adminWarn or $adminWarn eq "w" ) {
  154. $state = 'WARNING';
  155. } elsif ( $adminWarn eq "i" ) {
  156. $state = 'OK';
  157. } elsif ( $adminWarn eq "c" ) {
  158. $state = 'CRITICAL';
  159. } else { # If wrong value for -a, say warning
  160. $state = 'WARNING';
  161. }
  162. }
  163. ## Check operational status
  164. elsif ( $response->{$snmpIfOperStatus} == 2 ) {
  165. $state = 'CRITICAL';
  166. $answer = "Interface $name (index $snmpkey) is down.";
  167. } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
  168. if (defined $dormantWarn ) {
  169. if ($dormantWarn eq "w") {
  170. $state = 'WARNING';
  171. $answer = "Interface $name (index $snmpkey) is dormant.";
  172. }elsif($dormantWarn eq "c") {
  173. $state = 'CRITICAL';
  174. $answer = "Interface $name (index $snmpkey) is dormant.";
  175. }elsif($dormantWarn eq "i") {
  176. $state = 'OK';
  177. $answer = "Interface $name (index $snmpkey) is dormant.";
  178. }
  179. }else{
  180. # dormant interface - but warning/critical/ignore not requested
  181. $state = 'CRITICAL';
  182. $answer = "Interface $name (index $snmpkey) is dormant.";
  183. }
  184. } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
  185. $state = 'CRITICAL';
  186. $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
  187. } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
  188. $state = 'CRITICAL';
  189. $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
  190. } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
  191. $state = 'CRITICAL';
  192. $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
  193. } else {
  194. $state = 'OK';
  195. $answer = "Interface $name (index $snmpkey) is up.";
  196. }
  197. print ("$state: $answer\n");
  198. exit $ERRORS{$state};
  199. ### subroutines
  200. sub fetch_ifindex {
  201. my $oid = shift;
  202. my $lookup = shift;
  203. if (!defined ($response = $session->get_table($oid))) {
  204. $answer=$session->error;
  205. $session->close;
  206. $state = 'CRITICAL';
  207. printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
  208. $session->close;
  209. exit $ERRORS{$state};
  210. }
  211. foreach $key ( keys %{$response}) {
  212. if ($response->{$key} =~ /^$lookup$/) {
  213. $key =~ /.*\.(\d+)$/;
  214. $snmpkey = $1;
  215. #print "$lookup = $key / $snmpkey \n"; #debug
  216. }
  217. }
  218. unless (defined $snmpkey) {
  219. $session->close;
  220. $state = 'CRITICAL';
  221. printf "$state: Could not match $ifdescr on $hostname\n";
  222. exit $ERRORS{$state};
  223. }
  224. return $snmpkey;
  225. }
  226. sub usage() {
  227. printf "\nMissing arguments!\n";
  228. printf "\n";
  229. printf "usage: \n";
  230. printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
  231. printf "Copyright (C) 2000 Christoph Kron\n";
  232. printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
  233. printf "This programm is licensed under the terms of the ";
  234. printf "GNU General Public License\n(check source code for details)\n";
  235. printf "\n\n";
  236. exit $ERRORS{"UNKNOWN"};
  237. }
  238. sub print_help() {
  239. printf "check_ifoperstatus plugin for Nagios monitors operational \n";
  240. printf "status of a particular network interface on the target host\n";
  241. printf "\nUsage:\n";
  242. printf " -H (--hostname) Hostname to query - (required)\n";
  243. printf " -C (--community) SNMP read community (defaults to public,\n";
  244. printf " used with SNMP v1 and v2c\n";
  245. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  246. printf " 2 for SNMP v2c\n";
  247. printf " SNMP v2c will use get_bulk for less overhead\n";
  248. printf " if monitoring with -d\n";
  249. printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
  250. printf " -U (--secname) username for SNMPv3 context\n";
  251. printf " -c (--context) SNMPv3 context name (default is empty string)";
  252. printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
  253. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  254. printf " auth password and authEngineID\n";
  255. printf " -a (--authproto) Authentication protocol ( MD5 or SHA1)\n";
  256. printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
  257. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  258. printf " privacy password and authEngineID\n";
  259. printf " -k (--key) SNMP IfIndex value\n";
  260. printf " -d (--descr) SNMP ifDescr value\n";
  261. printf " -T (--type) SNMP ifType integer value (see http://www.iana.org/assignments/ianaiftype-mib)\n";
  262. printf " -p (--port) SNMP port (default 161)\n";
  263. printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
  264. printf " you don't know what this is. \n";
  265. printf " -n (--name) the value should match the returned ifName\n";
  266. printf " (Implies the use of -I)\n";
  267. printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
  268. printf " -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
  269. printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
  270. printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
  271. printf " -V (--version) Plugin version\n";
  272. printf " -h (--help) usage help \n\n";
  273. printf " -k or -d or -T must be specified\n\n";
  274. printf "Note: either -k or -d or -T must be specified and -d and -T are much more network \n";
  275. printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
  276. printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
  277. printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
  278. print_revision($PROGNAME, '@NP_VERSION@');
  279. }
  280. sub process_arguments() {
  281. $status = GetOptions(
  282. "V" => \$opt_V, "version" => \$opt_V,
  283. "h" => \$opt_h, "help" => \$opt_h,
  284. "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
  285. "C=s" => \$community, "community=s" => \$community,
  286. "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
  287. "a=s" => \$authproto, "authproto=s" => \$authproto,
  288. "U=s" => \$secname, "secname=s" => \$secname,
  289. "A=s" => \$authpass, "authpass=s" => \$authpass,
  290. "X=s" => \$privpass, "privpass=s" => \$privpass,
  291. "c=s" => \$context, "context=s" => \$context,
  292. "k=i" => \$snmpkey, "key=i",\$snmpkey,
  293. "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
  294. "l=s" => \$lastc, "lastchange=s" => \$lastc,
  295. "p=i" => \$port, "port=i" =>\$port,
  296. "H=s" => \$hostname, "hostname=s" => \$hostname,
  297. "I" => \$ifXTable, "ifmib" => \$ifXTable,
  298. "n=s" => \$ifName, "name=s" => \$ifName,
  299. "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
  300. "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
  301. "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
  302. "t=i" => \$timeout, "timeout=i" => \$timeout,
  303. "T=i" => \$iftype, "type=i" => \$iftype,
  304. );
  305. if ($status == 0){
  306. print_help();
  307. exit $ERRORS{'OK'};
  308. }
  309. if ($opt_V) {
  310. print_revision($PROGNAME,'@NP_VERSION@');
  311. exit $ERRORS{'OK'};
  312. }
  313. if ($opt_h) {
  314. print_help();
  315. exit $ERRORS{'OK'};
  316. }
  317. if (! utils::is_hostname($hostname)){
  318. usage();
  319. exit $ERRORS{"UNKNOWN"};
  320. }
  321. unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
  322. printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
  323. usage();
  324. exit $ERRORS{"UNKNOWN"};
  325. }
  326. if (defined $name) {
  327. $ifXTable=1;
  328. }
  329. if (defined $dormantWarn) {
  330. unless ($dormantWarn =~ /^(w|c|i)$/ ) {
  331. printf "Dormant alerts must be one of w|c|i \n";
  332. exit $ERRORS{'UNKNOWN'};
  333. }
  334. }
  335. unless (defined $timeout) {
  336. $timeout = $TIMEOUT;
  337. }
  338. if ($snmp_version =~ /3/ ) {
  339. # Must define a security level even though default is noAuthNoPriv
  340. # v3 requires a security username
  341. if (defined $seclevel && defined $secname) {
  342. # Must define a security level even though defualt is noAuthNoPriv
  343. unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
  344. usage();
  345. exit $ERRORS{"UNKNOWN"};
  346. }
  347. # Authentication wanted
  348. if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
  349. unless ( $authproto eq 'MD5' || $authproto eq 'SHA1' ) {
  350. usage();
  351. exit $ERRORS{"UNKNOWN"};
  352. }
  353. if ( !defined $authpass) {
  354. usage();
  355. exit $ERRORS{"UNKNOWN"};
  356. }else{
  357. if ($authpass =~ /^0x/ ) {
  358. $auth = "-authkey => $authpass" ;
  359. }else{
  360. $auth = "-authpassword => $authpass";
  361. }
  362. }
  363. }
  364. # Privacy (DES encryption) wanted
  365. if ($seclevel eq 'authPriv' ) {
  366. if (! defined $privpass) {
  367. usage();
  368. exit $ERRORS{"UNKNOWN"};
  369. }else{
  370. if ($privpass =~ /^0x/){
  371. $priv = "-privkey => $privpass";
  372. }else{
  373. $priv = "-privpassword => $privpass";
  374. }
  375. }
  376. }
  377. # Context name defined or default
  378. unless ( defined $context) {
  379. $context = "";
  380. }
  381. }else {
  382. usage();
  383. exit $ERRORS{'UNKNOWN'}; ;
  384. }
  385. } # end snmpv3
  386. if ( $snmp_version =~ /[12]/ ) {
  387. ($session, $error) = Net::SNMP->session(
  388. -hostname => $hostname,
  389. -community => $community,
  390. -port => $port,
  391. -version => $snmp_version,
  392. -maxmsgsize => $maxmsgsize
  393. );
  394. if (!defined($session)) {
  395. $state='UNKNOWN';
  396. $answer=$error;
  397. print ("$state: $answer\n");
  398. exit $ERRORS{$state};
  399. }
  400. }elsif ( $snmp_version =~ /3/ ) {
  401. if ($seclevel eq 'noAuthNoPriv') {
  402. ($session, $error) = Net::SNMP->session(
  403. -hostname => $hostname,
  404. -port => $port,
  405. -version => $snmp_version,
  406. -username => $secname,
  407. );
  408. }elsif ( $seclevel eq 'authNoPriv' ) {
  409. ($session, $error) = Net::SNMP->session(
  410. -hostname => $hostname,
  411. -port => $port,
  412. -version => $snmp_version,
  413. -username => $secname,
  414. $auth,
  415. -authprotocol => $authproto,
  416. );
  417. }elsif ($seclevel eq 'authPriv' ) {
  418. ($session, $error) = Net::SNMP->session(
  419. -hostname => $hostname,
  420. -port => $port,
  421. -version => $snmp_version,
  422. -username => $secname,
  423. $auth,
  424. -authprotocol => $authproto,
  425. $priv
  426. );
  427. }
  428. if (!defined($session)) {
  429. $state='UNKNOWN';
  430. $answer=$error;
  431. print ("$state: $answer\n");
  432. exit $ERRORS{$state};
  433. }
  434. }else{
  435. $state='UNKNOWN';
  436. print ("$state: No support for SNMP v$snmp_version yet\n");
  437. exit $ERRORS{$state};
  438. }
  439. }
  440. ## End validation