Title: [opsview] [11349] Updated check_snmp_netscreen_nsrp to use SNMPv3 library
Revision
11349
Author
hstevenson
Date
2013-02-05 12:20:52 +0000 (Tue, 05 Feb 2013)

Log Message

Updated check_snmp_netscreen_nsrp to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_netscreen_nsrp
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_netscreen_nsrp	2013-02-05 11:11:53 UTC (rev 11348)
+++ trunk/opsview-core/nagios-plugins/check_snmp_netscreen_nsrp	2013-02-05 12:20:52 UTC (rev 11349)
@@ -34,155 +34,87 @@
 #   ::= { nsrpVsdMemberEntry 3 }
 
 use strict;
-use lib qw ( /usr/local/nagios/perl/lib );
+use warnings;
+
+use FindBin qw($Bin);
+use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
 use Net::SNMP;
-use Getopt::Std;
+use Opsview::NagiosPlugin::SNMP;
 
-# About us
-my $script             = "check_snmp_netscreen_nsrp";
-my $script_version     = "0.1";
-my $script_description = "Checks NSRP state on a Netscreen device (by IP)";
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => "check_snmp_netscreen_nrsp",
+    version   => "0.2",
+    blurb     => "Checks NSRP state on a Netscreen device (by IP)",
+    snmp      => { "snmp-version" => 1, },
+);
 
-my $hostname;
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning states - see NSRP states below,
+                specify multiple states together, e.g.
+                -w 234 for 2, 3, and 4.},
+);
 
-# Performance data
-my $perfdata = "";
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical states - see warning.},
+);
 
-# SNMP variables
-my $oid_testoid =
-  ".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is responding
-my $oid_nsrpstate =
-  ".1.3.6.1.4.1.3224.6.2.2.1.3"; # Contains an entry for each NSRP VS
+$np->add_arg(
+    spec => "group|g=s",
+    help => qq{-g, --group=INTEGER
+    NSRP group to check state for (integer)},
+    required => 1,
+);
+$np->getopts;
 
-#my $oid_nsrpaddr = "1.3.6.1.4.1.3224.6.2.3.1.4";	# Contains an entry for each NSRP IP
-my $result    = 0;               # result of servers/connections
-my $community = "public";        # Default community
-my $timeout   = 2;               # SNMP timeout
-my $retmsg    = "";
-my $retval    = 0;
-my $version   = "1";
+my $warning  = $np->opts->warning;
+my $critical = $np->opts->critical;
+my $group    = $np->opts->group;
+
 my @warning;
 my @critical;
-my $state  = 0;
-my %states = (
-    0, "Undefined",      1, "Init",   2, "Master",
-    3, "Primary Backup", 4, "Backup", 5, "Ineligible",
-    6, "Inoperable"
-);
-my $group = 0;
-our ( $s, $e );
-my $port = 161;
 
-# Command line arguments
-our ( $opt_h, $opt_H, $opt_C, $opt_w, $opt_c, $opt_g, $opt_p );
-$opt_g = -1; # We have to do this because the user could specify '0'
-getopts( "hH:C:w:c:g:p:" );
-if ($opt_h) {
+if ( $warning && $warning !~ /^[0-6]+$/ ) {
+    print "Warning level is invalid, use -w [0-9]+ like -w 03456\n";
     usage();
-    exit 0;
-}
-if ($opt_H) {
-    $hostname = $opt_H;
-}
-else {
-    print "No hostname specified\n";
-    usage();
     exit 3;
 }
-if ($opt_C) {
-    $community = $opt_C;
-}
-if ($opt_p) {
-    $port = $opt_p;
-}
-
-# Must specify an IP address
-#if ($opt_a) {
-#	if($opt_a !~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
-#		print "$opt_a does not look like a valid IP address\n";
-#		usage();
-#		exit 3;
-#	}
-#} else {
-#	print "Must specify an IP address with -a\n";
-#	usage();
-#	exit 3;
-#}
-if ( $opt_g == -1 ) {
-    print "Must specify a group number with -g\n";
+if ( $critical && $critical !~ /^[0-6]+$/ ) {
+    print "Critical level is invalid, use -c [0-9]+ like -c 12\n";
     usage();
     exit 3;
 }
-else {
-    if ( $opt_g !~ /^[0-9]+$/ ) {
-        print "Group number must be an integer\n";
-        usage();
-        exit 3;
-    }
-
-    $group = $opt_g;
+if ($warning) {
+    @warning = split( //, $warning );
 }
 
-# Check the warning/critical levels are OK
-# User can specify levels as [0-6]+
-if ( $opt_c && $opt_w !~ /^[0-6]+$/ ) {
-    print "Warning level is invalid, use -w [0-9]+ like -w 03456\n";
-    usage();
-    exit 3;
+if ($critical) {
+    @critical = split( //, $critical );
 }
-if ( $opt_c && $opt_c !~ /^[0-6]+$/ ) {
-    print "Critical level is invalid, use -c [0-9]+ like -c 12\n";
-    usage();
-    exit 3;
-}
+my $s = $np->snmp;
 
-# Set warning and critical options
-@warning  = split( //, $opt_w );
-@critical = split( //, $opt_c );
+my $oid_testoid =
+  ".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is responding
+my $oid_nsrpstate =
+  ".1.3.6.1.4.1.3224.6.2.2.1.3"; # Contains an entry for each NSRP VS
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
+my $result;
+my $retmsg;
+my $retval;
 
-$script $script_version
+my %states = (
+    0, "Undefined",      1, "Init",   2, "Master",
+    3, "Primary Backup", 4, "Backup", 5, "Ineligible",
+    6, "Inoperable"
+);
 
-$script_description
-
-Usage: $script -H <hostname> -C <community> -w <warning> -c <critical>
-	       -a <ip_address>
-
-Options: -H     Hostname or IP address
-         -p     Port (default: 161)
-         -C     Community (default is public)
-         -w     Warning states - see NSRP states below,
-		specify multiple states together, e.g.
-		-w 234 for 2, 3, and 4.
-         -c     Critical states - see warning.
-         -g     NSRP group to check state for (integer)
-
-NSRP States:
-         0 Undefined
-         1 init
-         2 Master
-         3 Primary Backup
-         4 Backup
-         5 ineligible
-         6 inoperable
-
-
---------------------------------------------------------------------
-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
-------------------------------------------------------------------
-EOF
-
-}
-
 # Call this when you know you'll get a single value back
 sub get_oid_value {
-    our ( $oid, $result, $status, $returnstring );
+    our ( $oid, $status, $returnstring );
 
     $oid = shift(@_);
 
@@ -229,34 +161,6 @@
     return 0;
 }
 
-# Create the SNMP session
-( $s, $e ) = Net::SNMP->session(
-    -community => $community,
-    -hostname  => $hostname,
-    -version   => $version,
-    -timeout   => $timeout,
-    -port      => $port,
-);
-
-if ( !defined( $s->get_request($oid_testoid) ) ) {
-
-    # If we can't connect using SNMPv1 lets try as SNMPv2c
-    $s->close();
-    sleep 0.5;
-    $version = "2c";
-    ( $s, $e ) = Net::SNMP->session(
-        -community => $community,
-        -hostname  => $hostname,
-        -version   => $version,
-        -timeout   => $timeout,
-        -port      => $port,
-    );
-    if ( !defined( $s->get_request($oid_testoid) ) ) {
-        print "Agent not responding, tried SNMP v1 and v2\n";
-        exit 3;
-    }
-}
-
 # Get NSRP status for specified group
 $result = get_oid_value( "$oid_nsrpstate.$group" );
 
@@ -283,6 +187,8 @@
 # Output text
 $retmsg = "$retmsg - NSRP is $states{$result}($result) for group $group";
 
+my $perfdata;
+
 # Performance data
 if ( $states{$result} eq "Master" ) {
     $perfdata = "ismaster=1";

_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins

Reply via email to