4
0

check_ifoperstatus.pl 15 KB

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