diff options
-rw-r--r-- | Changes | 8 | ||||
-rw-r--r-- | lib/Nagios/Plugin/Performance.pm | 17 | ||||
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 10 |
3 files changed, 24 insertions, 11 deletions
@@ -1,4 +1,10 @@ | |||
1 | Revision history for Perl extension Nagios::Plugin. | 1 | Revision history for Perl module Nagios::Plugin. |
2 | |||
3 | 0.11 ??? | ||
4 | - Interface changed for parse_perfstring, returning empty | ||
5 | array if not parseable | ||
6 | - Fixed problem when parsing nagiosgraph data (linefeed at end | ||
7 | of perfdata) | ||
2 | 8 | ||
3 | 0.10 8th June 2006 | 9 | 0.10 8th June 2006 |
4 | First release to CPAN | 10 | First release to CPAN |
diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index eee1bee..82c1a3b 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm | |||
@@ -27,7 +27,7 @@ sub _parse { | |||
27 | my $class = shift; | 27 | my $class = shift; |
28 | my $string = shift; | 28 | my $string = shift; |
29 | my $p = $class->new; | 29 | my $p = $class->new; |
30 | $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)? *//; | 30 | $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?\s*//; |
31 | return undef unless ($1 && $2); | 31 | return undef unless ($1 && $2); |
32 | $p->label($1); | 32 | $p->label($1); |
33 | $p->value($2+0); | 33 | $p->value($2+0); |
@@ -44,10 +44,9 @@ sub parse_perfstring { | |||
44 | my $obj; | 44 | my $obj; |
45 | while ($perfstring) { | 45 | while ($perfstring) { |
46 | ($obj, $perfstring) = $class->_parse($perfstring); | 46 | ($obj, $perfstring) = $class->_parse($perfstring); |
47 | return undef unless $obj; | 47 | return () unless $obj; |
48 | push @perfs, $obj; | 48 | push @perfs, $obj; |
49 | } | 49 | } |
50 | return undef unless @perfs; | ||
51 | return @perfs; | 50 | return @perfs; |
52 | } | 51 | } |
53 | 52 | ||
@@ -63,9 +62,13 @@ Nagios::Plugin::Performance - Performance information in a perl object | |||
63 | use Nagios::Plugin::Performance; | 62 | use Nagios::Plugin::Performance; |
64 | 63 | ||
65 | @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); | 64 | @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); |
66 | print "1st label = ", $p[0]->label, $/; | 65 | if (@p) { |
67 | print "1st uom = ", $p[0]->uom, $/; | 66 | print "1st label = ", $p[0]->label, $/; |
68 | print "2nd crit = ", $p[1]->threshold->critical, $/; | 67 | print "1st uom = ", $p[0]->uom, $/; |
68 | print "2nd crit = ", $p[1]->threshold->critical, $/; | ||
69 | } else { | ||
70 | print "Cannot parse",$/; | ||
71 | } | ||
69 | 72 | ||
70 | =head1 DESCRIPTION | 73 | =head1 DESCRIPTION |
71 | 74 | ||
@@ -83,7 +86,7 @@ Once the performance string has been parsed, you can query the label, value, uom | |||
83 | =item Nagios::Plugin::Performance->parse_perfstring($string) | 86 | =item Nagios::Plugin::Performance->parse_perfstring($string) |
84 | 87 | ||
85 | Returns an array of Nagios::Plugin::Performance objects based on the string entered. | 88 | Returns an array of Nagios::Plugin::Performance objects based on the string entered. |
86 | If there is an error parsing the string, undef is returned. | 89 | If there is an error parsing the string, an empty array is returned. |
87 | 90 | ||
88 | =head1 OBJECT METHODS | 91 | =head1 OBJECT METHODS |
89 | 92 | ||
diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index 2fe2326..a00b2db 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t | |||
@@ -1,6 +1,6 @@ | |||
1 | 1 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 42; | 3 | use Test::More tests => 43; |
4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; | 4 | BEGIN { use_ok('Nagios::Plugin::Performance') }; |
5 | 5 | ||
6 | use Nagios::Plugin::Base; | 6 | use Nagios::Plugin::Base; |
@@ -21,8 +21,9 @@ cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay"); | |||
21 | cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); | 21 | cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); |
22 | cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); | 22 | cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); |
23 | 23 | ||
24 | ok( ! defined Nagios::Plugin::Performance->parse_perfstring("rubbish"), "Errors correctly"); | 24 | @p = Nagios::Plugin::Performance->parse_perfstring("rubbish"); |
25 | ok( ! defined Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); | 25 | ok( ! @p, "Errors correctly"); |
26 | ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); | ||
26 | 27 | ||
27 | @p = Nagios::Plugin::Performance->parse_perfstring( | 28 | @p = Nagios::Plugin::Performance->parse_perfstring( |
28 | "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); | 29 | "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); |
@@ -49,6 +50,9 @@ cmp_ok( $p[0]->uom, "eq", "", "uom empty"); | |||
49 | cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay"); | 50 | cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay"); |
50 | cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay"); | 51 | cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay"); |
51 | 52 | ||
53 | @p = Nagios::Plugin::Performance->parse_perfstring( "users=4;20;50;0\n" ); | ||
54 | ok( @p, "parse correctly with linefeed at end (nagiosgraph)"); | ||
55 | |||
52 | @p = Nagios::Plugin::Performance->parse_perfstring( | 56 | @p = Nagios::Plugin::Performance->parse_perfstring( |
53 | "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" ); | 57 | "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" ); |
54 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); | 58 | cmp_ok( $p[0]->label, "eq", "time", "label okay"); |