diff options
-rw-r--r-- | Changes | 6 | ||||
-rw-r--r-- | lib/Nagios/Plugin.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Functions.pm | 2 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 37 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 55 |
5 files changed, 85 insertions, 17 deletions
@@ -1,5 +1,11 @@ | |||
1 | Revision history for Perl module Nagios::Plugin. | 1 | Revision history for Perl module Nagios::Plugin. |
2 | 2 | ||
3 | 0.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 | |||
3 | 0.29 2nd December 2008 | 9 | 0.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 |
28 | our $VERSION = "0.29"; | 28 | our $VERSION = "0.30"; |
29 | 29 | ||
30 | sub new { | 30 | sub 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); | |||
12 | use Math::Calc::Units; | 12 | use Math::Calc::Units; |
13 | 13 | ||
14 | # Remember to update Nagios::Plugins as well | 14 | # Remember to update Nagios::Plugins as well |
15 | our $VERSION = "0.29"; | 15 | our $VERSION = "0.30"; |
16 | 16 | ||
17 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | 17 | our @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 | ||
14 | use Nagios::Plugin::Functions qw($value_re); | 14 | use Nagios::Plugin::Functions; |
15 | use Nagios::Plugin::Threshold; | 15 | use Nagios::Plugin::Threshold; |
16 | use Nagios::Plugin::Range; | 16 | use Nagios::Plugin::Range; |
17 | our ($VERSION) = $Nagios::Plugin::Functions::VERSION; | 17 | our ($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. | ||
27 | my $value = qr/[-+]?[\d\.,]+/; | ||
28 | my $value_re = qr/$value(?:e$value)?/; | ||
25 | my $value_with_negative_infinity = qr/$value_re|~/; | 29 | my $value_with_negative_infinity = qr/$value_re|~/; |
26 | sub _parse { | 30 | sub _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 | ||
59 | sub parse_perfstring { | 66 | sub 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 | ||
195 | Returns an array of Nagios::Plugin::Performance objects based on the string | 208 | Returns an array of Nagios::Plugin::Performance objects based on the string |
196 | entered. If there is an error parsing the string, an empty array is returned. | 209 | entered. If there is an error parsing the string - which may consists of several |
210 | sets of data - will return an array with all the successfully parsed sets. | ||
211 | |||
212 | If values are input with commas instead of periods, due to different locale settings, | ||
213 | then 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 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More; | 3 | use Test::More; |
4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; | ||
5 | |||
6 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; | ||
7 | |||
8 | use Nagios::Plugin::Functions; | 4 | use Nagios::Plugin::Functions; |
9 | Nagios::Plugin::Functions::_fake_exit(1); | 5 | Nagios::Plugin::Functions::_fake_exit(1); |
10 | 6 | ||
7 | |||
11 | my (@p, $p); | 8 | my (@p, $p); |
12 | my @test = ( | 9 | my @test = ( |
13 | { | 10 | { |
@@ -19,7 +16,10 @@ my @test = ( | |||
19 | }, | 16 | }, |
20 | ); | 17 | ); |
21 | 18 | ||
22 | plan tests => (8 * scalar @test) + 94; | 19 | plan tests => (8 * scalar @test) + 125; |
20 | |||
21 | use_ok('Nagios::Plugin::Performance'); | ||
22 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; | ||
23 | 23 | ||
24 | # Round-trip tests | 24 | # Round-trip tests |
25 | for my $t (@test) { | 25 | for my $t (@test) { |
@@ -72,6 +72,32 @@ ok( ! @p, "Errors correctly"); | |||
72 | ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); | 72 | ok( ! 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 "); | ||
78 | is( scalar @p, 1, "One bad piece of data - only one returned" ); | ||
79 | is( $p[0]->label, "pl", "label okay for different numeric"); | ||
80 | is( $p[0]->value, 0, "value okay"); | ||
81 | is( $p[0]->uom, "%", "uom okay"); | ||
82 | ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); | ||
83 | is( $p[0]->threshold->warning, "20", "warn okay"); | ||
84 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); | ||
85 | is( $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; "); | ||
89 | is( scalar @p, 1, "One bad piece of data - only one returned" ); | ||
90 | is( $p[0]->label, "pl", "label okay for different numeric"); | ||
91 | is( $p[0]->value, 0, "value okay"); | ||
92 | is( $p[0]->uom, "%", "uom okay"); | ||
93 | ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); | ||
94 | is( $p[0]->threshold->warning, "20", "warn okay"); | ||
95 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); | ||
96 | is( $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"); |
77 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); | 103 | 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"); | |||
194 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); | 220 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); |
195 | is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay"); | 221 | is( $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;;"); | ||
227 | is( $p[0]->label, "rta", "label okay for numeric with commas instead of periods"); | ||
228 | is( $p[0]->value, 1.391, "value okay"); | ||
229 | is( $p[0]->uom, "ms", "uom okay"); | ||
230 | ok( $p[0]->threshold->warning->is_set, "Warning range has been set"); | ||
231 | is( $p[0]->threshold->warning, "100.2", "warn okay"); | ||
232 | is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set"); | ||
233 | is( $p[0]->threshold->critical, "500.034", "warn okay"); | ||
234 | is( $p[1]->label, "pl", "label okay for different numeric"); | ||
235 | is( $p[1]->value, 0, "value okay"); | ||
236 | is( $p[1]->uom, "%", "uom okay"); | ||
237 | ok( $p[1]->threshold->warning->is_set, "Warning range has been set"); | ||
238 | is( $p[1]->threshold->warning, "20", "warn okay"); | ||
239 | is( $p[1]->threshold->critical->is_set, 1, "Critical range has been set"); | ||
240 | is( $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 |