Modified: trunk/opsview-core/nagios-plugins/check_snmp_vmware_memory
===================================================================
--- trunk/opsview-core/nagios-plugins/check_snmp_vmware_memory 2013-03-22 12:36:32 UTC (rev 11915)
+++ trunk/opsview-core/nagios-plugins/check_snmp_vmware_memory 2013-03-22 12:37:20 UTC (rev 11916)
@@ -20,106 +20,88 @@
# 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 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;
-# About us
-my $script = "check_snmp_vmware_memory";
-my $script_version = "0.4";
+use Storable qw(lock_store lock_retrieve);
-my @hostnames;
+my $np = Opsview::NagiosPlugin::SNMP->new(
+ usage => "Usage: %s",
+ shortname => "check_snmp_vmware_memory",
+ version => "0.5",
+ blurb => "Checks the memory usage of a VMware ESX host",
+ snmp => {
+ "snmp-version" => "2c",
+ "snmp-timeout" => 10,
+ "v1-fallback" => 1,
+ },
+);
-# 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_host_mem = "3.2.1.0";
my $oid_sc_mem = "3.2.2.0";
my $oid_vm_mem = "3.2.4.1.4.";
-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 $perfdata = ""; # Performance data
-my $version = "2c";
-my $warning = 0;
-my $critical = 0;
-my $result_text = 0;
-my $mem_total = 0;
-my $sc_mem = 0;
-my $mem_used = 0;
-my $vm_name = "";
-my $vmid = "";
-my $percent_used = 0;
-my $perf_data_percent = 0;
-my $hint_dir = "/usr/local/nagios/var/plugins/vmware/";
-my $hint_file = $hint_dir;
-my $port = 161;
-our ( $s, $e );
+$np->add_arg(
+ spec => "warning|w=s",
+ help => qq{-w, --warning=INTEGER
+ Warning threshold for free memory (MB)},
+ default => 0,
+);
-# Command line arguments
-our ( $opt_h, $opt_H, $opt_C, $opt_t, $opt_w, $opt_c, $opt_a, $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) {
+$np->add_arg(
+ spec => "critical|c=s",
+ help => qq{-c, --critical=INTEGER
+ Critical threshold for free memory (MB)},
+ default => 0,
+);
- # 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;
-}
+$np->add_arg(
+ spec => "memoryallocation|a",
+ help => qq{-a, --memoryallocation=INTEGER
+ Report the total VM memory allocation, rather
+ than actual memory utilization as reported by},
+);
-# 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_a) {
- $oid_vm_mem = "3.2.4.1.3.";
-}
-if ($opt_v) {
- $vm_name = $opt_v;
+$np->add_arg(
+ spec => "vm|v=s",
+ help => qq{-v, --vm=INTEGER
+ Report memory usage of this VM only (Overrides verbose flag)},
+);
+
+$np->add_arg(
+ spec => "percent|P",
+ help => qq{-P, --percent
+ Return performance data in percent},
+);
+
+$np->getopts;
+
+my $warning = $np->opts->warning;
+my $critical = $np->opts->critical;
+my $vm_name = "";
+my @hostnames = split( ',', $np->opts->hostname );
+my $perf_data_percent = 0;
+
+my $hint_file = "/usr/local/nagios/var/plugins/vmware/";
+my $vmid = "";
+my $mem_total;
+my $sc_mem = 0;
+my $mem_used = 0;
+my $perfdata = "";
+my $percent_used = 0;
+my $result_text = 0;
+
+if ( $np->opts->vm ) {
+ $vm_name = $np->opts->vm;
$hint_file = "${hint_file}esx_hint-${vm_name}.dat";
}
else {
@@ -128,53 +110,17 @@
exit 3;
}
}
-if ($opt_p) {
- $perf_data_percent = 1;
-}
-# Our hint file is found in here
-if ( !-d $hint_dir ) {
- mkdir $hint_dir
- or die( "Cannot create temporary data store directory $hint_dir" );
+if ( $np->opts->memoryallocation ) {
+ $oid_vm_mem = "3.2.4.1.3.";
}
-sub usage {
- print <<EOF
---------------------------------------------------------------------
-$script $script_version
-
-Checks the memory usage of 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 free memory (MB)
- -c Critical threshold for free memory (MB)
- -a Report the total VM memory allocation, rather
- than actual memory utilization as reported by
- VMware (due to transparent page sharing)
- -v Report memory usage of this VM only
- -p Return performance data in percent
-
-Note: As this plugin uses SNMP, it can only report the values which
- VMware makes available. As a result, the data returned is what
- VMware seems to refer to as 'active' memory.
-
---------------------------------------------------------------------
-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
-
+if ( $np->opts->percent ) {
+ $perf_data_percent = 1;
}
+my $s;
+
# Sensible units.
sub two_dp {
my $val = shift(@_);
@@ -226,7 +172,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 );
}
@@ -253,7 +199,7 @@
}
}
- print 'VM not found; availables VMs are: ',
+ print 'VM not found; available VMs are: ',
join( ", ", sort( keys(%$response) ) ), $/;
exit 3;
}
@@ -262,7 +208,6 @@
my $i = 0;
my $result = 0;
my $vm_id = shift(@_);
-
if ($vm_id) {
return get_oid_value( $oid_vmbase . $oid_vm_mem . $vm_id );
}
@@ -285,54 +230,25 @@
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 "Agent not responding";
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 ) {
@@ -362,7 +278,6 @@
if ( $sc_mem > 0 ) {
$sc_mem = int( $sc_mem / 1024 );
}
-
$mem_total = int( $mem_total / 1024 );
my $mem = get_vm_mem_used($vmid);
if ( !defined($mem) ) {