Title: [opsview] [11380] Update check_snmp_netscaler_vserver to use SNMPv3 library
Revision
11380
Author
hstevenson
Date
2013-02-07 18:55:13 +0000 (Thu, 07 Feb 2013)

Log Message

Update check_snmp_netscaler_vserver to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_netscaler_vserver
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_netscaler_vserver	2013-02-07 17:27:01 UTC (rev 11379)
+++ trunk/opsview-core/nagios-plugins/check_snmp_netscaler_vserver	2013-02-07 18:55:13 UTC (rev 11380)
@@ -20,110 +20,60 @@
 #    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 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_netscaler_vserver";
-my $script_version = "0.1";
-my $script_description =
-  "Checks status of Netscaler vserver (number of up/down physical servers)";
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => "check_snmp_netscaler_vserver",
+    version   => "0.2",
+    blurb =>
+      "Checks status of Netscaler vserver (number of up/down physical servers)",
+    snmp => {
+        "snmp-version" => 2,
+        "v1-fallback"  => 1,
+    },
+);
 
-my $hostname;
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning threshold},
+    default => 0,
+);
 
-# Performance data
-my $perfdata = "";
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical threshold},
+    default => 0,
+);
 
-# SNMP variables
-my $oid_testoid =
-  ".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is responding
-my $oid_servername_base =
-  "1.3.6.1.4.1.5951.1.3.2.1.1.5"; # Server names are under here
-my $total_servers = 0;            # Total number of physical servers in vserver
-my $up_servers    = 0;            # Currently operational servers in vserver
-my $community     = "public";     # Default community
-my $vserver_name  = "Unknown";    # Server name to check status of
-my $timeout       = 2;            # SNMP timeout
-my $retmsg        = "";
-my $retval        = 0;
-my $version       = "1";
-my $warning       = 0;
-my $critical      = 0;
-my $suboid;
-my $port = 161;
-our ( $s, $e );
+$np->add_arg(
+    spec => "server|s=s",
+    help => qq{-s, --server=STRING
+    vserver name},
+    required => 1,
+);
+$np->getopts;
 
-# Command line arguments
-our ( $opt_h, $opt_H, $opt_C, $opt_s, $opt_w, $opt_c, $opt_p );
-getopts( "hH:C:s:w:c:p:" );
-if ($opt_h) {
-    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_s) {
-    $vserver_name = $opt_s;
-}
-else {
-    print "Must specify vserver name with -s\n";
-    exit 3;
-}
-if ( !$opt_w or !$opt_c ) {
-    print "Must specify both warning and critical values!\n\n";
-    usage();
-    exit 3;
-}
-if ($opt_p) {
-    $port = $opt_p;
-}
+my $warning      = $np->opts->warning;
+my $critical     = $np->opts->critical;
+my $vserver_name = $np->opts->server;
 
-# Validate warning and critical
+my $s = $np->snmp;
 
-# Must have opt_w and opt_c
-$warning  = $opt_w;
-$critical = $opt_c;
+my $oid_testoid         = ".1.3.6.1.2.1.1.1.0";
+my $oid_servername_base = "1.3.6.1.4.1.5951.1.3.2.1.1.5";
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
+my $up_servers    = 0;
+my $total_servers = 0;
 
-$script $script_version
-
-$script_description
-
-Usage: $script -H <hostname> -C <community> -s <vserver_name>
-               -w <warning> -c <critical>
-
-Options: -H     Hostname or IP address
-         -p     Port (default: 161)
-         -C     Community (default is public)
-         -s     vserver name
-         -w     Warning threshold
-         -c     Critical threshold
-
---------------------------------------------------------------------
-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 );
@@ -182,7 +132,6 @@
 
     $s->get_table($base_oid);
     $results = $s->var_bind_list();
-
     foreach $key ( keys %$results ) {
         $key =~ s/^$base_oid//;
         push( @oids, $key );
@@ -191,36 +140,8 @@
     return @oids;
 }
 
-# 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 suboid of server so we can find out about physical services
-$suboid = get_suboid_by_name($vserver_name);
+my $suboid = get_suboid_by_name($vserver_name);
 if ( $suboid eq "" ) {
     print "UNKNOWN - Could not find specified server name $vserver_name\n";
     exit 3;
@@ -250,10 +171,10 @@
 }
 
 # Output text
-$retmsg = "$up_servers of $total_servers servers are up";
+my $retmsg = "$up_servers of $total_servers servers are up";
 
 # Performance data
-$perfdata = "total=$total_servers up=$up_servers;$warning;$critical";
+my $perfdata = "total=$total_servers up=$up_servers;$warning;$critical";
 
 # Show appropriate message
 if ( $up_servers < $critical ) {

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

Reply via email to