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

tonvoon at users.sourceforge.net tonvoon at users.sourceforge.net
Wed May 14 13:19:54 CEST 2008


Revision: 1993
          http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=1993&view=rev
Author:   tonvoon
Date:     2008-05-14 04:19:53 -0700 (Wed, 14 May 2008)

Log Message:
-----------
Fixed parsing of scientific notation

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/Range.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-05-13 09:14:45 UTC (rev 1992)
+++ Nagios-Plugin/trunk/Changes	2008-05-14 11:19:53 UTC (rev 1993)
@@ -1,5 +1,8 @@
 Revision history for Perl module Nagios::Plugin.
 
+0.27 14th May 2008
+  - Fixed parsing of performance data with scientific notation
+
 0.26 28th March 2008
   - Fixed test failure in t/Nagios-Plugin-Getopt-03.t (Thomas Guyot-Sionnest)
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2008-05-13 09:14:45 UTC (rev 1992)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2008-05-14 11:19:53 UTC (rev 1993)
@@ -12,14 +12,14 @@
 use Math::Calc::Units;
 
 # Remember to update Nagios::Plugins as well
-our $VERSION = "0.26";
+our $VERSION = "0.27";
 
 our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
 
 require Exporter;
 our @ISA = qw(Exporter);
 our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
-our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert);
+our @EXPORT_OK = qw(%ERRORS %STATUS_TEXT @STATUS_CODES get_shortname max_state convert $value_re);
 our %EXPORT_TAGS = (
     all => [ @EXPORT, @EXPORT_OK ],
     codes => [ @STATUS_CODES ],
@@ -42,6 +42,9 @@
 
 our %STATUS_TEXT = reverse %ERRORS;
 
+my $value = qr/[-+]?[\d\.]+/;
+our $value_re = qr/$value(?:e$value)?/;
+
 # _fake_exit flag and accessor/mutator, for testing
 my $_fake_exit = 0;
 sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit };

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm	2008-05-13 09:14:45 UTC (rev 1992)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm	2008-05-14 11:19:53 UTC (rev 1993)
@@ -11,7 +11,7 @@
     qw(label value uom warning critical min max)
 );
 
-use Nagios::Plugin::Functions;
+use Nagios::Plugin::Functions qw($value_re);
 use Nagios::Plugin::Threshold;
 use Nagios::Plugin::Range;
 our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
@@ -22,12 +22,11 @@
 	Nagios::Plugin::Functions::_use_die($_);
 }
 
-my $value_re = qr/[-+]?[\d\.]+/;
-my $value_re_with_negative_infinity = qr/$value_re|~/;
+my $value_with_negative_infinity = qr/$value_re|~/;
 sub _parse {
 	my $class = shift;
 	my $string = shift;
-	$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;
+	$string =~ s/^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?\s*//o;
 	return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne ""));
     my $p = $class->new(
         label => $1, value => $2+0, uom => $3, warning => $4, critical => $5, 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Range.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Range.pm	2008-05-13 09:14:45 UTC (rev 1992)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Range.pm	2008-05-14 11:19:53 UTC (rev 1993)
@@ -11,7 +11,7 @@
     qw(start end start_infinity end_infinity alert_on)
 );
 
-use Nagios::Plugin::Functions;
+use Nagios::Plugin::Functions qw(:DEFAULT $value_re);
 our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
 
 use overload
@@ -54,7 +54,7 @@
 
 	$string =~ s/\s//g;  # strip out any whitespace
 	# check for valid range definition
-	unless ( $string =~ /[\d~]/ && $string =~ m/^\@?(-?[\d.]+|~)?(:(-?[\d.]+)?)?$/ ) {
+	unless ( $string =~ /[\d~]/ && $string =~ m/^\@?($value_re|~)?(:($value_re)?)?$/ ) {
 	    carp "invalid range definition '$string'";
 	    return undef;
 	}
@@ -66,14 +66,14 @@
 	if ($string =~ s/^~//) {  # '~:x'
 	    $range->start_infinity(1);
 	}
-	if ( $string =~ m/^([\d\.-]+)?:/ ) {     # '10:'
+	if ( $string =~ m/^($value_re)?:/ ) {     # '10:'
 		my $start = $1;
 	    $range->_set_range_start($start) if defined $start;
 		$range->end_infinity(1);  # overridden below if there's an end specified
-	    $string =~ s/^([-\d\.]+)?://;
+	    $string =~ s/^($value_re)?://;
 	    $valid++;
 	}
-	if ($string =~ /^([-\d\.]+)$/) {   # 'x:10' or '10'
+	if ($string =~ /^($value_re)$/) {   # 'x:10' or '10'
 	    $range->_set_range_end($string);
 	    $valid++;
 	}

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2008-05-13 09:14:45 UTC (rev 1992)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2008-05-14 11:19:53 UTC (rev 1993)
@@ -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.26";
+our $VERSION = "0.27";
 
 sub new {
 	my $class = shift;

Modified: Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t
===================================================================
--- Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2008-05-13 09:14:45 UTC (rev 1992)
+++ Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2008-05-14 11:19:53 UTC (rev 1993)
@@ -1,6 +1,6 @@
 
 use strict;
-use Test::More tests => 111;
+use Test::More tests => 123;
 BEGIN { use_ok('Nagios::Plugin::Performance') };
 
 diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
@@ -162,13 +162,32 @@
 is( $p[0]->threshold->critical, "-120:-3", "crit okay");
 
 # Check infinity values are okay
- at p = Nagios::Plugin::Performance->parse_perfstring("salary=52GBP;~:23;45:");
+ at p = Nagios::Plugin::Performance->parse_perfstring("salary=52GBP;~:23.5;45.2:");
 is( $p[0]->label, "salary", "label okay");
 is( $p[0]->value, "52", "value okay");
 is( $p[0]->uom, "GBP", "uom okay");
 ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set");
 is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
-is( $p[0]->threshold->warning, "~:23", "warn okay");
-is( $p[0]->threshold->critical, "45:", "warn okay");
+is( $p[0]->threshold->warning, "~:23.5", "warn okay");
+is( $p[0]->threshold->critical, "45.2:", "warn okay");
 
+# Check scientific notation
+ at p = Nagios::Plugin::Performance->parse_perfstring("offset=1.120567322e-05");
+is( $p[0]->label, "offset", "label okay for scientific notation");
+is( $p[0]->value, 1.120567322e-05, "value okay");
+is( $p[0]->uom, "", "uom okay");
+ok( ! $p[0]->threshold->warning->is_set, "Warning range has not been set");
+ok( ! $p[0]->threshold->critical->is_set, "Critical range has not been set");
+
+
+# Check scientific notation with warnings and criticals
+ at p = Nagios::Plugin::Performance->parse_perfstring("offset=-1.120567322e-05unit;-1.1e-05:1.0e-03;4.3e+02:4.3e+25");
+is( $p[0]->label, "offset", "label okay for scientific notation in warnings and criticals");
+is( $p[0]->value, -1.120567322e-05, "value okay");
+is( $p[0]->uom, "unit", "uom okay");
+ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
+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");
+
 # 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