summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2008-05-14 11:19:53 +0000
committerTon Voon <tonvoon@users.sourceforge.net>2008-05-14 11:19:53 +0000
commitc47e1a9c28db2890f724ee57e59f3a3c30d7740c (patch)
tree5648f0b0ff1a1eb2478f75484a26e3fa42e1f455
parent60a00b6e423bfeeca3508398556e180955355079 (diff)
downloadmonitoring-plugin-perl-c47e1a9c28db2890f724ee57e59f3a3c30d7740c.tar.gz
Fixed parsing of scientific notation
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1993 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--Changes3
-rw-r--r--lib/Nagios/Plugin.pm2
-rw-r--r--lib/Nagios/Plugin/Functions.pm7
-rw-r--r--lib/Nagios/Plugin/Performance.pm7
-rw-r--r--lib/Nagios/Plugin/Range.pm10
-rw-r--r--t/Nagios-Plugin-Performance.t27
6 files changed, 40 insertions, 16 deletions
diff --git a/Changes b/Changes
index 8d7a7f8..36128d0 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
30.27 14th May 2008
4 - Fixed parsing of performance data with scientific notation
5
30.26 28th March 2008 60.26 28th March 2008
4 - Fixed test failure in t/Nagios-Plugin-Getopt-03.t (Thomas Guyot-Sionnest) 7 - Fixed test failure in t/Nagios-Plugin-Getopt-03.t (Thomas Guyot-Sionnest)
5 8
diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm
index 8f3a652..06f313d 100644
--- a/lib/Nagios/Plugin.pm
+++ b/lib/Nagios/Plugin.pm
@@ -25,7 +25,7 @@ our @EXPORT_OK = qw(%ERRORS);
25# CPAN stupidly won't index this module without a literal $VERSION here, 25# CPAN stupidly won't index this module without a literal $VERSION here,
26# so we're forced to duplicate it explicitly 26# so we're forced to duplicate it explicitly
27# Make sure you update $Nagios::Plugin::Functions::VERSION too 27# Make sure you update $Nagios::Plugin::Functions::VERSION too
28our $VERSION = "0.26"; 28our $VERSION = "0.27";
29 29
30sub new { 30sub new {
31 my $class = shift; 31 my $class = shift;
diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm
index 57c5b08..a37cdaf 100644
--- a/lib/Nagios/Plugin/Functions.pm
+++ b/lib/Nagios/Plugin/Functions.pm
@@ -12,14 +12,14 @@ use Params::Validate qw(:types validate);
12use Math::Calc::Units; 12use Math::Calc::Units;
13 13
14# Remember to update Nagios::Plugins as well 14# Remember to update Nagios::Plugins as well
15our $VERSION = "0.26"; 15our $VERSION = "0.27";
16 16
17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); 17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
18 18
19require Exporter; 19require Exporter;
20our @ISA = qw(Exporter); 20our @ISA = qw(Exporter);
21our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); 21our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
22our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert); 22our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert $value_re);
23our %EXPORT_TAGS = ( 23our %EXPORT_TAGS = (
24 all => [ @EXPORT, @EXPORT_OK ], 24 all => [ @EXPORT, @EXPORT_OK ],
25 codes => [ @STATUS_CODES ], 25 codes => [ @STATUS_CODES ],
@@ -42,6 +42,9 @@ our %ERRORS = (
42 42
43our %STATUS_TEXT = reverse %ERRORS; 43our %STATUS_TEXT = reverse %ERRORS;
44 44
45my $value = qr/[-+]?[\d\.]+/;
46our $value_re = qr/$value(?:e$value)?/;
47
45# _fake_exit flag and accessor/mutator, for testing 48# _fake_exit flag and accessor/mutator, for testing
46my $_fake_exit = 0; 49my $_fake_exit = 0;
47sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; 50sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm
index 403492c..a7655fc 100644
--- a/lib/Nagios/Plugin/Performance.pm
+++ b/lib/Nagios/Plugin/Performance.pm
@@ -11,7 +11,7 @@ __PACKAGE__->mk_ro_accessors(
11 qw(label value uom warning critical min max) 11 qw(label value uom warning critical min max)
12); 12);
13 13
14use Nagios::Plugin::Functions; 14use Nagios::Plugin::Functions qw($value_re);
15use Nagios::Plugin::Threshold; 15use Nagios::Plugin::Threshold;
16use Nagios::Plugin::Range; 16use Nagios::Plugin::Range;
17our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 17our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
@@ -22,12 +22,11 @@ sub import {
22 Nagios::Plugin::Functions::_use_die($_); 22 Nagios::Plugin::Functions::_use_die($_);
23} 23}
24 24
25my $value_re = qr/[-+]?[\d\.]+/; 25my $value_with_negative_infinity = qr/$value_re|~/;
26my $value_re_with_negative_infinity = qr/$value_re|~/;
27sub _parse { 26sub _parse {
28 my $class = shift; 27 my $class = shift;
29 my $string = shift; 28 my $string = shift;
30 $string =~ s/^([^=]+)=($value_re)([\w%]*);?($value_re_with_negative_infinity\:?$value_re?)?;?($value_re_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?\s*//o; 29 $string =~ s/^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?\s*//o;
31 return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); 30 return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne ""));
32 my $p = $class->new( 31 my $p = $class->new(
33 label => $1, value => $2+0, uom => $3, warning => $4, critical => $5, 32 label => $1, value => $2+0, uom => $3, warning => $4, critical => $5,
diff --git a/lib/Nagios/Plugin/Range.pm b/lib/Nagios/Plugin/Range.pm
index 3828d1a..32a0639 100644
--- a/lib/Nagios/Plugin/Range.pm
+++ b/lib/Nagios/Plugin/Range.pm
@@ -11,7 +11,7 @@ __PACKAGE__->mk_accessors(
11 qw(start end start_infinity end_infinity alert_on) 11 qw(start end start_infinity end_infinity alert_on)
12); 12);
13 13
14use Nagios::Plugin::Functions; 14use Nagios::Plugin::Functions qw(:DEFAULT $value_re);
15our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 15our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
16 16
17use overload 17use overload
@@ -54,7 +54,7 @@ sub parse_range_string {
54 54
55 $string =~ s/\s//g; # strip out any whitespace 55 $string =~ s/\s//g; # strip out any whitespace
56 # check for valid range definition 56 # check for valid range definition
57 unless ( $string =~ /[\d~]/ && $string =~ m/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) { 57 unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) {
58 carp "invalid range definition '$string'"; 58 carp "invalid range definition '$string'";
59 return undef; 59 return undef;
60 } 60 }
@@ -66,14 +66,14 @@ sub parse_range_string {
66 if ($string =~ s/^~//) { # '~:x' 66 if ($string =~ s/^~//) { # '~:x'
67 $range->start_infinity(1); 67 $range->start_infinity(1);
68 } 68 }
69 if ( $string =~ m/^([\d\.-]+)?:/ ) { # '10:' 69 if ( $string =~ m/^($value_re)?:/ ) { # '10:'
70 my $start = $1; 70 my $start = $1;
71 $range->_set_range_start($start) if defined $start; 71 $range->_set_range_start($start) if defined $start;
72 $range->end_infinity(1); # overridden below if there's an end specified 72 $range->end_infinity(1); # overridden below if there's an end specified
73 $string =~ s/^([-\d\.]+)?://; 73 $string =~ s/^($value_re)?://;
74 $valid++; 74 $valid++;
75 } 75 }
76 if ($string =~ /^([-\d\.]+)$/) { # 'x:10' or '10' 76 if ($string =~ /^($value_re)$/) { # 'x:10' or '10'
77 $range->_set_range_end($string); 77 $range->_set_range_end($string);
78 $valid++; 78 $valid++;
79 } 79 }
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t
index 7a28546..0c9ab74 100644
--- a/t/Nagios-Plugin-Performance.t
+++ b/t/Nagios-Plugin-Performance.t
@@ -1,6 +1,6 @@
1 1
2use strict; 2use strict;
3use Test::More tests => 111; 3use Test::More tests => 123;
4BEGIN { use_ok('Nagios::Plugin::Performance') }; 4BEGIN { use_ok('Nagios::Plugin::Performance') };
5 5
6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; 6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
@@ -162,13 +162,32 @@ is( $p[0]->threshold->warning, "-60:-5", "warn okay");
162is( $p[0]->threshold->critical, "-120:-3", "crit okay"); 162is( $p[0]->threshold->critical, "-120:-3", "crit okay");
163 163
164# Check infinity values are okay 164# Check infinity values are okay
165@p = Nagios::Plugin::Performance->parse_perfstring("salary=52GBP;~:23;45:"); 165@p = Nagios::Plugin::Performance->parse_perfstring("salary=52GBP;~:23.5;45.2:");
166is( $p[0]->label, "salary", "label okay"); 166is( $p[0]->label, "salary", "label okay");
167is( $p[0]->value, "52", "value okay"); 167is( $p[0]->value, "52", "value okay");
168is( $p[0]->uom, "GBP", "uom okay"); 168is( $p[0]->uom, "GBP", "uom okay");
169ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); 169ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set");
170is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); 170is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
171is( $p[0]->threshold->warning, "~:23", "warn okay"); 171is( $p[0]->threshold->warning, "~:23.5", "warn okay");
172is( $p[0]->threshold->critical, "45:", "warn okay"); 172is( $p[0]->threshold->critical, "45.2:", "warn okay");
173
174# Check scientific notation
175@p = Nagios::Plugin::Performance->parse_perfstring("offset=1.120567322e-05");
176is( $p[0]->label, "offset", "label okay for scientific notation");
177is( $p[0]->value, 1.120567322e-05, "value okay");
178is( $p[0]->uom, "", "uom okay");
179ok( ! $p[0]->threshold->warning->is_set, "Warning range has not been set");
180ok( ! $p[0]->threshold->critical->is_set, "Critical range has not been set");
181
182
183# Check scientific notation with warnings and criticals
184@p = Nagios::Plugin::Performance->parse_perfstring("offset=-1.120567322e-05unit;-1.1e-05:1.0e-03;4.3e+02:4.3e+25");
185is( $p[0]->label, "offset", "label okay for scientific notation in warnings and criticals");
186is( $p[0]->value, -1.120567322e-05, "value okay");
187is( $p[0]->uom, "unit", "uom okay");
188ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
189is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay");
190is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
191is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay");
173 192
174# add_perfdata tests in t/Nagios-Plugin-01.t 193# add_perfdata tests in t/Nagios-Plugin-01.t