[Nagiosplug-checkins] Nagios-Plugin/t Nagios-Plugin-01.t, 1.2, 1.3 Nagios-Plugin-02.t, 1.2, 1.3 Nagios-Plugin-03.t, 1.1, 1.2 check_stuff.pl, 1.1, 1.2 check_stuff.t, 1.1, 1.2

Nathan Vonnahme n8v at users.sourceforge.net
Fri Nov 10 02:26:18 CET 2006


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

Modified Files:
	Nagios-Plugin-01.t Nagios-Plugin-02.t Nagios-Plugin-03.t 
	check_stuff.pl check_stuff.t 
Log Message:
* exposed Getopt and Threshold functionality from top level Nagios::Plugin
* exchanged Class::Struct for Class::Accessor
* POD is not updated yet.



Index: check_stuff.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/check_stuff.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- check_stuff.t	7 Sep 2006 00:53:51 -0000	1.1
+++ check_stuff.t	10 Nov 2006 01:26:16 -0000	1.2
@@ -8,7 +8,7 @@
 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(

Index: check_stuff.pl
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/check_stuff.pl,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- check_stuff.pl	7 Sep 2006 00:53:51 -0000	1.1
+++ check_stuff.pl	10 Nov 2006 01:26:16 -0000	1.2
@@ -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 @@
     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 @@
 	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 @@
 #	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 @@
 );
 
 # Parse arguments and process standard ones (e.g. usage, help, version)
-$nagopts->getopts;
-
-
-my $p = Nagios::Plugin->new;
-
-$p->shortname($short_name);
+$p->getopts;
 
 
-# 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" 
 );
 

Index: Nagios-Plugin-03.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-03.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Nagios-Plugin-03.t	26 Sep 2006 04:11:39 -0000	1.1
+++ Nagios-Plugin-03.t	10 Nov 2006 01:26:16 -0000	1.2
@@ -10,7 +10,7 @@
 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 @@
 # 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 @@
 
 
 # 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 @@
 
 
 # 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 @@
 # 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' ) }, 

Index: Nagios-Plugin-02.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-02.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Nagios-Plugin-02.t	4 Oct 2006 22:02:39 -0000	1.2
+++ Nagios-Plugin-02.t	10 Nov 2006 01:26:16 -0000	1.3
@@ -16,7 +16,7 @@
 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 @@
 # 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'");

Index: Nagios-Plugin-01.t
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/t/Nagios-Plugin-01.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Nagios-Plugin-01.t	4 Oct 2006 22:02:39 -0000	1.2
+++ Nagios-Plugin-01.t	10 Nov 2006 01:26:16 -0000	1.3
@@ -11,21 +11,26 @@
 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 @@
 	);
 
 cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct");
+#diag "dumping perfdata:  ". Dumper ($p->perfdata);
 
 my $expected = {qw(
 		   -1    WARNING





More information about the Commits mailing list