[Nagiosplug-checkins] SF.net SVN: nagiosplug:[2157] Nagios-Plugin/trunk

dermoth at users.sourceforge.net dermoth at users.sourceforge.net
Tue Mar 3 11:37:26 CET 2009


Revision: 2157
          http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=2157&view=rev
Author:   dermoth
Date:     2009-03-03 10:37:26 +0000 (Tue, 03 Mar 2009)

Log Message:
-----------
Added parsing of labels with spaces (thanks to Kang)

From: Ton Voon <tonvoon at macbook.local>

Modified Paths:
--------------
    Nagios-Plugin/trunk/Changes
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm
    Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
    Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t
    Nagios-Plugin/trunk/t/Nagios-Plugin-Threshold.t

Modified: Nagios-Plugin/trunk/Changes
===================================================================
--- Nagios-Plugin/trunk/Changes	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/Changes	2009-03-03 10:37:26 UTC (rev 2157)
@@ -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
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Functions.pm	2009-03-03 10:37:26 UTC (rev 2157)
@@ -12,7 +12,7 @@
 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);
 

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Performance.pm	2009-03-03 10:37:26 UTC (rev 2157)
@@ -30,7 +30,7 @@
 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 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 @@
 	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);

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin/Threshold.pm	2009-03-03 10:37:26 UTC (rev 2157)
@@ -39,6 +39,9 @@
         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));

Modified: Nagios-Plugin/trunk/lib/Nagios/Plugin.pm
===================================================================
--- Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/lib/Nagios/Plugin.pm	2009-03-03 10:37:26 UTC (rev 2157)
@@ -25,7 +25,7 @@
 # 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;

Modified: Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t
===================================================================
--- Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/t/Nagios-Plugin-Performance.t	2009-03-03 10:37:26 UTC (rev 2157)
@@ -13,10 +13,34 @@
     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 @@
 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 @@
         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]->value, 45.6, "value okay");
 is( $p[0]->uom, "", "uom okay");
 
+
+# Test labels with spaces (returned by nsclient++)
+ at 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
+ at 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

Modified: Nagios-Plugin/trunk/t/Nagios-Plugin-Threshold.t
===================================================================
--- Nagios-Plugin/trunk/t/Nagios-Plugin-Threshold.t	2009-02-24 10:18:27 UTC (rev 2156)
+++ Nagios-Plugin/trunk/t/Nagios-Plugin-Threshold.t	2009-03-03 10:37:26 UTC (rev 2157)
@@ -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 @@
 
 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");


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Commits mailing list