4
0

check_ifstatus.pl 13 KB

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