From d84da4e2ec8569b8ff15356b6881df307c7333ca Mon Sep 17 00:00:00 2001 From: Nathan Vonnahme <n8v@users.sourceforge.net> Date: Fri, 10 Nov 2006 01:26:16 +0000 Subject: * exposed Getopt and Threshold functionality from top level Nagios::Plugin * exchanged Class::Struct for Class::Accessor * POD is not updated yet. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1536 f882894a-f735-0410-b71e-b25c423dba1c --- t/Nagios-Plugin-01.t | 14 ++++++--- t/Nagios-Plugin-02.t | 4 +-- t/Nagios-Plugin-03.t | 26 ++++++++-------- t/check_stuff.pl | 83 +++++++++++++++++----------------------------------- t/check_stuff.t | 2 +- 5 files changed, 53 insertions(+), 76 deletions(-) (limited to 't') diff --git a/t/Nagios-Plugin-01.t b/t/Nagios-Plugin-01.t index 9de5009..a73fce4 100644 --- a/t/Nagios-Plugin-01.t +++ b/t/Nagios-Plugin-01.t @@ -11,21 +11,26 @@ Nagios::Plugin::Functions::_fake_exit(1); diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" if $ENV{TEST_VERBOSE}; -my $p = Nagios::Plugin->new; +my $p = Nagios::Plugin->new (usage => "dummy usage"); isa_ok( $p, "Nagios::Plugin"); $p->shortname("PAGESIZE"); -is($p->shortname, "PAGESIZE", "shortname set correctly"); +is($p->shortname, "PAGESIZE", "shortname explicitly set correctly"); -$p = Nagios::Plugin->new; +$p = Nagios::Plugin->new (usage => "dummy usage"); is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); -$p = Nagios::Plugin->new( shortname => "SIZE" ); +$p = Nagios::Plugin->new( shortname => "SIZE", usage => "dummy usage" ); is($p->shortname, "SIZE", "shortname set correctly on new"); diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); +use Data::Dumper; +#diag "dumping p: ". Dumper $p; +#diag "dumping perfdata: ". Dumper $p->perfdata; + + $p->add_perfdata( label => "size", value => 1, @@ -34,6 +39,7 @@ $p->add_perfdata( ); cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); +#diag "dumping perfdata: ". Dumper ($p->perfdata); my $expected = {qw( -1 WARNING diff --git a/t/Nagios-Plugin-02.t b/t/Nagios-Plugin-02.t index 360e180..15ae3d6 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 ); +my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); 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; + $np = Nagios::Plugin->new (usage => "dummy usage", 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 0366156..0b7b8af 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 ); +my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); 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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); $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; +$np = Nagios::Plugin->new (usage => "dummy usage"); ($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; +$np = Nagios::Plugin->new (usage => "dummy usage"); ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, 'add_message dies on invalid code'); ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, diff --git a/t/check_stuff.pl b/t/check_stuff.pl index 51f551f..889e484 100755 --- a/t/check_stuff.pl +++ b/t/check_stuff.pl @@ -18,32 +18,25 @@ use strict; use warnings; -use Nagios::Plugin qw(%ERRORS); - -use Nagios::Plugin::Getopt; - +use Nagios::Plugin ; use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); '$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion $VERSION = $1; -$0 =~ m!^.*/([^/]+)$!; -$PROGNAME = $1; -# shortname is the identifier this script will give to Nagios. -# it's set here to the uppercase program name with file extension removed, -# e.g. check_stuff.pl -> CHECK_STUFF -my $short_name = uc $PROGNAME; -$short_name =~ s/\.\w+$//; +# get the base name of this script for use in the examples +use File::Basename; +$PROGNAME = basename($0); ############################################################################## # define and get the command line options. # see the command line option guidelines at -# +# http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS -# Instantiate Nagios::Plugin::Getopt object (usage and version are mandatory) -my $nagopts = Nagios::Plugin::Getopt->new( +# Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory) +my $p = Nagios::Plugin->new( usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] [ -c|--critical=<critical threshold> ] [ -w|--warning=<warning threshold> ] @@ -51,7 +44,7 @@ my $nagopts = Nagios::Plugin::Getopt->new( version => $VERSION, blurb => 'This plugin is an example of a Nagios plugin written in Perl using the Nagios::Plugin modules. It will generate a random integer between 1 and 20 (though you can specify the number with the -n option for testing), and will output OK, WARNING or CRITICAL if the resulting number is outside the specified thresholds.', - extra => qq{ + extra => " THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max' (or 'max'). If specified '\@min:max', a warning status will be generated @@ -70,16 +63,14 @@ Examples: Returns a warning if the resulting number is less than 10, or a critical error if it is less than 4. - -} - +" ); # Define and document the valid command line options # usage, help, version, timeout and verbose are defined by default. -$nagopts->arg( +$p->arg( spec => 'warning|w=s', help => @@ -91,17 +82,15 @@ qq{-w, --warning=INTEGER:INTEGER # default => 10, ); -$nagopts->arg( +$p->arg( spec => 'critical|c=s', help => qq{-c, --critical=INTEGER:INTEGER Minimum and maximum number of the generated result, outside of - which a critical will be generated. If omitted, a critical is - generated if no processes are running.}, - + which a critical will be generated. }, ); -$nagopts->arg( +$p->arg( spec => 'result|r=f', help => qq{-r, --result=INTEGER @@ -110,59 +99,41 @@ qq{-r, --result=INTEGER ); # Parse arguments and process standard ones (e.g. usage, help, version) -$nagopts->getopts; - +$p->getopts; -my $p = Nagios::Plugin->new; -$p->shortname($short_name); - - -# sanity checking on command line options -if ( (defined $nagopts->result) && ($nagopts->result < 0 || $nagopts->result > 20) ) { - $p->die( - return_code => $ERRORS{UNKNOWN}, - message => 'invalid number supplied for the -r option' - ); +# perform sanity checking on command line options +if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) { + $p->nagios_die( "invalid number supplied for the -r option" ); } -unless ( defined $nagopts->warning || defined $nagopts->critical ) { - $p->die( - return_code => $ERRORS{UNKNOWN}, - message => "you didn't supply a threshold argument" - ); +unless ( defined $p->opts->warning || defined $p->opts->critical ) { + $p->nagios_die( "you didn't supply a threshold argument" ); } -############################################################################## -# define a Nagios::Threshold object based on the command line options -my $t = $p->set_thresholds( warning => $nagopts->warning, critical => $nagopts->critical ); ############################################################################## # check stuff. # THIS is where you'd do your actual checking to get a real value for $result -# don't forget to timeout after $nagopts->timeout seconds, if applicable. +# don't forget to timeout after $p->opts->timeout seconds, if applicable. my $result; -if (defined $nagopts->result) { - $result = $nagopts->result; - print "using supplied result $result from command line\n" if $nagopts->verbose; +if (defined $p->opts->result) { # you got a 'result' option from the command line options + $result = $p->opts->result; + print "using supplied result $result from command line\n" if $p->opts->verbose; } else { $result = int rand(20)+1; - print "generated random result $result\n" if $nagopts->verbose; + print "generated random result $result\n" if $p->opts->verbose; } -print "status of result ($result) is ", $t->get_status($result), "\n" - if $nagopts->verbose; - - - ############################################################################## +# check the result against the defined warning and critical thresholds, # output the result and exit -$p->die( - return_code => $t->get_status($result), +$p->nagios_exit( + return_code => $p->check_threshold($result), message => "sample result was $result" ); diff --git a/t/check_stuff.t b/t/check_stuff.t index a748605..2d2ce38 100755 --- a/t/check_stuff.t +++ b/t/check_stuff.t @@ -8,7 +8,7 @@ my ($r,$args); my $s = 't/check_stuff.pl'; $s = 'perl -Ilib '.$s; -my $n = 'CHECK_STUFF'; +my $n = 'STUFF'; # Nagios status strings and exit codes my %e = qw( -- cgit v1.2.3-74-g34f1