From b0ff1e4262fa7f09c9cdb91206b6d2a2a1aa06a7 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Tue, 3 Mar 2009 10:36:02 +0000 Subject: Added parsing of labels with spaces (thanks to Kang) diff --git a/Changes b/Changes index 1f9034d..a751e2e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for Perl module Nagios::Plugin. +0.32 3rd March 2009 + - Handle performance data with quotes in the label (thanks to Kang) + - Die if default config file is not available and --extra-opts is set + 0.31 5th January 2009 - Check for valid numerical value before returning perfdata object diff --git a/lib/Nagios/Plugin.pm b/lib/Nagios/Plugin.pm index b0b053d..bd0d483 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.31"; +our $VERSION = "0.32"; sub new { my $class = shift; diff --git a/lib/Nagios/Plugin/Functions.pm b/lib/Nagios/Plugin/Functions.pm index c7e899c..165aafa 100644 --- a/lib/Nagios/Plugin/Functions.pm +++ b/lib/Nagios/Plugin/Functions.pm @@ -12,7 +12,7 @@ use Params::Validate qw(:types validate); use Math::Calc::Units; # Remember to update Nagios::Plugins as well -our $VERSION = "0.31"; +our $VERSION = "0.32"; our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); diff --git a/lib/Nagios/Plugin/Performance.pm b/lib/Nagios/Plugin/Performance.pm index c35653e..6b85dc0 100644 --- a/lib/Nagios/Plugin/Performance.pm +++ b/lib/Nagios/Plugin/Performance.pm @@ -30,7 +30,7 @@ my $value_with_negative_infinity = qr/$value_re|~/; sub _parse { my $class = shift; my $string = shift; - $string =~ /^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o; + $string =~ /^'?([^'=]+)'?=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o; return undef unless ((defined $1 && $1 ne "") && (defined $2 && $2 ne "")); my @info = ($1, $2, $3, $4, $5, $6, $7); # We convert any commas to periods, in the value fields @@ -60,8 +60,13 @@ sub _nvl { sub perfoutput { my $self = shift; + # Add quotes if label contains a space character + my $label = $self->label; + if ($label =~ / /) { + $label = "'$label'"; + } my $out = sprintf "%s=%s%s;%s;%s;%s;%s", - $self->label, + $label, $self->value, $self->_nvl($self->uom), $self->_nvl($self->warning), @@ -79,8 +84,9 @@ sub parse_perfstring { my $obj; while ($perfstring) { $perfstring =~ s/^\s*//; - if ($perfstring =~ /\s/) { - $perfstring =~ s/^(.*?)\s//; + # If there is more than 1 equals sign, split it out and parse individually + if (@{[$perfstring =~ /=/g]} > 1) { + $perfstring =~ s/^(.*?=.*?)\s//; $obj = $class->_parse($1); } else { $obj = $class->_parse($perfstring); diff --git a/lib/Nagios/Plugin/Threshold.pm b/lib/Nagios/Plugin/Threshold.pm index 145b89f..73fce53 100644 --- a/lib/Nagios/Plugin/Threshold.pm +++ b/lib/Nagios/Plugin/Threshold.pm @@ -39,6 +39,9 @@ sub _inflate return $value; } + # Another quick exit if $value is an empty string + return Nagios::Plugin::Range->new if $value eq ""; + # Otherwise parse $value my $range = Nagios::Plugin::Range->parse_range_string($value); nagios_die("Cannot parse $key range: '$value'") unless(defined($range)); diff --git a/t/Nagios-Plugin-Performance.t b/t/Nagios-Plugin-Performance.t index 8426828..bbf0b20 100644 --- a/t/Nagios-Plugin-Performance.t +++ b/t/Nagios-Plugin-Performance.t @@ -13,10 +13,34 @@ my @test = ( perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var", }, { perfoutput => '/var/long@:-/filesystem/name/and/bad/chars=218MB;9443;9448', label => '/var/long@:-/filesystem/name/and/bad/chars', rrdlabel => 'var_long____filesys', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => 'var_long____filesystem_name_and_bad_chars', + }, { + perfoutput => "'page file'=36%;80;90;", + expected_perfoutput => "'page file'=36%;80;90", + label => 'page file', + rrdlabel => 'page_file', + value => '36', + uom => '%', + warning => 80, + critical => 90, + min => undef, + max => undef, + clean_label => 'page_file', + }, { + perfoutput => "'data'=5;;;;", + expected_perfoutput => "data=5;;", + label => 'data', + rrdlabel => 'data', + value => 5, + uom => "", + warning => undef, + critical => undef, + min => undef, + max => undef, + clean_label => 'data', }, ); -plan tests => (8 * scalar @test) + 135; +plan tests => (11 * scalar @test) + 175; use_ok('Nagios::Plugin::Performance'); diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; @@ -25,14 +49,26 @@ diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performan for my $t (@test) { # Parse to components ($p) = Nagios::Plugin::Performance->parse_perfstring($t->{perfoutput}); + is ($p->value, $t->{value}, "value okay $t->{value}"); + is ($p->label, $t->{label}, "label okay $t->{label}"); + is ($p->uom, $t->{uom}, "uom okay $t->{uom}"); # Construct from components my @construct = qw(label value uom warning critical min max); $p = Nagios::Plugin::Performance->new(map { $_ => $t->{$_} } @construct); - is($p->perfoutput, $t->{perfoutput}, "perfoutput okay ($t->{perfoutput})"); + my $expected_perfoutput = $t->{perfoutput}; + if (exists $t->{expected_perfoutput}) { + $expected_perfoutput = $t->{expected_perfoutput}; + }; + is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)"); # Check threshold accessor - is($p->threshold->warning->end, $t->{warning}, "threshold warning okay ($t->{warning})"); - is($p->threshold->critical->end, $t->{critical}, "threshold critical okay ($t->{critical})"); + foreach my $type (qw(warning critical)) { + if (! defined $t->{$type}) { + isnt( $p->threshold->$type->is_set, "threshold $type not set"); + } else { + is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})"); + } + } is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay"); is($p->clean_label, $t->{clean_label}, "clean_label okay" ); @@ -42,10 +78,15 @@ for my $t (@test) { map({ $_ => $t->{$_} } @construct), threshold => Nagios::Plugin::Threshold->set_thresholds(warning => $t->{warning}, critical => $t->{critical}), ); - is($p->perfoutput, $t->{perfoutput}, "perfoutput okay ($t->{perfoutput})"); + is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)"); # Check warning/critical accessors - is($p->warning, $t->{warning}, "warning okay ($t->{warning})"); - is($p->critical, $t->{critical}, "critical okay ($t->{critical})"); + foreach my $type (qw(warning critical)) { + if (! defined $t->{$type}) { + isnt( $p->threshold->$type->is_set, "threshold $type not set"); + } else { + is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})"); + } + } } @@ -256,4 +297,56 @@ is( $p[0]->label, "other", "Ignored time=1800,600,300,0,3600, but allowed other= is( $p[0]->value, 45.6, "value okay"); is( $p[0]->uom, "", "uom okay"); + +# Test labels with spaces (returned by nsclient++) +@p = Nagios::Plugin::Performance->parse_perfstring("'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 'D:\ Label: Serial Number XA22aa2e'=8015MB;16388;18436;1;2048"); +is( $p[0]->label, "C:\ Label: Serial Number bc22aa2e"); +is( $p[0]->rrdlabel, "C__Label___Serial_N"); +is( $p[0]->value, 8015, "value okay"); +is( $p[0]->uom, "MB", "uom okay"); +is( $p[0]->threshold->warning->end, 16387, "warn okay"); +is( $p[0]->threshold->critical->end, 18435, "crit okay"); +is( $p[0]->min, 0, "min ok"); +is( $p[0]->max, 20484, "max ok"); + +is( $p[1]->label, "D:\ Label: Serial Number XA22aa2e", "label okay"); +is( $p[1]->rrdlabel, "D__Label__Serial_Nu", "rrd label okay"); +is( $p[1]->value, 8015, "value okay"); +is( $p[1]->uom, "MB", "uom okay"); +is( $p[1]->threshold->warning->end, 16388, "warn okay"); +is( $p[1]->threshold->critical->end, 18436, "crit okay"); +is( $p[1]->min, 1, "min ok"); +is( $p[1]->max, 2048, "max ok"); + + +# Mix labels with and without quotes +@p = Nagios::Plugin::Performance->parse_perfstring(" short=4 'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 end=5 "); +is( $p[0]->label, "short" ); +is( $p[0]->rrdlabel, "short"); +is( $p[0]->value, 4, "value okay"); +is( $p[0]->uom, "", "uom okay"); +isnt( $p[0]->threshold->warning->is_set, "warn okay"); +isnt( $p[0]->threshold->critical->is_set, "crit okay"); +is( $p[0]->min, undef, "min ok"); +is( $p[0]->max, undef, "max ok"); + +is( $p[1]->label, "C:\ Label: Serial Number bc22aa2e", "label okay"); +is( $p[1]->rrdlabel, "C__Label___Serial_N", "rrd label okay"); +is( $p[1]->value, 8015, "value okay"); +is( $p[1]->uom, "MB", "uom okay"); +is( $p[1]->threshold->warning->end, 16387, "warn okay"); +is( $p[1]->threshold->critical->end, 18435, "crit okay"); +is( $p[1]->min, 0, "min ok"); +is( $p[1]->max, 20484, "max ok"); + +is( $p[2]->label, "end" ); +is( $p[2]->rrdlabel, "end" ); +is( $p[2]->value, 5, "value okay"); +is( $p[2]->uom, "", "uom okay"); +isnt( $p[2]->threshold->warning->is_set, "warn okay"); +isnt( $p[2]->threshold->critical->is_set, 18436, "crit okay"); +is( $p[2]->min, undef, "min ok"); +is( $p[2]->max, undef, "max ok"); + + # add_perfdata tests in t/Nagios-Plugin-01.t diff --git a/t/Nagios-Plugin-Threshold.t b/t/Nagios-Plugin-Threshold.t index d3711bb..78d2189 100644 --- a/t/Nagios-Plugin-Threshold.t +++ b/t/Nagios-Plugin-Threshold.t @@ -1,6 +1,6 @@ use strict; -use Test::More tests => 87; +use Test::More tests => 93; BEGIN { use_ok('Nagios::Plugin::Threshold'); use_ok('Nagios::Plugin::Functions', ':all' ); @@ -13,6 +13,18 @@ diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold:: Nagios::Plugin::Functions::_fake_exit(1); +my $t; + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => undef, critical => undef); +ok( defined $t, "two undefs" ); +ok( ! $t->warning->is_set, "warning not set" ); +ok( ! $t->critical->is_set, "critical not set" ); + +$t = Nagios::Plugin::Threshold->set_thresholds(warning => "", critical => ""); +ok( defined $t, "two empty strings" ); +ok( ! $t->warning->is_set, "warning not set" ); +ok( ! $t->critical->is_set, "critical not set" ); + diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); ok( defined $t, "Threshold ('', '80') set"); -- cgit v0.10-9-g596f