summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Performance.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Performance.t')
-rw-r--r--t/Monitoring-Plugin-Performance.t357
1 files changed, 357 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Performance.t b/t/Monitoring-Plugin-Performance.t
new file mode 100644
index 0000000..bedb2e2
--- /dev/null
+++ b/t/Monitoring-Plugin-Performance.t
@@ -0,0 +1,357 @@
1
2use warnings;
3use strict;
4use Test::More;
5use Monitoring::Plugin::Functions;
6Monitoring::Plugin::Functions::_fake_exit(1);
7
8
9my (@p, $p);
10my @test = (
11 {
12 perfoutput => "/=382MB;15264;15269;0;32768", label => '/', rrdlabel => 'root', value => 382, uom => 'MB', warning => 15264, critical => 15269, min => 0, max => 32768, clean_label => "root",
13 }, {
14 perfoutput => "/var=218MB;9443;9448", label => '/var', rrdlabel => 'var', value => '218', uom => 'MB', warning => 9443, critical => 9448, min => undef, max => undef, clean_label => "var",
15 }, {
16 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',
17 }, {
18 perfoutput => "'page file'=36%;80;90;",
19 expected_perfoutput => "'page file'=36%;80;90",
20 label => 'page file',
21 rrdlabel => 'page_file',
22 value => '36',
23 uom => '%',
24 warning => 80,
25 critical => 90,
26 min => undef,
27 max => undef,
28 clean_label => 'page_file',
29 }, {
30 perfoutput => "'data'=5;;;;",
31 expected_perfoutput => "data=5;;",
32 label => 'data',
33 rrdlabel => 'data',
34 value => 5,
35 uom => "",
36 warning => undef,
37 critical => undef,
38 min => undef,
39 max => undef,
40 clean_label => 'data',
41 },
42);
43
44plan tests => (11 * scalar @test) + 176;
45
46use_ok('Monitoring::Plugin::Performance');
47diag "\nusing Monitoring::Plugin::Performance revision ". $Monitoring::Plugin::Performance::VERSION . "\n" if $ENV{TEST_VERBOSE};
48
49# Round-trip tests
50for my $t (@test) {
51 # Parse to components
52 ($p) = Monitoring::Plugin::Performance->parse_perfstring($t->{perfoutput});
53 is ($p->value, $t->{value}, "value okay $t->{value}");
54 is ($p->label, $t->{label}, "label okay $t->{label}");
55 is ($p->uom, $t->{uom}, "uom okay $t->{uom}");
56
57 # Construct from components
58 my @construct = qw(label value uom warning critical min max);
59 $p = Monitoring::Plugin::Performance->new(map { $_ => $t->{$_} } @construct);
60 my $expected_perfoutput = $t->{perfoutput};
61 if (exists $t->{expected_perfoutput}) {
62 $expected_perfoutput = $t->{expected_perfoutput};
63 };
64 is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)");
65 # Check threshold accessor
66 foreach my $type (qw(warning critical)) {
67 if (! defined $t->{$type}) {
68 isnt( $p->threshold->$type->is_set, "threshold $type not set");
69 } else {
70 is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})");
71 }
72 }
73 is($p->rrdlabel, $t->{rrdlabel}, "rrdlabel okay");
74 is($p->clean_label, $t->{clean_label}, "clean_label okay" );
75
76 # Construct using threshold
77 @construct = qw(label value uom min max);
78 $p = Monitoring::Plugin::Performance->new(
79 map({ $_ => $t->{$_} } @construct),
80 threshold => Monitoring::Plugin::Threshold->set_thresholds(warning => $t->{warning}, critical => $t->{critical}),
81 );
82 is($p->perfoutput, $expected_perfoutput, "perfoutput okay ($expected_perfoutput)");
83 # Check warning/critical accessors
84 foreach my $type (qw(warning critical)) {
85 if (! defined $t->{$type}) {
86 isnt( $p->threshold->$type->is_set, "threshold $type not set");
87 } else {
88 is($p->threshold->$type->end, $t->{$type}, "threshold $type okay ($t->{$type})");
89 }
90 }
91}
92
93
94# Test multiple parse_perfstrings
95@p = Monitoring::Plugin::Performance->parse_perfstring("/=382MB;15264;15269;; /var=218MB;9443;9448");
96cmp_ok( $p[0]->label, 'eq', "/", "label okay");
97cmp_ok( $p[0]->rrdlabel, 'eq', "root", "rrd label okay");
98cmp_ok( $p[0]->value, '==', 382, "value okay");
99cmp_ok( $p[0]->uom, 'eq', "MB", "uom okay");
100cmp_ok( $p[0]->threshold->warning->end, "==", 15264, "warn okay");
101cmp_ok( $p[0]->threshold->critical->end, "==", 15269, "crit okay");
102ok(! defined $p[0]->min, "min undef");
103ok(! defined $p[0]->max, "max undef");
104
105cmp_ok( $p[1]->label, 'eq', "/var", "label okay");
106cmp_ok( $p[1]->rrdlabel, 'eq', "var", "rrd label okay");
107cmp_ok( $p[1]->value, '==', 218, "value okay");
108cmp_ok( $p[1]->uom, 'eq', "MB", "uom okay");
109cmp_ok( $p[1]->threshold->warning->end, "==", 9443, "warn okay");
110cmp_ok( $p[1]->threshold->critical->end, "==", 9448, "crit okay");
111
112@p = Monitoring::Plugin::Performance->parse_perfstring("rubbish");
113ok( ! @p, "Errors correctly");
114ok( ! Monitoring::Plugin::Performance->parse_perfstring(""), "Errors on empty string");
115
116
117
118# Check 1 bad with 1 good format output
119@p = Monitoring::Plugin::Performance->parse_perfstring("rta=&391ms;100,200;500,034;0; pl=0%;20;60 ");
120is( scalar @p, 1, "One bad piece of data - only one returned" );
121is( $p[0]->label, "pl", "label okay for different numeric");
122is( $p[0]->value, 0, "value okay");
123is( $p[0]->uom, "%", "uom okay");
124ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
125is( $p[0]->threshold->warning, "20", "warn okay");
126is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
127is( $p[0]->threshold->critical, "60", "warn okay");
128
129# Same as above, but order swapped
130@p = Monitoring::Plugin::Performance->parse_perfstring(" pl=0%;20;60 rta=&391ms;100,200;500,034;0; ");
131is( scalar @p, 1, "One bad piece of data - only one returned" );
132is( $p[0]->label, "pl", "label okay for different numeric");
133is( $p[0]->value, 0, "value okay");
134is( $p[0]->uom, "%", "uom okay");
135ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
136is( $p[0]->threshold->warning, "20", "warn okay");
137is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
138is( $p[0]->threshold->critical, "60", "warn okay");
139
140
141
142
143@p = Monitoring::Plugin::Performance->parse_perfstring(
144 "time=0.001229s;0.000000;0.000000;0.000000;10.000000");
145cmp_ok( $p[0]->label, "eq", "time", "label okay");
146cmp_ok( $p[0]->value, "==", 0.001229, "value okay");
147cmp_ok( $p[0]->uom, "eq", "s", "uom okay");
148 ok( $p[0]->threshold->warning->is_set, "warn okay");
149 ok( $p[0]->threshold->critical->is_set, "crit okay");
150
151
152
153@p = Monitoring::Plugin::Performance->parse_perfstring(
154 "load1=0.000;5.000;9.000;0; load5=0.000;5.000;9.000;0; load15=0.000;5.000;9.000;0;");
155cmp_ok( $p[0]->label, "eq", "load1", "label okay");
156cmp_ok( $p[0]->value, "eq", "0", "value okay with 0 as string");
157cmp_ok( $p[0]->uom, "eq", "", "uom empty");
158cmp_ok( $p[0]->threshold->warning, "eq", "5", "warn okay");
159cmp_ok( $p[0]->threshold->critical, "eq", "9", "crit okay");
160cmp_ok( $p[1]->label, "eq", "load5", "label okay");
161cmp_ok( $p[2]->label, "eq", "load15", "label okay");
162
163@p = Monitoring::Plugin::Performance->parse_perfstring( "users=4;20;50;0" );
164cmp_ok( $p[0]->label, "eq", "users", "label okay");
165cmp_ok( $p[0]->value, "==", 4, "value okay");
166cmp_ok( $p[0]->uom, "eq", "", "uom empty");
167cmp_ok( $p[0]->threshold->warning, 'eq', "20", "warn okay");
168cmp_ok( $p[0]->threshold->critical, 'eq', "50", "crit okay");
169
170@p = Monitoring::Plugin::Performance->parse_perfstring( "users=4;20;50;0\n" );
171 ok( @p, "parse correctly with linefeed at end (nagiosgraph)");
172
173@p = Monitoring::Plugin::Performance->parse_perfstring(
174 "time=0.215300s;5.000000;10.000000;0.000000 size=426B;;;0" );
175cmp_ok( $p[0]->label, "eq", "time", "label okay");
176cmp_ok( $p[0]->value, "eq", "0.2153", "value okay");
177cmp_ok( $p[0]->uom, "eq", "s", "uom okay");
178cmp_ok( $p[0]->threshold->warning, 'eq', "5", "warn okay");
179cmp_ok( $p[0]->threshold->critical, 'eq', "10", "crit okay");
180cmp_ok( $p[1]->label, "eq", "size", "label okay");
181cmp_ok( $p[1]->value, "==", 426, "value okay");
182cmp_ok( $p[1]->uom, "eq", "B", "uom okay");
183 ok( ! $p[1]->threshold->warning->is_set, "warn okay");
184 ok( ! $p[1]->threshold->critical->is_set, "crit okay");
185
186# Edge cases
187@p = Monitoring::Plugin::Performance->parse_perfstring("/home/a-m=0;0;0 shared-folder:big=20 12345678901234567890=20");
188cmp_ok( $p[0]->rrdlabel, "eq", "home_a_m", "changing / to _");
189 ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
190cmp_ok( $p[1]->rrdlabel, "eq", "shared_folder_big", "replacing bad characters");
191cmp_ok( $p[2]->rrdlabel, "eq", "1234567890123456789", "shortening rrd label");
192
193# turn off fake_exit and enable use_die so we pick up on errors via plugin_die
194Monitoring::Plugin::Functions::_use_die(1);
195Monitoring::Plugin::Functions::_fake_exit(0);
196
197@p = Monitoring::Plugin::Performance->parse_perfstring("time=0.002722s;0.000000;0.000000;0.000000;10.000000");
198cmp_ok( $p[0]->label, "eq", "time", "label okay");
199cmp_ok( $p[0]->value, "eq", "0.002722", "value okay");
200cmp_ok( $p[0]->uom, "eq", "s", "uom okay");
201 ok( defined $p[0]->threshold->warning->is_set, "Warning range has been set");
202 ok( defined $p[0]->threshold->critical->is_set, "Critical range has been set");
203# The two below used to be cmp_ok, but Test::More 0.86 appears to have a problem with a stringification
204# of 0. See http://rt.cpan.org/Ticket/Display.html?id=41109
205# We need to force stringification for test. See RT 57709
206is( $p[0]->threshold->warning."", "0", "warn okay");
207is( $p[0]->threshold->critical."", "0", "crit okay");
208
209@p = Monitoring::Plugin::Performance->parse_perfstring("pct_used=73.7%;90;95");
210cmp_ok( $p[0]->label, "eq", "pct_used", "label okay");
211cmp_ok( $p[0]->value, "eq", "73.7", "value okay");
212cmp_ok( $p[0]->uom, "eq", "%", "uom okay");
213 ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set");
214 ok( defined eval { $p[0]->threshold->critical->is_set }, "Critical range has been set");
215cmp_ok( $p[0]->threshold->warning, 'eq', "90", "warn okay");
216cmp_ok( $p[0]->threshold->critical, 'eq', "95", "crit okay");
217
218# Check ranges are parsed correctly
219@p = Monitoring::Plugin::Performance->parse_perfstring("availability=93.8%;90:99;");
220is( $p[0]->label, "availability", "label okay");
221is( $p[0]->value, "93.8", "value okay");
222is( $p[0]->uom, "%", "uom okay");
223ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set");
224is( $p[0]->threshold->critical->is_set, 0, "Critical range has not been set");
225is( $p[0]->threshold->warning, "90:99", "warn okay");
226
227# Check that negative values are parsed correctly in value and ranges
228@p = Monitoring::Plugin::Performance->parse_perfstring("offset=-0.004476s;-60.000000:-5;-120.000000:-3;");
229is( $p[0]->label, "offset", "label okay");
230is( $p[0]->value, "-0.004476", "value okay");
231is( $p[0]->uom, "s", "uom okay");
232ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set");
233ok( defined eval { $p[0]->threshold->critical->is_set }, "Critical range has been set");
234is( $p[0]->threshold->warning, "-60:-5", "warn okay");
235is( $p[0]->threshold->critical, "-120:-3", "crit okay");
236
237# Check infinity values are okay
238@p = Monitoring::Plugin::Performance->parse_perfstring("salary=52GBP;~:23.5;45.2:");
239is( $p[0]->label, "salary", "label okay");
240is( $p[0]->value, "52", "value okay");
241is( $p[0]->uom, "GBP", "uom okay");
242ok( defined eval { $p[0]->threshold->warning->is_set }, "Warning range has been set");
243is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
244is( $p[0]->threshold->warning, "~:23.5", "warn okay");
245is( $p[0]->threshold->critical, "45.2:", "warn okay");
246
247# Check scientific notation
248@p = Monitoring::Plugin::Performance->parse_perfstring("offset=1.120567322e-05");
249is( $p[0]->label, "offset", "label okay for scientific notation");
250is( $p[0]->value, 1.120567322e-05, "value okay");
251is( $p[0]->uom, "", "uom okay");
252ok( ! $p[0]->threshold->warning->is_set, "Warning range has not been set");
253ok( ! $p[0]->threshold->critical->is_set, "Critical range has not been set");
254
255
256# Check scientific notation with warnings and criticals
257@p = Monitoring::Plugin::Performance->parse_perfstring("offset=-1.120567322e-05unit;-1.1e-05:1.0e-03;4.3e+02:4.3e+25");
258is( $p[0]->label, "offset", "label okay for scientific notation in warnings and criticals");
259is( $p[0]->value, -1.120567322e-05, "value okay");
260is( $p[0]->uom, "unit", "uom okay");
261ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
262is( $p[0]->threshold->warning, "-1.1e-05:0.001", "warn okay");
263is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
264is( $p[0]->threshold->critical, "430:4.3e+25", "warn okay");
265
266
267
268# Check different collation with commas instead of periods
269@p = Monitoring::Plugin::Performance->parse_perfstring("rta=1,391ms;100,200;500,034;0; pl=0%;20;60;;");
270is( $p[0]->label, "rta", "label okay for numeric with commas instead of periods");
271is( $p[0]->value, 1.391, "value okay");
272is( $p[0]->uom, "ms", "uom okay");
273ok( $p[0]->threshold->warning->is_set, "Warning range has been set");
274is( $p[0]->threshold->warning, "100.2", "warn okay");
275is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
276is( $p[0]->threshold->critical, "500.034", "warn okay");
277is( $p[1]->label, "pl", "label okay for different numeric");
278is( $p[1]->value, 0, "value okay");
279is( $p[1]->uom, "%", "uom okay");
280ok( $p[1]->threshold->warning->is_set, "Warning range has been set");
281is( $p[1]->threshold->warning, "20", "warn okay");
282is( $p[1]->threshold->critical->is_set, 1, "Critical range has been set");
283is( $p[1]->threshold->critical, "60", "warn okay");
284
285
286# Another set of comma separated stuff
287@p = Monitoring::Plugin::Performance->parse_perfstring("offset=-0,023545s;60,000000;120,000000;");
288is( $p[0]->label, "offset", "label okay for numeric with commas instead of periods");
289is( $p[0]->value, -0.023545, "value okay");
290is( $p[0]->uom, "s", "uom okay");
291is( $p[0]->threshold->warning->is_set, 1, "Warning range has been set");
292is( $p[0]->threshold->warning, 60, "warn okay");
293is( $p[0]->threshold->critical->is_set, 1, "Critical range has been set");
294is( $p[0]->threshold->critical, 120, "warn okay");
295
296# Some values with funny commas
297@p = Monitoring::Plugin::Performance->parse_perfstring("time=1800,600,300,0,3600 other=45.6");
298is( $p[0]->label, "other", "Ignored time=1800,600,300,0,3600, but allowed other=45.6");
299is( $p[0]->value, 45.6, "value okay");
300is( $p[0]->uom, "", "uom okay");
301
302
303# Test labels with spaces (returned by nsclient++)
304@p = Monitoring::Plugin::Performance->parse_perfstring("'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 'D:\ Label: Serial Number XA22aa2e'=8015MB;16388;18436;1;2048");
305is( $p[0]->label, "C:\ Label: Serial Number bc22aa2e");
306is( $p[0]->rrdlabel, "C__Label___Serial_N");
307is( $p[0]->value, 8015, "value okay");
308is( $p[0]->uom, "MB", "uom okay");
309is( $p[0]->threshold->warning->end, 16387, "warn okay");
310is( $p[0]->threshold->critical->end, 18435, "crit okay");
311is( $p[0]->min, 0, "min ok");
312is( $p[0]->max, 20484, "max ok");
313
314is( $p[1]->label, "D:\ Label: Serial Number XA22aa2e", "label okay");
315is( $p[1]->rrdlabel, "D__Label__Serial_Nu", "rrd label okay");
316is( $p[1]->value, 8015, "value okay");
317is( $p[1]->uom, "MB", "uom okay");
318is( $p[1]->threshold->warning->end, 16388, "warn okay");
319is( $p[1]->threshold->critical->end, 18436, "crit okay");
320is( $p[1]->min, 1, "min ok");
321is( $p[1]->max, 2048, "max ok");
322
323
324# Mix labels with and without quotes
325@p = Monitoring::Plugin::Performance->parse_perfstring(" short=4 'C:\ Label: Serial Number bc22aa2e'=8015MB;16387;18435;0;20484 end=5 ");
326is( $p[0]->label, "short" );
327is( $p[0]->rrdlabel, "short");
328is( $p[0]->value, 4, "value okay");
329is( $p[0]->uom, "", "uom okay");
330isnt( $p[0]->threshold->warning->is_set, "warn okay");
331isnt( $p[0]->threshold->critical->is_set, "crit okay");
332is( $p[0]->min, undef, "min ok");
333is( $p[0]->max, undef, "max ok");
334
335is( $p[1]->label, "C:\ Label: Serial Number bc22aa2e", "label okay");
336is( $p[1]->rrdlabel, "C__Label___Serial_N", "rrd label okay");
337is( $p[1]->value, 8015, "value okay");
338is( $p[1]->uom, "MB", "uom okay");
339is( $p[1]->threshold->warning->end, 16387, "warn okay");
340is( $p[1]->threshold->critical->end, 18435, "crit okay");
341is( $p[1]->min, 0, "min ok");
342is( $p[1]->max, 20484, "max ok");
343
344is( $p[2]->label, "end" );
345is( $p[2]->rrdlabel, "end" );
346is( $p[2]->value, 5, "value okay");
347is( $p[2]->uom, "", "uom okay");
348isnt( $p[2]->threshold->warning->is_set, "warn okay");
349isnt( $p[2]->threshold->critical->is_set, 18436, "crit okay");
350is( $p[2]->min, undef, "min ok");
351is( $p[2]->max, undef, "max ok");
352
353
354@p = Monitoring::Plugin::Performance->parse_perfstring("processes=9;WKFLSV32.exe;9=");
355is_deeply( \@p, [], "Fails parsing correctly");
356
357# add_perfdata tests in t/Monitoring-Plugin-01.t