[Nagiosplug-checkins] Nagios-Plugin/lib/Nagios Plugin.pm, 1.15, 1.16

Nathan Vonnahme n8v at users.sourceforge.net
Wed Nov 15 03:11:12 CET 2006


Update of /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv28417/lib/Nagios

Modified Files:
	Plugin.pm 
Log Message:
made 'usage' unmandatory in N::P::new().



Index: Plugin.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin.pm,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- Plugin.pm	10 Nov 2006 01:26:16 -0000	1.15
+++ Plugin.pm	15 Nov 2006 02:11:09 -0000	1.16
@@ -2,8 +2,6 @@
 package Nagios::Plugin;
 
 use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
-use Nagios::Plugin::Getopt;
-use Nagios::Plugin::Threshold; 
 use Params::Validate qw(:all);
 
 use strict;
@@ -12,7 +10,6 @@
 use Carp;
 use base qw(Class::Accessor::Fast);
 
-# do we need all of these to be accessible?
 Nagios::Plugin->mk_accessors(qw(
 								perfdata 
 								messages 
@@ -32,34 +29,41 @@
 	my $class = shift;
 #	my %args = @_;
 
-	my %args = validate( @_, {
-		shortname => 0,
-		usage => 1,
-		version => 0,
-		url => 0,
-		plugin => 0,
-		blurb => 0,
-		extra => 0,
-		license => 0,
-		timeout => 0 },
+	my %args = validate( @_,
+		{
+			shortname => 0,
+			usage     => 0,
+			version   => 0,
+			url       => 0,
+			plugin    => 0,
+			blurb     => 0,
+			extra     => 0,
+			license   => 0,
+			timeout   => 0
+		},
 	);
+
 	my $shortname = undef;
 	if (exists $args{shortname}) {
 		$shortname = $args{shortname};
 		delete $args{shortname};
 	}
 	my $self = {
-	    shortname => $shortname,
-	    perfdata => [],        # to be added later
-	    messages => {
-			warning => [],
+		shortname => $shortname,
+		perfdata  => [],           # to be added later
+		messages  => {
+			warning  => [],
 			critical => [],
-			ok => []
-			},
-		opts => new Nagios::Plugin::Getopt(%args),
-		threshold => undef,  # defined later
-	};		
+			ok       => []
+		},
+		opts      => undef,        # see below
+		threshold => undef,        # defined later
+	};
 	bless $self, $class;
+	if (exists $args{usage}) {
+		require Nagios::Plugin::Getopt;
+		$self->opts( new Nagios::Plugin::Getopt(%args) );
+	}
 	return $self;
 }
 
@@ -119,27 +123,53 @@
 		} );
 	}
 
-	if (! $self->threshold || exists $args{warning} || exists $args{critical}) {
-		$self->set_thresholds( 
-							   warning  => $args{warning} || $self->opts->warning , 
-							   critical => $args{critical} || $self->opts->critical ,
-							   );
-	}
 
+	# in order of preference, get warning and critical from
+	#  1.  explicit arguments to check_threshold
+	#  2.  previously explicitly set threshold object
+	#  3.  implicit options from Getopts object
 
+	if ( exists $args{warning} || exists $args{critical} ) {
+		$self->set_thresholds(
+			warning  => $args{warning},
+			critical => $args{critical},
+		);
+	}
+	elsif ( defined $self->threshold ) {
+		# noop
+	}
+	elsif ( defined $self->opts ) {
+		$self->set_thresholds(
+			warning  => $self->opts->warning,
+			critical => $self->opts->critical,
+		);
+	}
+	else {
+		return UNKNOWN;
+	}
+	
 	return $self->threshold->get_status($args{check});
 }
 
 # top level interface to my Nagios::Plugin::Getopt object
 sub arg {
     my $self = shift;
-	$self->opts->arg(@_);
+	$self->opts->arg(@_) if $self->_check_for_opts;
 }
 sub getopts {
     my $self = shift;
-	$self->opts->getopts(@_);
+	$self->opts->getopts(@_) if $self->_check_for_opts;
 }
 
+sub _check_for_opts {
+	my $self = shift;
+	croak
+		"You have to supply a 'usage' param to Nagios::Plugin::new() if you want to use Getopts from your Nagios::Plugin object."
+			unless ref $self->opts() eq 'Nagios::Plugin::Getopt';
+	return $self;
+}
+
+
 
 # -------------------------------------------------------------------------
 # NP::Functions::check_messages helpers and wrappers





More information about the Commits mailing list