Modified: trunk/opsview-core/nagios-plugins/check_snmp_ifstatus
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_ifstatus 2013-02-26 09:38:56 UTC (rev 11599)
+++ trunk/opsview-core/nagios-plugins/check_snmp_ifstatus 2013-02-26 11:41:37 UTC (rev 11600)
@@ -20,194 +20,78 @@
# along with Opsview; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
+use strict;
+use warnings;
-use lib qw ( /usr/local/nagios/perl/lib );
+use FindBin qw($Bin);
+use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
use Net::SNMP;
-use Getopt::Std;
+use Opsview::NagiosPlugin::SNMP;
-$script = "check_snmp_ifstatus";
-$script_version = "3.0";
+my $np = Opsview::NagiosPlugin::SNMP->new(
+ usage => "Usage: %s",
+ shortname => "check_snmp_ifstatus",
+ version => "3.1",
+ blurb => "Monitors status of specific Ethernet interface.",
+ snmp => {
+ "snmp-version" => 2,
+ "snmp-timeout" => 2,
+ "v1-fallback" => 1,
+ },
+);
-# SNMP options
-$version = "2c";
-$timeout = 2;
+$np->add_arg(
+ spec => "interface|i=s",
+ help => qq{-i, --interface=STRING
+ Target interface name
+ Eg: Serial0/0, FastEthernet0/12},
+ required => 1,
+);
-$number_of_interfaces = 0;
-$target_interface_index = 0;
+$np->getopts;
-$oid_sysdescr = ".1.3.6.1.2.1.1.1.0";
-$oid_ifnumber = ".1.3.6.1.2.1.2.1.0"; # number of interfaces on device
-$oid_ifdescr =
- ".1.3.6.1.2.1.2.2.1.2."; # need to append integer for specific interface
-$oid_iftype =
- ".1.3.6.1.2.1.2.2.1.3."; # need to append integer for specific interface
-$oid_ifmtu =
- ".1.3.6.1.2.1.2.2.1.4."; # need to append integer for specific interface
-$oid_ifspeed =
- ".1.3.6.1.2.1.2.2.1.5."; # need to append integer for specific interface
-$oid_ifphysaddress =
- ".1.3.6.1.2.1.2.2.1.6."; # need to append integer for specific interface
-$oid_ifadminstatus =
- ".1.3.6.1.2.1.2.2.1.7."; # need to append integer for specific interface
-$oid_ifoperstatus =
- ".1.3.6.1.2.1.2.2.1.8."; # need to append integer for specific interface
-$oid_iflastchange =
- ".1.3.6.1.2.1.2.2.1.9."; # need to append integer for specific interface
-$oid_ifinerrors =
- ".1.3.6.1.2.1.2.2.1.14."; # need to append integer for specific interface
-$oid_ifouterrors =
- ".1.3.6.1.2.1.2.2.1.20."; # need to append integer for specific interface
-$oid_ifoutqlen =
- ".1.3.6.1.2.1.2.2.1.21."; # need to append integer for specific interface
+my $target_interface = $np->opts->interface;
+my $hostname = $np->opts->hostname;
-$ifdescr = "n/a";
-$iftype = "n/a";
-$ifmtu = "n/a";
-$ifspeed = "n/a";
-$ifphysaddress = "n/a";
-$ifadminstatus = "n/a";
-$ifoperstatus = "n/a";
-$iflastchange = "n/a";
-$ifinerrors = "n/a";
-$ifouterrors = "n/a";
-$ifoutqlen = "n/a";
+my $s = $np->snmp;
-$warning = 0; # Warning threshold
-$critical = 0; # Critical threshold
+my $oid_sysdescr = ".1.3.6.1.2.1.1.1.0";
+my $oid_ifnumber = ".1.3.6.1.2.1.2.1.0";
+my $oid_ifdescr = ".1.3.6.1.2.1.2.2.1.2.";
+my $oid_iftype = ".1.3.6.1.2.1.2.2.1.3.";
+my $oid_ifmtu = ".1.3.6.1.2.1.2.2.1.4.";
+my $oid_ifspeed = ".1.3.6.1.2.1.2.2.1.5.";
+my $oid_ifphysaddress = ".1.3.6.1.2.1.2.2.1.6.";
+my $oid_ifadminstatus = ".1.3.6.1.2.1.2.2.1.7.";
+my $oid_ifoperstatus = ".1.3.6.1.2.1.2.2.1.8.";
+my $oid_iflastchange = ".1.3.6.1.2.1.2.2.1.9.";
+my $oid_ifinerrors = ".1.3.6.1.2.1.2.2.1.14.";
+my $oid_ifouterrors = ".1.3.6.1.2.1.2.2.1.20.";
+my $oid_ifoutqlen = ".1.3.6.1.2.1.2.2.1.21.";
+my $ifdescr = "n/a";
+my $iftype = "n/a";
+my $ifmtu = "n/a";
+my $ifspeed = "n/a";
+my $ifphysaddress = "n/a";
+my $ifadminstatus = "n/a";
+my $ifoperstatus = "n/a";
+my $iflastchange = "n/a";
+my $ifinerrors = "n/a";
+my $ifouterrors = "n/a";
+my $ifoutqlen = "n/a";
+my $target_interface_index;
-$snmpv3_username = "initial"; # SNMPv3 username
-$snmpv3_password = ""; # SNMPv3 password
-$snmpv3_authprotocol = "md5"; # SNMPv3 hash algorithm (md5 / sha)
-$snmpv3_privprotocol = "des"; # SNMPv3 encryption protocol (des / aes / aes128)
-$community = "public"; # Default community string (for SNMP v1 / v2c)
+my $warning = 0;
+my $critical = 0;
-$hostname = "192.168.10.21";
-my $port = 161;
+my $returnstring = "";
+my $status = 0;
-#$hostname = "212.113.28.134";
-$returnstring = "";
-
-$configfilepath = "/usr/local/nagios/etc";
-
-# Do we have enough information?
-if ( @ARGV < 1 ) {
- print "Too few arguments\n";
- usage();
-}
-
-getopts( "hH:C:U:P:a:e:i:w:v:p:" );
-if ($opt_h) {
- usage();
- exit(0);
-}
-if ($opt_H) {
- $hostname = $opt_H;
-
- # print "Hostname $opt_H\n";
-}
-else {
- print "No hostname specified\n";
- usage();
- exit(0);
-}
-if ($opt_C) {
- $community = $opt_C;
-}
-if ($opt_i) {
- $target_interface = $opt_i;
-}
-if ($opt_U) {
- $snmpv3_username = $opt_U;
-}
-if ($opt_P) {
- $snmpv3_password = $opt_P;
-}
-if ($opt_a) {
- $snmpv3_authprotocol = $opt_a;
-}
-if ($opt_e) {
- $snmpv3_privprotocol = $opt_e;
-}
-if ($opt_v) {
- $version = $opt_v;
-}
-if ($opt_p) {
- $port = $opt_p;
-}
-
-unless ($target_interface) {
- print "Must specify an interface name", $/;
- usage();
- exit 3;
-}
-
-# Create the SNMP session
-
-$oid_sysDescr =
- ".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is actually responding
-
-# Checks whether requested SNMP version is supported
-if ( $version !~ /^[13]|[2c]$/ ) {
- print "SNMP v$version not supported by this plugin\n";
+if ( !defined( $s->get_request($oid_sysdescr) ) ) {
+ print "Agent not responding, tried SNMP v1 and v2c\n";
exit(1);
}
-# Create the SNMP session
-if ( $version == "3" ) {
- ( $s, $e ) = Net::SNMP->session(
- -username => $snmpv3_username,
- -authpassword => $snmpv3_password,
- -authprotocol => $snmpv3_authprotocol,
- -privprotocol => $snmpv3_privprotocol,
- -hostname => $hostname,
- -version => $version,
- -timeout => $timeout,
- -port => $port,
- );
- if ($s) {
- }
- else {
- print "Agent not responding, tried SNMP v3 ($e)\n";
- exit(1);
- }
-}
-
-my $triedv2c = 0; # Track whether we've attempted SNMPv2c connection
-if ( $version == "2c" ) {
- ( $s, $e ) = Net::SNMP->session(
- -community => $community,
- -hostname => $hostname,
- -version => $version,
- -timeout => $timeout,
- -port => $port,
- );
- if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-
- # try SNMP v1 if v2c doesn't work
- $triedv2c = 1;
- $version = 1;
- }
-}
-
-if ( $version == "1" ) {
- ( $s, $e ) = Net::SNMP->session(
- -community => $community,
- -hostname => $hostname,
- -version => $version,
- -timeout => $timeout,
- -port => $port,
- );
- if ( !defined( $s->get_request($oid_sysDescr) ) ) {
- if ( $triedv2c == 1 ) {
- print "Agent not responding, tried SNMP v1 and v2c\n";
- }
- else {
- print "Agent not responding, tried SNMP v1\n";
- }
- exit(1);
- }
-}
-
if ( find_match() == 0 ) {
probe_interface();
}
@@ -245,6 +129,10 @@
sub find_match {
+ my $number_of_interfaces = 0;
+ my $oid_temp;
+ my $temp_interface_descr;
+
if ( !defined( $s->get_request($oid_ifnumber) ) ) {
if ( !defined( $s->get_request($oid_sysdescr) ) ) {
print "Status is a Warning Level - SNMP agent not responding\n";
@@ -264,7 +152,7 @@
}
}
- $index = 1;
+ my $index = 1;
while ( $index <= $number_of_interfaces ) {
$oid_temp = $oid_ifdescr . $index;
if ( !defined( $s->get_request($oid_temp) ) ) {
@@ -293,7 +181,10 @@
sub probe_interface {
- $oid_temp = $oid_ifdescr . $target_interface_index;
+ my $errorstring = "";
+ my $temp;
+
+ my $oid_temp = $oid_ifdescr . $target_interface_index;
if ( !defined( $s->get_request($oid_temp) ) ) {
}
else {
@@ -440,47 +331,11 @@
}
####################################################################
-# help and usage information #
-####################################################################
-
-sub usage {
- print << "USAGE";
---------------------------------------------------------------------
-$script v$script_version
-
-Monitors status of specific Ethernet interface.
-
-Usage: $script -H <hostname> -C <community> -i <interface name> [...]
-
-Options: -H Hostname or IP address
- -p Port (default: 161)
- -C Community (default is public)
- -U SNMPv3 username
- -P SNMPv3 password
- -a SNMPv3 hashing algorithm (default is MD5)
- -e SNMPv3 encryption protocol (default is DES)
- -v SNMP version (1, 2c or 3 supported)
- -i Target interface name
- Eg: Serial0/0, FastEthernet0/12
-
-
---------------------------------------------------------------------
-Copyright (C) 2003-2012 Opsview Limited. All rights reserved
-
-This program is free software; you can redistribute it or modify
-it under the terms of the GNU General Public License
---------------------------------------------------------------------
-
-USAGE
- exit 1;
-}
-
-####################################################################
# Appends string to existing $returnstring #
####################################################################
sub append {
- my $appendstring = @_[0];
+ my $appendstring = $_[0];
$returnstring = "$returnstring$appendstring";
}
@@ -489,7 +344,7 @@
####################################################################
sub return_interfacetype {
- my $iftype_int = @_[0];
+ my $iftype_int = $_[0];
my @iana = ();
my $returnstring = $iftype_int;