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 --- lib/Nagios/Plugin.pm | 3 +- lib/Nagios/Plugin/Base.pm | 131 +++++++++++++++++++++++++++++++++++++++-- lib/Nagios/Plugin/Getopt.pm | 7 +-- lib/Nagios/Plugin/Threshold.pm | 20 +++---- 4 files changed, 136 insertions(+), 25 deletions(-) (limited to 'lib/Nagios') 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 -- cgit v1.2.3-74-g34f1