diff options
| author | Gavin Carr <gonzai@users.sourceforge.net> | 2006-09-26 01:10:23 +0000 |
|---|---|---|
| committer | Gavin Carr <gonzai@users.sourceforge.net> | 2006-09-26 01:10:23 +0000 |
| commit | e548a22a92b3aa345f4a952fddb39cc693c35a70 (patch) | |
| tree | 606e3872d3070a90b5fa9881303eddb59efc8bbd | |
| parent | d8f912e8f5abb1476366cfea6e0fb368a9669ec4 (diff) | |
| download | monitoring-plugin-perl-e548a22a92b3aa345f4a952fddb39cc693c35a70.tar.gz | |
Rename NP::Base to NP::Functions; add check_messages() to NP::Functions.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1482 f882894a-f735-0410-b71e-b25c423dba1c
| -rw-r--r-- | MANIFEST | 5 | ||||
| -rw-r--r-- | Makefile.PL | 2 | ||||
| -rw-r--r-- | lib/Nagios/Plugin.pm | 6 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/Base.pm | 196 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/ExitResult.pm | 6 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 344 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/Getopt.pm | 6 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 4 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/Range.pm | 4 | ||||
| -rw-r--r-- | lib/Nagios/Plugin/Threshold.pm | 4 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Functions-01.t (renamed from t/Nagios-Plugin-Base.t) | 8 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Functions-02.t | 162 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Performance.t | 6 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Range.t | 10 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Threshold.t | 12 | ||||
| -rw-r--r-- | t/Nagios-Plugin.t | 7 |
16 files changed, 549 insertions, 233 deletions
| @@ -5,7 +5,8 @@ README | |||
| 5 | t/check_stuff.pl | 5 | t/check_stuff.pl |
| 6 | t/check_stuff.t | 6 | t/check_stuff.t |
| 7 | t/Nagios-Plugin.t | 7 | t/Nagios-Plugin.t |
| 8 | t/Nagios-Plugin-Base.t | 8 | t/Nagios-Plugin-Functions-01.t |
| 9 | t/Nagios-Plugin-Functions-02.t | ||
| 9 | t/Nagios-Plugin-Getopt-01.t | 10 | t/Nagios-Plugin-Getopt-01.t |
| 10 | t/Nagios-Plugin-Getopt-02.t | 11 | t/Nagios-Plugin-Getopt-02.t |
| 11 | t/Nagios-Plugin-Performance.t | 12 | t/Nagios-Plugin-Performance.t |
| @@ -15,7 +16,7 @@ lib/Nagios/Plugin.pm | |||
| 15 | lib/Nagios/Plugin/Performance.pm | 16 | lib/Nagios/Plugin/Performance.pm |
| 16 | lib/Nagios/Plugin/Range.pm | 17 | lib/Nagios/Plugin/Range.pm |
| 17 | lib/Nagios/Plugin/Threshold.pm | 18 | lib/Nagios/Plugin/Threshold.pm |
| 18 | lib/Nagios/Plugin/Base.pm | 19 | lib/Nagios/Plugin/Functions.pm |
| 19 | lib/Nagios/Plugin/Getopt.pm | 20 | lib/Nagios/Plugin/Getopt.pm |
| 20 | lib/Nagios/Plugin/ExitResult.pm | 21 | lib/Nagios/Plugin/ExitResult.pm |
| 21 | META.yml Module meta-data (added by MakeMaker) | 22 | META.yml Module meta-data (added by MakeMaker) |
diff --git a/Makefile.PL b/Makefile.PL index 30bfe77..c25369f 100644 --- a/Makefile.PL +++ b/Makefile.PL | |||
| @@ -4,7 +4,7 @@ use ExtUtils::MakeMaker; | |||
| 4 | # the contents of the Makefile that is written. | 4 | # the contents of the Makefile that is written. |
| 5 | WriteMakefile( | 5 | WriteMakefile( |
| 6 | NAME => 'Nagios::Plugin', | 6 | NAME => 'Nagios::Plugin', |
| 7 | VERSION_FROM => 'lib/Nagios/Plugin/Base.pm', # finds $VERSION | 7 | VERSION_FROM => 'lib/Nagios/Plugin/Functions.pm', # finds $VERSION |
| 8 | PREREQ_PM => {Params::Validate => 0.24}, # e.g., Module::Name => 1.1 | 8 | PREREQ_PM => {Params::Validate => 0.24}, # e.g., Module::Name => 1.1 |
| 9 | ($] >= 5.005 ? ## Add these new keywords supported since 5.005 | 9 | ($] >= 5.005 ? ## Add these new keywords supported since 5.005 |
| 10 | (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module | 10 | (ABSTRACT_FROM => 'lib/Nagios/Plugin.pm', # retrieve abstract from module |
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 41bff91..5ff6709 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm | |||
| @@ -11,7 +11,7 @@ struct "Nagios::__::Plugin" => { | |||
| 11 | 11 | ||
| 12 | package Nagios::Plugin; | 12 | package Nagios::Plugin; |
| 13 | 13 | ||
| 14 | use Nagios::Plugin::Base; | 14 | use Nagios::Plugin::Functions; |
| 15 | use Nagios::Plugin::Performance; | 15 | use Nagios::Plugin::Performance; |
| 16 | use Nagios::Plugin::Threshold; | 16 | use Nagios::Plugin::Threshold; |
| 17 | 17 | ||
| @@ -24,7 +24,7 @@ use Exporter; | |||
| 24 | our @ISA = qw(Exporter Nagios::__::Plugin); | 24 | our @ISA = qw(Exporter Nagios::__::Plugin); |
| 25 | our @EXPORT_OK = qw(%ERRORS); | 25 | our @EXPORT_OK = qw(%ERRORS); |
| 26 | 26 | ||
| 27 | our $VERSION = $Nagios::Plugin::Base::VERSION; | 27 | our $VERSION = $Nagios::Plugin::Functions::VERSION; |
| 28 | 28 | ||
| 29 | sub add_perfdata { | 29 | sub add_perfdata { |
| 30 | my ($self, %args) = @_; | 30 | my ($self, %args) = @_; |
| @@ -41,7 +41,7 @@ sub set_thresholds { shift; Nagios::Plugin::Threshold->set_thresholds(@_); } | |||
| 41 | 41 | ||
| 42 | sub die { | 42 | sub die { |
| 43 | my $self = shift; | 43 | my $self = shift; |
| 44 | Nagios::Plugin::Base::die(@_, { plugin => $self }); | 44 | Nagios::Plugin::Functions::die(@_, { plugin => $self }); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | 1; | 47 | 1; |
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm deleted file mode 100644 index 92651ed..0000000 --- a/lib/Nagios/Plugin/Base.pm +++ /dev/null | |||
| @@ -1,196 +0,0 @@ | |||
| 1 | # This module holds all exported variables | ||
| 2 | # and base functions | ||
| 3 | package Nagios::Plugin::Base; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use File::Basename; | ||
| 8 | |||
| 9 | our $VERSION = "0.13"; | ||
| 10 | |||
| 11 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | ||
| 12 | |||
| 13 | require Exporter; | ||
| 14 | our @ISA = qw(Exporter); | ||
| 15 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit %ERRORS)); | ||
| 16 | our @EXPORT_OK = qw(nagios_die %STATUS_TEXT); | ||
| 17 | our %EXPORT_TAGS = ( | ||
| 18 | all => [ @EXPORT, @EXPORT_OK ], | ||
| 19 | codes => [ @STATUS_CODES ], | ||
| 20 | functions => [ qw(nagios_exit nagios_die) ], | ||
| 21 | ); | ||
| 22 | |||
| 23 | use constant OK => 0; | ||
| 24 | use constant WARNING => 1; | ||
| 25 | use constant CRITICAL => 2; | ||
| 26 | use constant UNKNOWN => 3; | ||
| 27 | use constant DEPENDENT => 4; | ||
| 28 | |||
| 29 | our %ERRORS = ( | ||
| 30 | 'OK' => OK, | ||
| 31 | 'WARNING' => WARNING, | ||
| 32 | 'CRITICAL' => CRITICAL, | ||
| 33 | 'UNKNOWN' => UNKNOWN, | ||
| 34 | 'DEPENDENT' => DEPENDENT, | ||
| 35 | ); | ||
| 36 | |||
| 37 | our %STATUS_TEXT = reverse %ERRORS; | ||
| 38 | |||
| 39 | # _fake_exit flag and accessor/mutator, for testing | ||
| 40 | my $_fake_exit = 0; | ||
| 41 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | ||
| 42 | |||
| 43 | sub get_shortname { | ||
| 44 | my %arg = @_; | ||
| 45 | |||
| 46 | return $arg{plugin}->shortname if $arg{plugin}; | ||
| 47 | |||
| 48 | my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0); | ||
| 49 | $shortname =~ s/^CHECK_//; | ||
| 50 | return $shortname; | ||
| 51 | } | ||
| 52 | |||
| 53 | # nagios_exit( $code, $message ) | ||
| 54 | sub nagios_exit { | ||
| 55 | my ($code, $message, $arg) = @_; | ||
| 56 | |||
| 57 | # Handle named parameters | ||
| 58 | if (defined $code && ($code eq 'return_code' || $code eq 'message')) { | ||
| 59 | # Remove last argument if odd no and last is ref | ||
| 60 | if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { | ||
| 61 | $arg = pop @_; | ||
| 62 | } else { | ||
| 63 | undef $arg; | ||
| 64 | } | ||
| 65 | my %arg = @_; | ||
| 66 | $code = $arg{return_code}; | ||
| 67 | $message = $arg{message}; | ||
| 68 | } | ||
| 69 | $arg ||= {}; | ||
| 70 | |||
| 71 | # Handle string codes | ||
| 72 | $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; | ||
| 73 | |||
| 74 | # Set defaults | ||
| 75 | $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; | ||
| 76 | $message = '' unless defined $message; | ||
| 77 | $message = join(' ', @$message) if ref $message eq 'ARRAY'; | ||
| 78 | |||
| 79 | # Setup output | ||
| 80 | my $output = "$STATUS_TEXT{$code}"; | ||
| 81 | $output .= " - $message" if defined $message && $message ne ''; | ||
| 82 | my $shortname = get_shortname(plugin => $arg->{plugin}); | ||
| 83 | $output = "$shortname $output" if $shortname; | ||
| 84 | if ($arg->{plugin}) { | ||
| 85 | my $plugin = $arg->{plugin}; | ||
| 86 | $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata; | ||
| 87 | } | ||
| 88 | $output .= "\n"; | ||
| 89 | |||
| 90 | # Don't actually exit if _fake_exit set | ||
| 91 | if ($_fake_exit) { | ||
| 92 | require Nagios::Plugin::ExitResult; | ||
| 93 | return Nagios::Plugin::ExitResult->new($code, $output); | ||
| 94 | } | ||
| 95 | |||
| 96 | # Print output and exit | ||
| 97 | print $output; | ||
| 98 | exit $code; | ||
| 99 | } | ||
| 100 | |||
| 101 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | ||
| 102 | # Default $code: UNKNOWN | ||
| 103 | sub nagios_die { | ||
| 104 | my ($arg1, $arg2, $rest) = @_; | ||
| 105 | |||
| 106 | # Named parameters | ||
| 107 | if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { | ||
| 108 | return nagios_exit(@_); | ||
| 109 | } | ||
| 110 | |||
| 111 | # ($code, $message) | ||
| 112 | elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { | ||
| 113 | return nagios_exit(@_); | ||
| 114 | } | ||
| 115 | |||
| 116 | # ($message, $code) | ||
| 117 | elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { | ||
| 118 | return nagios_exit($arg2, $arg1, $rest); | ||
| 119 | } | ||
| 120 | |||
| 121 | # Else just assume $arg1 is the message and hope for the best | ||
| 122 | else { | ||
| 123 | return nagios_exit( UNKNOWN, $arg1, $rest ); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | # For backwards compatibility | ||
| 128 | sub die { nagios_die(@_); } | ||
| 129 | |||
| 130 | |||
| 131 | =pod old | ||
| 132 | |||
| 133 | my $exit_on_die = 1; | ||
| 134 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | ||
| 135 | my $print_on_die = 1; | ||
| 136 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | ||
| 137 | |||
| 138 | # Old version - TODO: remove | ||
| 139 | sub old_die { | ||
| 140 | my ($class, $args, $plugin) = @_; | ||
| 141 | my $return_code; | ||
| 142 | |||
| 143 | if ( exists $args->{return_code} | ||
| 144 | && exists $STATUS_TEXT{$args->{return_code}} | ||
| 145 | ) { | ||
| 146 | $return_code = $args->{return_code}; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $return_code = $ERRORS{UNKNOWN}; | ||
| 150 | } | ||
| 151 | my $message = $args->{message} || "Internal error"; | ||
| 152 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); | ||
| 153 | if ($plugin) { | ||
| 154 | $output = $plugin->shortname." $output" if $plugin->shortname; | ||
| 155 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; | ||
| 156 | } | ||
| 157 | if ($print_on_die) { | ||
| 158 | print $output, $/; | ||
| 159 | } | ||
| 160 | if ($exit_on_die) { | ||
| 161 | exit $return_code; | ||
| 162 | } else { | ||
| 163 | return $output; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | 1; | ||
| 170 | |||
| 171 | # vim:sw=4:sm:et | ||
| 172 | |||
| 173 | __END__ | ||
| 174 | |||
| 175 | =head1 NAME | ||
| 176 | |||
| 177 | Nagios::Plugin::Base - Base functions for Nagios::Plugins | ||
| 178 | |||
| 179 | =head1 DESCRIPTION | ||
| 180 | |||
| 181 | See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate | ||
| 182 | common backend functionality. | ||
| 183 | |||
| 184 | =head1 AUTHOR | ||
| 185 | |||
| 186 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
| 187 | |||
| 188 | =head1 COPYRIGHT AND LICENSE | ||
| 189 | |||
| 190 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
| 191 | |||
| 192 | This library is free software; you can redistribute it and/or modify | ||
| 193 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
| 194 | at your option, any later version of Perl 5 you may have available. | ||
| 195 | |||
| 196 | =cut | ||
diff --git a/lib/Nagios/Plugin/ExitResult.pm b/lib/Nagios/Plugin/ExitResult.pm index f059424..191c92a 100644 --- a/lib/Nagios/Plugin/ExitResult.pm +++ b/lib/Nagios/Plugin/ExitResult.pm | |||
| @@ -30,10 +30,10 @@ return codes when testing. | |||
| 30 | =head1 SYNOPSIS | 30 | =head1 SYNOPSIS |
| 31 | 31 | ||
| 32 | use Test::More; | 32 | use Test::More; |
| 33 | use Nagios::Plugin::Base; | 33 | use Nagios::Plugin::Functions; |
| 34 | 34 | ||
| 35 | # In a test file somewhere | 35 | # In a test file somewhere |
| 36 | Nagios::Plugin::Base::_fake_exit(1); | 36 | Nagios::Plugin::Functions::_fake_exit(1); |
| 37 | 37 | ||
| 38 | # Later ... | 38 | # Later ... |
| 39 | $e = nagios_exit( CRITICAL, 'aiiii ...' ); | 39 | $e = nagios_exit( CRITICAL, 'aiiii ...' ); |
| @@ -50,7 +50,7 @@ return codes when testing. | |||
| 50 | Nagios::Plugin::ExitResult is a tiny helper class intended for use | 50 | Nagios::Plugin::ExitResult is a tiny helper class intended for use |
| 51 | when testing other Nagios::Plugin modules. A Nagios::Plugin::ExitResult | 51 | when testing other Nagios::Plugin modules. A Nagios::Plugin::ExitResult |
| 52 | object is returned by nagios_exit() and friends when | 52 | object is returned by nagios_exit() and friends when |
| 53 | Nagios::Plugin::Base::_fake_exit has been set, instead of doing a | 53 | Nagios::Plugin::Functions::_fake_exit has been set, instead of doing a |
| 54 | conventional print + exit. | 54 | conventional print + exit. |
| 55 | 55 | ||
| 56 | =head1 AUTHOR | 56 | =head1 AUTHOR |
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm new file mode 100644 index 0000000..9c20288 --- /dev/null +++ b/lib/Nagios/Plugin/Functions.pm | |||
| @@ -0,0 +1,344 @@ | |||
| 1 | # This module holds all exported variables | ||
| 2 | # and base functions | ||
| 3 | package Nagios::Plugin::Functions; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use File::Basename; | ||
| 8 | use Params::Validate qw(validate :types); | ||
| 9 | |||
| 10 | our $VERSION = "0.13"; | ||
| 11 | |||
| 12 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | ||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | our @ISA = qw(Exporter); | ||
| 16 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); | ||
| 17 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT); | ||
| 18 | our %EXPORT_TAGS = ( | ||
| 19 | all => [ @EXPORT, @EXPORT_OK ], | ||
| 20 | codes => [ @STATUS_CODES ], | ||
| 21 | functions => [ qw(nagios_exit nagios_die check_messages) ], | ||
| 22 | ); | ||
| 23 | |||
| 24 | use constant OK => 0; | ||
| 25 | use constant WARNING => 1; | ||
| 26 | use constant CRITICAL => 2; | ||
| 27 | use constant UNKNOWN => 3; | ||
| 28 | use constant DEPENDENT => 4; | ||
| 29 | |||
| 30 | our %ERRORS = ( | ||
| 31 | 'OK' => OK, | ||
| 32 | 'WARNING' => WARNING, | ||
| 33 | 'CRITICAL' => CRITICAL, | ||
| 34 | 'UNKNOWN' => UNKNOWN, | ||
| 35 | 'DEPENDENT' => DEPENDENT, | ||
| 36 | ); | ||
| 37 | |||
| 38 | our %STATUS_TEXT = reverse %ERRORS; | ||
| 39 | |||
| 40 | # _fake_exit flag and accessor/mutator, for testing | ||
| 41 | my $_fake_exit = 0; | ||
| 42 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | ||
| 43 | |||
| 44 | sub get_shortname { | ||
| 45 | my %arg = @_; | ||
| 46 | |||
| 47 | return $arg{plugin}->shortname if $arg{plugin}; | ||
| 48 | |||
| 49 | my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0); | ||
| 50 | $shortname =~ s/^CHECK_//; | ||
| 51 | return $shortname; | ||
| 52 | } | ||
| 53 | |||
| 54 | # nagios_exit( $code, $message ) | ||
| 55 | sub nagios_exit { | ||
| 56 | my ($code, $message, $arg) = @_; | ||
| 57 | |||
| 58 | # Handle named parameters | ||
| 59 | if (defined $code && ($code eq 'return_code' || $code eq 'message')) { | ||
| 60 | # Remove last argument if odd no and last is ref | ||
| 61 | if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { | ||
| 62 | $arg = pop @_; | ||
| 63 | } else { | ||
| 64 | undef $arg; | ||
| 65 | } | ||
| 66 | my %arg = @_; | ||
| 67 | $code = $arg{return_code}; | ||
| 68 | $message = $arg{message}; | ||
| 69 | } | ||
| 70 | $arg ||= {}; | ||
| 71 | |||
| 72 | # Handle string codes | ||
| 73 | $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; | ||
| 74 | |||
| 75 | # Set defaults | ||
| 76 | $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; | ||
| 77 | $message = '' unless defined $message; | ||
| 78 | $message = join(' ', @$message) if ref $message eq 'ARRAY'; | ||
| 79 | |||
| 80 | # Setup output | ||
| 81 | my $output = "$STATUS_TEXT{$code}"; | ||
| 82 | $output .= " - $message" if defined $message && $message ne ''; | ||
| 83 | my $shortname = get_shortname(plugin => $arg->{plugin}); | ||
| 84 | $output = "$shortname $output" if $shortname; | ||
| 85 | if ($arg->{plugin}) { | ||
| 86 | my $plugin = $arg->{plugin}; | ||
| 87 | $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata; | ||
| 88 | } | ||
| 89 | $output .= "\n"; | ||
| 90 | |||
| 91 | # Don't actually exit if _fake_exit set | ||
| 92 | if ($_fake_exit) { | ||
| 93 | require Nagios::Plugin::ExitResult; | ||
| 94 | return Nagios::Plugin::ExitResult->new($code, $output); | ||
| 95 | } | ||
| 96 | |||
| 97 | # Print output and exit | ||
| 98 | print $output; | ||
| 99 | exit $code; | ||
| 100 | } | ||
| 101 | |||
| 102 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | ||
| 103 | # Default $code: UNKNOWN | ||
| 104 | sub nagios_die { | ||
| 105 | my ($arg1, $arg2, $rest) = @_; | ||
| 106 | |||
| 107 | # Named parameters | ||
| 108 | if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { | ||
| 109 | return nagios_exit(@_); | ||
| 110 | } | ||
| 111 | |||
| 112 | # ($code, $message) | ||
| 113 | elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { | ||
| 114 | return nagios_exit(@_); | ||
| 115 | } | ||
| 116 | |||
| 117 | # ($message, $code) | ||
| 118 | elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { | ||
| 119 | return nagios_exit($arg2, $arg1, $rest); | ||
| 120 | } | ||
| 121 | |||
| 122 | # Else just assume $arg1 is the message and hope for the best | ||
| 123 | else { | ||
| 124 | return nagios_exit( UNKNOWN, $arg1, $rest ); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | # For backwards compatibility | ||
| 129 | sub die { nagios_die(@_); } | ||
| 130 | |||
| 131 | |||
| 132 | # ------------------------------------------------------------------------ | ||
| 133 | # check_messages - return a status and/or message based on a set of | ||
| 134 | # message arrays. | ||
| 135 | # Returns a nagios status code in scalar context. | ||
| 136 | # Returns a code and a message in list context. | ||
| 137 | # The message is join($join, @array) for the relevant array for the code, | ||
| 138 | # or join($join_all, $message) for all arrays if $join_all is set. | ||
| 139 | sub check_messages { | ||
| 140 | my %arg = validate( @_, { | ||
| 141 | critical => { type => ARRAYREF }, | ||
| 142 | warning => { type => ARRAYREF }, | ||
| 143 | ok => { type => ARRAYREF | SCALAR, optional => 1 }, | ||
| 144 | 'join' => { default => ' ' }, | ||
| 145 | join_all => 0, | ||
| 146 | }); | ||
| 147 | $arg{join} = ' ' unless defined $arg{join}; | ||
| 148 | |||
| 149 | # Decide $code | ||
| 150 | my $code = OK; | ||
| 151 | $code ||= CRITICAL if @{$arg{critical}}; | ||
| 152 | $code ||= WARNING if @{$arg{warning}}; | ||
| 153 | return $code unless wantarray; | ||
| 154 | |||
| 155 | # Compose message | ||
| 156 | my $message = ''; | ||
| 157 | if ($arg{join_all}) { | ||
| 158 | $message = join( $arg{join_all}, | ||
| 159 | map { @$_ ? join( $arg{'join'}, @$_) : () } | ||
| 160 | $arg{critical}, | ||
| 161 | $arg{warning}, | ||
| 162 | $arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : [] | ||
| 163 | ); | ||
| 164 | } | ||
| 165 | |||
| 166 | else { | ||
| 167 | $message ||= join( $arg{'join'}, @{$arg{critical}} ) | ||
| 168 | if $code == CRITICAL; | ||
| 169 | $message ||= join( $arg{'join'}, @{$arg{warning}} ) | ||
| 170 | if $code == WARNING; | ||
| 171 | $message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok} | ||
| 172 | if $arg{ok}; | ||
| 173 | } | ||
| 174 | |||
| 175 | return ($code, $message); | ||
| 176 | } | ||
| 177 | |||
| 178 | # ------------------------------------------------------------------------ | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | # vim:sw=4:sm:et | ||
| 183 | |||
| 184 | __END__ | ||
| 185 | |||
| 186 | =head1 NAME | ||
| 187 | |||
| 188 | Nagios::Plugin::Functions - functions to simplify the creation of | ||
| 189 | Nagios plugins. | ||
| 190 | |||
| 191 | =head1 SYNOPSIS | ||
| 192 | |||
| 193 | # Constants OK, WARNING, CRITICAL, and UNKNOWN exported by default | ||
| 194 | use Nagios::Plugin::Functions; | ||
| 195 | |||
| 196 | # nagios_exit( ODE, $message ) - exit with error code CODE, | ||
| 197 | # and message "PLUGIN CODE - $message" | ||
| 198 | nagios_exit( CRITICAL, $critical_error ) if $critical_error; | ||
| 199 | nagios_exit( WARNING, $warning_error ) if $warning_error; | ||
| 200 | nagios_exit( OK, $result ); | ||
| 201 | |||
| 202 | # nagios_die( $message, [$CODE] ) - just like nagios_exit(), | ||
| 203 | # but CODE is optional, defaulting to UNKNOWN | ||
| 204 | do_something() | ||
| 205 | or nagios_die("do_something() failed horribly"); | ||
| 206 | do_something_critical() | ||
| 207 | or nagios_die("do_something_critical() failed", CRITICAL); | ||
| 208 | |||
| 209 | # check_messages - check a set of message arrays, returning a | ||
| 210 | # CODE and/or a result message | ||
| 211 | $code = check_messages(critical => \@crit, warning => \@warn); | ||
| 212 | ($code, $message) = check_messages( | ||
| 213 | critical => \@crit, warning => \@warn, | ||
| 214 | ok => \@ok ); | ||
| 215 | |||
| 216 | |||
| 217 | =head1 DESCRIPTION | ||
| 218 | |||
| 219 | This module is part of the Nagios::Plugin family, a set of modules | ||
| 220 | for simplifying the creation of Nagios plugins. This module exports | ||
| 221 | convenience functions for the class methods provided by | ||
| 222 | Nagios::Plugin. It is intended for those who prefer a simpler | ||
| 223 | functional interface, and who do not need the additional | ||
| 224 | functionality of Nagios::Plugin. | ||
| 225 | |||
| 226 | =head2 Exports | ||
| 227 | |||
| 228 | Nagios status code constants are exported by default: | ||
| 229 | |||
| 230 | OK | ||
| 231 | WARNING | ||
| 232 | CRITICAL | ||
| 233 | UNKNOWN | ||
| 234 | DEPENDENT | ||
| 235 | |||
| 236 | as are the following functions: | ||
| 237 | |||
| 238 | nagios_exit | ||
| 239 | nagios_die | ||
| 240 | check_messages | ||
| 241 | |||
| 242 | The following variables are exported only on request: | ||
| 243 | |||
| 244 | %ERRORS | ||
| 245 | %STATUS_TEXT | ||
| 246 | |||
| 247 | |||
| 248 | =head2 Functions | ||
| 249 | |||
| 250 | The following functions are supported: | ||
| 251 | |||
| 252 | =over 4 | ||
| 253 | |||
| 254 | =item nagios_exit( CODE, $message ) | ||
| 255 | |||
| 256 | Exit with return code CODE, and a standard nagios message of the | ||
| 257 | form "PLUGIN CODE - $message". | ||
| 258 | |||
| 259 | =item nagios_die( $message, [CODE] ) | ||
| 260 | |||
| 261 | Same as nagios_exit(), except that CODE is optional, defaulting | ||
| 262 | to UNKNOWN. | ||
| 263 | |||
| 264 | =item check_messages( critical => \@crit, warning => \@warn ) | ||
| 265 | |||
| 266 | Convenience function to check a set of message arrays and return | ||
| 267 | an appropriate nagios return code and/or a result message. Returns | ||
| 268 | only a return code in scalar context; returns a return code and an | ||
| 269 | error message in list context i.e. | ||
| 270 | |||
| 271 | # Scalar context | ||
| 272 | $code = check_messages(critical => \@crit, warning => \@warn); | ||
| 273 | # List context | ||
| 274 | ($code, $msg) = check_messages(critical => \@crit, warning => \@warn); | ||
| 275 | |||
| 276 | check_messages() accepts the following named arguments: | ||
| 277 | |||
| 278 | =over 4 | ||
| 279 | |||
| 280 | =item critical => ARRAYREF | ||
| 281 | |||
| 282 | An arrayref of critical error messages - check_messages() returns | ||
| 283 | CRITICAL if this arrayref is non-empty. Mandatory. | ||
| 284 | |||
| 285 | =item warning => ARRAYREF | ||
| 286 | |||
| 287 | An arrayref of warning error messages - check_messages() returns | ||
| 288 | WARNING if this arrayref is non-empty ('critical' is checked | ||
| 289 | first). Mandatory. | ||
| 290 | |||
| 291 | =item ok => ARRAYREF | SCALAR | ||
| 292 | |||
| 293 | An arrayref of informational messages (or a single scalar message), | ||
| 294 | used in list context if both the 'critical' and 'warning' arrayrefs | ||
| 295 | are empty. Optional. | ||
| 296 | |||
| 297 | =item join => SCALAR | ||
| 298 | |||
| 299 | A string used to join the relevant array to generate the message | ||
| 300 | string returned in list context i.e. if the 'critical' array @crit | ||
| 301 | is non-empty, check_messages would return: | ||
| 302 | |||
| 303 | join( $join, @crit ) | ||
| 304 | |||
| 305 | as the result message. Optional; default: ' ' (space). | ||
| 306 | |||
| 307 | =item join_all => SCALAR | ||
| 308 | |||
| 309 | By default, only one set of messages are joined and returned in the | ||
| 310 | result message i.e. if the result is CRITICAL, only the 'critical' | ||
| 311 | messages are included in the result; if WARNING, only the 'warning' | ||
| 312 | messages are included; if OK, the 'ok' messages are included (if | ||
| 313 | supplied) i.e. the default is to return an 'errors-only' type | ||
| 314 | message. | ||
| 315 | |||
| 316 | If join_all is supplied, however, it will be used as a string to | ||
| 317 | join the resultant critical, warning, and ok messages together i.e. | ||
| 318 | all messages are joined and returned. | ||
| 319 | |||
| 320 | =back | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | |||
| 325 | =head1 SEE ALSO | ||
| 326 | |||
| 327 | Nagios::Plugin; the nagios plugin developer guidelines at | ||
| 328 | http://nagiosplug.sourceforge.net/developer-guidelines.html. | ||
| 329 | |||
| 330 | |||
| 331 | =head1 AUTHORS | ||
| 332 | |||
| 333 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
| 334 | |||
| 335 | |||
| 336 | =head1 COPYRIGHT AND LICENSE | ||
| 337 | |||
| 338 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
| 339 | |||
| 340 | This library is free software; you can redistribute it and/or modify | ||
| 341 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
| 342 | at your option, any later version of Perl 5 you may have available. | ||
| 343 | |||
| 344 | =cut | ||
diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm index 7f32c3b..8a9fc1c 100644 --- a/lib/Nagios/Plugin/Getopt.pm +++ b/lib/Nagios/Plugin/Getopt.pm | |||
| @@ -12,9 +12,9 @@ use Carp; | |||
| 12 | use Params::Validate qw(:all); | 12 | use Params::Validate qw(:all); |
| 13 | use base qw(Class::Accessor); | 13 | use base qw(Class::Accessor); |
| 14 | 14 | ||
| 15 | use Nagios::Plugin::Base; | 15 | use Nagios::Plugin::Functions; |
| 16 | use vars qw($VERSION); | 16 | use vars qw($VERSION); |
| 17 | $VERSION = $Nagios::Plugin::Base::VERSION; | 17 | $VERSION = $Nagios::Plugin::Functions::VERSION; |
| 18 | 18 | ||
| 19 | # Standard defaults | 19 | # Standard defaults |
| 20 | my %DEFAULT = ( | 20 | my %DEFAULT = ( |
| @@ -586,7 +586,7 @@ Gavin Carr <gavin@openfusion.com.au> | |||
| 586 | 586 | ||
| 587 | =head1 COPYRIGHT AND LICENSE | 587 | =head1 COPYRIGHT AND LICENSE |
| 588 | 588 | ||
| 589 | Copyright 2005-2006 Gavin Carr. All Rights Reserved. | 589 | Copyright (C) 2006 by the Nagios Plugin Development Team. |
| 590 | 590 | ||
| 591 | This module is free software. It may be used, redistributed | 591 | This module is free software. It may be used, redistributed |
| 592 | and/or modified under either the terms of the Perl Artistic | 592 | and/or modified under either the terms of the Perl Artistic |
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 9e99f54..38803a6 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm | |||
| @@ -7,8 +7,8 @@ use warnings; | |||
| 7 | 7 | ||
| 8 | use Carp; | 8 | use Carp; |
| 9 | use Nagios::Plugin::Threshold; | 9 | use Nagios::Plugin::Threshold; |
| 10 | use Nagios::Plugin::Base; | 10 | use Nagios::Plugin::Functions; |
| 11 | our ($VERSION) = $Nagios::Plugin::Base::VERSION; | 11 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
| 12 | 12 | ||
| 13 | use Class::Struct; | 13 | use Class::Struct; |
| 14 | struct "Nagios::Plugin::Performance" => { | 14 | struct "Nagios::Plugin::Performance" => { |
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index b17cc96..f354a54 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm | |||
| @@ -6,8 +6,8 @@ use strict; | |||
| 6 | use warnings; | 6 | use warnings; |
| 7 | use Carp; | 7 | use Carp; |
| 8 | 8 | ||
| 9 | use Nagios::Plugin::Base; | 9 | use Nagios::Plugin::Functions; |
| 10 | our ($VERSION) = $Nagios::Plugin::Base::VERSION; | 10 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
| 11 | 11 | ||
| 12 | use overload | 12 | use overload |
| 13 | '""' => sub { shift->stringify }; | 13 | '""' => sub { shift->stringify }; |
diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index d7a8177..3e14d82 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm | |||
| @@ -6,8 +6,8 @@ use strict; | |||
| 6 | use warnings; | 6 | use warnings; |
| 7 | 7 | ||
| 8 | use Nagios::Plugin::Range; | 8 | use Nagios::Plugin::Range; |
| 9 | use Nagios::Plugin::Base qw(:codes nagios_die); | 9 | use Nagios::Plugin::Functions qw(:codes nagios_die); |
| 10 | our ($VERSION) = $Nagios::Plugin::Base::VERSION; | 10 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
| 11 | 11 | ||
| 12 | use Class::Struct; | 12 | use Class::Struct; |
| 13 | struct "Nagios::Plugin::Threshold" => { | 13 | struct "Nagios::Plugin::Threshold" => { |
diff --git a/t/Nagios-Plugin-Base.t b/t/Nagios-Plugin-Functions-01.t index 68a02fe..7401945 100644 --- a/t/Nagios-Plugin-Base.t +++ b/t/Nagios-Plugin-Functions-01.t | |||
| @@ -2,17 +2,17 @@ | |||
| 2 | use strict; | 2 | use strict; |
| 3 | use Test::More tests => 111; | 3 | use Test::More tests => 111; |
| 4 | 4 | ||
| 5 | BEGIN { use_ok("Nagios::Plugin::Base", ":all"); } | 5 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); } |
| 6 | Nagios::Plugin::Base::_fake_exit(1); | 6 | Nagios::Plugin::Functions::_fake_exit(1); |
| 7 | 7 | ||
| 8 | my $this_version=$Nagios::Plugin::Base::VERSION; | 8 | my $this_version=$Nagios::Plugin::Functions::VERSION; |
| 9 | foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { | 9 | foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { |
| 10 | my $mod = "Nagios::Plugin$m"; | 10 | my $mod = "Nagios::Plugin$m"; |
| 11 | use_ok($mod); | 11 | use_ok($mod); |
| 12 | # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing | 12 | # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing |
| 13 | my $v = "$mod"."::VERSION"; | 13 | my $v = "$mod"."::VERSION"; |
| 14 | my $a = eval "\$$v"; | 14 | my $a = eval "\$$v"; |
| 15 | is($a, $this_version, "Version number for $mod the same as Base: $this_version"); | 15 | is($a, $this_version, "Version number for $mod the same as Functions: $this_version"); |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | # Hardcoded checks of constants | 18 | # Hardcoded checks of constants |
diff --git a/t/Nagios-Plugin-Functions-02.t b/t/Nagios-Plugin-Functions-02.t new file mode 100644 index 0000000..981e2eb --- /dev/null +++ b/t/Nagios-Plugin-Functions-02.t | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | # check_messages tests | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Test::More tests => 33; | ||
| 5 | |||
| 6 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all") } | ||
| 7 | |||
| 8 | my ($code, $message); | ||
| 9 | |||
| 10 | # ------------------------------------------------------------------------- | ||
| 11 | # Check codes | ||
| 12 | my @codes = ( | ||
| 13 | [ [ qw(Critical) ], [ qw(Warning) ], CRITICAL ], | ||
| 14 | [ [], [ qw(Warning) ], WARNING ], | ||
| 15 | [ [], [], OK ], | ||
| 16 | ); | ||
| 17 | my $i = 0; | ||
| 18 | for (@codes) { | ||
| 19 | $i++; | ||
| 20 | $code = check_messages( critical => $_->[0], warning => $_->[1] ); | ||
| 21 | is($code, $_->[2], "Code test $i returned $STATUS_TEXT{$_->[2]}"); | ||
| 22 | } | ||
| 23 | |||
| 24 | # ------------------------------------------------------------------------- | ||
| 25 | # Check messages | ||
| 26 | my %arrays = ( | ||
| 27 | critical => [ qw(A B C) ], | ||
| 28 | warning => [ qw(D E F) ], | ||
| 29 | ok => [ qw(G H I) ], | ||
| 30 | ); | ||
| 31 | my %messages = map { $_ => join(' ', @{$arrays{$_}}) } keys %arrays; | ||
| 32 | |||
| 33 | # critical, warning | ||
| 34 | ($code, $message) = check_messages( | ||
| 35 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
| 36 | ); | ||
| 37 | is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); | ||
| 38 | is($message, $messages{critical}, "(critical, warning) message is $message"); | ||
| 39 | |||
| 40 | # critical, warning, ok | ||
| 41 | ($code, $message) = check_messages( | ||
| 42 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
| 43 | ok => $arrays{ok}, | ||
| 44 | ); | ||
| 45 | is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); | ||
| 46 | is($message, $messages{critical}, "(critical, warning, ok) message is $message"); | ||
| 47 | |||
| 48 | # critical, warning, $ok | ||
| 49 | ($code, $message) = check_messages( | ||
| 50 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
| 51 | ok => 'G H I', | ||
| 52 | ); | ||
| 53 | is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); | ||
| 54 | is($message, $messages{critical}, "(critical, warning, \$ok) message is $message"); | ||
| 55 | |||
| 56 | # warning | ||
| 57 | ($code, $message) = check_messages( | ||
| 58 | critical => [], warning => $arrays{warning}, | ||
| 59 | ); | ||
| 60 | is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); | ||
| 61 | is($message, $messages{warning}, "(warning) message is $message"); | ||
| 62 | |||
| 63 | # warning, ok | ||
| 64 | ($code, $message) = check_messages( | ||
| 65 | critical => [], warning => $arrays{warning}, ok => $arrays{ok}, | ||
| 66 | ); | ||
| 67 | is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); | ||
| 68 | is($message, $messages{warning}, "(warning, ok) message is $message"); | ||
| 69 | |||
| 70 | # ok | ||
| 71 | ($code, $message) = check_messages( | ||
| 72 | critical => [], warning => [], ok => $arrays{ok}, | ||
| 73 | ); | ||
| 74 | is($code, OK, "(ok) code is $STATUS_TEXT{$code}"); | ||
| 75 | is($message, $messages{ok}, "(ok) message is $message"); | ||
| 76 | |||
| 77 | # $ok | ||
| 78 | ($code, $message) = check_messages( | ||
| 79 | critical => [], warning => [], ok => 'G H I', | ||
| 80 | ); | ||
| 81 | is($code, OK, "(\$ok) code is $STATUS_TEXT{$code}"); | ||
| 82 | is($message, $messages{ok}, "(\$ok) message is $message"); | ||
| 83 | |||
| 84 | # ------------------------------------------------------------------------- | ||
| 85 | # explicit join | ||
| 86 | my $join = '+'; | ||
| 87 | ($code, $message) = check_messages( | ||
| 88 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
| 89 | join => $join, | ||
| 90 | ); | ||
| 91 | is($message, join($join, @{$arrays{critical}}), "joined '$join' (critical, warning) message is $message"); | ||
| 92 | $join = ''; | ||
| 93 | ($code, $message) = check_messages( | ||
| 94 | critical => [], warning => $arrays{warning}, | ||
| 95 | join => $join, | ||
| 96 | ); | ||
| 97 | is($message, join($join, @{$arrays{warning}}), "joined '$join' (warning) message is $message"); | ||
| 98 | $join = undef; | ||
| 99 | ($code, $message) = check_messages( | ||
| 100 | critical => [], warning => [], ok => $arrays{ok}, | ||
| 101 | join => $join, | ||
| 102 | ); | ||
| 103 | is($message, join(' ', @{$arrays{ok}}), "joined undef (ok) message is $message"); | ||
| 104 | |||
| 105 | # ------------------------------------------------------------------------- | ||
| 106 | # join_all messages | ||
| 107 | my $join_all = ' :: '; | ||
| 108 | my $msg_all_cwo = join($join_all, map { join(' ', @{$arrays{$_}}) } | ||
| 109 | qw(critical warning ok)); | ||
| 110 | my $msg_all_cw = join($join_all, map { join(' ', @{$arrays{$_}}) } | ||
| 111 | qw(critical warning)); | ||
| 112 | my $msg_all_wo = join($join_all, map { join(' ', @{$arrays{$_}}) } | ||
| 113 | qw(warning ok)); | ||
| 114 | |||
| 115 | # critical, warning, ok | ||
| 116 | ($code, $message) = check_messages( | ||
| 117 | critical => $arrays{critical}, warning => $arrays{warning}, ok => $arrays{ok}, | ||
| 118 | join_all => $join_all, | ||
| 119 | ); | ||
| 120 | is($code, CRITICAL, "(critical, warning, ok) code is $STATUS_TEXT{$code}"); | ||
| 121 | is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, ok) message is $message"); | ||
| 122 | |||
| 123 | # critical, warning, $ok | ||
| 124 | ($code, $message) = check_messages( | ||
| 125 | critical => $arrays{critical}, warning => $arrays{warning}, ok => 'G H I', | ||
| 126 | join_all => $join_all, | ||
| 127 | ); | ||
| 128 | is($code, CRITICAL, "(critical, warning, \$ok) code is $STATUS_TEXT{$code}"); | ||
| 129 | is($message, $msg_all_cwo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); | ||
| 130 | |||
| 131 | # critical, warning | ||
| 132 | ($code, $message) = check_messages( | ||
| 133 | critical => $arrays{critical}, warning => $arrays{warning}, | ||
| 134 | join_all => $join_all, | ||
| 135 | ); | ||
| 136 | is($code, CRITICAL, "(critical, warning) code is $STATUS_TEXT{$code}"); | ||
| 137 | is($message, $msg_all_cw, "join_all '$join_all' (critical, warning) message is $message"); | ||
| 138 | |||
| 139 | # warning, ok | ||
| 140 | ($code, $message) = check_messages( | ||
| 141 | critical => [], warning => $arrays{warning}, ok => $arrays{ok}, | ||
| 142 | join_all => $join_all, | ||
| 143 | ); | ||
| 144 | is($code, WARNING, "(warning, ok) code is $STATUS_TEXT{$code}"); | ||
| 145 | is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, ok) message is $message"); | ||
| 146 | |||
| 147 | # warning, $ok | ||
| 148 | ($code, $message) = check_messages( | ||
| 149 | critical => [], warning => $arrays{warning}, ok => 'G H I', | ||
| 150 | join_all => $join_all, | ||
| 151 | ); | ||
| 152 | is($code, WARNING, "(warning, \$ok) code is $STATUS_TEXT{$code}"); | ||
| 153 | is($message, $msg_all_wo, "join_all '$join_all' (critical, warning, \$ok) message is $message"); | ||
| 154 | |||
| 155 | # warning | ||
| 156 | ($code, $message) = check_messages( | ||
| 157 | critical => [], warning => $arrays{warning}, | ||
| 158 | join_all => $join_all, | ||
| 159 | ); | ||
| 160 | is($code, WARNING, "(warning) code is $STATUS_TEXT{$code}"); | ||
| 161 | is($message, 'D E F', "join_all '$join_all' (critical, warning) message is $message"); | ||
| 162 | |||
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index 1da3191..e0eb2f6 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t | |||
| @@ -3,10 +3,10 @@ use strict; | |||
| 3 | use Test::More tests => 49; | 3 | use Test::More tests => 49; |
| 4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; | 4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; |
| 5 | 5 | ||
| 6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; | 6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; |
| 7 | 7 | ||
| 8 | use Nagios::Plugin::Base; | 8 | use Nagios::Plugin::Functions; |
| 9 | Nagios::Plugin::Base::_fake_exit(1); | 9 | Nagios::Plugin::Functions::_fake_exit(1); |
| 10 | 10 | ||
| 11 | my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); | 11 | my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); |
| 12 | cmp_ok( $p[0]->label, 'eq', "/", "label okay"); | 12 | cmp_ok( $p[0]->label, 'eq', "/", "label okay"); |
diff --git a/t/Nagios-Plugin-Range.t b/t/Nagios-Plugin-Range.t index f01518d..df5dbd5 100644 --- a/t/Nagios-Plugin-Range.t +++ b/t/Nagios-Plugin-Range.t | |||
| @@ -2,13 +2,17 @@ | |||
| 2 | use strict; | 2 | use strict; |
| 3 | use Test::More qw(no_plan); #tests => 123; | 3 | use Test::More qw(no_plan); #tests => 123; |
| 4 | 4 | ||
| 5 | BEGIN { use_ok('Nagios::Plugin::Range') }; | 5 | BEGIN { |
| 6 | use_ok('Nagios::Plugin::Range'); | ||
| 7 | # Silence warnings unless TEST_VERBOSE is set | ||
| 8 | $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; | ||
| 9 | }; | ||
| 6 | 10 | ||
| 7 | diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n"; | 11 | diag "\nusing Nagios::Plugin::Range revision ". $Nagios::Plugin::Range::VERSION . "\n" if $ENV{TEST_VERBOSE}; |
| 8 | 12 | ||
| 9 | my $r; | 13 | my $r; |
| 10 | 14 | ||
| 11 | diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:"; | 15 | diag "'garbage in' checks -- you should see 7 invalid range definition warnings here:" if $ENV{TEST_VERBOSE}; |
| 12 | 16 | ||
| 13 | foreach (qw( | 17 | foreach (qw( |
| 14 | : | 18 | : |
diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index 677c054..cdb8d77 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t | |||
| @@ -4,7 +4,7 @@ use Test::More tests => 71; | |||
| 4 | #use Test::Exception; # broken for now so we don't need this. | 4 | #use Test::Exception; # broken for now so we don't need this. |
| 5 | BEGIN { | 5 | BEGIN { |
| 6 | use_ok('Nagios::Plugin::Threshold'); | 6 | use_ok('Nagios::Plugin::Threshold'); |
| 7 | use_ok('Nagios::Plugin::Base', ':all' ); | 7 | use_ok('Nagios::Plugin::Functions', ':all' ); |
| 8 | # Silence warnings unless TEST_VERBOSE is set | 8 | # Silence warnings unless TEST_VERBOSE is set |
| 9 | $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; | 9 | $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; |
| 10 | } | 10 | } |
| @@ -12,7 +12,7 @@ BEGIN { | |||
| 12 | diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n" | 12 | diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n" |
| 13 | if $ENV{TEST_VERBOSE}; | 13 | if $ENV{TEST_VERBOSE}; |
| 14 | 14 | ||
| 15 | Nagios::Plugin::Base::_fake_exit(1); | 15 | Nagios::Plugin::Functions::_fake_exit(1); |
| 16 | 16 | ||
| 17 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; | 17 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; |
| 18 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); | 18 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); |
| @@ -96,8 +96,8 @@ test_expected_statuses( $t, $expected ); | |||
| 96 | goto SKIP_DEATH; | 96 | goto SKIP_DEATH; |
| 97 | diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; | 97 | diag "threshold: test pure crap for arguments - default to OK." if $ENV{TEST_VERBOSE}; |
| 98 | diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; | 98 | diag "you should see one invalid range definition warning and an UNKNOWN line here:\n"; |
| 99 | Nagios::Plugin::Base->print_on_die(1); | 99 | Nagios::Plugin::Functions->print_on_die(1); |
| 100 | Nagios::Plugin::Base->exit_on_die(1); | 100 | Nagios::Plugin::Functions->exit_on_die(1); |
| 101 | 101 | ||
| 102 | dies_ok( sub { | 102 | dies_ok( sub { |
| 103 | $t = Nagios::Plugin::Threshold->set_thresholds( | 103 | $t = Nagios::Plugin::Threshold->set_thresholds( |
| @@ -106,8 +106,8 @@ dies_ok( sub { | |||
| 106 | ) | 106 | ) |
| 107 | }, "bad thresholds cause death" | 107 | }, "bad thresholds cause death" |
| 108 | ); | 108 | ); |
| 109 | Nagios::Plugin::Base->print_on_die(0); | 109 | Nagios::Plugin::Functions->print_on_die(0); |
| 110 | Nagios::Plugin::Base->exit_on_die(0); | 110 | Nagios::Plugin::Functions->exit_on_die(0); |
| 111 | SKIP_DEATH: | 111 | SKIP_DEATH: |
| 112 | 112 | ||
| 113 | 113 | ||
diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t index 213e514..0ae2113 100644 --- a/t/Nagios-Plugin.t +++ b/t/Nagios-Plugin.t | |||
| @@ -4,10 +4,11 @@ use Test::More tests => 12; | |||
| 4 | 4 | ||
| 5 | BEGIN { use_ok('Nagios::Plugin') }; | 5 | BEGIN { use_ok('Nagios::Plugin') }; |
| 6 | 6 | ||
| 7 | use Nagios::Plugin::Base; | 7 | use Nagios::Plugin::Functions; |
| 8 | Nagios::Plugin::Base::_fake_exit(1); | 8 | Nagios::Plugin::Functions::_fake_exit(1); |
| 9 | 9 | ||
| 10 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; | 10 | diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n" |
| 11 | if $ENV{TEST_VERBOSE}; | ||
| 11 | 12 | ||
| 12 | my $p = Nagios::Plugin->new; | 13 | my $p = Nagios::Plugin->new; |
| 13 | isa_ok( $p, "Nagios::Plugin"); | 14 | isa_ok( $p, "Nagios::Plugin"); |
