#!/usr/bin/perl -w # Check Network Interface IO via SNMP. # Plugin uses OID (1.3.6.1.2.1.2.2) SNMP MIB-2 Interfaces ifTable. # # # Copyright (C) 2009 by Herbert Stadler # email: hestadler@gmx.at # License Information: # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . # # ############################################################################ use POSIX; use strict; use Getopt::Long; use lib "."; use lib "/usr/lib/nagios/plugins"; use lib "/usr/lib64/nagios/plugins"; use lib "/usr/local/nagios/libexec"; use utils qw(%ERRORS); use Net::SNMP qw(oid_lex_sort oid_base_match); my ($opt_version,$opt_help,$opt_verbose); my ($opt_timeout,$opt_license,$opt_interface); my ($opt_hostname,$opt_community,$opt_port,$opt_snmpvers); my ($opt_username,$opt_authpasswd,$opt_authproto); my ($opt_privpasswd,$opt_privproto); my ($opt_warn,$opt_crit); my ($PROGNAME,$REVISION); my ($state,$msg); use constant DEFAULT_TIMEOUT =>30; use constant DEFAULT_PORT =>161; use constant DEFAULT_COMMUNITY =>"public"; use constant DEFAULT_SNMPVERS =>"2"; use constant DEFAULT_PRIVPROTO =>"DES"; use constant DEFAULT_AUTHPROTO =>"MD5"; use constant WAIT_BETWEEN_MEASUREMENT =>5; use constant COUNTER_MAXVAL =>4294967296; # SNMP MIB-2 Interface OID Definitions my $ifTable ="1.3.6.1.2.1.2.2"; my $ifEntry ="1.3.6.1.2.1.2.2.1"; my $ifIndex ="1.3.6.1.2.1.2.2.1.1"; my $ifDescr ="1.3.6.1.2.1.2.2.1.2"; my $ifAdminStatus ="1.3.6.1.2.1.2.2.1.7"; my $ifOperStatus ="1.3.6.1.2.1.2.2.1.8"; my $ifInOctets ="1.3.6.1.2.1.2.2.1.10"; my $ifOutOctets ="1.3.6.1.2.1.2.2.1.16"; $ENV{'PATH'}=''; $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; $PROGNAME = "check_snmp_netifio"; $REVISION = "1.1"; # checking commandline arguments my $arg_status = check_args(); if ($arg_status){ print "ERROR: some arguments wrong\n"; exit $ERRORS{"UNKNOWN"}; } # set alarmhandler for timeout handling $SIG{'ALRM'} = sub { print ("ERROR: plugin timed out after $opt_timeout seconds \n"); exit $ERRORS{"UNKNOWN"}; }; alarm($opt_timeout); # let's see if the server wants to speak with us my ($snmp_session,$snmp_error)=open_snmp_session($opt_hostname); if ( ! defined ($snmp_session)) { print "ERROR: Could not open connection: $snmp_error \n"; exit $ERRORS{'UNKNOWN'}; } $snmp_session->translate(['-endofmibview'=>0,'-nosuchobject'=>0,'-nosuchinstance'=>0]); if ( $opt_verbose ) { printf("1st Measurement of Network-Interface IO-Load\n"); } # get ifTable 1st Measurement my $ifTable_1st=get_table($ifTable); if ( $opt_verbose ) { printf("Waiting %d seconds\n",WAIT_BETWEEN_MEASUREMENT); } # now we are waiting some seconds sleep (WAIT_BETWEEN_MEASUREMENT); if ( $opt_verbose ) { printf("2nd Measurement of Network-Interface IO-Load\n"); } # get ifTable 2nd Measurement my $ifTable_2nd=get_table($ifTable); $snmp_session->close; if ( $opt_verbose ) { print_ifTable ("1st Measurement",$ifTable_1st); print_ifTable ("2nd Measurement",$ifTable_2nd); } my $perfdata=""; my $ifTableDiff=Build_Diff_Values(); foreach my $l_key (oid_lex_sort(keys(%{$ifTableDiff}))){ next if (!(oid_base_match($ifIndex,$l_key))); my $l_val=$ifTableDiff->{$l_key}; next if ( $ifTableDiff->{$ifDescr.".".$l_val} ne $opt_interface ); my $InMB =$ifTableDiff->{$ifInOctets.".".$l_val}; if ( $InMB < 0 ) { # check 32 bit overrun $InMB += COUNTER_MAXVAL; } $InMB = $InMB /1024/1024; my $OutMB=$ifTableDiff->{$ifOutOctets.".".$l_val}; if ( $OutMB < 0 ) { # check 32 bit overrun $OutMB += COUNTER_MAXVAL; } $OutMB = $OutMB /1024/1024; # Calculate average values my $InMBsec =$InMB / WAIT_BETWEEN_MEASUREMENT; my $OutMBsec=$OutMB / WAIT_BETWEEN_MEASUREMENT; # Build performance data line $perfdata=sprintf("InMBsec=%.2f OutMBsec=%.2f",$InMBsec,$OutMBsec); if ( $opt_verbose ) { printf ("Interace found in SNMP-MIB-2 Table\n"); printf ("IndexNr : %d\n",$l_val); printf ("Device : %s\n",$ifTableDiff->{$ifDescr.".".$l_val}); printf ("In Octets : %d (%.2f MB)(%.2f MB/sec)\n",$ifTableDiff->{$ifInOctets.".".$l_val},$InMB,$InMBsec); printf ("Out Octets : %d (%.2f MB)(%.2f MB/sec)\n",$ifTableDiff->{$ifOutOctets.".".$l_val},$OutMB,$OutMBsec); } # in case we use some other calculated fields my $checkval=$InMBsec; my $txt ="In"; if ( $checkval < $opt_warn ) { $msg = sprintf("%s OK - No Problems found (%s %.2f MB/s)",$opt_interface,$txt,$checkval); $state = $ERRORS{'OK'}; }elsif ( $checkval < $opt_crit ) { $msg = sprintf("%s WARN - %s %.2f MB/s",$opt_interface,$txt,$checkval); $state = $ERRORS{'WARNING'}; }else{ $msg = sprintf("%s CRIT - %s %.2f MB/s",$opt_interface,$txt,$checkval); $state = $ERRORS{'CRITICAL'}; } last; } # attach performance data line $msg.="|".$perfdata; # and now "over and out" print "$msg\n"; exit $state; #--------------------------------------------------------------------------# # S U B R O U T I N E S # #--------------------------------------------------------------------------# #-------------------------------------------------------------------------- sub open_snmp_session { #-------------------------------------------------------------------------- my ($l_host)=@_; my ($snmp_session,$snmp_error); # open SNMP Session to Server if ( $opt_snmpvers eq "3" ) { if ( defined ($opt_authpasswd)) { if ( defined ($opt_privpasswd)) { ($snmp_session,$snmp_error)=Net::SNMP->session( -hostname => $l_host, -port => $opt_port, -timeout => 2, -retries => 2, -maxmsgsize => 16384, -version => $opt_snmpvers, -username => $opt_username, -authpassword => $opt_authpasswd, -authprotocol => $opt_authproto, -privpassword => $opt_privpasswd, -privprotocol => $opt_privproto, ); } else { ($snmp_session,$snmp_error)=Net::SNMP->session( -hostname => $l_host, -port => $opt_port, -timeout => 2, -retries => 2, -maxmsgsize => 16384, -version => $opt_snmpvers, -username => $opt_username, -authpassword => $opt_authpasswd, -authprotocol => $opt_authproto, ); } } else { ($snmp_session,$snmp_error)=Net::SNMP->session( -hostname => $l_host, -port => $opt_port, -timeout => 2, -retries => 2, -maxmsgsize => 16384, -version => $opt_snmpvers, -username => $opt_username, ); } } else { ($snmp_session,$snmp_error)=Net::SNMP->session( -hostname => $l_host, -community => $opt_community, -port => $opt_port, -timeout => 2, -retries => 2, -maxmsgsize => 16384, -version => $opt_snmpvers, ); } return ($snmp_session,$snmp_error); } #-------------------------------------------------------------------------- sub get_table { #-------------------------------------------------------------------------- my ($l_oid)=@_; my $l_snmp_result=$snmp_session->get_table( -baseoid => $l_oid ); #if ( ! defined ($l_snmp_result)) { if ($snmp_session->error_status != 0) { print "ERROR %d: get_table: ",$snmp_session->error_status,$snmp_session->error,"\n"; $snmp_session->close; exit $ERRORS{'UNKNOWN'}; } return $l_snmp_result; } #-------------------------------------------------------------------------- sub check_args { #-------------------------------------------------------------------------- Getopt::Long::Configure('bundling'); GetOptions ("V" => \$opt_version, "version" => \$opt_version, "L" => \$opt_license, "license" => \$opt_license, "v" => \$opt_verbose, "verbose" => \$opt_verbose, "h|?" => \$opt_help, "help" => \$opt_help, "t=i" => \$opt_timeout, "timeout=i" => \$opt_timeout, "H=s" => \$opt_hostname, "hostname=s" => \$opt_hostname, "i=s" => \$opt_interface, "interface=s" => \$opt_interface, "C=s" => \$opt_community, "community=s" => \$opt_community, "p=i" => \$opt_port, "port=i" => \$opt_port, "s=s" => \$opt_snmpvers, "snmpvers=s" => \$opt_snmpvers, "u=s" => \$opt_username, "username=s" => \$opt_username, "o=s" => \$opt_authpasswd, "authpass=s" => \$opt_authpasswd, "r=s" => \$opt_authproto, "authprot=s" => \$opt_authproto, "O=s" => \$opt_privpasswd, "privpass=s" => \$opt_privpasswd, "R=s" => \$opt_privproto, "privprot=s" => \$opt_privproto, "w=s" => \$opt_warn, "warn=s" => \$opt_warn, "c=s" => \$opt_crit, "crit=s" => \$opt_crit, ); if ($opt_license) { print_gpl($PROGNAME,$REVISION); exit $ERRORS{'OK'}; } if ($opt_version) { print_revision($PROGNAME,$REVISION); exit $ERRORS{'OK'}; } if ($opt_help) { print_help(); exit $ERRORS{'OK'}; } if ( ! defined($opt_hostname)){ print "\nERROR: Hostname not defined\n\n"; print_usage(); exit $ERRORS{'UNKNOWN'}; } if ( ! defined($opt_interface)){ print "\nERROR: Network interface not defined\n\n"; print_usage(); exit $ERRORS{'UNKNOWN'}; } unless (defined $opt_snmpvers) { $opt_snmpvers = DEFAULT_SNMPVERS; } if (($opt_snmpvers ne "1") && ($opt_snmpvers ne "2") && ($opt_snmpvers ne "3")) { printf ("\nERROR: SNMP Version %s unknown\n",$opt_snmpvers); print_usage(); exit $ERRORS{'UNKNOWN'}; } unless (defined $opt_warn) { print "\nERROR: parameter -w not defined\n\n"; print_usage(); exit ($ERRORS{'UNKNOWN'}); } unless (defined $opt_crit) { print "\nERROR: parameter -c not defined\n\n"; print_usage(); exit ($ERRORS{'UNKNOWN'}); } if ( $opt_warn > $opt_crit) { print "\nERROR: parameter -w greater than parameter -c\n\n"; print_usage(); exit ($ERRORS{'UNKNOWN'}); } unless (defined $opt_timeout) { $opt_timeout = DEFAULT_TIMEOUT; } unless (defined $opt_port) { $opt_port = DEFAULT_PORT; } unless (defined $opt_community) { $opt_community = DEFAULT_COMMUNITY; } if (defined $opt_privpasswd) { unless (defined $opt_privproto) { $opt_privproto = DEFAULT_PRIVPROTO; } } if (defined $opt_authpasswd) { unless (defined $opt_authproto) { $opt_authproto = DEFAULT_AUTHPROTO; } } if ($opt_snmpvers eq 3) { unless (defined $opt_username) { printf ("\nERROR: SNMP Version %s: please define username\n",$opt_snmpvers); print_usage(); exit $ERRORS{'UNKNOWN'}; } } return $ERRORS{'OK'}; } #-------------------------------------------------------------------------- sub print_usage { #-------------------------------------------------------------------------- print "Usage: $PROGNAME [-h] [-L] [-t timeout] [-v] [-V] [-C community] [-p port] [-s 1|2|3] -H hostname -d netinterfacd -w -c \n\n"; print "SNMP version 3 specific: [-u username] [-o authpass] [-r authprot] [-O privpass] [-R privprot]\n"; } #-------------------------------------------------------------------------- sub print_help { #-------------------------------------------------------------------------- print_revision($PROGNAME,$REVISION); printf("\n"); print_usage(); printf("\n"); printf(" Check Network Interface IO via SNMP-MIB-2\n\n"); printf("-t (--timeout) Timeout in seconds (default=%d)\n",DEFAULT_TIMEOUT); printf("-H (--hostname) Host to monitor\n"); printf("-i (--interface) Network Interface to monitor\n"); printf("-s (--snmpvers) SNMP Version [1|2|3] (default=%d)\n",DEFAULT_SNMPVERS); printf("-C (--community) SNMP Community (default=%s)\n",DEFAULT_COMMUNITY); printf("-p (--port) SNMP Port (default=%d)\n",DEFAULT_PORT); printf("-w (--warn) Parameter warning for MB In/Out/sec\n"); printf("-c (--crit) Parameter critical for MB In/Out/sec\n"); printf("-h (--help) Help\n"); printf("-V (--version) Programm version\n"); printf("-v (--verbose) Print some useful information\n"); printf("-L (--license) Print license information\n"); printf("\nSNMP version 3 specific arguments:\n"); printf("-u (--username) Security Name\n"); printf("-o (--authpassword) Authentication password\n"); printf("-r (--authprotocol) Authentication protocol [md5|sha]\n"); printf("-O (--privpassword) Privacy password\n"); printf("-R (--privprotocol) Privacy protocol [des|aes|3des]\n"); printf("\n"); } #-------------------------------------------------------------------------- sub print_ifTable { #-------------------------------------------------------------------------- my ($l_description,$ifTable)=@_; printtable (sprintf("Interface Table - %s",$l_description)); print ("======================================\n"); foreach my $l_key (oid_lex_sort(keys(%{$ifTable}))){ next if (!(oid_base_match($ifIndex,$l_key))); my $l_val=$ifTable->{$l_key}; next if ( $ifTable->{$ifDescr.".".$l_val} ne $opt_interface ); printtabular("Index", $l_val); printtabular("Description", $ifTable->{$ifDescr.".".$l_val}); printtabular("Admin Status", $ifTable->{$ifAdminStatus.".".$l_val}); printtabular("Oper Status", $ifTable->{$ifOperStatus.".".$l_val}); printtabular("IN Octets", $ifTable->{$ifInOctets.".".$l_val}); printtabular("OUT Octets", $ifTable->{$ifOutOctets.".".$l_val}); printf("\n"); } } #-------------------------------------------------------------------------- sub printhead { #-------------------------------------------------------------------------- my ($l_head)=@_; printf ("\n%-40s\n",$l_head); } #-------------------------------------------------------------------------- sub printtable { #-------------------------------------------------------------------------- my ($l_head)=@_; printf ("%-40s\n",$l_head); } #-------------------------------------------------------------------------- sub printscalar { #-------------------------------------------------------------------------- my ($l_arg,$l_oid)=@_; printf ("%-35s: %-30s\n",$l_arg,$l_oid); } #-------------------------------------------------------------------------- sub printtabular { #-------------------------------------------------------------------------- my ($l_arg,$l_oid)=@_; printf ("%-35s: %-30s\n",$l_arg,$l_oid); } #-------------------------------------------------------------------------- sub Build_Diff_Values { #-------------------------------------------------------------------------- my $hDiffTable={}; foreach my $l_key (oid_lex_sort(keys(%{$ifTable_2nd}))){ next if (!(oid_base_match($ifIndex,$l_key))); my $index_2nd=$ifTable_2nd->{$l_key}; my $l_interface=$ifTable_2nd->{$ifDescr.".".$index_2nd}; next if ( $l_interface ne $opt_interface ); my $index_1st=Get_Index_Nr_1st_Table($l_interface); my $IO_In =$ifTable_2nd->{$ifInOctets.".".$index_2nd} - $ifTable_1st->{$ifInOctets.".".$index_1st}; my $IO_Out =$ifTable_2nd->{$ifOutOctets.".".$index_2nd} - $ifTable_1st->{$ifOutOctets.".".$index_1st}; $hDiffTable->{$l_key} = $index_2nd; $hDiffTable->{$ifDescr.".".$index_2nd} = $l_interface; $hDiffTable->{$ifInOctets.".".$index_2nd} = $IO_In; $hDiffTable->{$ifOutOctets.".".$index_2nd} = $IO_Out; } return($hDiffTable); } #-------------------------------------------------------------------------- sub Get_Index_Nr_1st_Table { #-------------------------------------------------------------------------- my ($l_interface)=@_; foreach my $l_key (keys(%{$ifTable_1st})){ next if (!(oid_base_match($ifIndex,$l_key))); my $l_val=$ifTable_1st->{$l_key}; my $l_interface_1st=$ifTable_1st->{$ifDescr.".".$l_val}; next if ( $l_interface_1st ne $l_interface ); return($l_val); } return(undef); } #-------------------------------------------------------------------------- sub print_gpl { #-------------------------------------------------------------------------- print <. EOD } #-------------------------------------------------------------------------- sub print_revision { #-------------------------------------------------------------------------- my ($l_prog,$l_revision)=@_; print < -i bond0 -w 80 -c 90 for more information concerning this plugin call: check_snmp_netifio -h perldoc check_snmp_netifio =head1 AUTHOR Herbert Stadler, Austria (hestadler@gmx.at) July 2009 This plugin is a contribution to the nagios community. =head1 REQUIRED SOFTWARE from search.cpan.org Net::SNMP Package e.g: Net-SNMP-5.2.0.tar.gz =head1 HOW TO CHECK THE SERVER FUNCTIONALITY Example: snmpwalk -On -c public -v 1 1.3.6.1.2.1.2.2 should return some lines =head1 CONFIGURATION IN NAGIOS Copy this plugin to the nagios plugin installation directory e.g.: /usr/lib(64)/nagios/plugin COMMAND DEFINITION: # "check_snmp_netifio" command definition define command{ command_name check_snmp_netifio command_line $USER1$/check_snmp_netifio -H $HOSTADDRESS$ -i $ARG1$ -w $ARG2$ -c $ARG3$ } =head1 PLUGIN HISTORY Version 1.0 - 2009-07-01 first release Version 1.1 - 2010-03-24 check error_status of snmp call =head1 COPYRIGHT AND DISCLAIMER Copyright (C) 2009 by Herbert Stadler email: hestadler@gmx.at License Information: This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . =cut