[Nagiosplug-checkins] Nagios-Plugin/lib/Nagios/Plugin Base.pm, 1.3, 1.4 Getopt.pm, 1.4, 1.5 Threshold.pm, 1.5, 1.6
Gavin Carr
gonzai at users.sourceforge.net
Mon Sep 11 03:57:28 CEST 2006
Update of /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv17327/lib/Nagios/Plugin
Modified Files:
Base.pm Getopt.pm Threshold.pm
Log Message:
Add constants, nagios_exit, and nagios_die to Nagios::Plugin::Base.
Index: Base.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Base.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- Base.pm 31 Aug 2006 08:19:01 -0000 1.3
+++ Base.pm 11 Sep 2006 01:57:26 -0000 1.4
@@ -4,24 +4,139 @@
use strict;
use warnings;
+use File::Basename;
our $VERSION = "0.13";
-use Exporter;
+our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
+
+require Exporter;
our @ISA = qw(Exporter);
-our @EXPORT = qw(%ERRORS);
+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) ],
+);
-our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
+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 @@
}
}
+=cut
+
1;
+
+# vim:sw=4:sm:et
+
__END__
=head1 NAME
@@ -73,5 +193,4 @@
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
Index: Threshold.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Threshold.pm,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- Threshold.pm 7 Sep 2006 00:53:51 -0000 1.5
+++ Threshold.pm 11 Sep 2006 01:57:26 -0000 1.6
@@ -6,7 +6,7 @@
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 @@
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 @@
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 @@
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 @@
=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
Index: Getopt.pm
===================================================================
RCS file: /cvsroot/nagiosplug/Nagios-Plugin/lib/Nagios/Plugin/Getopt.pm,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Getopt.pm 7 Sep 2006 00:53:51 -0000 1.4
+++ Getopt.pm 11 Sep 2006 01:57:26 -0000 1.5
@@ -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 @@
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 @@
plugin => { default => $plugin },
blurb => 0,
extra => 0,
- license => { default => $DEFAULT{licence} },
+ license => { default => $DEFAULT{license} },
timeout => { default => $DEFAULT{timeout} },
});
@@ -400,7 +399,7 @@
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
More information about the Commits
mailing list