From 9692fb72f6f428dbed4e107dd83fa906854babff Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Wed, 7 Feb 2007 17:35:38 +0000 Subject: Added max_state function git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1615 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index 9983456..43f371c 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm @@ -18,11 +18,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); +our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state); our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], codes => [ @STATUS_CODES ], - functions => [ qw(nagios_exit nagios_die check_messages) ], + functions => [ qw(nagios_exit nagios_die check_messages max_state) ], ); use constant OK => 0; @@ -56,6 +56,15 @@ sub get_shortname { return $shortname; } +sub max_state { + return CRITICAL if grep { $_ == CRITICAL } @_; + return WARNING if grep { $_ == WARNING } @_; + return OK if grep { $_ == OK } @_; + return UNKNOWN if grep { $_ == UNKNOWN } @_; + return DEPENDENT if grep { $_ == DEPENDENT } @_; + return UNKNOWN; +} + # nagios_exit( $code, $message ) sub nagios_exit { my ($code, $message, $arg) = @_; @@ -197,7 +206,7 @@ __END__ =head1 NAME Nagios::Plugin::Functions - functions to simplify the creation of -Nagios plugins. +Nagios plugins =head1 SYNOPSIS @@ -259,6 +268,7 @@ The following variables and functions are exported only on request: %ERRORS %STATUS_TEXT get_shortname + max_state =head2 FUNCTIONS @@ -349,6 +359,12 @@ imported. =back +=item max_state(@a) + +Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN, +DEPENDENT + +=back =head1 SEE ALSO diff --git a/t/Nagios-Plugin-Functions-03.t b/t/Nagios-Plugin-Functions-03.t new file mode 100644 index 0000000..3706e4c --- /dev/null +++ b/t/Nagios-Plugin-Functions-03.t @@ -0,0 +1,21 @@ +# max_state tests + +use strict; +use Test::More tests => 8; + +BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } + +my $new_state = max_state( OK, WARNING ); + +is( $new_state, WARNING, "Moved up to WARNING" ); +is( max_state( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); + +$new_state = max_state( $new_state, CRITICAL ); +is( $new_state, CRITICAL, "Now at CRITICAL" ); +is( max_state( OK, OK ), OK, "This is OK" ); + +is( max_state( OK, UNKNOWN ), OK, "This is still OK, not UNKNOWN" ); + +is( max_state( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); + +is( max_state(), UNKNOWN, "Return UNKNOWN if no parameters" ); -- cgit v0.10-9-g596f