From c47e1a9c28db2890f724ee57e59f3a3c30d7740c Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Wed, 14 May 2008 11:19:53 +0000 Subject: Fixed parsing of scientific notation git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1993 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Changes b/Changes index 8d7a7f8..36128d0 100644 --- a/Changes +++ b/Changes @@ -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) 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); # 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; 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); 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 %ERRORS = ( 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 }; 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( 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 @@ sub import { 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, 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( 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 @@ sub parse_range_string { $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 @@ sub parse_range_string { 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++; } 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 @@ 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->warning, "-60:-5", "warn okay"); is( $p[0]->threshold->critical, "-120:-3", "crit okay"); # Check infinity values are okay -@p = Nagios::Plugin::Performance->parse_perfstring("salary=52GBP;~:23;45:"); +@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 +@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 +@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 -- cgit v0.10-9-g596f