summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Nagios/Plugin/Functions.pm22
-rw-r--r--t/Nagios-Plugin-Functions-03.t21
2 files changed, 40 insertions, 3 deletions
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);
18require Exporter; 18require Exporter;
19our @ISA = qw(Exporter); 19our @ISA = qw(Exporter);
20our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); 20our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
21our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname); 21our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state);
22our %EXPORT_TAGS = ( 22our %EXPORT_TAGS = (
23 all => [ @EXPORT, @EXPORT_OK ], 23 all => [ @EXPORT, @EXPORT_OK ],
24 codes => [ @STATUS_CODES ], 24 codes => [ @STATUS_CODES ],
25 functions => [ qw(nagios_exit nagios_die check_messages) ], 25 functions => [ qw(nagios_exit nagios_die check_messages max_state) ],
26); 26);
27 27
28use constant OK => 0; 28use constant OK => 0;
@@ -56,6 +56,15 @@ sub get_shortname {
56 return $shortname; 56 return $shortname;
57} 57}
58 58
59sub max_state {
60 return CRITICAL if grep { $_ == CRITICAL } @_;
61 return WARNING if grep { $_ == WARNING } @_;
62 return OK if grep { $_ == OK } @_;
63 return UNKNOWN if grep { $_ == UNKNOWN } @_;
64 return DEPENDENT if grep { $_ == DEPENDENT } @_;
65 return UNKNOWN;
66}
67
59# nagios_exit( $code, $message ) 68# nagios_exit( $code, $message )
60sub nagios_exit { 69sub nagios_exit {
61 my ($code, $message, $arg) = @_; 70 my ($code, $message, $arg) = @_;
@@ -197,7 +206,7 @@ __END__
197=head1 NAME 206=head1 NAME
198 207
199Nagios::Plugin::Functions - functions to simplify the creation of 208Nagios::Plugin::Functions - functions to simplify the creation of
200Nagios plugins. 209Nagios plugins
201 210
202=head1 SYNOPSIS 211=head1 SYNOPSIS
203 212
@@ -259,6 +268,7 @@ The following variables and functions are exported only on request:
259 %ERRORS 268 %ERRORS
260 %STATUS_TEXT 269 %STATUS_TEXT
261 get_shortname 270 get_shortname
271 max_state
262 272
263 273
264=head2 FUNCTIONS 274=head2 FUNCTIONS
@@ -349,6 +359,12 @@ imported.
349 359
350=back 360=back
351 361
362=item max_state(@a)
363
364Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN,
365DEPENDENT
366
367=back
352 368
353=head1 SEE ALSO 369=head1 SEE ALSO
354 370
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 @@
1# max_state tests
2
3use strict;
4use Test::More tests => 8;
5
6BEGIN { use_ok("Nagios::Plugin::Functions", ":all") }
7
8my $new_state = max_state( OK, WARNING );
9
10is( $new_state, WARNING, "Moved up to WARNING" );
11is( max_state( $new_state, UNKNOWN ), WARNING, "Still at WARNING" );
12
13$new_state = max_state( $new_state, CRITICAL );
14is( $new_state, CRITICAL, "Now at CRITICAL" );
15is( max_state( OK, OK ), OK, "This is OK" );
16
17is( max_state( OK, UNKNOWN ), OK, "This is still OK, not UNKNOWN" );
18
19is( max_state( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" );
20
21is( max_state(), UNKNOWN, "Return UNKNOWN if no parameters" );