diff options
-rw-r--r-- | MANIFEST | 1 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 128 | ||||
-rw-r--r-- | lib/Nagios/Plugin/ExitResult.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Getopt.pm | 4 | ||||
-rw-r--r-- | t/Nagios-Plugin-01.t | 14 | ||||
-rw-r--r-- | t/Nagios-Plugin-02.t | 4 | ||||
-rw-r--r-- | t/Nagios-Plugin-03.t | 26 | ||||
-rwxr-xr-x | t/check_stuff.pl | 83 | ||||
-rwxr-xr-x | t/check_stuff.t | 2 |
10 files changed, 160 insertions, 106 deletions
@@ -7,6 +7,7 @@ t/check_stuff.t | |||
7 | t/Nagios-Plugin-01.t | 7 | t/Nagios-Plugin-01.t |
8 | t/Nagios-Plugin-02.t | 8 | t/Nagios-Plugin-02.t |
9 | t/Nagios-Plugin-03.t | 9 | t/Nagios-Plugin-03.t |
10 | t/Nagios-Plugin-04.t | ||
10 | t/Nagios-Plugin-Functions-01.t | 11 | t/Nagios-Plugin-Functions-01.t |
11 | t/Nagios-Plugin-Functions-02.t | 12 | t/Nagios-Plugin-Functions-02.t |
12 | t/Nagios-Plugin-Getopt-01.t | 13 | t/Nagios-Plugin-Getopt-01.t |
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 71d12ed..1fcd28a 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm | |||
@@ -1,30 +1,67 @@ | |||
1 | # This is only because Class::Struct doesn't allow subclasses | ||
2 | # Trick stolen from Class::DBI | ||
3 | ###package Nagios::__::Plugin; | ||
4 | |||
5 | use Class::Struct; | ||
6 | struct "Nagios::__::Plugin" => { | ||
7 | perfdata => '@', | ||
8 | shortname => '$', | ||
9 | messages => '%', | ||
10 | }; | ||
11 | 1 | ||
12 | package Nagios::Plugin; | 2 | package Nagios::Plugin; |
13 | 3 | ||
14 | use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); | 4 | use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); |
5 | use Nagios::Plugin::Getopt; | ||
6 | use Nagios::Plugin::Threshold; | ||
7 | use Params::Validate qw(:all); | ||
15 | 8 | ||
16 | use strict; | 9 | use strict; |
17 | use warnings; | 10 | use warnings; |
18 | 11 | ||
19 | use Carp; | 12 | use Carp; |
13 | use base qw(Class::Accessor::Fast); | ||
14 | |||
15 | # do we need all of these to be accessible? | ||
16 | Nagios::Plugin->mk_accessors(qw( | ||
17 | perfdata | ||
18 | messages | ||
19 | opts | ||
20 | threshold | ||
21 | )); | ||
20 | 22 | ||
21 | use Exporter; | 23 | use Exporter; |
22 | our @ISA = qw(Exporter Nagios::__::Plugin); | 24 | our @ISA = qw(Exporter); |
23 | our @EXPORT = (@STATUS_CODES); | 25 | our @EXPORT = (@STATUS_CODES); |
24 | our @EXPORT_OK = qw(%ERRORS); | 26 | our @EXPORT_OK = qw(%ERRORS); |
25 | 27 | ||
26 | # Remember to update Nagios::Plugin::Functions as well! | 28 | # Remember to update Nagios::Plugin::Functions as well! |
27 | our $VERSION = "0.14"; | 29 | our $VERSION = "0.15"; |
30 | |||
31 | sub new { | ||
32 | my $class = shift; | ||
33 | # my %args = @_; | ||
34 | |||
35 | my %args = validate( @_, { | ||
36 | shortname => 0, | ||
37 | usage => 1, | ||
38 | version => 0, | ||
39 | url => 0, | ||
40 | plugin => 0, | ||
41 | blurb => 0, | ||
42 | extra => 0, | ||
43 | license => 0, | ||
44 | timeout => 0 }, | ||
45 | ); | ||
46 | my $shortname = undef; | ||
47 | if (exists $args{shortname}) { | ||
48 | $shortname = $args{shortname}; | ||
49 | delete $args{shortname}; | ||
50 | } | ||
51 | my $self = { | ||
52 | shortname => $shortname, | ||
53 | perfdata => [], # to be added later | ||
54 | messages => { | ||
55 | warning => [], | ||
56 | critical => [], | ||
57 | ok => [] | ||
58 | }, | ||
59 | opts => new Nagios::Plugin::Getopt(%args), | ||
60 | threshold => undef, # defined later | ||
61 | }; | ||
62 | bless $self, $class; | ||
63 | return $self; | ||
64 | } | ||
28 | 65 | ||
29 | sub add_perfdata { | 66 | sub add_perfdata { |
30 | my ($self, %args) = @_; | 67 | my ($self, %args) = @_; |
@@ -38,9 +75,9 @@ sub all_perfoutput { | |||
38 | } | 75 | } |
39 | 76 | ||
40 | sub set_thresholds { | 77 | sub set_thresholds { |
41 | shift; | 78 | my $self = shift; |
42 | require Nagios::Plugin::Threshold; | 79 | require Nagios::Plugin::Threshold; |
43 | Nagios::Plugin::Threshold->set_thresholds(@_); | 80 | return $self->threshold( Nagios::Plugin::Threshold->set_thresholds(@_)); |
44 | } | 81 | } |
45 | 82 | ||
46 | # NP::Functions wrappers | 83 | # NP::Functions wrappers |
@@ -56,14 +93,54 @@ sub die { | |||
56 | my $self = shift; | 93 | my $self = shift; |
57 | Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self }); | 94 | Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self }); |
58 | } | 95 | } |
96 | |||
59 | # Override default shortname accessor to add default | 97 | # Override default shortname accessor to add default |
60 | sub shortname { | 98 | sub shortname { |
61 | my $self = shift; | 99 | my $self = shift; |
62 | $self->{'Nagios::__::Plugin::shortname'} = shift if @_; | 100 | $self->{shortname} = shift if @_; |
63 | return $self->{'Nagios::__::Plugin::shortname'} || | 101 | return $self->{shortname} || |
64 | Nagios::Plugin::Functions::get_shortname(); | 102 | Nagios::Plugin::Functions::get_shortname(); |
65 | } | 103 | } |
66 | 104 | ||
105 | # top level interface to Nagios::Plugin::Threshold | ||
106 | sub check_threshold { | ||
107 | my $self = shift; | ||
108 | |||
109 | my %args; | ||
110 | |||
111 | if ( $#_ == 0 && ! ref $_[0]) { # one positional param | ||
112 | %args = (check => shift); | ||
113 | } | ||
114 | else { | ||
115 | %args = validate ( @_, { # named params | ||
116 | check => 1, | ||
117 | warning => 0, | ||
118 | critical => 0, | ||
119 | } ); | ||
120 | } | ||
121 | |||
122 | if (! $self->threshold || exists $args{warning} || exists $args{critical}) { | ||
123 | $self->set_thresholds( | ||
124 | warning => $args{warning} || $self->opts->warning , | ||
125 | critical => $args{critical} || $self->opts->critical , | ||
126 | ); | ||
127 | } | ||
128 | |||
129 | |||
130 | return $self->threshold->get_status($args{check}); | ||
131 | } | ||
132 | |||
133 | # top level interface to my Nagios::Plugin::Getopt object | ||
134 | sub arg { | ||
135 | my $self = shift; | ||
136 | $self->opts->arg(@_); | ||
137 | } | ||
138 | sub getopts { | ||
139 | my $self = shift; | ||
140 | $self->opts->getopts(@_); | ||
141 | } | ||
142 | |||
143 | |||
67 | # ------------------------------------------------------------------------- | 144 | # ------------------------------------------------------------------------- |
68 | # NP::Functions::check_messages helpers and wrappers | 145 | # NP::Functions::check_messages helpers and wrappers |
69 | 146 | ||
@@ -80,8 +157,8 @@ sub add_message { | |||
80 | croak "Error code '$code' not supported by add_message" | 157 | croak "Error code '$code' not supported by add_message" |
81 | if $code eq 'unknown' || $code eq 'dependent'; | 158 | if $code eq 'unknown' || $code eq 'dependent'; |
82 | 159 | ||
83 | $self->messages($code, []) unless $self->messages($code); | 160 | $self->messages($code, []) unless $self->messages->{$code}; |
84 | push @{$self->messages($code)}, @messages; | 161 | push @{$self->messages->{$code}}, @messages; |
85 | } | 162 | } |
86 | 163 | ||
87 | sub check_messages { | 164 | sub check_messages { |
@@ -90,7 +167,7 @@ sub check_messages { | |||
90 | 167 | ||
91 | # Add object messages to any passed in as args | 168 | # Add object messages to any passed in as args |
92 | for my $code (qw(critical warning ok)) { | 169 | for my $code (qw(critical warning ok)) { |
93 | my $messages = $self->messages($code) || []; | 170 | my $messages = $self->messages->{$code} || []; |
94 | if ($args{$code}) { | 171 | if ($args{$code}) { |
95 | unless (ref $args{$code} eq 'ARRAY') { | 172 | unless (ref $args{$code} eq 'ARRAY') { |
96 | if ($code eq 'ok') { | 173 | if ($code eq 'ok') { |
@@ -123,9 +200,10 @@ __END__ | |||
123 | Nagios::Plugin - a family of perl modules to streamline writing Nagios | 200 | Nagios::Plugin - a family of perl modules to streamline writing Nagios |
124 | plugins | 201 | plugins |
125 | 202 | ||
126 | |||
127 | =head1 SYNOPSIS | 203 | =head1 SYNOPSIS |
128 | 204 | ||
205 | # TODO NJV -- make this more like check_stuff. | ||
206 | |||
129 | # Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default | 207 | # Constants OK, WARNING, CRITICAL, and UNKNOWN are exported by default |
130 | # See also Nagios::Plugin::Functions for a functional interface | 208 | # See also Nagios::Plugin::Functions for a functional interface |
131 | use Nagios::Plugin; | 209 | use Nagios::Plugin; |
@@ -188,9 +266,7 @@ plugins | |||
188 | # | size=36kB;10:25;25: time=... | 266 | # | size=36kB;10:25;25: time=... |
189 | 267 | ||
190 | # Option handling methods (NOT YET IMPLEMENTED - use | 268 | # Option handling methods (NOT YET IMPLEMENTED - use |
191 | # Nagios::Plugin::Getopt for now) | 269 | # Nagios::Plugin::Getopt for |
192 | |||
193 | |||
194 | 270 | ||
195 | =head1 DESCRIPTION | 271 | =head1 DESCRIPTION |
196 | 272 | ||
@@ -280,6 +356,7 @@ NOT YET IMPLEMENTED - use Nagios::Plugin::Threshold directly for now. | |||
280 | 356 | ||
281 | =over 4 | 357 | =over 4 |
282 | 358 | ||
359 | =item check_threshold( $value ) | ||
283 | =item check_threshold( check => $value, warning => $warn, critical => $crit ) | 360 | =item check_threshold( check => $value, warning => $warn, critical => $crit ) |
284 | 361 | ||
285 | =back | 362 | =back |
@@ -382,6 +459,8 @@ section of the Nagios Plugin guidelines | |||
382 | 459 | ||
383 | =head2 OPTION HANDLING METHODS | 460 | =head2 OPTION HANDLING METHODS |
384 | 461 | ||
462 | TODO | ||
463 | |||
385 | NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now. | 464 | NOT YET IMPLEMENTED - use Nagios::Plugin::Getopt directly for now. |
386 | 465 | ||
387 | 466 | ||
@@ -426,9 +505,6 @@ http://nagiosplug.sourceforge.net. | |||
426 | 505 | ||
427 | Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt>. | 506 | Originally by Ton Voon, E<lt>ton.voon@altinity.comE<gt>. |
428 | 507 | ||
429 | Nathan Vonnahme added extra tests and subsequent fixes. | ||
430 | |||
431 | |||
432 | =head1 COPYRIGHT AND LICENSE | 508 | =head1 COPYRIGHT AND LICENSE |
433 | 509 | ||
434 | Copyright (C) 2006 by Nagios Plugin Development Team | 510 | Copyright (C) 2006 by Nagios Plugin Development Team |
diff --git a/lib/Nagios/Plugin/ExitResult.pm b/lib/Nagios/Plugin/ExitResult.pm index 191c92a..b161e9e 100644 --- a/lib/Nagios/Plugin/ExitResult.pm +++ b/lib/Nagios/Plugin/ExitResult.pm | |||
@@ -39,7 +39,7 @@ return codes when testing. | |||
39 | $e = nagios_exit( CRITICAL, 'aiiii ...' ); | 39 | $e = nagios_exit( CRITICAL, 'aiiii ...' ); |
40 | print $e->message; | 40 | print $e->message; |
41 | print $e->return_code; | 41 | print $e->return_code; |
42 | 42 | ||
43 | # NP::ExitResult also stringifies to the message output | 43 | # NP::ExitResult also stringifies to the message output |
44 | like(nagios_exit( WARNING, 'foobar'), qr/^foo/, 'matches!'); | 44 | like(nagios_exit( WARNING, 'foobar'), qr/^foo/, 'matches!'); |
45 | 45 | ||
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index 5772c97..41ec27a 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm | |||
@@ -11,7 +11,7 @@ use File::Basename; | |||
11 | use Params::Validate qw(validate :types); | 11 | use Params::Validate qw(validate :types); |
12 | 12 | ||
13 | # Remember to update Nagios::Plugins as well | 13 | # Remember to update Nagios::Plugins as well |
14 | our $VERSION = "0.14"; | 14 | our $VERSION = "0.15"; |
15 | 15 | ||
16 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | 16 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); |
17 | 17 | ||
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm index 8a9fc1c..bbf1fc9 100644 --- a/lib/Nagios/Plugin/Getopt.pm +++ b/lib/Nagios/Plugin/Getopt.pm | |||
@@ -306,7 +306,7 @@ This documentation applies to version 0.01 of Nagios::Plugin::Getopt. | |||
306 | 306 | ||
307 | use Nagios::Plugin::Getopt; | 307 | use Nagios::Plugin::Getopt; |
308 | 308 | ||
309 | # Instantiate object (usage and version are mandatory) | 309 | # Instantiate object (usage is mandatory) |
310 | $ng = Nagios::Plugin::Getopt->new( | 310 | $ng = Nagios::Plugin::Getopt->new( |
311 | usage => "Usage: %s -H <host> -w <warning_threshold> | 311 | usage => "Usage: %s -H <host> -w <warning_threshold> |
312 | -c <critical threshold>", | 312 | -c <critical threshold>", |
@@ -352,7 +352,7 @@ additional arguments to be easily defined. | |||
352 | 352 | ||
353 | =head2 CONSTRUCTOR | 353 | =head2 CONSTRUCTOR |
354 | 354 | ||
355 | # Instantiate object (usage and version are mandatory) | 355 | # Instantiate object (usage is mandatory) |
356 | $ng = Nagios::Plugin::Getopt->new( | 356 | $ng = Nagios::Plugin::Getopt->new( |
357 | usage => 'Usage: %s --hello', | 357 | usage => 'Usage: %s --hello', |
358 | version => '0.01', | 358 | version => '0.01', |
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); | |||
11 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" | 11 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" |
12 | if $ENV{TEST_VERBOSE}; | 12 | if $ENV{TEST_VERBOSE}; |
13 | 13 | ||
14 | my $p = Nagios::Plugin->new; | 14 | my $p = Nagios::Plugin->new (usage => "dummy usage"); |
15 | isa_ok( $p, "Nagios::Plugin"); | 15 | isa_ok( $p, "Nagios::Plugin"); |
16 | 16 | ||
17 | $p->shortname("PAGESIZE"); | 17 | $p->shortname("PAGESIZE"); |
18 | is($p->shortname, "PAGESIZE", "shortname set correctly"); | 18 | is($p->shortname, "PAGESIZE", "shortname explicitly set correctly"); |
19 | 19 | ||
20 | $p = Nagios::Plugin->new; | 20 | $p = Nagios::Plugin->new (usage => "dummy usage"); |
21 | is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); | 21 | is($p->shortname, "NAGIOS-PLUGIN-01", "shortname should default on new"); |
22 | 22 | ||
23 | $p = Nagios::Plugin->new( shortname => "SIZE" ); | 23 | $p = Nagios::Plugin->new( shortname => "SIZE", usage => "dummy usage" ); |
24 | is($p->shortname, "SIZE", "shortname set correctly on new"); | 24 | is($p->shortname, "SIZE", "shortname set correctly on new"); |
25 | 25 | ||
26 | diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; | 26 | diag "warn if < 10, critical if > 25 " if $ENV{TEST_VERBOSE}; |
27 | my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); | 27 | my $t = $p->set_thresholds( warning => "10:25", critical => "~:25" ); |
28 | 28 | ||
29 | use Data::Dumper; | ||
30 | #diag "dumping p: ". Dumper $p; | ||
31 | #diag "dumping perfdata: ". Dumper $p->perfdata; | ||
32 | |||
33 | |||
29 | $p->add_perfdata( | 34 | $p->add_perfdata( |
30 | label => "size", | 35 | label => "size", |
31 | value => 1, | 36 | value => 1, |
@@ -34,6 +39,7 @@ $p->add_perfdata( | |||
34 | ); | 39 | ); |
35 | 40 | ||
36 | cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); | 41 | cmp_ok( $p->all_perfoutput, 'eq', "size=1kB;10:25;~:25", "Perfdata correct"); |
42 | #diag "dumping perfdata: ". Dumper ($p->perfdata); | ||
37 | 43 | ||
38 | my $expected = {qw( | 44 | my $expected = {qw( |
39 | -1 WARNING | 45 | -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}"); | |||
16 | is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); | 16 | is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); |
17 | 17 | ||
18 | my $plugin = 'TEST_PLUGIN'; | 18 | my $plugin = 'TEST_PLUGIN'; |
19 | my $np = Nagios::Plugin->new( shortname => $plugin ); | 19 | my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); |
20 | is($np->shortname, $plugin, "shortname() is $plugin"); | 20 | is($np->shortname, $plugin, "shortname() is $plugin"); |
21 | 21 | ||
22 | # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) | 22 | # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) |
@@ -151,7 +151,7 @@ for (@ok) { | |||
151 | # shortname testing | 151 | # shortname testing |
152 | SKIP: { | 152 | SKIP: { |
153 | skip "requires File::Basename", 2 unless eval { require File::Basename }; | 153 | skip "requires File::Basename", 2 unless eval { require File::Basename }; |
154 | $np = Nagios::Plugin->new; | 154 | $np = Nagios::Plugin->new (usage => "dummy usage", version => "1"); |
155 | $plugin = uc File::Basename::basename($0); | 155 | $plugin = uc File::Basename::basename($0); |
156 | $plugin =~ s/\..*$//; | 156 | $plugin =~ s/\..*$//; |
157 | is($np->shortname, $plugin, "shortname() is '$plugin'"); | 157 | 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 { | |||
10 | Nagios::Plugin::Functions::_fake_exit(1); | 10 | Nagios::Plugin::Functions::_fake_exit(1); |
11 | 11 | ||
12 | my $plugin = 'NP_CHECK_MESSAGES_03'; | 12 | my $plugin = 'NP_CHECK_MESSAGES_03'; |
13 | my $np = Nagios::Plugin->new( shortname => $plugin ); | 13 | my $np = Nagios::Plugin->new( shortname => $plugin, usage => "dummy usage" ); |
14 | is($np->shortname, $plugin, "shortname() is $plugin"); | 14 | is($np->shortname, $plugin, "shortname() is $plugin"); |
15 | 15 | ||
16 | my ($code, $message); | 16 | my ($code, $message); |
@@ -172,33 +172,33 @@ is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $mess | |||
172 | # add_messages | 172 | # add_messages |
173 | 173 | ||
174 | # Constant codes | 174 | # Constant codes |
175 | $np = Nagios::Plugin->new; | 175 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
176 | $np->add_message( CRITICAL, "A B C" ); | 176 | $np->add_message( CRITICAL, "A B C" ); |
177 | $np->add_message( WARNING, "D E F" ); | 177 | $np->add_message( WARNING, "D E F" ); |
178 | ($code, $message) = $np->check_messages(); | 178 | ($code, $message) = $np->check_messages(); |
179 | is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); | 179 | is($code, CRITICAL, "(CRITICAL, WARNING) code is $STATUS_TEXT{$code}"); |
180 | is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); | 180 | is($message, $messages{critical}, "(CRITICAL, WARNING) message is $message"); |
181 | 181 | ||
182 | $np = Nagios::Plugin->new; | 182 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
183 | $np->add_message( CRITICAL, "A B C" ); | 183 | $np->add_message( CRITICAL, "A B C" ); |
184 | ($code, $message) = $np->check_messages(); | 184 | ($code, $message) = $np->check_messages(); |
185 | is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); | 185 | is($code, CRITICAL, "(CRITICAL) code is $STATUS_TEXT{$code}"); |
186 | is($message, $messages{critical}, "(CRITICAL) message is $message"); | 186 | is($message, $messages{critical}, "(CRITICAL) message is $message"); |
187 | 187 | ||
188 | $np = Nagios::Plugin->new; | 188 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
189 | $np->add_message( WARNING, "D E F" ); | 189 | $np->add_message( WARNING, "D E F" ); |
190 | ($code, $message) = $np->check_messages(); | 190 | ($code, $message) = $np->check_messages(); |
191 | is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); | 191 | is($code, WARNING, "(WARNING) code is $STATUS_TEXT{$code}"); |
192 | is($message, $messages{warning}, "(WARNING) message is $message"); | 192 | is($message, $messages{warning}, "(WARNING) message is $message"); |
193 | 193 | ||
194 | $np = Nagios::Plugin->new; | 194 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
195 | $np->add_message( WARNING, "D E F" ); | 195 | $np->add_message( WARNING, "D E F" ); |
196 | $np->add_message( OK, "G H I" ); | 196 | $np->add_message( OK, "G H I" ); |
197 | ($code, $message) = $np->check_messages(); | 197 | ($code, $message) = $np->check_messages(); |
198 | is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); | 198 | is($code, WARNING, "(WARNING, OK) code is $STATUS_TEXT{$code}"); |
199 | is($message, $messages{warning}, "(WARNING, OK) message is $message"); | 199 | is($message, $messages{warning}, "(WARNING, OK) message is $message"); |
200 | 200 | ||
201 | $np = Nagios::Plugin->new; | 201 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
202 | $np->add_message( OK, "G H I" ); | 202 | $np->add_message( OK, "G H I" ); |
203 | ($code, $message) = $np->check_messages(); | 203 | ($code, $message) = $np->check_messages(); |
204 | is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); | 204 | is($code, OK, "(OK) code is $STATUS_TEXT{$code}"); |
@@ -206,33 +206,33 @@ is($message, $messages{ok}, "(OK) message is $message"); | |||
206 | 206 | ||
207 | 207 | ||
208 | # String codes | 208 | # String codes |
209 | $np = Nagios::Plugin->new; | 209 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
210 | $np->add_message( critical => "A B C" ); | 210 | $np->add_message( critical => "A B C" ); |
211 | $np->add_message( warning => "D E F" ); | 211 | $np->add_message( warning => "D E F" ); |
212 | ($code, $message) = $np->check_messages(); | 212 | ($code, $message) = $np->check_messages(); |
213 | is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); | 213 | is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); |
214 | is($message, $messages{critical}, "(critical, warning) message is $message"); | 214 | is($message, $messages{critical}, "(critical, warning) message is $message"); |
215 | 215 | ||
216 | $np = Nagios::Plugin->new; | 216 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
217 | $np->add_message( critical => "A B C" ); | 217 | $np->add_message( critical => "A B C" ); |
218 | ($code, $message) = $np->check_messages(); | 218 | ($code, $message) = $np->check_messages(); |
219 | is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); | 219 | is($code, CRITICAL, "(critical) code is $STATUS_TEXT{$code}"); |
220 | is($message, $messages{critical}, "(critical) message is $message"); | 220 | is($message, $messages{critical}, "(critical) message is $message"); |
221 | 221 | ||
222 | $np = Nagios::Plugin->new; | 222 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
223 | $np->add_message( warning => "D E F" ); | 223 | $np->add_message( warning => "D E F" ); |
224 | ($code, $message) = $np->check_messages(); | 224 | ($code, $message) = $np->check_messages(); |
225 | is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); | 225 | is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); |
226 | is($message, $messages{warning}, "(warning) message is $message"); | 226 | is($message, $messages{warning}, "(warning) message is $message"); |
227 | 227 | ||
228 | $np = Nagios::Plugin->new; | 228 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
229 | $np->add_message( warning => "D E F" ); | 229 | $np->add_message( warning => "D E F" ); |
230 | $np->add_message( ok => "G H I" ); | 230 | $np->add_message( ok => "G H I" ); |
231 | ($code, $message) = $np->check_messages(); | 231 | ($code, $message) = $np->check_messages(); |
232 | is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); | 232 | is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); |
233 | is($message, $messages{warning}, "(warning, ok) message is $message"); | 233 | is($message, $messages{warning}, "(warning, ok) message is $message"); |
234 | 234 | ||
235 | $np = Nagios::Plugin->new; | 235 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
236 | $np->add_message( ok => "G H I" ); | 236 | $np->add_message( ok => "G H I" ); |
237 | ($code, $message) = $np->check_messages(); | 237 | ($code, $message) = $np->check_messages(); |
238 | is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); | 238 | is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); |
@@ -240,7 +240,7 @@ is($message, $messages{ok}, "(ok) message is $message"); | |||
240 | 240 | ||
241 | 241 | ||
242 | # No add_message | 242 | # No add_message |
243 | $np = Nagios::Plugin->new; | 243 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
244 | ($code, $message) = $np->check_messages(); | 244 | ($code, $message) = $np->check_messages(); |
245 | is($code, OK, "() code is $STATUS_TEXT{$code}"); | 245 | is($code, OK, "() code is $STATUS_TEXT{$code}"); |
246 | is($message, '', "() message is ''"); | 246 | is($message, '', "() message is ''"); |
@@ -250,7 +250,7 @@ is($message, '', "() message is ''"); | |||
250 | # Error conditions | 250 | # Error conditions |
251 | 251 | ||
252 | # add_message errors | 252 | # add_message errors |
253 | $np = Nagios::Plugin->new; | 253 | $np = Nagios::Plugin->new (usage => "dummy usage"); |
254 | ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, | 254 | ok(! defined eval { $np->add_message( foobar => 'hi mum' ) }, |
255 | 'add_message dies on invalid code'); | 255 | 'add_message dies on invalid code'); |
256 | ok(! defined eval { $np->add_message( OKAY => 'hi mum' ) }, | 256 | 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 @@ | |||
18 | use strict; | 18 | use strict; |
19 | use warnings; | 19 | use warnings; |
20 | 20 | ||
21 | use Nagios::Plugin qw(%ERRORS); | 21 | use Nagios::Plugin ; |
22 | |||
23 | use Nagios::Plugin::Getopt; | ||
24 | |||
25 | 22 | ||
26 | use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); | 23 | use vars qw($VERSION $PROGNAME $verbose $warn $critical $timeout $result); |
27 | '$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion | 24 | '$Revision$' =~ /^.*(\d+.\d+) \$$/; # Use The Revision from RCS/CVS/Subversion |
28 | $VERSION = $1; | 25 | $VERSION = $1; |
29 | $0 =~ m!^.*/([^/]+)$!; | ||
30 | $PROGNAME = $1; | ||
31 | 26 | ||
32 | # shortname is the identifier this script will give to Nagios. | 27 | # get the base name of this script for use in the examples |
33 | # it's set here to the uppercase program name with file extension removed, | 28 | use File::Basename; |
34 | # e.g. check_stuff.pl -> CHECK_STUFF | 29 | $PROGNAME = basename($0); |
35 | my $short_name = uc $PROGNAME; | ||
36 | $short_name =~ s/\.\w+$//; | ||
37 | 30 | ||
38 | 31 | ||
39 | ############################################################################## | 32 | ############################################################################## |
40 | # define and get the command line options. | 33 | # define and get the command line options. |
41 | # see the command line option guidelines at | 34 | # see the command line option guidelines at |
42 | # | 35 | # http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOPTIONS |
43 | 36 | ||
44 | 37 | ||
45 | # Instantiate Nagios::Plugin::Getopt object (usage and version are mandatory) | 38 | # Instantiate Nagios::Plugin object (the 'usage' parameter is mandatory) |
46 | my $nagopts = Nagios::Plugin::Getopt->new( | 39 | my $p = Nagios::Plugin->new( |
47 | usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] | 40 | usage => "Usage: %s [ -v|--verbose ] [-H <host>] [-t <timeout>] |
48 | [ -c|--critical=<critical threshold> ] | 41 | [ -c|--critical=<critical threshold> ] |
49 | [ -w|--warning=<warning threshold> ] | 42 | [ -w|--warning=<warning threshold> ] |
@@ -51,7 +44,7 @@ my $nagopts = Nagios::Plugin::Getopt->new( | |||
51 | version => $VERSION, | 44 | version => $VERSION, |
52 | 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.', | 45 | 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.', |
53 | 46 | ||
54 | extra => qq{ | 47 | extra => " |
55 | 48 | ||
56 | THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max' | 49 | THRESHOLDs for -w and -c are specified 'min:max' or 'min:' or ':max' |
57 | (or 'max'). If specified '\@min:max', a warning status will be generated | 50 | (or 'max'). If specified '\@min:max', a warning status will be generated |
@@ -70,16 +63,14 @@ Examples: | |||
70 | Returns a warning if the resulting number is less than 10, or a | 63 | Returns a warning if the resulting number is less than 10, or a |
71 | critical error if it is less than 4. | 64 | critical error if it is less than 4. |
72 | 65 | ||
73 | 66 | " | |
74 | } | ||
75 | |||
76 | ); | 67 | ); |
77 | 68 | ||
78 | 69 | ||
79 | # Define and document the valid command line options | 70 | # Define and document the valid command line options |
80 | # usage, help, version, timeout and verbose are defined by default. | 71 | # usage, help, version, timeout and verbose are defined by default. |
81 | 72 | ||
82 | $nagopts->arg( | 73 | $p->arg( |
83 | spec => 'warning|w=s', | 74 | spec => 'warning|w=s', |
84 | 75 | ||
85 | help => | 76 | help => |
@@ -91,17 +82,15 @@ qq{-w, --warning=INTEGER:INTEGER | |||
91 | # default => 10, | 82 | # default => 10, |
92 | ); | 83 | ); |
93 | 84 | ||
94 | $nagopts->arg( | 85 | $p->arg( |
95 | spec => 'critical|c=s', | 86 | spec => 'critical|c=s', |
96 | help => | 87 | help => |
97 | qq{-c, --critical=INTEGER:INTEGER | 88 | qq{-c, --critical=INTEGER:INTEGER |
98 | Minimum and maximum number of the generated result, outside of | 89 | Minimum and maximum number of the generated result, outside of |
99 | which a critical will be generated. If omitted, a critical is | 90 | which a critical will be generated. }, |
100 | generated if no processes are running.}, | ||
101 | |||
102 | ); | 91 | ); |
103 | 92 | ||
104 | $nagopts->arg( | 93 | $p->arg( |
105 | spec => 'result|r=f', | 94 | spec => 'result|r=f', |
106 | help => | 95 | help => |
107 | qq{-r, --result=INTEGER | 96 | qq{-r, --result=INTEGER |
@@ -110,59 +99,41 @@ qq{-r, --result=INTEGER | |||
110 | ); | 99 | ); |
111 | 100 | ||
112 | # Parse arguments and process standard ones (e.g. usage, help, version) | 101 | # Parse arguments and process standard ones (e.g. usage, help, version) |
113 | $nagopts->getopts; | 102 | $p->getopts; |
114 | |||
115 | 103 | ||
116 | my $p = Nagios::Plugin->new; | ||
117 | 104 | ||
118 | $p->shortname($short_name); | 105 | # perform sanity checking on command line options |
119 | 106 | if ( (defined $p->opts->result) && ($p->opts->result < 0 || $p->opts->result > 20) ) { | |
120 | 107 | $p->nagios_die( "invalid number supplied for the -r option" ); | |
121 | # sanity checking on command line options | ||
122 | if ( (defined $nagopts->result) && ($nagopts->result < 0 || $nagopts->result > 20) ) { | ||
123 | $p->die( | ||
124 | return_code => $ERRORS{UNKNOWN}, | ||
125 | message => 'invalid number supplied for the -r option' | ||
126 | ); | ||
127 | } | 108 | } |
128 | 109 | ||
129 | unless ( defined $nagopts->warning || defined $nagopts->critical ) { | 110 | unless ( defined $p->opts->warning || defined $p->opts->critical ) { |
130 | $p->die( | 111 | $p->nagios_die( "you didn't supply a threshold argument" ); |
131 | return_code => $ERRORS{UNKNOWN}, | ||
132 | message => "you didn't supply a threshold argument" | ||
133 | ); | ||
134 | } | 112 | } |
135 | 113 | ||
136 | ############################################################################## | ||
137 | # define a Nagios::Threshold object based on the command line options | ||
138 | my $t = $p->set_thresholds( warning => $nagopts->warning, critical => $nagopts->critical ); | ||
139 | 114 | ||
140 | 115 | ||
141 | ############################################################################## | 116 | ############################################################################## |
142 | # check stuff. | 117 | # check stuff. |
143 | 118 | ||
144 | # THIS is where you'd do your actual checking to get a real value for $result | 119 | # THIS is where you'd do your actual checking to get a real value for $result |
145 | # don't forget to timeout after $nagopts->timeout seconds, if applicable. | 120 | # don't forget to timeout after $p->opts->timeout seconds, if applicable. |
146 | my $result; | 121 | my $result; |
147 | if (defined $nagopts->result) { | 122 | if (defined $p->opts->result) { # you got a 'result' option from the command line options |
148 | $result = $nagopts->result; | 123 | $result = $p->opts->result; |
149 | print "using supplied result $result from command line\n" if $nagopts->verbose; | 124 | print "using supplied result $result from command line\n" if $p->opts->verbose; |
150 | } | 125 | } |
151 | else { | 126 | else { |
152 | $result = int rand(20)+1; | 127 | $result = int rand(20)+1; |
153 | print "generated random result $result\n" if $nagopts->verbose; | 128 | print "generated random result $result\n" if $p->opts->verbose; |
154 | } | 129 | } |
155 | 130 | ||
156 | print "status of result ($result) is ", $t->get_status($result), "\n" | ||
157 | if $nagopts->verbose; | ||
158 | |||
159 | |||
160 | |||
161 | 131 | ||
162 | ############################################################################## | 132 | ############################################################################## |
133 | # check the result against the defined warning and critical thresholds, | ||
163 | # output the result and exit | 134 | # output the result and exit |
164 | $p->die( | 135 | $p->nagios_exit( |
165 | return_code => $t->get_status($result), | 136 | return_code => $p->check_threshold($result), |
166 | message => "sample result was $result" | 137 | message => "sample result was $result" |
167 | ); | 138 | ); |
168 | 139 | ||
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); | |||
8 | my $s = 't/check_stuff.pl'; | 8 | my $s = 't/check_stuff.pl'; |
9 | $s = 'perl -Ilib '.$s; | 9 | $s = 'perl -Ilib '.$s; |
10 | 10 | ||
11 | my $n = 'CHECK_STUFF'; | 11 | my $n = 'STUFF'; |
12 | 12 | ||
13 | # Nagios status strings and exit codes | 13 | # Nagios status strings and exit codes |
14 | my %e = qw( | 14 | my %e = qw( |