From 7f33b6abe3b5e9ee14de2683f9412ac6641a2fcd Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Sat, 13 Dec 2008 14:05:22 +0000 Subject: Fixed parsing of numeric values with commas instead of periods. Fixed test plan for CPAN test failures of 0.29. Change to parse_perfstring to return back successfully parsed fields, rather than an empty field, when errors seen diff --git a/Changes b/Changes index 90a1e0c..d11f51e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,11 @@ Revision history for Perl module Nagios::Plugin. +0.30 13th December 2008 + - Fixed performance parsing when numeric fields had commas instead of periods due to locale settings + - If a performance set is not parseable, instead of returning an empty array, will return all the successfully + parsed sets + - Fixed test plan for Nagios-Plugin-Performance.t + 0.29 2nd December 2008 - clean_label, for cleaning up a label for RRD, but without truncation diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index eb3ccdc..2c9099f 100644 --- a/lib/Nagios/Plugin.pm +++ b/lib/Nagios/Plugin.pm @@ -25,7 +25,7 @@ our @EXPORT_OK = qw(%ERRORS); # CPAN stupidly won't index this module without a literal $VERSION here, # so we're forced to duplicate it explicitly # Make sure you update $Nagios::Plugin::Functions::VERSION too -our $VERSION = "0.29"; +our $VERSION = "0.30"; sub new { my $class = shift; diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index ffa23f0..b7348f3 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm @@ -12,7 +12,7 @@ use Params::Validate qw(:types validate); use Math::Calc::Units; # Remember to update Nagios::Plugins as well -our $VERSION = "0.29"; +our $VERSION = "0.30"; our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index a9f9198..df591fb 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -11,7 +11,7 @@ __PACKAGE__->mk_ro_accessors( qw(label value uom warning critical min max) ); -use Nagios::Plugin::Functions qw($value_re); +use Nagios::Plugin::Functions; use Nagios::Plugin::Threshold; use Nagios::Plugin::Range; our ($VERSION) = $Nagios::Plugin::Functions::VERSION; @@ -22,17 +22,24 @@ sub import { Nagios::Plugin::Functions::_use_die($_); } +# This is NOT the same as N::P::Functions::value_re. We leave that to be the strict +# version. This one allows commas to be part of the numeric value. +my $value = qr/[-+]?[\d\.,]+/; +my $value_re = qr/$value(?:e$value)?/; my $value_with_negative_infinity = qr/$value_re|~/; sub _parse { my $class = shift; my $string = shift; - $string =~ s/^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?\s*//o; + $string =~ /^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o; return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); + my @info = ($1, $2, $3, $4, $5, $6, $7); + # We convert any commas to periods, in the value fields + map { defined $info[$_] && $info[$_] =~ s/,/./go } (1, 3, 4, 5, 6); my $p = $class->new( - label => $1, value => $2+0, uom => $3, warning => $4, critical => $5, - min => $6, max => $7 + label => $info[0], value => $info[1]+0, uom => $info[2], warning => $info[3], critical => $info[4], + min => $info[5], max => $info[6] ); - return ($p, $string); + return $p; } # Map undef to '' @@ -58,12 +65,18 @@ sub perfoutput { sub parse_perfstring { my ($class, $perfstring) = @_; - my @perfs; + my @perfs = (); my $obj; while ($perfstring) { - ($obj, $perfstring) = $class->_parse($perfstring); - return () unless $obj; - push @perfs, $obj; + $perfstring =~ s/^\s*//; + if ($perfstring =~ /\s/) { + $perfstring =~ s/^(.*?)\s//; + $obj = $class->_parse($1); + } else { + $obj = $class->_parse($perfstring); + $perfstring = ""; + } + push @perfs, $obj if $obj; } return @perfs; } @@ -193,7 +206,11 @@ attributes. =item Nagios::Plugin::Performance->parse_perfstring($string) Returns an array of Nagios::Plugin::Performance objects based on the string -entered. If there is an error parsing the string, an empty array is returned. +entered. If there is an error parsing the string - which may consists of several +sets of data - will return an array with all the successfully parsed sets. + +If values are input with commas instead of periods, due to different locale settings, +then it will still be parsed, but the commas will be converted to periods. =back diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index ceb82c5..d167979 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t @@ -1,13 +1,10 @@ use strict; use Test::More; -BEGIN { use_ok('Nagios::Plugin::Performance') }; - -diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; - use Nagios::Plugin::Functions; Nagios::Plugin::Functions::_fake_exit(1); + my (@p, $p); my @test = ( { @@ -19,7 +16,10 @@ my @test = ( }, ); -plan tests => (8 * scalar @test) + 94; +plan tests => (8 * scalar @test) + 125; + +use_ok('Nagios::Plugin::Performance'); +diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; # Round-trip tests for my $t (@test) { @@ -72,6 +72,32 @@ ok( ! @p, "Errors correctly"); ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); + +# Check 1 bad with 1 good format output +@p = Nagios::Plugin::Performance->parse_perfstring("rta=&391ms;100,200;500,034;0; pl=0%;20;60 "); +is( scalar @p, 1, "One bad piece of data - only one returned" ); +is( $p[0]->label, "pl", "label okay for different numeric"); +is( $p[0]->value, 0, "value okay"); +is( $p[0]->uom, "%", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "20", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "60", "warn okay"); + +# Same as above, but order swapped +@p = Nagios::Plugin::Performance->parse_perfstring(" pl=0%;20;60 rta=&391ms;100,200;500,034;0; "); +is( scalar @p, 1, "One bad piece of data - only one returned" ); +is( $p[0]->label, "pl", "label okay for different numeric"); +is( $p[0]->value, 0, "value okay"); +is( $p[0]->uom, "%", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "20", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "60", "warn okay"); + + + + @p = Nagios::Plugin::Performance->parse_perfstring( "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); cmp_ok( $p[0]->label, "eq", "time", "label okay"); @@ -194,4 +220,23 @@ is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay"); is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay"); + + +# Check different collation with commas instead of periods +@p = Nagios::Plugin::Performance->parse_perfstring("rta=1,391ms;100,200;500,034;0; pl=0%;20;60;;"); +is( $p[0]->label, "rta", "label okay for numeric with commas instead of periods"); +is( $p[0]->value, 1.391, "value okay"); +is( $p[0]->uom, "ms", "uom okay"); +ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); +is( $p[0]->threshold->warning, "100.2", "warn okay"); +is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[0]->threshold->critical, "500.034", "warn okay"); +is( $p[1]->label, "pl", "label okay for different numeric"); +is( $p[1]->value, 0, "value okay"); +is( $p[1]->uom, "%", "uom okay"); +ok( $p[1]->threshold->warning->is_set, "Warning range has been set"); +is( $p[1]->threshold->warning, "20", "warn okay"); +is( $p[1]->threshold->critical->is_set, 1, "Critical range has been set"); +is( $p[1]->threshold->critical, "60", "warn okay"); + # add_perfdata tests in t/Nagios-Plugin-01.t -- cgit v0.10-9-g596f