From 1f8410dd1914449ce52ffa8fd47a308c5b372e52 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Mon, 11 Sep 2006 01:57:26 +0000 Subject: Add constants, nagios_exit, and nagios_die to Nagios::Plugin::Base. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1477 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/MANIFEST b/MANIFEST index 61f396b..6b5de1b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,9 @@ README t/check_stuff.pl t/check_stuff.t t/Nagios-Plugin.t +t/Nagios-Plugin-Base.t +t/Nagios-Plugin-Getopt-01.t +t/Nagios-Plugin-Getopt-02.t t/Nagios-Plugin-Performance.t t/Nagios-Plugin-Range.t t/Nagios-Plugin-Threshold.t @@ -14,4 +17,5 @@ lib/Nagios/Plugin/Range.pm lib/Nagios/Plugin/Threshold.pm lib/Nagios/Plugin/Base.pm lib/Nagios/Plugin/Getopt.pm +lib/Nagios/Plugin/ExitResult.pm META.yml Module meta-data (added by MakeMaker) diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 0915571..88c15c5 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -43,8 +43,7 @@ sub shortname { shift; @_ ? $shortname = shift : $shortname } sub die { my $self = shift; - my %args = @_; - Nagios::Plugin::Base->die(\%args, $self); + Nagios::Plugin::Base::die(@_, { plugin => $self }); } 1; diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm index c2e9902..92651ed 100644 --- a/lib/Nagios/Plugin/Base.pm +++ b/lib/Nagios/Plugin/Base.pm @@ -4,24 +4,139 @@ package Nagios::Plugin::Base; use strict; use warnings; +use File::Basename; our $VERSION = "0.13"; -use Exporter; -our @ISA = qw(Exporter); -our @EXPORT = qw(%ERRORS); +our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); -our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = (@STATUS_CODES, qw(nagios_exit %ERRORS)); +our @EXPORT_OK = qw(nagios_die %STATUS_TEXT); +our %EXPORT_TAGS = ( + all => [ @EXPORT, @EXPORT_OK ], + codes => [ @STATUS_CODES ], + functions => [ qw(nagios_exit nagios_die) ], +); + +use constant OK => 0; +use constant WARNING => 1; +use constant CRITICAL => 2; +use constant UNKNOWN => 3; +use constant DEPENDENT => 4; + +our %ERRORS = ( + 'OK' => OK, + 'WARNING' => WARNING, + 'CRITICAL' => CRITICAL, + 'UNKNOWN' => UNKNOWN, + 'DEPENDENT' => DEPENDENT, +); our %STATUS_TEXT = reverse %ERRORS; +# _fake_exit flag and accessor/mutator, for testing +my $_fake_exit = 0; +sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; + +sub get_shortname { + my %arg = @_; + + return $arg{plugin}->shortname if $arg{plugin}; + + my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0); + $shortname =~ s/^CHECK_//; + return $shortname; +} + +# nagios_exit( $code, $message ) +sub nagios_exit { + my ($code, $message, $arg) = @_; + + # Handle named parameters + if (defined $code && ($code eq 'return_code' || $code eq 'message')) { + # Remove last argument if odd no and last is ref + if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { + $arg = pop @_; + } else { + undef $arg; + } + my %arg = @_; + $code = $arg{return_code}; + $message = $arg{message}; + } + $arg ||= {}; + + # Handle string codes + $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; + + # Set defaults + $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; + $message = '' unless defined $message; + $message = join(' ', @$message) if ref $message eq 'ARRAY'; + + # Setup output + my $output = "$STATUS_TEXT{$code}"; + $output .= " - $message" if defined $message && $message ne ''; + my $shortname = get_shortname(plugin => $arg->{plugin}); + $output = "$shortname $output" if $shortname; + if ($arg->{plugin}) { + my $plugin = $arg->{plugin}; + $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata; + } + $output .= "\n"; + + # Don't actually exit if _fake_exit set + if ($_fake_exit) { + require Nagios::Plugin::ExitResult; + return Nagios::Plugin::ExitResult->new($code, $output); + } + + # Print output and exit + print $output; + exit $code; +} + +# nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) +# Default $code: UNKNOWN +sub nagios_die { + my ($arg1, $arg2, $rest) = @_; + + # Named parameters + if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { + return nagios_exit(@_); + } + + # ($code, $message) + elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { + return nagios_exit(@_); + } + + # ($message, $code) + elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { + return nagios_exit($arg2, $arg1, $rest); + } + + # Else just assume $arg1 is the message and hope for the best + else { + return nagios_exit( UNKNOWN, $arg1, $rest ); + } +} + +# For backwards compatibility +sub die { nagios_die(@_); } + + +=pod old my $exit_on_die = 1; sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; my $print_on_die = 1; sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; -sub die { +# Old version - TODO: remove +sub old_die { my ($class, $args, $plugin) = @_; my $return_code; @@ -49,7 +164,12 @@ sub die { } } +=cut + 1; + +# vim:sw=4:sm:et + __END__ =head1 NAME @@ -73,5 +193,4 @@ This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available. - =cut diff --git a/lib/Nagios/Plugin/Getopt.pm b/lib/Nagios/Plugin/Getopt.pm index d38dced..7f32c3b 100644 --- a/lib/Nagios/Plugin/Getopt.pm +++ b/lib/Nagios/Plugin/Getopt.pm @@ -5,7 +5,6 @@ package Nagios::Plugin::Getopt; -use 5.005003; use strict; use File::Basename; use Getopt::Long qw(:config no_ignore_case bundling); @@ -21,7 +20,7 @@ $VERSION = $Nagios::Plugin::Base::VERSION; my %DEFAULT = ( timeout => 15, verbose => 0, - licence => + license => "This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. It may be used, redistributed and/or modified under the terms of the GNU General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).", @@ -265,7 +264,7 @@ sub _init plugin => { default => $plugin }, blurb => 0, extra => 0, - license => { default => $DEFAULT{licence} }, + license => { default => $DEFAULT{license} }, timeout => { default => $DEFAULT{timeout} }, }); @@ -400,7 +399,7 @@ Short plugin description, included in the longer --help output License text, included in the longer --help output (see below for an example). By default, this is set to the standard nagios plugins -GPL licence text: +GPL license text: This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY. It may be used, redistributed and/or modified under the terms of the GNU diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 9e7b938..d7a8177 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -6,7 +6,7 @@ use strict; use warnings; use Nagios::Plugin::Range; -use Nagios::Plugin::Base; +use Nagios::Plugin::Base qw(:codes nagios_die); our ($VERSION) = $Nagios::Plugin::Base::VERSION; use Class::Struct; @@ -23,10 +23,7 @@ sub set_thresholds { if (defined $r) { $t->warning($r); } else { - Nagios::Plugin::Base->die( { - return_code => $ERRORS{UNKNOWN}, - message => "Warning range incorrect: '$args{warning}'" - } ); + nagios_die( "Warning range incorrect: '$args{warning}'" ); } } if (defined $args{critical}) { @@ -34,10 +31,7 @@ sub set_thresholds { if (defined $r) { $t->critical($r); } else { - Nagios::Plugin::Base->die( { - return_code => $ERRORS{UNKNOWN}, - message => "Critical range incorrect: '$args{critical}'" - } ); + nagios_die( "Critical range incorrect: '$args{critical}'" ); } } return $t; @@ -48,15 +42,15 @@ sub get_status { if ($self->critical->is_set) { if ($self->critical->check_range($value) == 1) { - return $ERRORS{CRITICAL}; + return CRITICAL; } } if ($self->warning->is_set) { if ($self->warning->check_range($value) == 1) { - return $ERRORS{WARNING}; + return WARNING; } } - return $ERRORS{OK}; + return OK; } 1; @@ -81,7 +75,7 @@ Returns the warning or critical range as a Nagios::Plugin::Range object. =item get_status($value) -Given a value, will see if the value breeches the critical or the warning range. Returns the status code. +Given a value, will see if the value breaches the critical or the warning range. Returns the status code. =back diff --git a/t/Nagios-Plugin-Base.t b/t/Nagios-Plugin-Base.t index 589f331..68a02fe 100644 --- a/t/Nagios-Plugin-Base.t +++ b/t/Nagios-Plugin-Base.t @@ -1,8 +1,10 @@ use strict; -use Test::More tests => 11; +use Test::More tests => 111; + +BEGIN { use_ok("Nagios::Plugin::Base", ":all"); } +Nagios::Plugin::Base::_fake_exit(1); -use_ok("Nagios::Plugin::Base"); my $this_version=$Nagios::Plugin::Base::VERSION; foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { my $mod = "Nagios::Plugin$m"; @@ -12,3 +14,140 @@ foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { my $a = eval "\$$v"; is($a, $this_version, "Version number for $mod the same as Base: $this_version"); } + +# Hardcoded checks of constants +ok(defined %ERRORS, '%ERRORS defined'); +is(OK, $ERRORS{OK}, "OK => $ERRORS{OK}"); +is(WARNING, $ERRORS{WARNING}, "WARNING => $ERRORS{WARNING}"); +is(CRITICAL, $ERRORS{CRITICAL}, "CRITICAL => $ERRORS{CRITICAL}"); +is(UNKNOWN, $ERRORS{UNKNOWN}, "UNKNOWN => $ERRORS{UNKNOWN}"); +is(DEPENDENT, $ERRORS{DEPENDENT}, "DEPENDENT => $ERRORS{DEPENDENT}"); + +# Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg ) +my $r; +my @ok = ( + [ OK, "OK", 'test the first', ], + [ WARNING, "WARNING", 'test the second', ], + [ CRITICAL, "CRITICAL", 'test the third', ], + [ UNKNOWN, "UNKNOWN", 'test the fourth', ], + [ DEPENDENT, "DEPENDENT", 'test the fifth', ], +); +for (@ok) { + # CONSTANT + $r = nagios_exit($_->[0], $_->[2]); + is($r->return_code, $_->[0], + sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_exit(%s, $msg) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $string + $r = nagios_exit($_->[1], $_->[2]); + is($r->return_code, $_->[0], + sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); +} + +# nagios_exit code corner cases +my @ugly1 = ( + [ -1, 'testing code -1' ], + [ 7, 'testing code 7' ], + [ undef, 'testing code undef' ], + [ '', qq(testing code '') ], + [ 'string', qq(testing code 'string') ], +); +for (@ugly1) { + $r = nagios_exit($_->[0], $_->[1]); + my $display = defined $_->[0] ? "'$_->[0]'" : 'undef'; + is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN); + like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, + sprintf('nagios_exit(%s, $msg) output matched "%s"', + $display, 'UNKNOWN.*' . $_->[1])); +} + +# nagios_exit message corner cases +my @ugly2 = ( + [ '' ], + [ undef ], + [ UNKNOWN ], +); +for (@ugly2) { + $r = nagios_exit(CRITICAL, $_->[0]); + my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; + my $display2 = defined $_->[0] ? $_->[0] : ''; + like($r->message, qr/CRITICAL\b.*\b$display2$/, + sprintf('nagios_exit(%s, $msg) output matched "%s"', + $display1, "CRITICAL.*$display2")); +} + +# Test nagios_die( $msg ) +my @msg = ( + [ 'die you dog' ], + [ '' ], + [ undef ], +); +for (@msg) { + $r = nagios_die($_->[0]); + my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef"; + my $display2 = defined $_->[0] ? $_->[0] : ''; + is($r->return_code, UNKNOWN, + sprintf('nagios_die(%s) returned UNKNOWN', $display1)); + like($r->message, qr/UNKNOWN\b.*\b$display2$/, + sprintf('nagios_die(%s) output matched "%s"', $display1, + "UNKNOWN.*$display2")); +} + +# Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), +# nagios_die( $string, $msg ), and nagios_die( $msg, $string ) +@ok = ( + [ OK, "OK", 'test the first', ], + [ WARNING, "WARNING", 'test the second', ], + [ CRITICAL, "CRITICAL", 'test the third', ], + [ UNKNOWN, "UNKNOWN", 'test the fourth', ], + [ DEPENDENT, "DEPENDENT", 'test the fifth', ], +); +for (@ok) { + # CONSTANT, $msg + $r = nagios_die($_->[0], $_->[2]); + is($r->return_code, $_->[0], + sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_die(%s, $msg) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $msg, CONSTANT + $r = nagios_die($_->[2], $_->[0]); + is($r->return_code, $_->[0], + sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_die($msg, %s) output matched "%s"', + $_->[1], $_->[1] . '.*' . $_->[2])); + + # $string, $msg + $r = nagios_die($_->[1], $_->[2]); + is($r->return_code, $_->[0], + sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + + # $string, $msg + $r = nagios_die($_->[2], $_->[1]); + is($r->return_code, $_->[0], + sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0])); + like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); + like($r, qr/$_->[1]\b.*\b$_->[2]$/, + sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], + $_->[1] . '.*' . $_->[2])); +} + diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index 0f52de1..1da3191 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t @@ -6,7 +6,7 @@ BEGIN { use_ok('Nagios::Plugin::Performance') }; diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n"; use Nagios::Plugin::Base; -Nagios::Plugin::Base->exit_on_die(0); +Nagios::Plugin::Base::_fake_exit(1); my @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); cmp_ok( $p[0]->label, 'eq', "/", "label okay"); diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index bb8b578..677c054 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t @@ -2,13 +2,17 @@ use strict; use Test::More tests => 71; #use Test::Exception; # broken for now so we don't need this. -BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Base') }; +BEGIN { + use_ok('Nagios::Plugin::Threshold'); + use_ok('Nagios::Plugin::Base', ':all' ); + # Silence warnings unless TEST_VERBOSE is set + $SIG{__WARN__} = sub { warn $_[0] if $ENV{TEST_VERBOSE} }; +} -diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n"; +diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::VERSION . "\n" + if $ENV{TEST_VERBOSE}; -Nagios::Plugin::Base->exit_on_die(0); -Nagios::Plugin::Base->print_on_die(0); -my %STATUS_TEXT = reverse %ERRORS; +Nagios::Plugin::Base::_fake_exit(1); diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); @@ -45,7 +49,7 @@ sub test_expected_statuses { test_expected_statuses( $t, $expected ); diag "threshold: warn if less than 5 or more than 33." if $ENV{TEST_VERBOSE}; -$t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => ""); +eval { $t = Nagios::Plugin::Threshold->set_thresholds(warning => "5:33", critical => "") }; ok( defined $t, "Threshold ('5:33', '') set"); cmp_ok( $t->warning->start, '==', 5, "Warning start set"); cmp_ok( $t->warning->end, '==', 33, "Warning end set"); diff --git a/t/Nagios-Plugin.t b/t/Nagios-Plugin.t index ed25aca..8921dc5 100644 --- a/t/Nagios-Plugin.t +++ b/t/Nagios-Plugin.t @@ -5,8 +5,7 @@ use Test::More tests => 9; BEGIN { use_ok('Nagios::Plugin') }; use Nagios::Plugin::Base; -Nagios::Plugin::Base->exit_on_die(0); -Nagios::Plugin::Base->print_on_die(0); +Nagios::Plugin::Base::_fake_exit(1); diag "\nusing Nagios::Plugin revision ". $Nagios::Plugin::VERSION . "\n"; -- cgit v0.10-9-g596f