summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changes6
-rw-r--r--lib/Nagios/Plugin.pm2
-rw-r--r--lib/Nagios/Plugin/Functions.pm2
-rw-r--r--lib/Nagios/Plugin/Performance.pm37
-rw-r--r--t/Nagios-Plugin-Performance.t55
5 files changed, 85 insertions, 17 deletions
diff --git a/Changes b/Changes
index 90a1e0c..d11f51e 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,11 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
30.30 13th December 2008
4 - Fixed performance parsing when numeric fields had commas instead of periods due to locale settings
5 - If a performance set is not parseable, instead of returning an empty array, will return all the successfully
6 parsed sets
7 - Fixed test plan for Nagios-Plugin-Performance.t
8
30.29 2nd December 2008 90.29 2nd December 2008
4 - clean_label, for cleaning up a label for RRD, but without truncation 10 - clean_label, for cleaning up a label for RRD, but without truncation
5 11
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);
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
28our $VERSION = "0.29"; 28our $VERSION = "0.30";
29 29
30sub new { 30sub new {
31 my $class = shift; 31 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);
12use Math::Calc::Units; 12use Math::Calc::Units;
13 13
14# Remember to update Nagios::Plugins as well 14# Remember to update Nagios::Plugins as well
15our $VERSION = "0.29"; 15our $VERSION = "0.30";
16 16
17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); 17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
18 18
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(
11 qw(label value uom warning critical min max) 11 qw(label value uom warning critical min max)
12); 12);
13 13
14use Nagios::Plugin::Functions qw($value_re); 14use Nagios::Plugin::Functions;
15use Nagios::Plugin::Threshold; 15use Nagios::Plugin::Threshold;
16use Nagios::Plugin::Range; 16use Nagios::Plugin::Range;
17our ($VERSION) = $Nagios::Plugin::Functions::VERSION; 17our ($VERSION) = $Nagios::Plugin::Functions::VERSION;
@@ -22,17 +22,24 @@ sub import {
22 Nagios::Plugin::Functions::_use_die($_); 22 Nagios::Plugin::Functions::_use_die($_);
23} 23}
24 24
25# This is NOT the same as N::P::Functions::value_re. We leave that to be the strict
26# version. This one allows commas to be part of the numeric value.
27my $value = qr/[-+]?[\d\.,]+/;
28my $value_re = qr/$value(?:e$value)?/;
25my $value_with_negative_infinity = qr/$value_re|~/; 29my $value_with_negative_infinity = qr/$value_re|~/;
26sub _parse { 30sub _parse {
27 my $class = shift; 31 my $class = shift;
28 my $string = shift; 32 my $string = shift;
29 $string =~ s/^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?\s*//o; 33 $string =~ /^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o;
30 return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); 34 return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne ""));
35 my @info = ($1, $2, $3, $4, $5, $6, $7);
36 # We convert any commas to periods, in the value fields
37 map { defined $info[$_] && $info[$_] =~ s/,/./go } (1, 3, 4, 5, 6);
31 my $p = $class->new( 38 my $p = $class->new(
32 label => $1, value => $2+0, uom => $3, warning => $4, critical => $5, 39 label => $info[0], value => $info[1]+0, uom => $info[2], warning => $info[3], critical => $info[4],
33 min => $6, max => $7 40 min => $info[5], max => $info[6]
34 ); 41 );
35 return ($p, $string); 42 return $p;
36} 43}
37 44
38# Map undef to '' 45# Map undef to ''
@@ -58,12 +65,18 @@ sub perfoutput {
58 65
59sub parse_perfstring { 66sub parse_perfstring {
60 my ($class, $perfstring) = @_; 67 my ($class, $perfstring) = @_;
61 my @perfs; 68 my @perfs = ();
62 my $obj; 69 my $obj;
63 while ($perfstring) { 70 while ($perfstring) {
64 ($obj, $perfstring) = $class->_parse($perfstring); 71 $perfstring =~ s/^\s*//;
65 return () unless $obj; 72 if ($perfstring =~ /\s/) {
66 push @perfs, $obj; 73 $perfstring =~ s/^(.*?)\s//;
74 $obj = $class->_parse($1);
75 } else {
76 $obj = $class->_parse($perfstring);
77 $perfstring = "";
78 }
79 push @perfs, $obj if $obj;
67 } 80 }
68 return @perfs; 81 return @perfs;
69} 82}
@@ -193,7 +206,11 @@ attributes.
193=item Nagios::Plugin::Performance->parse_perfstring($string) 206=item Nagios::Plugin::Performance->parse_perfstring($string)
194 207
195Returns an array of Nagios::Plugin::Performance objects based on the string 208Returns an array of Nagios::Plugin::Performance objects based on the string
196entered. If there is an error parsing the string, an empty array is returned. 209entered. If there is an error parsing the string - which may consists of several
210sets of data - will return an array with all the successfully parsed sets.
211
212If values are input with commas instead of periods, due to different locale settings,
213then it will still be parsed, but the commas will be converted to periods.
197 214
198=back 215=back
199 216
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 @@
1 1
2use strict; 2use strict;
3use Test::More; 3use Test::More;
4BEGIN { use_ok('Nagios::Plugin::Performance') };
5
6diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
7
8use Nagios::Plugin::Functions; 4use Nagios::Plugin::Functions;
9Nagios::Plugin::Functions::_fake_exit(1); 5Nagios::Plugin::Functions::_fake_exit(1);
10 6
7
11my (@p, $p); 8my (@p, $p);
12my @test = ( 9my @test = (
13 { 10 {
@@ -19,7 +16,10 @@ my @test = (
19 }, 16 },
20); 17);
21 18
22plan tests => (8 * scalar @test) + 94; 19plan tests => (8 * scalar @test) + 125;
20
21use_ok('Nagios::Plugin::Performance');
22diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
23 23
24# Round-trip tests 24# Round-trip tests
25for my $t (@test) { 25for my $t (@test) {
@@ -72,6 +72,32 @@ ok( ! @p, "Errors correctly");
72ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); 72ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string");
73 73
74 74
75
76# Check 1 bad with 1 good format output
77@p = Nagios::Plugin::Performance->parse_perfstring("rta=&391ms;100,200;500,034;0; pl=0%;20;60 ");
78is( scalar @p, 1, "One bad piece of data - only one returned" );
79is( $p[0]->label, "pl", "label okay for different numeric");
80is( $p[0]->value, 0, "value okay");
81is( $p[0]->uom, "%", "uom okay");
82ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
83is( $p[0]->threshold->warning, "20", "warn okay");
84is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
85is( $p[0]->threshold->critical, "60", "warn okay");
86
87# Same as above, but order swapped
88@p = Nagios::Plugin::Performance->parse_perfstring(" pl=0%;20;60 rta=&391ms;100,200;500,034;0; ");
89is( scalar @p, 1, "One bad piece of data - only one returned" );
90is( $p[0]->label, "pl", "label okay for different numeric");
91is( $p[0]->value, 0, "value okay");
92is( $p[0]->uom, "%", "uom okay");
93ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
94is( $p[0]->threshold->warning, "20", "warn okay");
95is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
96is( $p[0]->threshold->critical, "60", "warn okay");
97
98
99
100
75@p = Nagios::Plugin::Performance->parse_perfstring( 101@p = Nagios::Plugin::Performance->parse_perfstring(
76 "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); 102 "time=0.001229s;0.000000;0.000000;0.000000;10.000000");
77cmp_ok( $p[0]->label, "eq", "time", "label okay"); 103cmp_ok( $p[0]->label, "eq", "time", "label okay");
@@ -194,4 +220,23 @@ is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay");
194is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); 220is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
195is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay"); 221is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay");
196 222
223
224
225# Check different collation with commas instead of periods
226@p = Nagios::Plugin::Performance->parse_perfstring("rta=1,391ms;100,200;500,034;0; pl=0%;20;60;;");
227is( $p[0]->label, "rta", "label okay for numeric with commas instead of periods");
228is( $p[0]->value, 1.391, "value okay");
229is( $p[0]->uom, "ms", "uom okay");
230ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
231is( $p[0]->threshold->warning, "100.2", "warn okay");
232is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
233is( $p[0]->threshold->critical, "500.034", "warn okay");
234is( $p[1]->label, "pl", "label okay for different numeric");
235is( $p[1]->value, 0, "value okay");
236is( $p[1]->uom, "%", "uom okay");
237ok( $p[1]->threshold->warning->is_set, "Warning range has been set");
238is( $p[1]->threshold->warning, "20", "warn okay");
239is( $p[1]->threshold->critical->is_set, 1, "Critical range has been set");
240is( $p[1]->threshold->critical, "60", "warn okay");
241
197# add_perfdata tests in t/Nagios-Plugin-01.t 242# add_perfdata tests in t/Nagios-Plugin-01.t