Title: [opsview] [11914] Updated check_snmp_vmware_cpu to use SNMPv3 library
Revision
11914
Author
hstevenson
Date
2013-03-22 12:34:06 +0000 (Fri, 22 Mar 2013)

Log Message

Updated check_snmp_vmware_cpu to use SNMPv3 library

Modified Paths


Modified: trunk/opsview-core/nagios-plugins/check_snmp_vmware_cpu
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_vmware_cpu	2013-03-22 12:17:00 UTC (rev 11913)
+++ trunk/opsview-core/nagios-plugins/check_snmp_vmware_cpu	2013-03-22 12:34:06 UTC (rev 11914)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 #
 # AUTHORS:
-#	Copyright (C) 2003-2013 Opsview Limited. All rights reserved
+#	Copyright (C) 2003-2012 Opsview Limited. All rights reserved
 #	Written by Neil Ferguson
 #
 #    This file is part of Opsview
@@ -20,110 +20,83 @@
 #    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 Storable qw(lock_store lock_retrieve);
+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;
+use Storable qw(lock_store lock_retrieve);
 
-# About us
-my $script         = "check_snmp_vmware_cpu";
-my $script_version = "0.1";
+my $script = "check_snmp_vmware_cpu";
 
-my @hostnames;
+my $np = Opsview::NagiosPlugin::SNMP->new(
+    usage     => "Usage: %s",
+    shortname => $script,
+    version   => "0.2",
+    blurb     => "Checks the CPU usage on a VMware ESX host",
+    snmp      => {
+        "snmp-version" => 2,
+        "snmp-timeout" => 10,
+        "v1-fallback"  => 1,
+    },
+);
 
-# Our store
-my $store;
-my $store_dir    = "/usr/local/nagios/var/plugins/check_snmp_vmware_cpu/";
-my $store_basefn = "";
-my $storename;
+$np->add_arg(
+    spec => "warning|w=s",
+    help => qq{-w, --warning=INTEGER
+    Warning threshold for CPU usage (%)},
+    default => 0,
+);
+
+$np->add_arg(
+    spec => "critical|c=s",
+    help => qq{-c, --critical=INTEGER
+    Critical threshold for CPU usage (%)},
+    default => 0,
+);
+
+$np->add_arg(
+    spec => "vm|V=s",
+    help => qq{-V, --vm=STRING
+    Report memory usage of this VM only},
+);
+
+$np->getopts;
+
+my $warning   = $np->opts->warning;
+my $critical  = $np->opts->critical;
+my $vm_name   = "";
 my $hint_dir  = "/usr/local/nagios/var/plugins/vmware/";
 my $hint_file = $hint_dir;
+my @hostnames = split( ',', $np->opts->hostname );
 
+if ( $np->opts->vm ) {
+    $vm_name   = $np->opts->vm;
+    $hint_file = "${hint_file}esx_hint-${vm_name}.dat";
+}
+
+my $s = $np->snmp;
+
 # 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_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_vmbase   = ".1.3.6.1.4.1.6876.";
 my $oid_vm_cpu   = "3.1.2.1.3.";
-my $community    = "public";                    # Default community
-my $timeout      = 10;                          # SNMP timeout
-my $retval       = 3;                           # Innocent until proven guilty
-my $retmsg       = "";                          # Text to return from plugin
-my $version      = "2c";
-my $warning      = 0;
-my $critical     = 0;
+
+my $retval       = 3;
+my $retmsg       = "";
 my $result_text  = 0;
 my $cpu_util     = 0;
-my $vm_name      = "";
 my $percent_used = 0;
-my $port         = 161;
 
-our ( $s, $e );
+my $store;
+my $store_dir    = "/usr/local/nagios/var/plugins/check_snmp_vmware_cpu/";
+my $store_basefn = "";
+my $storename;
 
-# Command line arguments
-our ( $opt_h, $opt_H, $opt_C, $opt_t, $opt_w, $opt_c, $opt_v, $opt_p );
-getopts( "hH:C:t:w:c:av:p:" );
-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_p) {
-    $port = $opt_p;
-}
-
-# Thresholds
-if ($opt_w) {
-    if ( $opt_w =~ /^\d+$/ ) {
-        $warning = $opt_w;
-    }
-    else {
-        print "Warning value must be an integer\n";
-        exit 3;
-    }
-}
-if ($opt_c) {
-    if ( $opt_c =~ /^\d+$/ ) {
-        $critical = $opt_c;
-    }
-    else {
-        print "Critical value must an integer\n";
-        exit 3;
-    }
-}
-if ($opt_v) {
-    $vm_name   = $opt_v;
-    $hint_file = "${hint_file}esx_hint-${vm_name}.dat";
-}
-else {
-    if ( @hostnames > 1 ) {
-        print "Cannot specify more than one hostname unless you use -v\n";
-        exit 3;
-    }
-}
-
 # Temporary store directory must exist
 if ( !-d $store_dir ) {
     mkdir $store_dir
@@ -142,44 +115,6 @@
 # Set our base store name as we now have our hostname
 $store_basefn = "$store_dir/$script";
 
-sub usage {
-    print <<EOF
---------------------------------------------------------------------
-$script $script_version
-
-Checks the CPU usage on a VMware ESX host
-
-Usage: $script -H <hostnames> -C <community> [...]
-
-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)
-         -w     Warning threshold for CPU usage (%)
-         -c     Critical threshold for CPU usage (%)
-         -v     Report memory usage of this VM only
-
-This plugin returns the CPU usage of one VM, or all VMs on the
-specified ESX host.
-
-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
-
-}
-
 sub two_dp {
     my $val = shift(@_);
     return int( $val * 100 ) / 100;
@@ -229,7 +164,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 );
         }
@@ -257,7 +192,7 @@
         }
     }
 
-    print 'VM not found; availables VMs are: ',
+    print 'VM not found; available VMs are: ',
       join( ", ", sort( keys(%$response) ) ), $/;
     exit 3;
 }
@@ -268,7 +203,7 @@
     our ( $prev_cpu, $prev_time );
     my $store_fn;
 
-    if ($opt_v) {
+    if ( $np->opts->vm ) {
         $store_fn = $store_basefn . "-$vm.dat";
     }
     else {
@@ -282,6 +217,7 @@
         $prev_cpu  = $store->{$vm}[1];
     }
     else {
+        print "debug";
 
         # Force usage to 0% if we have no previous data
         $prev_time = 0;
@@ -297,7 +233,7 @@
     my $cpu = shift(@_);
     my $store_fn;
 
-    if ($opt_v) {
+    if ( $np->opts->vm ) {
         $store_fn = $store_basefn . "-$vm.dat";
     }
     else {
@@ -322,12 +258,18 @@
 
     # Reporting on a single VM
     if ($vm_name) {
+
         $vmid = get_vm_id($vm_name);
         return undef if !defined($vmid);
-
+        print "\n\n" . $oid_vmbase . $oid_vm_cpu . $vmid . "\n\n";
         $cur_cpu = get_oid_value( $oid_vmbase . $oid_vm_cpu . $vmid );
 
         ( $prev_time, $prev_cpu ) = get_vm_stats( $vm_name, $cur_cpu );
+        print "\n\n"
+          . $cur_cpu . "  "
+          . $prev_cpu . "  "
+          . time() . "  "
+          . $prev_time . "  ";
 
         # Work out the usage in that time (seconds/sec)
         $cpu_usage = ( $cur_cpu - $prev_cpu ) / ( time() - $prev_time ) * 100;
@@ -381,56 +323,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,
-    );
-
     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 "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;
 }
 
 # If we're checking all VMs, we need to start an SNMP session, otherwise
 # get_vm_id() will take care of this
-if ( !$opt_v ) {
-    create_snmp_session( @hostnames[0] );
+if ( !$np->opts->vm ) {
+    create_snmp_session( $hostnames[0] );
 
     # Check for an SNMP error...
     if ( $s->error ) {
@@ -440,7 +344,7 @@
 }
 
 # Get all the CPU info
-my $cpu_util = get_vm_cpu_util();
+$cpu_util = get_vm_cpu_util();
 if ( !defined($cpu_util) ) {
     if ( defined($vm_name) ) {
         print "Could not find VM $vm_name\n";

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

Reply via email to