summaryrefslogtreecommitdiffstats
path: root/t
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 /t
parentc4f93de55930896e83d3abb519190704332b4e8f (diff)
downloadmonitoring-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.t107
-rw-r--r--t/Nagios-Plugin-Threshold.t14
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
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");