diff options
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 22 | ||||
-rw-r--r-- | t/Nagios-Plugin-Functions-03.t | 21 |
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); | |||
18 | require Exporter; | 18 | require Exporter; |
19 | our @ISA = qw(Exporter); | 19 | our @ISA = qw(Exporter); |
20 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); | 20 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); |
21 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname); | 21 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state); |
22 | our %EXPORT_TAGS = ( | 22 | our %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 | ||
28 | use constant OK => 0; | 28 | use constant OK => 0; |
@@ -56,6 +56,15 @@ sub get_shortname { | |||
56 | return $shortname; | 56 | return $shortname; |
57 | } | 57 | } |
58 | 58 | ||
59 | sub 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 ) |
60 | sub nagios_exit { | 69 | sub 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 | ||
199 | Nagios::Plugin::Functions - functions to simplify the creation of | 208 | Nagios::Plugin::Functions - functions to simplify the creation of |
200 | Nagios plugins. | 209 | Nagios 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 | |||
364 | Returns the worst state in the array. Order is: CRITICAL, WARNING, OK, UNKNOWN, | ||
365 | DEPENDENT | ||
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 | |||
3 | use strict; | ||
4 | use Test::More tests => 8; | ||
5 | |||
6 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } | ||
7 | |||
8 | my $new_state = max_state( OK, WARNING ); | ||
9 | |||
10 | is( $new_state, WARNING, "Moved up to WARNING" ); | ||
11 | is( max_state( $new_state, UNKNOWN ), WARNING, "Still at WARNING" ); | ||
12 | |||
13 | $new_state = max_state( $new_state, CRITICAL ); | ||
14 | is( $new_state, CRITICAL, "Now at CRITICAL" ); | ||
15 | is( max_state( OK, OK ), OK, "This is OK" ); | ||
16 | |||
17 | is( max_state( OK, UNKNOWN ), OK, "This is still OK, not UNKNOWN" ); | ||
18 | |||
19 | is( max_state( OK, OK, OK, OK, OK, WARNING ), WARNING, "Use WARNING in this list" ); | ||
20 | |||
21 | is( max_state(), UNKNOWN, "Return UNKNOWN if no parameters" ); | ||