check_ifoperstatus.pl 15 KB

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