diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2008-05-14 11:19:53 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2008-05-14 11:19:53 +0000 |
commit | c47e1a9c28db2890f724ee57e59f3a3c30d7740c (patch) | |
tree | 5648f0b0ff1a1eb2478f75484a26e3fa42e1f455 | |
parent | 60a00b6e423bfeeca3508398556e180955355079 (diff) | |
download | monitoring-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-- | Changes | 3 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 7 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 7 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Range.pm | 10 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 27 |
6 files changed, 40 insertions, 16 deletions
@@ -1,5 +1,8 @@ | |||
1 | Revision history for Perl module Nagios::Plugin. | 1 | Revision history for Perl module Nagios::Plugin. |
2 | 2 | ||
3 | 0.27 14th May 2008 | ||
4 | - Fixed parsing of performance data with scientific notation | ||
5 | |||
3 | 0.26 28th March 2008 | 6 | 0.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 |
28 | our $VERSION = "0.26"; | 28 | our $VERSION = "0.27"; |
29 | 29 | ||
30 | sub new { | 30 | sub 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); | |||
12 | use Math::Calc::Units; | 12 | use Math::Calc::Units; |
13 | 13 | ||
14 | # Remember to update Nagios::Plugins as well | 14 | # Remember to update Nagios::Plugins as well |
15 | our $VERSION = "0.26"; | 15 | our $VERSION = "0.27"; |
16 | 16 | ||
17 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | 17 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); |
18 | 18 | ||
19 | require Exporter; | 19 | require Exporter; |
20 | our @ISA = qw(Exporter); | 20 | our @ISA = qw(Exporter); |
21 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); | 21 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages)); |
22 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert); | 22 | our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert $value_re); |
23 | our %EXPORT_TAGS = ( | 23 | our %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 | ||
43 | our %STATUS_TEXT = reverse %ERRORS; | 43 | our %STATUS_TEXT = reverse %ERRORS; |
44 | 44 | ||
45 | my $value = qr/[-+]?[\d\.]+/; | ||
46 | our $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 |
46 | my $_fake_exit = 0; | 49 | my $_fake_exit = 0; |
47 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | 50 | sub _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 | ||
14 | use Nagios::Plugin::Functions; | 14 | use Nagios::Plugin::Functions qw($value_re); |
15 | use Nagios::Plugin::Threshold; | 15 | use Nagios::Plugin::Threshold; |
16 | use Nagios::Plugin::Range; | 16 | use Nagios::Plugin::Range; |
17 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 17 | our ($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 | ||
25 | my $value_re = qr/[-+]?[\d\.]+/; | 25 | my $value_with_negative_infinity = qr/$value_re|~/; |
26 | my $value_re_with_negative_infinity = qr/$value_re|~/; | ||
27 | sub _parse { | 26 | sub _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 | ||
14 | use Nagios::Plugin::Functions; | 14 | use Nagios::Plugin::Functions qw(:DEFAULT $value_re); |
15 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 15 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; |
16 | 16 | ||
17 | use overload | 17 | use 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 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 111; | 3 | use Test::More tests => 123; |
4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; | 4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; |
5 | 5 | ||
6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; | 6 | diag "\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"); | |||
162 | is( $p[0]->threshold->critical, "-120:-3", "crit okay"); | 162 | is( $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:"); |
166 | is( $p[0]->label, "salary", "label okay"); | 166 | is( $p[0]->label, "salary", "label okay"); |
167 | is( $p[0]->value, "52", "value okay"); | 167 | is( $p[0]->value, "52", "value okay"); |
168 | is( $p[0]->uom, "GBP", "uom okay"); | 168 | is( $p[0]->uom, "GBP", "uom okay"); |
169 | ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); | 169 | ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set"); |
170 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); | 170 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); |
171 | is( $p[0]->threshold->warning, "~:23", "warn okay"); | 171 | is( $p[0]->threshold->warning, "~:23.5", "warn okay"); |
172 | is( $p[0]->threshold->critical, "45:", "warn okay"); | 172 | is( $p[0]->threshold->critical, "45.2:", "warn okay"); |
173 | |||
174 | # Check scientific notation | ||
175 | @p = Nagios::Plugin::Performance->parse_perfstring("offset=1.120567322e-05"); | ||
176 | is( $p[0]->label, "offset", "label okay for scientific notation"); | ||
177 | is( $p[0]->value, 1.120567322e-05, "value okay"); | ||
178 | is( $p[0]->uom, "", "uom okay"); | ||
179 | ok( ! $p[0]->threshold->warning->is_set, "Warning range has not been set"); | ||
180 | ok( ! $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"); | ||
185 | is( $p[0]->label, "offset", "label okay for scientific notation in warnings and criticals"); | ||
186 | is( $p[0]->value, -1.120567322e-05, "value okay"); | ||
187 | is( $p[0]->uom, "unit", "uom okay"); | ||
188 | ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); | ||
189 | is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay"); | ||
190 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); | ||
191 | is( $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 |