check_ifstatus.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #!@PERL@ -w
  2. #
  3. # check_ifstatus.pl - nagios plugin
  4. #
  5. #
  6. # Copyright (C) 2000 Christoph Kron
  7. # Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh)
  8. # Added -x option (4/2003)
  9. # Added -u option (4/2003)
  10. # Added -M option (10/2003)
  11. # Added SNMPv3 support (10/2003)
  12. # Added -n option (07/2014)
  13. # Added -d option (04/2020)
  14. #
  15. # This program is free software; you can redistribute it and/or
  16. # modify it under the terms of the GNU General Public License
  17. # as published by the Free Software Foundation; either version 2
  18. # of the License, or (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program; if not, write to the Free Software
  27. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  28. # MA 02110-1301, USA
  29. #
  30. # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
  31. #
  32. # 11.01.2000 Version 1.0
  33. #
  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::Configure('bundling');
  43. my $PROGNAME = "check_ifstatus";
  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 $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 $timeout ;
  60. my $state = "UNKNOWN";
  61. my $answer = "";
  62. my $snmpkey=0;
  63. my $snmpoid=0;
  64. my $key=0;
  65. my $community = "public";
  66. my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
  67. my ($seclevel, $authproto, $secname, $authpass, $privpass, $privproto, $auth, $priv, $context);
  68. my $port = 161;
  69. my @snmpoids;
  70. my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
  71. my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
  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 $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 $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
  77. my $hostname;
  78. my $session;
  79. my $error;
  80. my $response;
  81. my %ifStatus;
  82. my $ifup =0 ;
  83. my $ifdown =0;
  84. my $ifdormant = 0;
  85. my $ifexclude = 0 ;
  86. my $ifunused = 0;
  87. my $ifmessage = "";
  88. my $snmp_version = 1;
  89. my $ifXTable;
  90. my $opt_h ;
  91. my $opt_V ;
  92. my $opt_u;
  93. my $opt_n;
  94. my $opt_x ;
  95. my $opt_d;
  96. my %excluded ;
  97. my %unused_names ;
  98. my @unused_ports ;
  99. my %session_opts;
  100. my @exclude_descriptions;
  101. # Just in case of problems, let's not hang Nagios
  102. $SIG{'ALRM'} = sub {
  103. print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
  104. exit $ERRORS{"UNKNOWN"};
  105. };
  106. #Option checking
  107. $status = process_arguments();
  108. if ($status != 0)
  109. {
  110. print_help() ;
  111. exit $ERRORS{'OK'};
  112. }
  113. alarm($timeout);
  114. ($session, $error) = Net::SNMP->session(%session_opts);
  115. if (!defined($session)) {
  116. $state='UNKNOWN';
  117. $answer=$error;
  118. print ("$state: $answer\n");
  119. exit $ERRORS{$state};
  120. }
  121. push(@snmpoids,$snmpIfOperStatus);
  122. push(@snmpoids,$snmpIfAdminStatus);
  123. push(@snmpoids,$snmpIfDescr);
  124. push(@snmpoids,$snmpIfType);
  125. push(@snmpoids,$snmpLocIfDescr) if ( defined $ifXTable);
  126. push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
  127. push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
  128. foreach $snmpoid (@snmpoids) {
  129. if (!defined($response = $session->get_table($snmpoid))) {
  130. $answer=$session->error;
  131. $session->close;
  132. $state = 'CRITICAL';
  133. if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
  134. print ("$state: Device does not support ifTable - try without -I option\n");
  135. }else{
  136. print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
  137. }
  138. exit $ERRORS{$state};
  139. }
  140. foreach $snmpkey (keys %{$response}) {
  141. $snmpkey =~ /.*\.(\d+)$/;
  142. $key = $1;
  143. $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
  144. }
  145. }
  146. $session->close;
  147. alarm(0);
  148. foreach $key (keys %ifStatus) {
  149. # skip unused interfaces
  150. my $ifName = $ifStatus{$key}{$snmpIfDescr};
  151. if (!defined($ifStatus{$key}{'notInUse'}) && !grep(/^${ifName}/, @unused_ports )) {
  152. # check only if interface is administratively up
  153. if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
  154. #check only if interface is not excluded (by name)
  155. if (!defined $unused_names{$ifStatus{$key}{$snmpIfDescr}} ) {
  156. #check only if interface is not excluded (by description)
  157. #counter for matching descriptions
  158. my $match_descr = 0;
  159. foreach my $description (@exclude_descriptions) {
  160. if (defined ($ifStatus{$key}{$snmpLocIfDescr}) && $ifStatus{$key}{$snmpLocIfDescr} =~ /^$description/) { $match_descr = 1; }
  161. }
  162. if ($match_descr == 0) {
  163. # check only if interface type is not listed in %excluded
  164. if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
  165. if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ; }
  166. if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
  167. $ifdown++ ;
  168. if (defined $ifXTable) {
  169. $ifmessage .= sprintf("%s: down -> %s<BR>\n", $ifStatus{$key}{$snmpIfName}, $ifStatus{$key}{$snmpIfAlias});
  170. }else{
  171. $ifmessage .= sprintf("%s: down <BR>\n",$ifStatus{$key}{$snmpIfDescr});
  172. }
  173. }
  174. if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
  175. } else {
  176. $ifexclude++;
  177. }
  178. } else {
  179. $ifexclude++;
  180. }
  181. } else {
  182. $ifunused++;
  183. }
  184. }
  185. } else {
  186. $ifunused++;
  187. }
  188. }
  189. if ($ifdown > 0) {
  190. $state = 'CRITICAL';
  191. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
  192. $hostname,
  193. $ifup,
  194. $ifdown,
  195. $ifdormant,
  196. $ifexclude,
  197. $ifunused);
  198. $answer = $answer . $ifmessage . "\n";
  199. }
  200. else {
  201. $state = 'OK';
  202. $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
  203. $hostname,
  204. $ifup,
  205. $ifdown,
  206. $ifdormant,
  207. $ifexclude,
  208. $ifunused);
  209. }
  210. my $perfdata = sprintf("up=%d down=%d dormant=%d excluded=%d unused=%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
  211. print ("$state: $answer |$perfdata\n");
  212. exit $ERRORS{$state};
  213. sub usage($) {
  214. print "$_[0]\n";
  215. print_usage();
  216. exit $ERRORS{"UNKNOWN"};
  217. }
  218. sub print_usage() {
  219. printf "\n";
  220. printf "usage: \n";
  221. printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
  222. printf "Copyright (C) 2000 Christoph Kron\n";
  223. printf "Updates 5/2002 Subhendu Ghosh\n";
  224. support();
  225. printf "\n\n";
  226. }
  227. sub print_help() {
  228. print_revision($PROGNAME, '@NP_VERSION@');
  229. print_usage();
  230. printf "check_ifstatus plugin for Nagios monitors operational \n";
  231. printf "status of each network interface on the target host\n";
  232. printf "\nUsage:\n";
  233. printf " -H (--hostname) Hostname to query - (required)\n";
  234. printf " -C (--community) SNMP read community (defaults to public,\n";
  235. printf " used with SNMP v1 and v2c\n";
  236. printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
  237. printf " 2 for SNMP v2c\n";
  238. printf " SNMP v2c will use get_bulk for less overhead\n";
  239. printf " 3 for SNMPv3 (requires -U option)";
  240. printf " -p (--port) SNMP port (default 161)\n";
  241. printf " -I (--ifmib) Agent supports IFMIB ifXTable. For Cisco - this will provide\n";
  242. printf " the descriptive name. Do not use if you don't know what this is. \n";
  243. printf " -x (--exclude) A comma separated list of ifType values that should be excluded \n";
  244. printf " from the report (default for an empty list is PPP(23).\n";
  245. printf " -d (--exclude_ports_by_description) A comma separated list of LocIfDescr values that should be excluded \n";
  246. printf " from the report (default is an empty exclusion list). Done using regexp '/^arg/', ex:\n";
  247. printf " '-d connect,test' will match with descriptions like 'testing phase' but not 'in testing'.\n";
  248. printf " -n (--unused_ports_by_name) A comma separated list of ifDescr values that should be excluded \n";
  249. printf " from the report (default is an empty exclusion list).\n";
  250. printf " -u (--unused_ports) A comma separated list of ifIndex values that should be excluded \n";
  251. printf " from the report (default is an empty exclusion list).\n";
  252. printf " See the IANAifType-MIB for a list of interface types.\n";
  253. printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
  254. printf " -U (--secname) username for SNMPv3 context\n";
  255. printf " -c (--context) SNMPv3 context name (default is empty string)\n";
  256. printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
  257. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  258. printf " auth password and authEngineID\n";
  259. printf " -a (--authproto) Authentication protocol (MD5 or SHA1)\n";
  260. printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
  261. printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
  262. printf " privacy password and authEngineID\n";
  263. printf " -P (--privproto) privacy protocol (DES or AES; default: DES)\n";
  264. printf " -M (--maxmsgsize) Max message size - useful only for v1 or v2c\n";
  265. printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
  266. printf " -V (--version) Plugin version\n";
  267. printf " -h (--help) usage help \n\n";
  268. print_revision($PROGNAME, '@NP_VERSION@');
  269. }
  270. sub process_arguments() {
  271. $status = GetOptions(
  272. "V" => \$opt_V, "version" => \$opt_V,
  273. "h" => \$opt_h, "help" => \$opt_h,
  274. "v=s" => \$snmp_version, "snmp_version=s" => \$snmp_version,
  275. "C=s" => \$community, "community=s" => \$community,
  276. "d=s" => \$opt_d, "exclude_ports_by_description=s" => \$opt_d,
  277. "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
  278. "a=s" => \$authproto, "authproto=s" => \$authproto,
  279. "U=s" => \$secname, "secname=s" => \$secname,
  280. "A=s" => \$authpass, "authpass=s" => \$authpass,
  281. "X=s" => \$privpass, "privpass=s" => \$privpass,
  282. "P=s" => \$privproto, "privproto=s" => \$privproto,
  283. "c=s" => \$context, "context=s" => \$context,
  284. "p=i" => \$port, "port=i" => \$port,
  285. "H=s" => \$hostname, "hostname=s" => \$hostname,
  286. "I" => \$ifXTable, "ifmib" => \$ifXTable,
  287. "x:s" => \$opt_x, "exclude:s" => \$opt_x,
  288. "u=s" => \$opt_u, "unused_ports=s" => \$opt_u,
  289. "n=s" => \$opt_n, "unused_ports_by_name=s" => \$opt_n,
  290. "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
  291. "t=i" => \$timeout, "timeout=i" => \$timeout,
  292. );
  293. if ($status == 0){
  294. print_help();
  295. exit $ERRORS{'OK'};
  296. }
  297. if ($opt_V) {
  298. print_revision($PROGNAME,'@NP_VERSION@');
  299. exit $ERRORS{'OK'};
  300. }
  301. if ($opt_h) {
  302. print_help();
  303. exit $ERRORS{'OK'};
  304. }
  305. unless (defined $timeout) {
  306. $timeout = $TIMEOUT;
  307. }
  308. # Net::SNMP wants an integer
  309. $snmp_version = 2 if $snmp_version eq "2c";
  310. if ($snmp_version !~ /^[123]$/){
  311. $state='UNKNOWN';
  312. print ("$state: No support for SNMP v$snmp_version yet\n");
  313. exit $ERRORS{$state};
  314. }
  315. %session_opts = (
  316. -hostname => $hostname,
  317. -port => $port,
  318. -version => $snmp_version,
  319. -maxmsgsize => $maxmsgsize
  320. );
  321. $session_opts{'-community'} = $community if (defined $community && $snmp_version =~ /[12]/);
  322. if ($snmp_version =~ /3/ ) {
  323. # Must define a security level even though default is noAuthNoPriv
  324. # v3 requires a security username
  325. if (defined $seclevel && defined $secname) {
  326. $session_opts{'-username'} = $secname;
  327. # Must define a security level even though default is noAuthNoPriv
  328. unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
  329. usage("Must define a valid security level even though default is noAuthNoPriv");
  330. }
  331. # Authentication wanted
  332. if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
  333. if (defined $authproto && $authproto ne 'MD5' && $authproto ne 'SHA1') {
  334. usage("Auth protocol can be either MD5 or SHA1");
  335. }
  336. $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
  337. if ( !defined $authpass) {
  338. usage("Auth password/key is not defined");
  339. }else{
  340. if ($authpass =~ /^0x/ ) {
  341. $session_opts{'-authkey'} = $authpass ;
  342. }else{
  343. $session_opts{'-authpassword'} = $authpass ;
  344. }
  345. }
  346. }
  347. # Privacy (DES encryption) wanted
  348. if ($seclevel eq 'authPriv' ) {
  349. if (! defined $privpass) {
  350. usage("Privacy passphrase/key is not defined");
  351. }else{
  352. if ($privpass =~ /^0x/){
  353. $session_opts{'-privkey'} = $privpass;
  354. }else{
  355. $session_opts{'-privpassword'} = $privpass;
  356. }
  357. }
  358. $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
  359. }
  360. # Context name defined or default
  361. unless ( defined $context) {
  362. $context = "";
  363. }
  364. }else {
  365. usage("Security level or name is not defined");
  366. }
  367. } # end snmpv3
  368. # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
  369. if (defined $opt_x) {
  370. my @x = split(/,/, $opt_x);
  371. if ( @x) {
  372. foreach $key (@x){
  373. $excluded{$key} = 1;
  374. }
  375. }else{
  376. $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
  377. }
  378. }
  379. # Exclude interfaces by descriptions
  380. if (defined $opt_d) {
  381. @exclude_descriptions = split(/,/,$opt_d);
  382. }
  383. # Excluded interface descriptors
  384. if (defined $opt_n) {
  385. my @unused = split(/,/,$opt_n);
  386. if ( @unused ) {
  387. foreach $key (@unused) {
  388. $unused_names{$key} = 1;
  389. }
  390. }
  391. }
  392. # Excluded interface ports (ifIndex) - management reasons
  393. if ($opt_u) {
  394. @unused_ports = split(/,/,$opt_u);
  395. foreach $key (@unused_ports) {
  396. $ifStatus{$key}{'notInUse'}++ ;
  397. }
  398. }
  399. if (! utils::is_hostname($hostname)){
  400. usage("Hostname invalid or not given");
  401. exit $ERRORS{"UNKNOWN"};
  402. }
  403. if ($snmp_version !~ /[123]/) {
  404. $state='UNKNOWN';
  405. print ("$state: No support for SNMP v$snmp_version yet\n");
  406. exit $ERRORS{$state};
  407. }
  408. return $ERRORS{"OK"};
  409. }