4
0

check_ifstatus.pl 12 KB

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