From 53d04242544a69e5fe226211bfb0186414941a9f Mon Sep 17 00:00:00 2001 From: Thomas Guyot-Sionnest Date: Wed, 19 Nov 2008 03:58:09 +0000 Subject: Add max_state_* interface warper to Nagios::Plugin object git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@2082 f882894a-f735-0410-b71e-b25c423dba1c 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 { my $self = shift; Nagios::Plugin::Functions::nagios_die(@_, { plugin => $self }); } +sub max_state { + Nagios::Plugin::Functions::max_state(@_); +} +sub max_state_alt { + Nagios::Plugin::Functions::max_state_alt(@_); +} # Override default shortname accessor to add default sub shortname { @@ -483,6 +489,11 @@ Set C<$_use_die> flag if this functionality is required (see test code). Alias for nagios_die(). Deprecated. +=item max_state, max_state_alt + +These are wrapper function for Nagios::Plugin::Functions::max_state and +Nagios::Plugin::Functions::max_state_alt. + =back =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); require Exporter; our @ISA = qw(Exporter); our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); -our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert $value_re); +our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state max_state_alt convert $value_re); our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], codes => [ @STATUS_CODES ], - functions => [ qw(nagios_exit nagios_die check_messages max_state convert) ], + functions => [ qw(nagios_exit nagios_die check_messages max_state max_state_alt convert) ], ); use constant OK => 0; @@ -73,6 +73,15 @@ sub max_state { return UNKNOWN; } +sub max_state_alt { + return CRITICAL if grep { $_ == CRITICAL } @_; + return WARNING if grep { $_ == WARNING } @_; + return UNKNOWN if grep { $_ == UNKNOWN } @_; + return DEPENDENT if grep { $_ == DEPENDENT } @_; + return OK if grep { $_ == OK } @_; + return UNKNOWN; +} + # nagios_exit( $code, $message ) sub nagios_exit { my ($code, $message, $arg) = @_; @@ -303,6 +312,7 @@ The following variables and functions are exported only on request: %STATUS_TEXT get_shortname max_state + max_state_alt =head2 FUNCTIONS @@ -395,6 +405,18 @@ imported. Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN, DEPENDENT +The typical usage of max_state is to initialise the state as UNKNOWN and use +it on the result of various test. If no test were performed successfully the +state will still be UNKNOWN. + +=item max_state_alt(@a) + +Returns the worst state in the array. Order is: CRITICAL, WARNING, UNKNOWN, +DEPENDENT, OK + +This is a true definition of a max state (OK last) and should be used if the +internal tests performed can return UNKNOWN. + =back =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 @@ +# max_state_alt tests + +use strict; +use Test::More tests => 8; + +BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } + +my $new_state = max_state_alt( OK, WARNING ); + +is( $new_state, WARNING, "Moved up to WARNING" ); +is( max_state_alt( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); + +$new_state = max_state_alt( $new_state, CRITICAL ); +is( $new_state, CRITICAL, "Now at CRITICAL" ); +is( max_state_alt( OK, OK ), OK, "This is OK" ); + +is( max_state_alt( OK, UNKNOWN ), UNKNOWN, "This is UNKNOWN" ); + +is( max_state_alt( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); + +is( max_state_alt(), UNKNOWN, "Return UNKNOWN if no parameters" ); -- cgit v0.10-9-g596f