From 96933fd2e1f53aff9c9ef26639fafe9a84ec754e Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Fri, 4 Aug 2006 20:22:31 +0000 Subject: Lots of extra tests and subsequent fixes (Nathan Vonnahme) git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1466 f882894a-f735-0410-b71e-b25c423dba1c --- lib/Nagios/Plugin.pm | 9 +++++--- lib/Nagios/Plugin/Base.pm | 45 ++++++++++++++++++++++++++-------------- lib/Nagios/Plugin/Performance.pm | 3 +++ lib/Nagios/Plugin/Range.pm | 44 +++++++++++++++++++++++++-------------- lib/Nagios/Plugin/Threshold.pm | 4 ++++ 5 files changed, 71 insertions(+), 34 deletions(-) (limited to 'lib') diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index 2acc6ea..4e95e98 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -23,7 +23,7 @@ use Exporter; our @ISA = qw(Exporter Nagios::__::Plugin); our @EXPORT_OK = qw(%ERRORS); -our $VERSION = '0.12'; +our $VERSION = '0.13'; sub add_perfdata { my ($self, %args) = @_; @@ -59,7 +59,8 @@ Nagios::Plugin - Object oriented helper routines for your Nagios plugin use Nagios::Plugin qw(%ERRORS); $p = Nagios::Plugin->new( shortname => "PAGESIZE" ); - $threshold = $p->set_thresholds( warning => "10:25", critical => "25:" ); + $threshold = $p->set_thresholds( warning => "10:25", critical => "~:25" ); + # Critical if outside -INF to 25, ie > 25. Warn if outside 10-25, ie < 10 # ... collect current metric into $value if ($trouble_getting_metric) { @@ -120,7 +121,7 @@ Initializes a new Nagios::Plugin object. Can specify the shortname here. =head1 OBJECT METHODS -=item set_thresholds( warning => "10:25", critical => "25:" ) +=item set_thresholds( warning => "10:25", critical => "~:25" ) Sets the thresholds, based on the range specification at http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT. @@ -148,6 +149,8 @@ http://nagiosplug.sourceforge.net Ton Voon, Eton.voon@altinity.comE +Thanks to Nathan Vonnahme for loads of extra tests and subsequent fixes. + =head1 COPYRIGHT AND LICENSE Copyright (C) 2006 by Nagios Plugin Development Team diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm index f3a7f7c..d1678a5 100644 --- a/lib/Nagios/Plugin/Base.pm +++ b/lib/Nagios/Plugin/Base.pm @@ -5,6 +5,9 @@ package Nagios::Plugin::Base; use strict; use warnings; +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; + use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(%ERRORS); @@ -13,28 +16,38 @@ our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); our %STATUS_TEXT = reverse %ERRORS; + 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 { - my ($class, $args, $plugin) = @_; - my $return_code = $args->{return_code} || 3; - my $message = $args->{message} || "Internal error"; - my $output = join(" ", $STATUS_TEXT{$return_code}, $message); - if ($plugin) { - $output = $plugin->shortname." $output" if $plugin->shortname; - $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; - } - if ($print_on_die) { - print $output, $/; - } - if ($exit_on_die) { - exit $return_code; - } else { - return $output; - } + my ($class, $args, $plugin) = @_; + my $return_code; + + if ( exists $args->{return_code} + && exists $STATUS_TEXT{$args->{return_code}} + ) { + $return_code = $args->{return_code}; + } + else { + $return_code = $ERRORS{UNKNOWN}; + } + my $message = $args->{message} || "Internal error"; + my $output = join(" ", $STATUS_TEXT{$return_code}, $message); + if ($plugin) { + $output = $plugin->shortname." $output" if $plugin->shortname; + $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; + } + if ($print_on_die) { + print $output, $/; + } + if ($exit_on_die) { + exit $return_code; + } else { + return $output; + } } 1; diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index 45109ae..2af22bd 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -7,6 +7,9 @@ use warnings; use Carp; use Nagios::Plugin::Threshold; +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; + use Class::Struct; struct "Nagios::Plugin::Performance" => { label => '$', diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index c03001a..c0c47e4 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm @@ -4,6 +4,10 @@ use 5.008004; use strict; use warnings; +use Carp; + +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; use overload '""' => sub { shift->stringify }; @@ -17,8 +21,8 @@ struct "Nagios::Plugin::Range" => { alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set }; -my $outside = 0; -my $inside = 1; +use constant OUTSIDE => 0; +use constant INSIDE => 1; sub stringify { my $self = shift; @@ -49,22 +53,32 @@ sub set_range_end { sub parse_range_string { my ($class, $string) = @_; my $valid = 0; - my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => $outside); + my $range = $class->new( start => 0, start_infinity => 0, end => 0, end_infinity => 1, alert_on => OUTSIDE); + + $string =~ s/\s//g; # strip out any whitespace + # check for valid range definition + unless ( $string =~ /[\d~]/ && $string =~ m/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) { + carp "invalid range definition '$string'"; + return undef; + } if ($string =~ s/^\@//) { - $range->alert_on($inside); + $range->alert_on(INSIDE); } - if ($string =~ s/^~//) { - $range->start_infinity(1); + + if ($string =~ s/^~//) { # '~:x' + $range->start_infinity(1); } - if (($_) = $string =~ /^([-\d\.]+)?:/) { - $range->set_range_start($_) if defined $_; - $string =~ s/^([-\d\.]+)?://; - $valid++ + if ( $string =~ m/^([\d\.-]+)?:/ ) { # '10:' + my $start = $1; + $range->set_range_start($start) if defined $start; + $range->end_infinity(1); # overridden below if there's an end specified + $string =~ s/^([-\d\.]+)?://; + $valid++; } - if ($string =~ /^([-\d\.]+)$/) { - $range->set_range_end($string); - $valid++; + if ($string =~ /^([-\d\.]+)$/) { # 'x:10' or '10' + $range->set_range_end($string); + $valid++; } if ($valid && ($range->start_infinity == 1 || $range->end_infinity == 1 || $range->start <= $range->end)) { @@ -78,7 +92,7 @@ sub check_range { my ($self, $value) = @_; my $false = 0; my $true = 1; - if ($self->alert_on == $inside) { + if ($self->alert_on == INSIDE) { $false = 1; $true = 0; } @@ -89,7 +103,7 @@ sub check_range { return $true; } } elsif ($self->start_infinity == 0 && $self->end_infinity == 1) { - if ($self->start <= $value) { + if ( $value >= $self->start ) { return $false; } else { return $true; diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 1b332b9..494383f 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -7,6 +7,8 @@ use warnings; use Nagios::Plugin::Range; use Nagios::Plugin::Base; +use Nagios::Plugin; +our ($VERSION) = $Nagios::Plugin::VERSION; use Class::Struct; struct "Nagios::Plugin::Threshold" => { @@ -44,6 +46,7 @@ sub set_thresholds { sub get_status { my ($self, $value) = @_; + if ($self->critical->is_set) { if ($self->critical->check_range($value) == 1) { return $ERRORS{CRITICAL}; @@ -54,6 +57,7 @@ sub get_status { return $ERRORS{WARNING}; } } + return $ERRORS{OK}; } 1; -- cgit v1.2.3-74-g34f1