Title: [opsview] [11917] Updated check_snmp_vmware_netio to use SNMPv3 library
Revision
11917
Author
hstevenson
Date
2013-03-22 12:37:53 +0000 (Fri, 22 Mar 2013)

Log Message

Updated check_snmp_vmware_netio to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_vmware_netio
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_vmware_netio	2013-03-22 12:37:20 UTC (rev 11916)
+++ trunk/opsview-core/nagios-plugins/check_snmp_vmware_netio	2013-03-22 12:37:53 UTC (rev 11917)
@@ -20,141 +20,104 @@
 #    along with Opsview; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #
-
+use warnings;
 use strict;
 
-use lib qw ( /usr/local/nagios/perl/lib );
-use Storable qw(lock_store lock_retrieve);
+use FindBin qw($Bin);
+use lib "/opt/opsview/perl/lib/perl5", "$Bin/../lib";
 use Net::SNMP;
-use Getopt::Std;
+use Opsview::NagiosPlugin::SNMP;
+use Storable qw(lock_store lock_retrieve);
 
-# About us
-my $script         = "check_snmp_vmware_netio";
-my $script_version = "0.1";
-my $script_description =
-  "Reports the network throughput on a specified VMware interface";
+my $script = "check_snmp_vmware_netio";
 
-my $store_dir = "/usr/local/nagios/var/plugins/check_snmp_vmware_netio/";
-my $store_fn  = "${store_dir}/$script.dat";
-my $store;
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => $script,
+    version   => "0.2",
+    blurb => "Reports the network throughput on a specified VMware interface",
+    snmp  => {
+        "snmp-version" => "2c",
+        "snmp-timeout" => 5,
+        "v1-fallback"  => 1,
+    },
+);
 
-my $hint_dir  = "/usr/local/nagios/var/plugins/vmware/";
-my $hint_file = $hint_dir;
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning threshold (KB/sec)},
+    default => 0,
+);
 
-my @hostnames;
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical threshold (KB/sec)},
+    default => 0,
+);
 
-# SNMP variables
-my $oid_sysDescr =
-  ".1.3.6.1.2.1.1.1.0"; # Used to check whether SNMP is actually responding
-my $oid_namebase      = ".1.3.6.1.4.1.6876.2.1.1.2.";
-my $oid_vmidbase      = ".1.3.6.1.4.1.6876.2.1.1.7.";
-my $oid_nicbase       = ".1.3.6.1.4.1.6876.3.4.1.3.";
-my $oid_macbase       = ".1.3.6.1.4.1.6876.3.4.1.4.";
-my $oid_rxbase        = ".1.3.6.1.4.1.6876.3.4.1.9.";
-my $oid_txbase        = ".1.3.6.1.4.1.6876.3.4.1.7.";
-my $vm_name           = "";
-my $vm_mac            = "";
-my $mode              = "name";
-my $community         = "public";                    # Default community
-my $timeout           = 5;                           # SNMP timeout
-my $retval            = 3;
-my $retmsg            = 3;
-my $perfdata          = "";
-my $warning           = 0;
-my $critical          = 0;
-my $warn_in           = 0;
-my $crit_in           = 0;
-my $warn_out          = 0;
-my $crit_out          = 0;
-my $throughput_in     = 0;
-my $throughput_in_kb  = 0;
-my $throughput_out    = 0;
-my $throughput_out_kb = 0;
-my $version           = "2c";
-my $port              = 161;
+$np->add_arg(
+    spec => "vm|v=s",
+    help => qq{-v, --vm=STRING
+    VM name (alternatively, specify MAC)},
+);
 
-our ( $s, $e );
+$np->add_arg(
+    spec => "mac|m=s",
+    help => qq{-m, --mac=STRING
+    MAC address},
+);
 
-# Command line arguments
-our (
-    $opt_h, $opt_H, $opt_C, $opt_t, $opt_v, $opt_m,
-    $opt_w, $opt_c, $opt_i, $opt_o, $opt_p
+$np->add_arg(
+    spec => "input|i",
+    help => qq{-i, --input
+    Warning and Critical on in throughput},
 );
-getopts( "hH:C:t:v:m:w:c:iop:" );
-if ($opt_h) {
-    usage();
-    exit 0;
-}
-if ($opt_H) {
-    @hostnames = split( ',', $opt_H );
-}
-else {
-    print "No hostnames specified\n";
-    usage();
-    exit 3;
-}
-if ($opt_C) {
-    $community = $opt_C;
-}
-if ($opt_t) {
 
-    # Validity test - must be numeric
-    unless ( $opt_t =~ /^\d+$/ ) {
-        print "Specify time in seconds - $opt_t is not a valid integer\n";
-        exit 3;
-    }
-    $timeout = $opt_t;
-}
-if ($opt_v) {
-    $vm_name   = $opt_v;
+$np->add_arg(
+    spec => "output|o",
+    help => qq{-o, --output
+    Warning and Critical on out throughput},
+);
+
+$np->getopts;
+
+my $warning   = $np->opts->warning;
+my $critical  = $np->opts->critical;
+my $vm_name   = "";
+my $vm_mac    = "";
+my $mode      = "name";
+my @hostnames = split( ',', $np->opts->hostname );
+
+my $hintdir   = "/usr/local/nagios/var/plugins/vmware/";
+my $hint_file = $hintdir;
+
+my $warn_in  = 0;
+my $crit_in  = 0;
+my $warn_out = 0;
+my $crit_out = 0;
+
+if ( $np->opts->vm ) {
+    $vm_name   = $np->opts->vm;
     $hint_file = "${hint_file}esx_hint-${vm_name}.dat";
 }
-elsif ($opt_m) {
-    if ($opt_v) {
-        print "Must specify either VM name or MAC address\n";
-        usage();
-        exit 3;
-    }
-    $vm_mac    = $opt_m;
+elsif ( $np->opts->mac ) {
     $mode      = "mac";
+    $vm_mac    = $np->opts->mac;
     $hint_file = "${hint_file}esx_hint-${vm_mac}.dat";
 }
-else {
-    print "Must specify either MAC address or VM name\n";
-    usage();
-    exit 3;
-}
-if ($opt_w) {
-    if ( $opt_w =~ /^\d+$/ ) {
-        $warning = $opt_w;
-    }
-    else {
-        print "Must specify warning threshold as an integer in KB/sec\n";
-        usage();
-        exit 3;
-    }
-}
-if ($opt_c) {
-    if ( $opt_c =~ /^\d+$/ ) {
-        $critical = $opt_c;
-    }
-    else {
-        print "Must specify critical threshold as an integer in KB/sec\n";
-        usage();
-        exit 3;
-    }
-}
-if ( $opt_i && $opt_o ) {
+if ( $np->opts->input && $np->opts->output ) {
     print
       "Specify either of -i or -o, or specify neither option to mean 'both'\n";
     usage();
     exit 3;
 }
-if ($opt_i) {
+if ( $np->opts->input ) {
     $warn_in = $warning;
     $crit_in = $critical;
 }
-elsif ($opt_o) {
+elsif ( $np->opts->output ) {
     $warn_out = $warning;
     $crit_out = $critical;
 }
@@ -164,61 +127,29 @@
     $warn_out = $warning;
     $crit_out = $critical;
 }
-if ($opt_p) {
-    $port = $opt_p;
-}
 
-# Our temporary store directory
-if ( !-d $store_dir ) {
-    mkdir $store_dir
-      or die( "Cannot create temporary data store directory $store_dir" );
-}
+my $retval            = 3;
+my $retmsg            = 3;
+my $perfdata          = "";
+my $throughput_in     = 0;
+my $throughput_in_kb  = 0;
+my $throughput_out    = 0;
+my $throughput_out_kb = 0;
 
-# Our hint file is found in here
-if ( !-d $hint_dir ) {
-    mkdir $hint_dir
-      or die( "Cannot create temporary data store directory $hint_dir" );
-}
+my $oid_sysDescr = ".1.3.6.1.2.1.1.1.0";
+my $oid_namebase = ".1.3.6.1.4.1.6876.2.1.1.2";
+my $oid_vmidbase = ".1.3.6.1.4.1.6876.2.1.1.7.";
+my $oid_nicbase  = ".1.3.6.1.4.1.6876.3.4.1.3";
+my $oid_macbase  = ".1.3.6.1.4.1.6876.3.4.1.4.";
+my $oid_rxbase   = ".1.3.6.1.4.1.6876.3.4.1.9.";
+my $oid_txbase   = ".1.3.6.1.4.1.6876.3.4.1.7.";
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
-$script $script_version
+my $store_dir = "/usr/local/nagios/var/plugins/check_snmp_vmware_netio/";
+my $store_fn  = "${store_dir}/$script.dat";
+my $store;
 
-$script_description
+my $s = $np->snmp;
 
-Usage: $script -H <hostnames> -C <community> [-t <timeout>] ...
-
-Options: -H     Comma separated list of hostnames or IP addresses
-                of the VMware ESX hosts on which the VM could be
-                found.
-         -p     Port (default: 161)
-         -C     SNMP community string
-         -t     SNMP timeout (in seconds)
-         -v     VM name (alternatively, specify MAC)
-         -m     MAC address
-         -w     Warning threshold (KB/sec)
-         -c     Critical threshold (KB/sec)
-
-Using the VM name is fine when you only have one interface on that
-VM, but you should use the MAC address otherwise.
-
-Note:  This plugin uses counters in VMware's SNMP MIB, which are
-       updated by VMware every 20 seconds.  If you run the plugin
-       more frequently than this then it will often return '0'.
-       For best (most accurate) results, the plugin should probably
-       be run at 300 second intervals (or more).
-
---------------------------------------------------------------------     
-Copyright (C) 2003-2013 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 );
@@ -278,7 +209,7 @@
     # an option
     if ( grep( /^${hint_host}$/, @hostnames ) ) {
         my $item;
-        while ( @hostnames[0] ne $hint_host ) {
+        while ( $hostnames[0] ne $hint_host ) {
             $item = shift(@hostnames);
             push( @hostnames, $item );
         }
@@ -289,8 +220,8 @@
 
         $response = $s->get_table($oid_namebase);
         next if !defined($response);
-
         %$response = reverse %$response;
+
         if ( exists $response->{$name} ) {
 
             # Found the VM, write a hint for next time
@@ -324,7 +255,7 @@
     # an option
     if ( grep( /^${hint_host}$/, @hostnames ) ) {
         my $item;
-        while ( @hostnames[0] ne $hint_host ) {
+        while ( $hostnames[0] ne $hint_host ) {
             $item = shift(@hostnames);
             push( @hostnames, $item );
         }
@@ -371,7 +302,6 @@
         $nic = get_nic_id( $oid_nicbase, $vmid );
     }
     else {
-
         # Store by MAC not by name
         $storename = $mac;
 
@@ -410,48 +340,18 @@
 
 sub create_snmp_session {
     my $snmp_host = shift(@_);
-
-    # Create the SNMP session
-    $version = "2c";
-    ( $s, $e ) = Net::SNMP->session(
-        -community => $community,
-        -hostname  => $snmp_host,
-        -version   => $version,
-        -timeout   => $timeout,
-        -port      => $port,
+    $s = $np->snmp(
+        {
+            hostname         => $snmp_host,
+            undef_on_failure => 1
+        }
     );
 
     if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-
-        # If we can't connect using SNMPv1 lets try as SNMPv2
-        $s->close();
-        sleep 0.5;
-        $version = "1";
-        ( $s, $e ) = Net::SNMP->session(
-            -community => $community,
-            -hostname  => $snmp_host,
-            -version   => $version,
-            -timeout   => $timeout,
-            -port      => $port,
-        );
-        if ( !defined( $s->get_request($oid_sysDescr) ) ) {
-            print "Agent not responding, tried SNMP v1 and v2\n";
-            return 1;
-        }
-    }
-
-    if ( $s->error ) {
-        print "SNMP Error: ", $s->error, $/;
+        print "SNMP agent not responding\n";
         return 1;
     }
 
-    #set the session size to be bigger
-    $s->max_msg_size(65535);
-    if ( $s->error ) {
-        print "SNMP Error: ", $s->error, $/;
-        return 1;
-    }
-
     return 0;
 }
 

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

Reply via email to