summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Nagios/Plugin.pm11
-rw-r--r--lib/Nagios/Plugin/Functions.pm26
-rw-r--r--t/Nagios-Plugin-Functions-04.t21
3 files changed, 56 insertions, 2 deletions
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 06f313d..5f9a905 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -99,6 +99,12 @@ sub die {
99 my $self = shift; 99 my $self = shift;
100 Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self }); 100 Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self });
101} 101}
102sub max_state {
103 Nagios::Plugin::Functions::max_state(@_);
104}
105sub max_state_alt {
106 Nagios::Plugin::Functions::max_state_alt(@_);
107}
102 108
103# Override default shortname accessor to add default 109# Override default shortname accessor to add default
104sub shortname { 110sub shortname {
@@ -483,6 +489,11 @@ Set C<$_use_die> flag if this functionality is required (see test code).
483 489
484Alias for nagios_die(). Deprecated. 490Alias for nagios_die(). Deprecated.
485 491
492=item max_state, max_state_alt
493
494These are wrapper function for Nagios::Plugin::Functions::max_state and
495Nagios::Plugin::Functions::max_state_alt.
496
486=back 497=back
487 498
488=head2 THRESHOLD METHODS 499=head2 THRESHOLD METHODS
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
index a37cdaf..b44157c 100644
--- a/lib/Nagios/Plugin/Functions.pm
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -19,11 +19,11 @@ our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
19require Exporter; 19require Exporter;
20our @ISA = qw(Exporter); 20our @ISA = qw(Exporter);
21our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); 21our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
22our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert $value_re); 22our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state max_state_alt convert $value_re);
23our %EXPORT_TAGS = ( 23our %EXPORT_TAGS = (
24 all => [ @EXPORT, @EXPORT_OK ], 24 all => [ @EXPORT, @EXPORT_OK ],
25 codes => [ @STATUS_CODES ], 25 codes => [ @STATUS_CODES ],
26 functions => [ qw(nagios_exit nagios_die check_messages max_state convert) ], 26 functions => [ qw(nagios_exit nagios_die check_messages max_state max_state_alt convert) ],
27); 27);
28 28
29use constant OK => 0; 29use constant OK => 0;
@@ -73,6 +73,15 @@ sub max_state {
73 return UNKNOWN; 73 return UNKNOWN;
74} 74}
75 75
76sub max_state_alt {
77 return CRITICAL if grep { $_ == CRITICAL } @_;
78 return WARNING if grep { $_ == WARNING } @_;
79 return UNKNOWN if grep { $_ == UNKNOWN } @_;
80 return DEPENDENT if grep { $_ == DEPENDENT } @_;
81 return OK if grep { $_ == OK } @_;
82 return UNKNOWN;
83}
84
76# nagios_exit( $code, $message ) 85# nagios_exit( $code, $message )
77sub nagios_exit { 86sub nagios_exit {
78 my ($code, $message, $arg) = @_; 87 my ($code, $message, $arg) = @_;
@@ -303,6 +312,7 @@ The following variables and functions are exported only on request:
303 %STATUS_TEXT 312 %STATUS_TEXT
304 get_shortname 313 get_shortname
305 max_state 314 max_state
315 max_state_alt
306 316
307 317
308=head2 FUNCTIONS 318=head2 FUNCTIONS
@@ -395,6 +405,18 @@ imported.
395Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN, 405Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN,
396DEPENDENT 406DEPENDENT
397 407
408The typical usage of max_state is to initialise the state as UNKNOWN and use
409it on the result of various test. If no test were performed successfully the
410state will still be UNKNOWN.
411
412=item max_state_alt(@a)
413
414Returns the worst state in the array. Order is: CRITICAL, WARNING, UNKNOWN,
415DEPENDENT, OK
416
417This is a true definition of a max state (OK last) and should be used if the
418internal tests performed can return UNKNOWN.
419
398=back 420=back
399 421
400=head1 SEE ALSO 422=head1 SEE ALSO
diff --git a/t/Nagios-Plugin-Functions-04.t b/t/Nagios-Plugin-Functions-04.t
new file mode 100644
index 0000000..d3ff05c
--- /dev/null
+++ b/t/Nagios-Plugin-Functions-04.t
@@ -0,0 +1,21 @@
1# max_state_alt tests
2
3use strict;
4use Test::More tests => 8;
5
6BEGIN { use_ok("Nagios::Plugin::Functions", ":all") }
7
8my $new_state = max_state_alt( OK, WARNING );
9
10is( $new_state, WARNING, "Moved up to WARNING" );
11is( max_state_alt( $new_state, UNKNOWN ), WARNING, "Still at WARNING" );
12
13$new_state = max_state_alt( $new_state, CRITICAL );
14is( $new_state, CRITICAL, "Now at CRITICAL" );
15is( max_state_alt( OK, OK ), OK, "This is OK" );
16
17is( max_state_alt( OK, UNKNOWN ), UNKNOWN, "This is UNKNOWN" );
18
19is( max_state_alt( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" );
20
21is( max_state_alt(), UNKNOWN, "Return UNKNOWN if no parameters" );