Title: [opsview] [11527] (Re)Updated check_snmp_interfaces to use SNMPv3 library
Revision
11527
Author
hstevenson
Date
2013-02-19 13:14:15 +0000 (Tue, 19 Feb 2013)

Log Message

(Re)Updated check_snmp_interfaces to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_interfaces
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_interfaces	2013-02-19 13:06:38 UTC (rev 11526)
+++ trunk/opsview-core/nagios-plugins/check_snmp_interfaces	2013-02-19 13:14:15 UTC (rev 11527)
@@ -20,22 +20,30 @@
 #    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;
 
-my $script         = "check_snmp_interfaces";
-my $script_version = "2.2";
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => "check_snmp_interfaces",
+    version   => "2.3",
+    blurb     => "Tracks status of Ethernet interfaces",
+    snmp      => {
+        "snmp-version" => 1,
+        "snmp-timeout" => 2,
+    },
+);
 
-# SNMP options
-my $version = "2c";
-my $timeout = 2;
+$np->getopts;
 
-# $metric = 1;
+my $hostname = $np->opts->hostname;
+my $s        = $np->snmp;
+
 my $firsttime        = 0;
 my $change           = 0;
 my $error            = 0;
@@ -46,78 +54,14 @@
 my $oid_sysDescr     = ".1.3.6.1.2.1.1.1.0";
 my $baseifdescr      = ".1.3.6.1.2.1.2.2.1.2";
 my $baseifoperstatus = ".1.3.6.1.2.1.2.2.1.8";
-my $hostname         = "192.168.10.21";
-my $port             = 161;
+my $returnstring     = "";
 
-#$hostname = "212.113.28.134";
-my $returnstring = "";
-
-my $community      = "public";               # Default community string
-my $configfilepath = "/usr/local/nagios/etc";
-
-# Do we have enough information?
-if ( @ARGV < 1 ) {
-    print "Too few arguments\n";
-    usage();
-}
-
-my $opts = {};
-getopts( "hH:C:p:", $opts );
-if ( $opts->{h} ) {
-    usage();
-    exit(0);
-}
-if ( $opts->{H} ) {
-    $hostname = $opts->{H};
-
-    # print "Hostname $opt_H\n";
-}
-else {
-    print "No hostname specified\n";
-    usage();
-    exit(0);
-}
-if ( $opts->{C} ) {
-    $community = $opts->{C};
-
-    # print "Using community $opt_C\n";
-}
-else {
-
-    # print "Using community $community\n";
-}
-if ( $opts->{p} ) {
-    $port = $opts->{p};
-}
-
 my $statusfile = "/usr/local/nagios/var/status_$hostname.db";
 readstatus();
 
-# Create the SNMP session
-
-$version = "1";
-my ( $s, $e ) = Net::SNMP->session(
-    -community => $community,
-    -hostname  => $hostname,
-    -version   => $version,
-    -timeout   => $timeout,
-    -port      => $port,
-);
-
 if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-
-    # If we can't connect using SNMPv1 lets try as SNMPv2
-    $s->close();
-    sleep 0.5;
-    $version = "2c";
-    ( $s, $e ) = Net::SNMP->session(
-        -community => $community,
-        -hostname  => $hostname,
-        -version   => $version,
-        -timeout   => $timeout,
-    );
     if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-        print "Agent not responding, tried SNMP v1 and v2\n";
+        print "Agent not responding\n";
         exit(3);
     }
 }
@@ -220,17 +164,3 @@
     $returnstring = "$returnstring$appendstring";
 }
 
-sub usage {
-    print << "USAGE";
-
-$script v$script_version
-
-Tracks status of Ethernet interfaces
-
-Usage: $script -H <hostname> -c <community> [...]
-Options: -H 		Hostname or IP address
-         -C 		Community (default is public)
-         -p 		Port (default: 161)
-USAGE
-    exit 1;
-}

_______________________________________________
Opsview-checkins mailing list
Opsview-checkins@lists.opsview.org
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to