summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2006-09-26 01:10:23 +0000
committerGavin Carr <gonzai@users.sourceforge.net>2006-09-26 01:10:23 +0000
commite548a22a92b3aa345f4a952fddb39cc693c35a70 (patch)
tree606e3872d3070a90b5fa9881303eddb59efc8bbd
parentd8f912e8f5abb1476366cfea6e0fb368a9669ec4 (diff)
downloadmonitoring-plugin-perl-e548a22a92b3aa345f4a952fddb39cc693c35a70.tar.gz
Rename NP::Base to NP::Functions; add check_messages() to NP::Functions.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1482 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--MANIFEST5
-rw-r--r--Makefile.PL2
-rw-r--r--lib/Nagios/Plugin.pm6
-rw-r--r--lib/Nagios/Plugin/Base.pm196
-rw-r--r--lib/Nagios/Plugin/ExitResult.pm6
-rw-r--r--lib/Nagios/Plugin/Functions.pm344
-rw-r--r--lib/Nagios/Plugin/Getopt.pm6
-rw-r--r--lib/Nagios/Plugin/Performance.pm4
-rw-r--r--lib/Nagios/Plugin/Range.pm4
-rw-r--r--lib/Nagios/Plugin/Threshold.pm4
-rw-r--r--t/Nagios-Plugin-Functions-01.t (renamed from t/Nagios-Plugin-Base.t)8
-rw-r--r--t/Nagios-Plugin-Functions-02.t162
-rw-r--r--t/Nagios-Plugin-Performance.t6
-rw-r--r--t/Nagios-Plugin-Range.t10
-rw-r--r--t/Nagios-Plugin-Threshold.t12
-rw-r--r--t/Nagios-Plugin.t7
16 files changed, 549 insertions, 233 deletions
diff --git a/MANIFEST b/MANIFEST
index 6b5de1b..9fc9611 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -5,7 +5,8 @@ README
5t/check_stuff.pl 5t/check_stuff.pl
6t/check_stuff.t 6t/check_stuff.t
7t/Nagios-Plugin.t 7t/Nagios-Plugin.t
8t/Nagios-Plugin-Base.t 8t/Nagios-Plugin-Functions-01.t
9t/Nagios-Plugin-Functions-02.t
9t/Nagios-Plugin-Getopt-01.t 10t/Nagios-Plugin-Getopt-01.t
10t/Nagios-Plugin-Getopt-02.t 11t/Nagios-Plugin-Getopt-02.t
11t/Nagios-Plugin-Performance.t 12t/Nagios-Plugin-Performance.t
@@ -15,7 +16,7 @@ lib/Nagios/Plugin.pm
15lib/Nagios/Plugin/Performance.pm 16lib/Nagios/Plugin/Performance.pm
16lib/Nagios/Plugin/Range.pm 17lib/Nagios/Plugin/Range.pm
17lib/Nagios/Plugin/Threshold.pm 18lib/Nagios/Plugin/Threshold.pm
18lib/Nagios/Plugin/Base.pm 19lib/Nagios/Plugin/Functions.pm
19lib/Nagios/Plugin/Getopt.pm 20lib/Nagios/Plugin/Getopt.pm
20lib/Nagios/Plugin/ExitResult.pm 21lib/Nagios/Plugin/ExitResult.pm
21META.yml Module meta-data (added by MakeMaker) 22META.yml Module meta-data (added by MakeMaker)
diff --git a/Makefile.PL b/Makefile.PL
index 30bfe77..c25369f 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -4,7 +4,7 @@ use ExtUtils::MakeMaker;
4# the contents of the Makefile that is written. 4# the contents of the Makefile that is written.
5WriteMakefile( 5WriteMakefile(
6 NAME => 'Nagios::Plugin', 6 NAME => 'Nagios::Plugin',
7 VERSION_FROM => 'lib/Nagios/Plugin/Base.pm', # finds $VERSION 7 VERSION_FROM => 'lib/Nagios/Plugin/Functions.pm', # finds $VERSION
8 PREREQ_PM => {Params::Validate => 0.24}, # e.g., Module::Name => 1.1 8 PREREQ_PM => {Params::Validate => 0.24}, # e.g., Module::Name => 1.1
9 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 9 ($] >= 5.005 ? ## Add these new keywords supported since 5.005
10 (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module 10 (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 41bff91..5ff6709 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -11,7 +11,7 @@ struct "Nagios::__::Plugin" => {
11 11
12package Nagios::Plugin; 12package Nagios::Plugin;
13 13
14use Nagios::Plugin::Base; 14use Nagios::Plugin::Functions;
15use Nagios::Plugin::Performance; 15use Nagios::Plugin::Performance;
16use Nagios::Plugin::Threshold; 16use Nagios::Plugin::Threshold;
17 17
@@ -24,7 +24,7 @@ use Exporter;
24our @ISA = qw(Exporter Nagios::__::Plugin); 24our @ISA = qw(Exporter Nagios::__::Plugin);
25our @EXPORT_OK = qw(%ERRORS); 25our @EXPORT_OK = qw(%ERRORS);
26 26
27our $VERSION = $Nagios::Plugin::Base::VERSION; 27our $VERSION = $Nagios::Plugin::Functions::VERSION;
28 28
29sub add_perfdata { 29sub add_perfdata {
30 my ($self, %args) = @_; 30 my ($self, %args) = @_;
@@ -41,7 +41,7 @@ sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); }
41 41
42sub die { 42sub die {
43 my $self = shift; 43 my $self = shift;
44 Nagios::Plugin::Base::die(@_, { plugin => $self }); 44 Nagios::Plugin::Functions::die(@_, { plugin => $self });
45} 45}
46 46
471; 471;
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm
deleted file mode 100644
index 92651ed..0000000
--- a/lib/Nagios/Plugin/Base.pm
+++ /dev/null
@@ -1,196 +0,0 @@
1# This module holds all exported variables
2# and base functions
3package Nagios::Plugin::Base;
4
5use strict;
6use warnings;
7use File::Basename;
8
9our $VERSION = "0.13";
10
11our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
12
13require Exporter;
14our @ISA = qw(Exporter);
15our @EXPORT = (@STATUS_CODES, qw(nagios_exit %ERRORS));
16our @EXPORT_OK = qw(nagios_die %STATUS_TEXT);
17our %EXPORT_TAGS = (
18 all => [ @EXPORT, @EXPORT_OK ],
19 codes => [ @STATUS_CODES ],
20 functions => [ qw(nagios_exit nagios_die) ],
21);
22
23use constant OK => 0;
24use constant WARNING => 1;
25use constant CRITICAL => 2;
26use constant UNKNOWN => 3;
27use constant DEPENDENT => 4;
28
29our %ERRORS = (
30 'OK' => OK,
31 'WARNING' => WARNING,
32 'CRITICAL' => CRITICAL,
33 'UNKNOWN' => UNKNOWN,
34 'DEPENDENT' => DEPENDENT,
35);
36
37our %STATUS_TEXT = reverse %ERRORS;
38
39# _fake_exit flag and accessor/mutator, for testing
40my $_fake_exit = 0;
41sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
42
43sub get_shortname {
44 my %arg = @_;
45
46 return $arg{plugin}->shortname if $arg{plugin};
47
48 my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0);
49 $shortname =~ s/^CHECK_//;
50 return $shortname;
51}
52
53# nagios_exit( $code, $message )
54sub nagios_exit {
55 my ($code, $message, $arg) = @_;
56
57 # Handle named parameters
58 if (defined $code && ($code eq 'return_code' || $code eq 'message')) {
59 # Remove last argument if odd no and last is ref
60 if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) {
61 $arg = pop @_;
62 } else {
63 undef $arg;
64 }
65 my %arg = @_;
66 $code = $arg{return_code};
67 $message = $arg{message};
68 }
69 $arg ||= {};
70
71 # Handle string codes
72 $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
73
74 # Set defaults
75 $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
76 $message = '' unless defined $message;
77 $message = join(' ', @$message) if ref $message eq 'ARRAY';
78
79 # Setup output
80 my $output = "$STATUS_TEXT{$code}";
81 $output .= " - $message" if defined $message && $message ne '';
82 my $shortname = get_shortname(plugin => $arg->{plugin});
83 $output = "$shortname $output" if $shortname;
84 if ($arg->{plugin}) {
85 my $plugin = $arg->{plugin};
86 $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata;
87 }
88 $output .= "\n";
89
90 # Don't actually exit if _fake_exit set
91 if ($_fake_exit) {
92 require Nagios::Plugin::ExitResult;
93 return Nagios::Plugin::ExitResult->new($code, $output);
94 }
95
96 # Print output and exit
97 print $output;
98 exit $code;
99}
100
101# nagios_die( $message, [ $code ]) OR nagios_die( $code, $message )
102# Default $code: UNKNOWN
103sub nagios_die {
104 my ($arg1, $arg2, $rest) = @_;
105
106 # Named parameters
107 if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) {
108 return nagios_exit(@_);
109 }
110
111 # ($code, $message)
112 elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) {
113 return nagios_exit(@_);
114 }
115
116 # ($message, $code)
117 elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) {
118 return nagios_exit($arg2, $arg1, $rest);
119 }
120
121 # Else just assume $arg1 is the message and hope for the best
122 else {
123 return nagios_exit( UNKNOWN, $arg1, $rest );
124 }
125}
126
127# For backwards compatibility
128sub die { nagios_die(@_); }
129
130
131=pod old
132
133my $exit_on_die = 1;
134sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die };
135my $print_on_die = 1;
136sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die };
137
138# Old version - TODO: remove
139sub old_die {
140 my ($class, $args, $plugin) = @_;
141 my $return_code;
142
143 if ( exists $args->{return_code}
144 && exists $STATUS_TEXT{$args->{return_code}}
145 ) {
146 $return_code = $args->{return_code};
147 }
148 else {
149 $return_code = $ERRORS{UNKNOWN};
150 }
151 my $message = $args->{message} || "Internal error";
152 my $output = join(" ", $STATUS_TEXT{$return_code}, $message);
153 if ($plugin) {
154 $output = $plugin->shortname." $output" if $plugin->shortname;
155 $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata;
156 }
157 if ($print_on_die) {
158 print $output, $/;
159 }
160 if ($exit_on_die) {
161 exit $return_code;
162 } else {
163 return $output;
164 }
165}
166
167=cut
168
1691;
170
171# vim:sw=4:sm:et
172
173__END__
174
175=head1 NAME
176
177Nagios::Plugin::Base - Base functions for Nagios::Plugins
178
179=head1 DESCRIPTION
180
181See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate
182common backend functionality.
183
184=head1 AUTHOR
185
186Ton Voon, E<lt>ton.voon@altinity.comE<gt>
187
188=head1 COPYRIGHT AND LICENSE
189
190Copyright (C) 2006 by Nagios Plugin Development Team
191
192This library is free software; you can redistribute it and/or modify
193it under the same terms as Perl itself, either Perl version 5.8.4 or,
194at your option, any later version of Perl 5 you may have available.
195
196=cut
diff --git a/lib/Nagios/Plugin/ExitResult.pm b/lib/Nagios/Plugin/ExitResult.pm
index f059424..191c92a 100644
--- a/lib/Nagios/Plugin/ExitResult.pm
+++ b/lib/Nagios/Plugin/ExitResult.pm
@@ -30,10 +30,10 @@ return codes when testing.
30=head1 SYNOPSIS 30=head1 SYNOPSIS
31 31
32 use Test::More; 32 use Test::More;
33 use Nagios::Plugin::Base; 33 use Nagios::Plugin::Functions;
34 34
35 # In a test file somewhere 35 # In a test file somewhere
36 Nagios::Plugin::Base::_fake_exit(1); 36 Nagios::Plugin::Functions::_fake_exit(1);
37 37
38 # Later ... 38 # Later ...
39 $e = nagios_exit( CRITICAL, 'aiiii ...' ); 39 $e = nagios_exit( CRITICAL, 'aiiii ...' );
@@ -50,7 +50,7 @@ return codes when testing.
50Nagios::Plugin::ExitResult is a tiny helper class intended for use 50Nagios::Plugin::ExitResult is a tiny helper class intended for use
51when testing other Nagios::Plugin modules. A Nagios::Plugin::ExitResult 51when testing other Nagios::Plugin modules. A Nagios::Plugin::ExitResult
52object is returned by nagios_exit() and friends when 52object is returned by nagios_exit() and friends when
53Nagios::Plugin::Base::_fake_exit has been set, instead of doing a 53Nagios::Plugin::Functions::_fake_exit has been set, instead of doing a
54conventional print + exit. 54conventional print + exit.
55 55
56=head1 AUTHOR 56=head1 AUTHOR
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
new file mode 100644
index 0000000..9c20288
--- /dev/null
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -0,0 +1,344 @@
1# This module holds all exported variables
2# and base functions
3package Nagios::Plugin::Functions;
4
5use strict;
6use warnings;
7use File::Basename;
8use Params::Validate qw(validate :types);
9
10our $VERSION = "0.13";
11
12our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
13
14require Exporter;
15our @ISA = qw(Exporter);
16our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
17our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT);
18our %EXPORT_TAGS = (
19 all => [ @EXPORT, @EXPORT_OK ],
20 codes => [ @STATUS_CODES ],
21 functions => [ qw(nagios_exit nagios_die check_messages) ],
22);
23
24use constant OK => 0;
25use constant WARNING => 1;
26use constant CRITICAL => 2;
27use constant UNKNOWN => 3;
28use constant DEPENDENT => 4;
29
30our %ERRORS = (
31 'OK' => OK,
32 'WARNING' => WARNING,
33 'CRITICAL' => CRITICAL,
34 'UNKNOWN' => UNKNOWN,
35 'DEPENDENT' => DEPENDENT,
36);
37
38our %STATUS_TEXT = reverse %ERRORS;
39
40# _fake_exit flag and accessor/mutator, for testing
41my $_fake_exit = 0;
42sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
43
44sub get_shortname {
45 my %arg = @_;
46
47 return $arg{plugin}->shortname if $arg{plugin};
48
49 my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0);
50 $shortname =~ s/^CHECK_//;
51 return $shortname;
52}
53
54# nagios_exit( $code, $message )
55sub nagios_exit {
56 my ($code, $message, $arg) = @_;
57
58 # Handle named parameters
59 if (defined $code && ($code eq 'return_code' || $code eq 'message')) {
60 # Remove last argument if odd no and last is ref
61 if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) {
62 $arg = pop @_;
63 } else {
64 undef $arg;
65 }
66 my %arg = @_;
67 $code = $arg{return_code};
68 $message = $arg{message};
69 }
70 $arg ||= {};
71
72 # Handle string codes
73 $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
74
75 # Set defaults
76 $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
77 $message = '' unless defined $message;
78 $message = join(' ', @$message) if ref $message eq 'ARRAY';
79
80 # Setup output
81 my $output = "$STATUS_TEXT{$code}";
82 $output .= " - $message" if defined $message && $message ne '';
83 my $shortname = get_shortname(plugin => $arg->{plugin});
84 $output = "$shortname $output" if $shortname;
85 if ($arg->{plugin}) {
86 my $plugin = $arg->{plugin};
87 $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata;
88 }
89 $output .= "\n";
90
91 # Don't actually exit if _fake_exit set
92 if ($_fake_exit) {
93 require Nagios::Plugin::ExitResult;
94 return Nagios::Plugin::ExitResult->new($code, $output);
95 }
96
97 # Print output and exit
98 print $output;
99 exit $code;
100}
101
102# nagios_die( $message, [ $code ]) OR nagios_die( $code, $message )
103# Default $code: UNKNOWN
104sub nagios_die {
105 my ($arg1, $arg2, $rest) = @_;
106
107 # Named parameters
108 if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) {
109 return nagios_exit(@_);
110 }
111
112 # ($code, $message)
113 elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) {
114 return nagios_exit(@_);
115 }
116
117 # ($message, $code)
118 elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) {
119 return nagios_exit($arg2, $arg1, $rest);
120 }
121
122 # Else just assume $arg1 is the message and hope for the best
123 else {
124 return nagios_exit( UNKNOWN, $arg1, $rest );
125 }
126}
127
128# For backwards compatibility
129sub die { nagios_die(@_); }
130
131
132# ------------------------------------------------------------------------
133# check_messages - return a status and/or message based on a set of
134# message arrays.
135# Returns a nagios status code in scalar context.
136# Returns a code and a message in list context.
137# The message is join($join, @array) for the relevant array for the code,
138# or join($join_all, $message) for all arrays if $join_all is set.
139sub check_messages {
140 my %arg = validate( @_, {
141 critical => { type => ARRAYREF },
142 warning => { type => ARRAYREF },
143 ok => { type => ARRAYREF | SCALAR, optional => 1 },
144 'join' => { default => ' ' },
145 join_all => 0,
146 });
147 $arg{join} = ' ' unless defined $arg{join};
148
149 # Decide $code
150 my $code = OK;
151 $code ||= CRITICAL if @{$arg{critical}};
152 $code ||= WARNING if @{$arg{warning}};
153 return $code unless wantarray;
154
155 # Compose message
156 my $message = '';
157 if ($arg{join_all}) {
158 $message = join( $arg{join_all},
159 map { @$_ ? join( $arg{'join'}, @$_) : () }
160 $arg{critical},
161 $arg{warning},
162 $arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : []
163 );
164 }
165
166 else {
167 $message ||= join( $arg{'join'}, @{$arg{critical}} )
168 if $code == CRITICAL;
169 $message ||= join( $arg{'join'}, @{$arg{warning}} )
170 if $code == WARNING;
171 $message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok}
172 if $arg{ok};
173 }
174
175 return ($code, $message);
176}
177
178# ------------------------------------------------------------------------
179
1801;
181
182# vim:sw=4:sm:et
183
184__END__
185
186=head1 NAME
187
188Nagios::Plugin::Functions - functions to simplify the creation of
189Nagios plugins.
190
191=head1 SYNOPSIS
192
193 # Constants OK, WARNING, CRITICAL, and UNKNOWN exported by default
194 use Nagios::Plugin::Functions;
195
196 # nagios_exit( ODE, $message ) - exit with error code CODE,
197 # and message "PLUGIN CODE - $message"
198 nagios_exit( CRITICAL, $critical_error ) if $critical_error;
199 nagios_exit( WARNING, $warning_error ) if $warning_error;
200 nagios_exit( OK, $result );
201
202 # nagios_die( $message, [$CODE] ) - just like nagios_exit(),
203 # but CODE is optional, defaulting to UNKNOWN
204 do_something()
205 or nagios_die("do_something() failed horribly");
206 do_something_critical()
207 or nagios_die("do_something_critical() failed", CRITICAL);
208
209 # check_messages - check a set of message arrays, returning a
210 # CODE and/or a result message
211 $code = check_messages(critical => \@crit, warning => \@warn);
212 ($code, $message) = check_messages(
213 critical => \@crit, warning => \@warn,
214 ok => \@ok );
215
216
217=head1 DESCRIPTION
218
219This module is part of the Nagios::Plugin family, a set of modules
220for simplifying the creation of Nagios plugins. This module exports
221convenience functions for the class methods provided by
222Nagios::Plugin. It is intended for those who prefer a simpler
223functional interface, and who do not need the additional
224functionality of Nagios::Plugin.
225
226=head2 Exports
227
228Nagios status code constants are exported by default:
229
230 OK
231 WARNING
232 CRITICAL
233 UNKNOWN
234 DEPENDENT
235
236as are the following functions:
237
238 nagios_exit
239 nagios_die
240 check_messages
241
242The following variables are exported only on request:
243
244 %ERRORS
245 %STATUS_TEXT
246
247
248=head2 Functions
249
250The following functions are supported:
251
252=over 4
253
254=item nagios_exit( CODE, $message )
255
256Exit with return code CODE, and a standard nagios message of the
257form "PLUGIN CODE - $message".
258
259=item nagios_die( $message, [CODE] )
260
261Same as nagios_exit(), except that CODE is optional, defaulting
262to UNKNOWN.
263
264=item check_messages( critical => \@crit, warning => \@warn )
265
266Convenience function to check a set of message arrays and return
267an appropriate nagios return code and/or a result message. Returns
268only a return code in scalar context; returns a return code and an
269error message in list context i.e.
270
271 # Scalar context
272 $code = check_messages(critical => \@crit, warning => \@warn);
273 # List context
274 ($code, $msg) = check_messages(critical => \@crit, warning => \@warn);
275
276check_messages() accepts the following named arguments:
277
278=over 4
279
280=item critical => ARRAYREF
281
282An arrayref of critical error messages - check_messages() returns
283CRITICAL if this arrayref is non-empty. Mandatory.
284
285=item warning => ARRAYREF
286
287An arrayref of warning error messages - check_messages() returns
288WARNING if this arrayref is non-empty ('critical' is checked
289first). Mandatory.
290
291=item ok => ARRAYREF | SCALAR
292
293An arrayref of informational messages (or a single scalar message),
294used in list context if both the 'critical' and 'warning' arrayrefs
295are empty. Optional.
296
297=item join => SCALAR
298
299A string used to join the relevant array to generate the message
300string returned in list context i.e. if the 'critical' array @crit
301is non-empty, check_messages would return:
302
303 join( $join, @crit )
304
305as the result message. Optional; default: ' ' (space).
306
307=item join_all => SCALAR
308
309By default, only one set of messages are joined and returned in the
310result message i.e. if the result is CRITICAL, only the 'critical'
311messages are included in the result; if WARNING, only the 'warning'
312messages are included; if OK, the 'ok' messages are included (if
313supplied) i.e. the default is to return an 'errors-only' type
314message.
315
316If join_all is supplied, however, it will be used as a string to
317join the resultant critical, warning, and ok messages together i.e.
318all messages are joined and returned.
319
320=back
321
322=back
323
324
325=head1 SEE ALSO
326
327Nagios::Plugin; the nagios plugin developer guidelines at
328http://nagiosplug.sourceforge.net/developer-guidelines.html.
329
330
331=head1 AUTHORS
332
333Ton Voon, E<lt>ton.voon@altinity.comE<gt>
334
335
336=head1 COPYRIGHT AND LICENSE
337
338Copyright (C) 2006 by Nagios Plugin Development Team
339
340This library is free software; you can redistribute it and/or modify
341it under the same terms as Perl itself, either Perl version 5.8.4 or,
342at your option, any later version of Perl 5 you may have available.
343
344=cut
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm
index 7f32c3b..8a9fc1c 100644
--- a/lib/Nagios/Plugin/Getopt.pm
+++ b/lib/Nagios/Plugin/Getopt.pm
@@ -12,9 +12,9 @@ use Carp;
12use Params::Validate qw(:all); 12use Params::Validate qw(:all);
13use base qw(Class::Accessor); 13use base qw(Class::Accessor);
14 14
15use Nagios::Plugin::Base; 15use Nagios::Plugin::Functions;
16use vars qw($VERSION); 16use vars qw($VERSION);
17$VERSION = $Nagios::Plugin::Base::VERSION; 17$VERSION = $Nagios::Plugin::Functions::VERSION;
18 18
19# Standard defaults 19# Standard defaults
20my %DEFAULT = ( 20my %DEFAULT = (
@@ -586,7 +586,7 @@ Gavin Carr <gavin@openfusion.com.au>
586 586
587=head1 COPYRIGHT AND LICENSE 587=head1 COPYRIGHT AND LICENSE
588 588
589Copyright 2005-2006 Gavin Carr. All Rights Reserved. 589Copyright (C) 2006 by the Nagios Plugin Development Team.
590 590
591This module is free software. It may be used, redistributed 591This module is free software. It may be used, redistributed
592and/or modified under either the terms of the Perl Artistic 592and/or modified under either the terms of the Perl Artistic
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm
index 9e99f54..38803a6 100644
--- a/lib/Nagios/Plugin/Performance.pm
+++ b/lib/Nagios/Plugin/Performance.pm
@@ -7,8 +7,8 @@ use warnings;
7 7
8use Carp; 8use Carp;
9use Nagios::Plugin::Threshold; 9use Nagios::Plugin::Threshold;
10use Nagios::Plugin::Base; 10use Nagios::Plugin::Functions;
11our ($VERSION) = $Nagios::Plugin::Base::VERSION; 11our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
12 12
13use Class::Struct; 13use Class::Struct;
14struct "Nagios::Plugin::Performance" => { 14struct "Nagios::Plugin::Performance" => {
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm
index b17cc96..f354a54 100644
--- a/lib/Nagios/Plugin/Range.pm
+++ b/lib/Nagios/Plugin/Range.pm
@@ -6,8 +6,8 @@ use strict;
6use warnings; 6use warnings;
7use Carp; 7use Carp;
8 8
9use Nagios::Plugin::Base; 9use Nagios::Plugin::Functions;
10our ($VERSION) = $Nagios::Plugin::Base::VERSION; 10our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
11 11
12use overload 12use overload
13 '""' => sub { shift->stringify }; 13 '""' => sub { shift->stringify };
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm
index d7a8177..3e14d82 100644
--- a/lib/Nagios/Plugin/Threshold.pm
+++ b/lib/Nagios/Plugin/Threshold.pm
@@ -6,8 +6,8 @@ use strict;
6use warnings; 6use warnings;
7 7
8use Nagios::Plugin::Range; 8use Nagios::Plugin::Range;
9use Nagios::Plugin::Base qw(:codes nagios_die); 9use Nagios::Plugin::Functions qw(:codes nagios_die);
10our ($VERSION) = $Nagios::Plugin::Base::VERSION; 10our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
11 11
12use Class::Struct; 12use Class::Struct;
13struct "Nagios::Plugin::Threshold" => { 13struct "Nagios::Plugin::Threshold" => {
diff --git a/t/Nagios-Plugin-Base.t b/t/Nagios-Plugin-Functions-01.t
index 68a02fe..7401945 100644
--- a/t/Nagios-Plugin-Base.t
+++ b/t/Nagios-Plugin-Functions-01.t
@@ -2,17 +2,17 @@
2use strict; 2use strict;
3use Test::More tests => 111; 3use Test::More tests => 111;
4 4
5BEGIN { use_ok("Nagios::Plugin::Base", ":all"); } 5BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); }
6Nagios::Plugin::Base::_fake_exit(1); 6Nagios::Plugin::Functions::_fake_exit(1);
7 7
8my $this_version=$Nagios::Plugin::Base::VERSION; 8my $this_version=$Nagios::Plugin::Functions::VERSION;
9foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { 9foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
10 my $mod = "Nagios::Plugin$m"; 10 my $mod = "Nagios::Plugin$m";
11 use_ok($mod); 11 use_ok($mod);
12 # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing 12 # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing
13 my $v = "$mod"."::VERSION"; 13 my $v = "$mod"."::VERSION";
14 my $a = eval "\$$v"; 14 my $a = eval "\$$v";
15 is($a, $this_version, "Version number for $mod the same as Base: $this_version"); 15 is($a, $this_version, "Version number for $mod the same as Functions: $this_version");
16} 16}
17 17
18# Hardcoded checks of constants 18# Hardcoded checks of constants
diff --git a/t/Nagios-Plugin-Functions-02.t b/t/Nagios-Plugin-Functions-02.t
new file mode 100644
index 0000000..981e2eb
--- /dev/null
+++ b/t/Nagios-Plugin-Functions-02.t
@@ -0,0 +1,162 @@
1# check_messages tests
2
3use strict;
4use Test::More tests => 33;
5
6BEGIN { use_ok("Nagios::Plugin::Functions", ":all") }
7
8my ($code, $message);
9
10# -------------------------------------------------------------------------
11# Check codes
12my @codes = (
13 [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ],
14 [ [], [ qw(Warning) ], WARNING ],
15 [ [], [], OK ],
16);
17my $i = 0;
18for (@codes) {
19 $i++;
20 $code = check_messages( critical => $_->[0], warning => $_->[1] );
21 is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}");
22}
23
24# -------------------------------------------------------------------------
25# Check messages
26my %arrays = (
27 critical => [ qw(A B C) ],
28 warning => [ qw(D E F) ],
29 ok => [ qw(G H I) ],
30);
31my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays;
32
33# critical, warning
34($code, $message) = check_messages(
35 critical => $arrays{critical}, warning => $arrays{warning},
36);
37is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
38is($message, $messages{critical}, "(critical, warning) message is $message");
39
40# critical, warning, ok
41($code, $message) = check_messages(
42 critical => $arrays{critical}, warning => $arrays{warning},
43 ok => $arrays{ok},
44);
45is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
46is($message, $messages{critical}, "(critical, warning, ok) message is $message");
47
48# critical, warning, $ok
49($code, $message) = check_messages(
50 critical => $arrays{critical}, warning => $arrays{warning},
51 ok => 'G H I',
52);
53is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
54is($message, $messages{critical}, "(critical, warning, \$ok) message is $message");
55
56# warning
57($code, $message) = check_messages(
58 critical => [], warning => $arrays{warning},
59);
60is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
61is($message, $messages{warning}, "(warning) message is $message");
62
63# warning, ok
64($code, $message) = check_messages(
65 critical => [], warning => $arrays{warning}, ok => $arrays{ok},
66);
67is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
68is($message, $messages{warning}, "(warning, ok) message is $message");
69
70# ok
71($code, $message) = check_messages(
72 critical => [], warning => [], ok => $arrays{ok},
73);
74is($code, OK, "(ok) code is $STATUS_TEXT{$code}");
75is($message, $messages{ok}, "(ok) message is $message");
76
77# $ok
78($code, $message) = check_messages(
79 critical => [], warning => [], ok => 'G H I',
80);
81is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}");
82is($message, $messages{ok}, "(\$ok) message is $message");
83
84# -------------------------------------------------------------------------
85# explicit join
86my $join = '+';
87($code, $message) = check_messages(
88 critical => $arrays{critical}, warning => $arrays{warning},
89 join => $join,
90);
91is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message");
92$join = '';
93($code, $message) = check_messages(
94 critical => [], warning => $arrays{warning},
95 join => $join,
96);
97is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message");
98$join = undef;
99($code, $message) = check_messages(
100 critical => [], warning => [], ok => $arrays{ok},
101 join => $join,
102);
103is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message");
104
105# -------------------------------------------------------------------------
106# join_all messages
107my $join_all = ' :: ';
108my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) }
109 qw(critical warning ok));
110my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) }
111 qw(critical warning));
112my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) }
113 qw(warning ok));
114
115# critical, warning, ok
116($code, $message) = check_messages(
117 critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok},
118 join_all => $join_all,
119);
120is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}");
121is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message");
122
123# critical, warning, $ok
124($code, $message) = check_messages(
125 critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I',
126 join_all => $join_all,
127);
128is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}");
129is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message");
130
131# critical, warning
132($code, $message) = check_messages(
133 critical => $arrays{critical}, warning => $arrays{warning},
134 join_all => $join_all,
135);
136is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}");
137is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message");
138
139# warning, ok
140($code, $message) = check_messages(
141 critical => [], warning => $arrays{warning}, ok => $arrays{ok},
142 join_all => $join_all,
143);
144is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}");
145is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message");
146
147# warning, $ok
148($code, $message) = check_messages(
149 critical => [], warning => $arrays{warning}, ok => 'G H I',
150 join_all => $join_all,
151);
152is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}");
153is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message");
154
155# warning
156($code, $message) = check_messages(
157 critical => [], warning => $arrays{warning},
158 join_all => $join_all,
159);
160is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}");
161is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message");
162
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t
index 1da3191..e0eb2f6 100644
--- a/t/Nagios-Plugin-Performance.t
+++ b/t/Nagios-Plugin-Performance.t
@@ -3,10 +3,10 @@ use strict;
3use Test::More tests => 49; 3use Test::More tests => 49;
4BEGIN { use_ok('Nagios::Plugin::Performance') }; 4BEGIN { use_ok('Nagios::Plugin::Performance') };
5 5
6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; 6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
7 7
8use Nagios::Plugin::Base; 8use Nagios::Plugin::Functions;
9Nagios::Plugin::Base::_fake_exit(1); 9Nagios::Plugin::Functions::_fake_exit(1);
10 10
11my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); 11my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448");
12cmp_ok( $p[0]->label, 'eq', "/", "label okay"); 12cmp_ok( $p[0]->label, 'eq', "/", "label okay");
diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t
index f01518d..df5dbd5 100644
--- a/t/Nagios-Plugin-Range.t
+++ b/t/Nagios-Plugin-Range.t
@@ -2,13 +2,17 @@
2use strict; 2use strict;
3use Test::More qw(no_plan); #tests => 123; 3use Test::More qw(no_plan); #tests => 123;
4 4
5BEGIN { use_ok('Nagios::Plugin::Range') }; 5BEGIN {
6 use_ok('Nagios::Plugin::Range');
7 # Silence warnings unless TEST_VERBOSE is set
8 $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
9};
6 10
7diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n"; 11diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n" if $ENV{TEST_VERBOSE};
8 12
9my $r; 13my $r;
10 14
11diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:"; 15diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:" if $ENV{TEST_VERBOSE};
12 16
13foreach (qw( 17foreach (qw(
14 : 18 :
diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t
index 677c054..cdb8d77 100644
--- a/t/Nagios-Plugin-Threshold.t
+++ b/t/Nagios-Plugin-Threshold.t
@@ -4,7 +4,7 @@ use Test::More tests => 71;
4#use Test::Exception; # broken for now so we don't need this. 4#use Test::Exception; # broken for now so we don't need this.
5BEGIN { 5BEGIN {
6 use_ok('Nagios::Plugin::Threshold'); 6 use_ok('Nagios::Plugin::Threshold');
7 use_ok('Nagios::Plugin::Base', ':all' ); 7 use_ok('Nagios::Plugin::Functions', ':all' );
8 # Silence warnings unless TEST_VERBOSE is set 8 # Silence warnings unless TEST_VERBOSE is set
9 $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; 9 $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} };
10} 10}
@@ -12,7 +12,7 @@ BEGIN {
12diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n" 12diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"
13 if $ENV{TEST_VERBOSE}; 13 if $ENV{TEST_VERBOSE};
14 14
15Nagios::Plugin::Base::_fake_exit(1); 15Nagios::Plugin::Functions::_fake_exit(1);
16 16
17diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; 17diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE};
18my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); 18my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80");
@@ -96,8 +96,8 @@ test_expected_statuses( $t, $expected );
96goto SKIP_DEATH; 96goto SKIP_DEATH;
97diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; 97diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE};
98diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; 98diag "you should see one invalid range definition warning and an UNKNOWN line here:\n";
99Nagios::Plugin::Base->print_on_die(1); 99Nagios::Plugin::Functions->print_on_die(1);
100Nagios::Plugin::Base->exit_on_die(1); 100Nagios::Plugin::Functions->exit_on_die(1);
101 101
102dies_ok( sub { 102dies_ok( sub {
103 $t = Nagios::Plugin::Threshold->set_thresholds( 103 $t = Nagios::Plugin::Threshold->set_thresholds(
@@ -106,8 +106,8 @@ dies_ok( sub {
106 ) 106 )
107 }, "bad thresholds cause death" 107 }, "bad thresholds cause death"
108); 108);
109Nagios::Plugin::Base->print_on_die(0); 109Nagios::Plugin::Functions->print_on_die(0);
110Nagios::Plugin::Base->exit_on_die(0); 110Nagios::Plugin::Functions->exit_on_die(0);
111SKIP_DEATH: 111SKIP_DEATH:
112 112
113 113
diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t
index 213e514..0ae2113 100644
--- a/t/Nagios-Plugin.t
+++ b/t/Nagios-Plugin.t
@@ -4,10 +4,11 @@ use Test::More tests => 12;
4 4
5BEGIN { use_ok('Nagios::Plugin') }; 5BEGIN { use_ok('Nagios::Plugin') };
6 6
7use Nagios::Plugin::Base; 7use Nagios::Plugin::Functions;
8Nagios::Plugin::Base::_fake_exit(1); 8Nagios::Plugin::Functions::_fake_exit(1);
9 9
10diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; 10diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"
11 if $ENV{TEST_VERBOSE};
11 12
12my $p = Nagios::Plugin->new; 13my $p = Nagios::Plugin->new;
13isa_ok( $p, "Nagios::Plugin"); 14isa_ok( $p, "Nagios::Plugin");