From 4f0eadbd97ead5726b35843537dcce6d585b1164 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Fri, 9 Jun 2006 10:53:22 +0000 Subject: Fixed problems parsing in nagiosgraph git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1423 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/Changes b/Changes index 9095567..6779851 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,10 @@ -Revision history for Perl extension Nagios::Plugin. +Revision history for Perl module Nagios::Plugin. + +0.11 ??? + - Interface changed for parse_perfstring, returning empty + array if not parseable + - Fixed problem when parsing nagiosgraph data (linefeed at end + of perfdata) 0.10 8th June 2006 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 { my $class = shift; my $string = shift; my $p = $class->new; - $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)? *//; + $string =~ s/^([^=]+)=([\d\.]+)(\w*);?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?;?([\d\.]+)?\s*//; return undef unless ($1 && $2); $p->label($1); $p->value($2+0); @@ -44,10 +44,9 @@ sub parse_perfstring { my $obj; while ($perfstring) { ($obj, $perfstring) = $class->_parse($perfstring); - return undef unless $obj; + return () unless $obj; push @perfs, $obj; } - return undef unless @perfs; return @perfs; } @@ -63,9 +62,13 @@ Nagios::Plugin::Performance - Performance information in a perl object use Nagios::Plugin::Performance; @p = Nagios::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448"); - print "1st label = ", $p[0]->label, $/; - print "1st uom = ", $p[0]->uom, $/; - print "2nd crit = ", $p[1]->threshold->critical, $/; + if (@p) { + print "1st label = ", $p[0]->label, $/; + print "1st uom = ", $p[0]->uom, $/; + print "2nd crit = ", $p[1]->threshold->critical, $/; + } else { + print "Cannot parse",$/; + } =head1 DESCRIPTION @@ -83,7 +86,7 @@ Once the performance string has been parsed, you can query the label, value, uom =item Nagios::Plugin::Performance->parse_perfstring($string) Returns an array of Nagios::Plugin::Performance objects based on the string entered. -If there is an error parsing the string, undef is returned. +If there is an error parsing the string, an empty array is returned. =head1 OBJECT METHODS 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 @@ use strict; -use Test::More tests => 42; +use Test::More tests => 43; BEGIN { use_ok('Nagios::Plugin::Performance') }; use Nagios::Plugin::Base; @@ -21,8 +21,9 @@ cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay"); cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay"); cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay"); -ok( ! defined Nagios::Plugin::Performance->parse_perfstring("rubbish"), "Errors correctly"); -ok( ! defined Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); +@p = Nagios::Plugin::Performance->parse_perfstring("rubbish"); +ok( ! @p, "Errors correctly"); +ok( ! Nagios::Plugin::Performance->parse_perfstring(""), "Errors on empty string"); @p = Nagios::Plugin::Performance->parse_perfstring( "time=0.001229s;0.000000;0.000000;0.000000;10.000000"); @@ -49,6 +50,9 @@ cmp_ok( $p[0]->uom, "eq", "", "uom empty"); cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay"); cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay"); +@p = Nagios::Plugin::Performance->parse_perfstring( "users=4;20;50;0\n" ); + ok( @p, "parse correctly with linefeed at end (nagiosgraph)"); + @p = Nagios::Plugin::Performance->parse_perfstring( "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" ); cmp_ok( $p[0]->label, "eq", "time", "label okay"); -- cgit v0.10-9-g596f