From f948ceecba9b5f6cb0aef9aae6f6be18ba6feffe Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 15 Jun 2006 09:11:54 +0000 Subject: is_set method to see if a range has been set git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1429 f882894a-f735-0410-b71e-b25c423dba1c --- Changes | 1 + lib/Nagios/Plugin/Range.pm | 8 +++++++- lib/Nagios/Plugin/Threshold.pm | 6 +++--- t/Nagios-Plugin-Performance.t | 11 ++++++----- t/Nagios-Plugin-Threshold.t | 8 ++++---- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Changes b/Changes index a86a117..15812bd 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ Revision history for Perl module Nagios::Plugin. - rrdlabel method available to get a performance label, converted to something rrd can use - fixes to parse_perfstring routine if values are 0 + - is_set method for range object to see if warning/critical range is set 0.11 14th June 2006 - Interface changed for parse_perfstring, returning empty diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm index 3d6f613..c03001a 100644 --- a/lib/Nagios/Plugin/Range.pm +++ b/lib/Nagios/Plugin/Range.pm @@ -14,7 +14,7 @@ struct "Nagios::Plugin::Range" => { end => '$', start_infinity => '$', # TRUE / FALSE end_infinity => '$', # TRUE / FALSE - alert_on => '$', # OUTSIDE 0, INSIDE 1 + alert_on => '$', # OUTSIDE 0, INSIDE 1, not defined == range not set }; my $outside = 0; @@ -22,11 +22,17 @@ my $inside = 1; sub stringify { my $self = shift; + return "" unless $self->is_set; return (($self->alert_on) ? "@" : "") . (($self->start_infinity == 1) ? "~:" : (($self->start == 0)?"":$self->start.":")) . (($self->end_infinity == 1) ? "" : $self->end); } +sub is_set { + my $self = shift; + (! defined $self->alert_on) ? 0 : 1; +} + sub set_range_start { my ($self, $value) = @_; $self->start($value+0); # Force scalar into number diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 9c5d042..1b332b9 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -16,7 +16,7 @@ struct "Nagios::Plugin::Threshold" => { sub set_thresholds { my ($class, %args) = @_; - my $t = $class->new; + my $t = $class->new( warning => Nagios::Plugin::Range->new, critical => Nagios::Plugin::Range->new ); if (defined $args{warning}) { my $r = Nagios::Plugin::Range->parse_range_string($args{warning}); if (defined $r) { @@ -44,12 +44,12 @@ sub set_thresholds { sub get_status { my ($self, $value) = @_; - if ($self->critical) { + if ($self->critical->is_set) { if ($self->critical->check_range($value) == 1) { return $ERRORS{CRITICAL}; } } - if ($self->warning) { + if ($self->warning->is_set) { if ($self->warning->check_range($value) == 1) { return $ERRORS{WARNING}; } diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index aa0ab64..b10a988 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t @@ -1,6 +1,6 @@ use strict; -use Test::More tests => 43; +use Test::More tests => 49; BEGIN { use_ok('Nagios::Plugin::Performance') }; use Nagios::Plugin::Base; @@ -65,12 +65,13 @@ cmp_ok( $p[0]->threshold->critical, 'eq', "10", "crit okay"); cmp_ok( $p[1]->label, "eq", "size", "label okay"); cmp_ok( $p[1]->value, "==", 426, "value okay"); cmp_ok( $p[1]->uom, "eq", "B", "uom okay"); - ok( ! defined $p[1]->threshold->warning, "warn okay"); - ok( ! defined $p[1]->threshold->critical, "crit okay"); + ok( ! $p[1]->threshold->warning->is_set, "warn okay"); + ok( ! $p[1]->threshold->critical->is_set, "crit okay"); -# RRDlabel testing -@p = Nagios::Plugin::Performance->parse_perfstring("/home/a-m=0 shared-folder:big=20 12345678901234567890=20"); +# Edge cases +@p = Nagios::Plugin::Performance->parse_perfstring("/home/a-m=0;0;0 shared-folder:big=20 12345678901234567890=20"); cmp_ok( $p[0]->rrdlabel, "eq", "home_a_m", "changing / to _"); + ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters"); cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label"); diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index 764f7b0..97d4fcc 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t @@ -8,14 +8,14 @@ Nagios::Plugin::Base->print_on_die(0); my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); ok( defined $t, "Threshold ('', '80') set"); -ok( ! defined $t->warning, "Warning not set"); +ok( ! $t->warning->is_set, "Warning not set"); cmp_ok( $t->critical->end, '==', 80, "Critical set correctly"); $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"); -ok( ! defined $t->critical, "Critical not set"); +ok( ! $t->critical->is_set, "Critical not set"); $t = Nagios::Plugin::Threshold->set_thresholds(warning => "30", critical => "60"); ok( defined $t, "Threshold ('30', '60') set"); @@ -27,6 +27,6 @@ cmp_ok( $t->get_status(69), '==', $ERRORS{CRITICAL}, "69 - critical"); $t = Nagios::Plugin::Threshold->set_thresholds(warning => "total", critical => "rubbish"); ok( defined $t, "Threshold object created although ..."); -ok( ! defined $t->warning, "Warning not set"); -ok( ! defined $t->critical, "Critical not set"); +ok( ! $t->warning->is_set, "Warning not set"); +ok( ! $t->critical->is_set, "Critical not set"); -- cgit v1.2.3-74-g34f1