[Nagiosplug-checkins] SF.net SVN: nagiosplug:[2111] Nagios-Plugin/trunk

dermoth at users.sourceforge.net dermoth at users.sourceforge.net
Sat Dec 13 15:08:33 CET 2008


Revision: 2111
          http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=2111&view=rev
Author:   dermoth
Date:     2008-12-13 14:08:33 +0000 (Sat, 13 Dec 2008)

Log Message:
-----------
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

Modified Paths:
--------------
    Nagios-Plugin/trunk/Changes
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
    Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t

Modified: Nagios-Plugin/trunk/Changes
===================================================================
--- Nagios-Plugin/trunk/Changes	2008-12-03 13:04:32 UTC (rev 2110)
+++ Nagios-Plugin/trunk/Changes	2008-12-13 14:08:33 UTC (rev 2111)
@@ -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
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2008-12-03 13:04:32 UTC (rev 2110)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2008-12-13 14:08:33 UTC (rev 2111)
@@ -12,7 +12,7 @@
 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);
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm	2008-12-03 13:04:32 UTC (rev 2110)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm	2008-12-13 14:08:33 UTC (rev 2111)
@@ -11,7 +11,7 @@
     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 @@
 	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 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,8 +206,12 @@
 =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
 
 =head1 OBJECT METHODS (ACCESSORS)

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2008-12-03 13:04:32 UTC (rev 2110)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2008-12-13 14:08:33 UTC (rev 2111)
@@ -25,7 +25,7 @@
 # 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;

Modified: Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t
===================================================================
--- Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2008-12-03 13:04:32 UTC (rev 2110)
+++ Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2008-12-13 14:08:33 UTC (rev 2111)
@@ -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,8 +16,11 @@
   },
 );
 
-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) {
     # Parse to components
@@ -72,6 +72,32 @@
 ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string");
 
 
+
+# Check 1 bad with 1 good format output
+ at 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
+ at 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->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
+ at 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


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Commits mailing list