From 22509ac75b3ae04f35b22c87bbd85c643bb1db2b Mon Sep 17 00:00:00 2001 From: Nathan Vonnahme Date: Wed, 15 Nov 2006 02:11:10 +0000 Subject: made 'usage' unmandatory in N::P::new(). git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1538 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 1fcd28a..f1b1807 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -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 warnings; 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 @@ sub new { 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,28 +123,54 @@ sub check_threshold { } ); } - 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 diff --git a/t/Nagios-Plugin-01.t b/t/Nagios-Plugin-01.t index a73fce4..db12c35 100644 --- a/t/Nagios-Plugin-01.t +++ b/t/Nagios-Plugin-01.t @@ -11,16 +11,16 @@ Nagios::Plugin::Functions::_fake_exit(1); diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" if $ENV{TEST_VERBOSE}; -my $p = Nagios::Plugin->new (usage => "dummy usage"); +my $p = Nagios::Plugin->new(); isa_ok( $p, "Nagios::Plugin"); $p->shortname("PAGESIZE"); is($p->shortname, "PAGESIZE", "shortname explicitly set correctly"); -$p = Nagios::Plugin->new (usage => "dummy usage"); +$p = Nagios::Plugin->new(); is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); -$p = Nagios::Plugin->new( shortname => "SIZE", usage => "dummy usage" ); +$p = Nagios::Plugin->new( shortname => "SIZE", () ); is($p->shortname, "SIZE", "shortname set correctly on new"); diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t index 15ae3d6..f64fb6f 100644 --- a/t/Nagios-Plugin-02.t +++ b/t/Nagios-Plugin-02.t @@ -16,7 +16,7 @@ is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); my $plugin = 'TEST_PLUGIN'; -my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); +my $np = Nagios::Plugin->new( shortname => $plugin ); is($np->shortname, $plugin, "shortname() is $plugin"); # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) @@ -151,7 +151,7 @@ for (@ok) { # shortname testing SKIP: { skip "requires File::Basename", 2 unless eval { require File::Basename }; - $np = Nagios::Plugin->new (usage => "dummy usage", version => "1"); + $np = Nagios::Plugin->new( version => "1"); $plugin = uc File::Basename::basename($0); $plugin =~ s/\..*$//; is($np->shortname, $plugin, "shortname() is '$plugin'"); diff --git a/t/Nagios-Plugin-03.t b/t/Nagios-Plugin-03.t index 0b7b8af..bc4f5e3 100644 --- a/t/Nagios-Plugin-03.t +++ b/t/Nagios-Plugin-03.t @@ -10,7 +10,7 @@ BEGIN { Nagios::Plugin::Functions::_fake_exit(1); my $plugin = 'NP_CHECK_MESSAGES_03'; -my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); +my $np = Nagios::Plugin->new( shortname => $plugin, () ); is($np->shortname, $plugin, "shortname() is $plugin"); my ($code, $message); @@ -172,33 +172,33 @@ is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $mess # add_messages # Constant codes -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( CRITICAL, "A B C" ); $np->add_message( WARNING, "D E F" ); ($code, $message) = $np->check_messages(); is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( CRITICAL, "A B C" ); ($code, $message) = $np->check_messages(); is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); is($message, $messages{critical}, "(CRITICAL) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( WARNING, "D E F" ); ($code, $message) = $np->check_messages(); is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); is($message, $messages{warning}, "(WARNING) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( WARNING, "D E F" ); $np->add_message( OK, "G H I" ); ($code, $message) = $np->check_messages(); is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); is($message, $messages{warning}, "(WARNING, OK) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( OK, "G H I" ); ($code, $message) = $np->check_messages(); is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); @@ -206,33 +206,33 @@ is($message, $messages{ok}, "(OK) message is $message"); # String codes -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( critical => "A B C" ); $np->add_message( warning => "D E F" ); ($code, $message) = $np->check_messages(); is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); is($message, $messages{critical}, "(critical, warning) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( critical => "A B C" ); ($code, $message) = $np->check_messages(); is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); is($message, $messages{critical}, "(critical) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( warning => "D E F" ); ($code, $message) = $np->check_messages(); is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); is($message, $messages{warning}, "(warning) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( warning => "D E F" ); $np->add_message( ok => "G H I" ); ($code, $message) = $np->check_messages(); is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); is($message, $messages{warning}, "(warning, ok) message is $message"); -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); $np->add_message( ok => "G H I" ); ($code, $message) = $np->check_messages(); is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); @@ -240,7 +240,7 @@ is($message, $messages{ok}, "(ok) message is $message"); # No add_message -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); ($code, $message) = $np->check_messages(); is($code, OK, "() code is $STATUS_TEXT{$code}"); is($message, '', "() message is ''"); @@ -250,7 +250,7 @@ is($message, '', "() message is ''"); # Error conditions # add_message errors -$np = Nagios::Plugin->new (usage => "dummy usage"); +$np = Nagios::Plugin->new(); ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, 'add_message dies on invalid code'); ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, -- cgit v0.10-9-g596f