diff options
author | Ton Voon <tonvoon@macbook.local> | 2009-03-03 10:36:02 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@macbook.local> | 2009-03-03 10:36:02 +0000 |
commit | b0ff1e4262fa7f09c9cdb91206b6d2a2a1aa06a7 (patch) | |
tree | 2637e1eb1fbb9d09b7f4c5c97affaad1411b19f7 /t | |
parent | c4f93de55930896e83d3abb519190704332b4e8f (diff) | |
download | monitoring-plugin-perl-b0ff1e4262fa7f09c9cdb91206b6d2a2a1aa06a7.tar.gz |
Added parsing of labels with spaces (thanks to Kang)
Diffstat (limited to 't')
-rw-r--r-- | t/Nagios-Plugin-Performance.t | 107 | ||||
-rw-r--r-- | t/Nagios-Plugin-Threshold.t | 14 |
2 files changed, 113 insertions, 8 deletions
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 | ||
19 | plan tests => (8 * scalar @test) + 135; | 43 | plan tests => (11 * scalar @test) + 175; |
20 | 44 | ||
21 | use_ok('Nagios::Plugin::Performance'); | 45 | use_ok('Nagios::Plugin::Performance'); |
22 | diag "\nusing Nagios::Plugin::Performance revision ". $Nagios::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE}; | 46 | 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 | |||
25 | for my $t (@test) { | 49 | for 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= | |||
256 | is( $p[0]->value, 45.6, "value okay"); | 297 | is( $p[0]->value, 45.6, "value okay"); |
257 | is( $p[0]->uom, "", "uom okay"); | 298 | is( $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"); | ||
303 | is( $p[0]->label, "C:\ Label: Serial Number bc22aa2e"); | ||
304 | is( $p[0]->rrdlabel, "C__Label___Serial_N"); | ||
305 | is( $p[0]->value, 8015, "value okay"); | ||
306 | is( $p[0]->uom, "MB", "uom okay"); | ||
307 | is( $p[0]->threshold->warning->end, 16387, "warn okay"); | ||
308 | is( $p[0]->threshold->critical->end, 18435, "crit okay"); | ||
309 | is( $p[0]->min, 0, "min ok"); | ||
310 | is( $p[0]->max, 20484, "max ok"); | ||
311 | |||
312 | is( $p[1]->label, "D:\ Label: Serial Number XA22aa2e", "label okay"); | ||
313 | is( $p[1]->rrdlabel, "D__Label__Serial_Nu", "rrd label okay"); | ||
314 | is( $p[1]->value, 8015, "value okay"); | ||
315 | is( $p[1]->uom, "MB", "uom okay"); | ||
316 | is( $p[1]->threshold->warning->end, 16388, "warn okay"); | ||
317 | is( $p[1]->threshold->critical->end, 18436, "crit okay"); | ||
318 | is( $p[1]->min, 1, "min ok"); | ||
319 | is( $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 "); | ||
324 | is( $p[0]->label, "short" ); | ||
325 | is( $p[0]->rrdlabel, "short"); | ||
326 | is( $p[0]->value, 4, "value okay"); | ||
327 | is( $p[0]->uom, "", "uom okay"); | ||
328 | isnt( $p[0]->threshold->warning->is_set, "warn okay"); | ||
329 | isnt( $p[0]->threshold->critical->is_set, "crit okay"); | ||
330 | is( $p[0]->min, undef, "min ok"); | ||
331 | is( $p[0]->max, undef, "max ok"); | ||
332 | |||
333 | is( $p[1]->label, "C:\ Label: Serial Number bc22aa2e", "label okay"); | ||
334 | is( $p[1]->rrdlabel, "C__Label___Serial_N", "rrd label okay"); | ||
335 | is( $p[1]->value, 8015, "value okay"); | ||
336 | is( $p[1]->uom, "MB", "uom okay"); | ||
337 | is( $p[1]->threshold->warning->end, 16387, "warn okay"); | ||
338 | is( $p[1]->threshold->critical->end, 18435, "crit okay"); | ||
339 | is( $p[1]->min, 0, "min ok"); | ||
340 | is( $p[1]->max, 20484, "max ok"); | ||
341 | |||
342 | is( $p[2]->label, "end" ); | ||
343 | is( $p[2]->rrdlabel, "end" ); | ||
344 | is( $p[2]->value, 5, "value okay"); | ||
345 | is( $p[2]->uom, "", "uom okay"); | ||
346 | isnt( $p[2]->threshold->warning->is_set, "warn okay"); | ||
347 | isnt( $p[2]->threshold->critical->is_set, 18436, "crit okay"); | ||
348 | is( $p[2]->min, undef, "min ok"); | ||
349 | is( $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 | ||
2 | use strict; | 2 | use strict; |
3 | use Test::More tests => 87; | 3 | use Test::More tests => 93; |
4 | BEGIN { | 4 | BEGIN { |
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 | ||
14 | Nagios::Plugin::Functions::_fake_exit(1); | 14 | Nagios::Plugin::Functions::_fake_exit(1); |
15 | 15 | ||
16 | my $t; | ||
17 | |||
18 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => undef, critical => undef); | ||
19 | ok( defined $t, "two undefs" ); | ||
20 | ok( ! $t->warning->is_set, "warning not set" ); | ||
21 | ok( ! $t->critical->is_set, "critical not set" ); | ||
22 | |||
23 | $t = Nagios::Plugin::Threshold->set_thresholds(warning => "", critical => ""); | ||
24 | ok( defined $t, "two empty strings" ); | ||
25 | ok( ! $t->warning->is_set, "warning not set" ); | ||
26 | ok( ! $t->critical->is_set, "critical not set" ); | ||
27 | |||
16 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; | 28 | diag "threshold: critical if > 80" if $ENV{TEST_VERBOSE}; |
17 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); | 29 | my $t = Nagios::Plugin::Threshold->set_thresholds(critical => "80"); |
18 | ok( defined $t, "Threshold ('', '80') set"); | 30 | ok( defined $t, "Threshold ('', '80') set"); |