check_ifoperstatus.pl 15 KB

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