summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <tonvoon@macbook.local>2009-03-03 10:36:02 +0000
committerTon Voon <tonvoon@macbook.local>2009-03-03 10:36:02 +0000
commitb0ff1e4262fa7f09c9cdb91206b6d2a2a1aa06a7 (patch)
tree2637e1eb1fbb9d09b7f4c5c97affaad1411b19f7
parentc4f93de55930896e83d3abb519190704332b4e8f (diff)
downloadmonitoring-plugin-perl-b0ff1e4262fa7f09c9cdb91206b6d2a2a1aa06a7.tar.gz
Added parsing of labels with spaces (thanks to Kang)
-rw-r--r--Changes4
-rw-r--r--lib/Nagios/Plugin.pm2
-rw-r--r--lib/Nagios/Plugin/Functions.pm2
-rw-r--r--lib/Nagios/Plugin/Performance.pm14
-rw-r--r--lib/Nagios/Plugin/Threshold.pm3
-rw-r--r--t/Nagios-Plugin-Performance.t107
-rw-r--r--t/Nagios-Plugin-Threshold.t14
7 files changed, 132 insertions, 14 deletions
diff --git a/Changes b/Changes
index 1f9034d..a751e2e 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
1Revision history for Perl module Nagios::Plugin. 1Revision history for Perl module Nagios::Plugin.
2 2
30.32 3rd March 2009
4 - Handle performance data with quotes in the label (thanks to Kang)
5 - Die if default config file is not available and --extra-opts is set
6
30.31 5th January 2009 70.31 5th January 2009
4 - Check for valid numerical value before returning perfdata object 8 - Check for valid numerical value before returning perfdata object
5 9
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);
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
28our $VERSION = "0.31"; 28our $VERSION = "0.32";
29 29
30sub new { 30sub new {
31 my $class = shift; 31 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);
12use Math::Calc::Units; 12use Math::Calc::Units;
13 13
14# Remember to update Nagios::Plugins as well 14# Remember to update Nagios::Plugins as well
15our $VERSION = "0.31"; 15our $VERSION = "0.32";
16 16
17our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); 17our @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 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|~/;
30sub _parse { 30sub _parse {
31 my $class = shift; 31 my $class = shift;
32 my $string = shift; 32 my $string = shift;
33 $string =~ /^([^=]+)=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o; 33 $string =~ /^'?([^'=]+)'?=($value_re)([\w%]*);?($value_with_negative_infinity\:?$value_re?)?;?($value_with_negative_infinity\:?$value_re?)?;?($value_re)?;?($value_re)?/o;
34 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); 35 my @info = ($1, $2, $3, $4, $5, $6, $7);
36 # We convert any commas to periods, in the value fields 36 # We convert any commas to periods, in the value fields
@@ -60,8 +60,13 @@ sub _nvl {
60 60
61sub perfoutput { 61sub perfoutput {
62 my $self = shift; 62 my $self = shift;
63 # Add quotes if label contains a space character
64 my $label = $self->label;
65 if ($label =~ / /) {
66 $label = "'$label'";
67 }
63 my $out = sprintf "%s=%s%s;%s;%s;%s;%s", 68 my $out = sprintf "%s=%s%s;%s;%s;%s;%s",
64 $self->label, 69 $label,
65 $self->value, 70 $self->value,
66 $self->_nvl($self->uom), 71 $self->_nvl($self->uom),
67 $self->_nvl($self->warning), 72 $self->_nvl($self->warning),
@@ -79,8 +84,9 @@ sub parse_perfstring {
79 my $obj; 84 my $obj;
80 while ($perfstring) { 85 while ($perfstring) {
81 $perfstring =~ s/^\s*//; 86 $perfstring =~ s/^\s*//;
82 if ($perfstring =~ /\s/) { 87 # If there is more than 1 equals sign, split it out and parse individually
83 $perfstring =~ s/^(.*?)\s//; 88 if (@{[$perfstring =~ /=/g]} > 1) {
89 $perfstring =~ s/^(.*?=.*?)\s//;
84 $obj = $class->_parse($1); 90 $obj = $class->_parse($1);
85 } else { 91 } else {
86 $obj = $class->_parse($perfstring); 92 $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
39 return $value; 39 return $value;
40 } 40 }
41 41
42 # Another quick exit if $value is an empty string
43 return Nagios::Plugin::Range->new if $value eq "";
44
42 # Otherwise parse $value 45 # Otherwise parse $value
43 my $range = Nagios::Plugin::Range->parse_range_string($value); 46 my $range = Nagios::Plugin::Range->parse_range_string($value);
44 nagios_die("Cannot parse $key range: '$value'") unless(defined($range)); 47 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 = (
13 perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var", 13 perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var",
14 }, { 14 }, {
15 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', 15 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',
16 }, {
17 perfoutput => "'page file'=36%;80;90;",
18 expected_perfoutput => "'page file'=36%;80;90",
19 label => 'page file',
20 rrdlabel => 'page_file',
21 value => '36',
22 uom => '%',
23 warning => 80,
24 critical => 90,
25 min => undef,
26 max => undef,
27 clean_label => 'page_file',
28 }, {
29 perfoutput => "'data'=5;;;;",
30 expected_perfoutput => "data=5;;",
31 label => 'data',
32 rrdlabel => 'data',
33 value => 5,
34 uom => "",
35 warning => undef,
36 critical => undef,
37 min => undef,
38 max => undef,
39 clean_label => 'data',
16 }, 40 },
17); 41);
18 42
19plan tests => (8 * scalar @test) + 135; 43plan tests => (11 * scalar @test) + 175;
20 44
21use_ok('Nagios::Plugin::Performance'); 45use_ok('Nagios::Plugin::Performance');
22diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; 46diag "\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
25for my $t (@test) { 49for my $t (@test) {
26 # Parse to components 50 # Parse to components
27 ($p) = Nagios::Plugin::Performance->parse_perfstring($t->{perfoutput}); 51 ($p) = Nagios::Plugin::Performance->parse_perfstring($t->{perfoutput});
52 is ($p->value, $t->{value}, "value okay $t->{value}");
53 is ($p->label, $t->{label}, "label okay $t->{label}");
54 is ($p->uom, $t->{uom}, "uom okay $t->{uom}");
28 55
29 # Construct from components 56 # Construct from components
30 my @construct = qw(label value uom warning critical min max); 57 my @construct = qw(label value uom warning critical min max);
31 $p = Nagios::Plugin::Performance->new(map { $_ => $t->{$_} } @construct); 58 $p = Nagios::Plugin::Performance->new(map { $_ => $t->{$_} } @construct);
32 is($p->perfoutput, $t->{perfoutput}, "perfoutput okay ($t->{perfoutput})"); 59 my $expected_perfoutput = $t->{perfoutput};
60 if (exists $t->{expected_perfoutput}) {
61 $expected_perfoutput = $t->{expected_perfoutput};
62 };
63 is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)");
33 # Check threshold accessor 64 # Check threshold accessor
34 is($p->threshold->warning->end, $t->{warning}, "threshold warning okay ($t->{warning})"); 65 foreach my $type (qw(warning critical)) {
35 is($p->threshold->critical->end, $t->{critical}, "threshold critical okay ($t->{critical})"); 66 if (! defined $t->{$type}) {
67 isnt( $p->threshold->$type->is_set, "threshold $type not set");
68 } else {
69 is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})");
70 }
71 }
36 is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay"); 72 is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay");
37 is($p->clean_label, $t->{clean_label}, "clean_label okay" ); 73 is($p->clean_label, $t->{clean_label}, "clean_label okay" );
38 74
@@ -42,10 +78,15 @@ for my $t (@test) {
42 map({ $_ => $t->{$_} } @construct), 78 map({ $_ => $t->{$_} } @construct),
43 threshold => Nagios::Plugin::Threshold->set_thresholds(warning => $t->{warning}, critical => $t->{critical}), 79 threshold => Nagios::Plugin::Threshold->set_thresholds(warning => $t->{warning}, critical => $t->{critical}),
44 ); 80 );
45 is($p->perfoutput, $t->{perfoutput}, "perfoutput okay ($t->{perfoutput})"); 81 is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)");
46 # Check warning/critical accessors 82 # Check warning/critical accessors
47 is($p->warning, $t->{warning}, "warning okay ($t->{warning})"); 83 foreach my $type (qw(warning critical)) {
48 is($p->critical, $t->{critical}, "critical okay ($t->{critical})"); 84 if (! defined $t->{$type}) {
85 isnt( $p->threshold->$type->is_set, "threshold $type not set");
86 } else {
87 is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})");
88 }
89 }
49} 90}
50 91
51 92
@@ -256,4 +297,56 @@ is( $p[0]->label, "other", "Ignored time=1800,600,300,0,3600, but allowed other=
256is( $p[0]->value, 45.6, "value okay"); 297is( $p[0]->value, 45.6, "value okay");
257is( $p[0]->uom, "", "uom okay"); 298is( $p[0]->uom, "", "uom okay");
258 299
300
301# Test labels with spaces (returned by nsclient++)
302@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");
303is( $p[0]->label, "C:\ Label: Serial Number bc22aa2e");
304is( $p[0]->rrdlabel, "C__Label___Serial_N");
305is( $p[0]->value, 8015, "value okay");
306is( $p[0]->uom, "MB", "uom okay");
307is( $p[0]->threshold->warning->end, 16387, "warn okay");
308is( $p[0]->threshold->critical->end, 18435, "crit okay");
309is( $p[0]->min, 0, "min ok");
310is( $p[0]->max, 20484, "max ok");
311
312is( $p[1]->label, "D:\ Label: Serial Number XA22aa2e", "label okay");
313is( $p[1]->rrdlabel, "D__Label__Serial_Nu", "rrd label okay");
314is( $p[1]->value, 8015, "value okay");
315is( $p[1]->uom, "MB", "uom okay");
316is( $p[1]->threshold->warning->end, 16388, "warn okay");
317is( $p[1]->threshold->critical->end, 18436, "crit okay");
318is( $p[1]->min, 1, "min ok");
319is( $p[1]->max, 2048, "max ok");
320
321
322# Mix labels with and without quotes
323@p = Nagios::Plugin::Performance->parse_perfstring(" short=4 'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 end=5 ");
324is( $p[0]->label, "short" );
325is( $p[0]->rrdlabel, "short");
326is( $p[0]->value, 4, "value okay");
327is( $p[0]->uom, "", "uom okay");
328isnt( $p[0]->threshold->warning->is_set, "warn okay");
329isnt( $p[0]->threshold->critical->is_set, "crit okay");
330is( $p[0]->min, undef, "min ok");
331is( $p[0]->max, undef, "max ok");
332
333is( $p[1]->label, "C:\ Label: Serial Number bc22aa2e", "label okay");
334is( $p[1]->rrdlabel, "C__Label___Serial_N", "rrd label okay");
335is( $p[1]->value, 8015, "value okay");
336is( $p[1]->uom, "MB", "uom okay");
337is( $p[1]->threshold->warning->end, 16387, "warn okay");
338is( $p[1]->threshold->critical->end, 18435, "crit okay");
339is( $p[1]->min, 0, "min ok");
340is( $p[1]->max, 20484, "max ok");
341
342is( $p[2]->label, "end" );
343is( $p[2]->rrdlabel, "end" );
344is( $p[2]->value, 5, "value okay");
345is( $p[2]->uom, "", "uom okay");
346isnt( $p[2]->threshold->warning->is_set, "warn okay");
347isnt( $p[2]->threshold->critical->is_set, 18436, "crit okay");
348is( $p[2]->min, undef, "min ok");
349is( $p[2]->max, undef, "max ok");
350
351
259# add_perfdata tests in t/Nagios-Plugin-01.t 352# 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 @@
1 1
2use strict; 2use strict;
3use Test::More tests => 87; 3use Test::More tests => 93;
4BEGIN { 4BEGIN {
5 use_ok('Nagios::Plugin::Threshold'); 5 use_ok('Nagios::Plugin::Threshold');
6 use_ok('Nagios::Plugin::Functions', ':all' ); 6 use_ok('Nagios::Plugin::Functions', ':all' );
@@ -13,6 +13,18 @@ diag "\nusing Nagios::Plugin::Threshold revision ". $Nagios::Plugin::Threshold::
13 13
14Nagios::Plugin::Functions::_fake_exit(1); 14Nagios::Plugin::Functions::_fake_exit(1);
15 15
16my $t;
17
18$t = Nagios::Plugin::Threshold->set_thresholds(warning => undef, critical => undef);
19ok( defined $t, "two undefs" );
20ok( ! $t->warning->is_set, "warning not set" );
21ok( ! $t->critical->is_set, "critical not set" );
22
23$t = Nagios::Plugin::Threshold->set_thresholds(warning => "", critical => "");
24ok( defined $t, "two empty strings" );
25ok( ! $t->warning->is_set, "warning not set" );
26ok( ! $t->critical->is_set, "critical not set" );
27
16diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; 28diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE};
17my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); 29my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80");
18ok( defined $t, "Threshold ('', '80') set"); 30ok( defined $t, "Threshold ('', '80') set");