check_ifoperstatus.pl 15 KB

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