forked from munin-monitoring/munin
/
munin_node_spoolwriter.t
203 lines (161 loc) · 6.41 KB
/
munin_node_spoolwriter.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# vim: sw=4 : ts=4 : et
use warnings;
use strict;
use Test::More tests => 25;
use Test::LongString;
use POSIX ();
use File::Temp qw( tempdir );
use Data::Dumper;
use File::Slurp;
use Munin::Node::SpoolWriter;
# like touch(1).
sub touch { open my $fh, '>>', (shift) or die $!; close $fh }
### new
{
my $dir = POSIX::getcwd();
my $writer = new_ok('Munin::Node::SpoolWriter' => [
spooldir => $dir,
], 'spooldir provided to constructor');
is($writer->{spooldir}, $dir, 'spooldir key is set');
isa_ok($writer->{spooldirhandle}, 'GLOB', 'spooldirhandle is a glob');
}
{
eval { Munin::Node::SpoolWriter->new(fnord => 'blort') };
like($@, qr/./, 'Dies if no spooldir provided');
}
### write
{
my $dir = tempdir( CLEANUP => 1 );
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
$writer->write(1234567890, 'fnord', [
'graph_title CPU usage',
'graph_order system user nice idle iowait irq softirq',
'graph_args --base 1000 -r --lower-limit 0 --upper-limit 200',
'update_rate 86400',
'system.label system',
'system.value 999999'
]);
my $data_file = "$dir/munin-daemon.fnord.1234483200" . "." . Munin::Node::SpoolWriter::DEFAULT_TIME;
ok( -r $data_file, 'spool file is readable') or last;
my $data = read_file($data_file);
is_string($data, <<EOC, 'Data was written correctly');
timestamp 1234567890
multigraph fnord
graph_title CPU usage
graph_order system user nice idle iowait irq softirq
graph_args --base 1000 -r --lower-limit 0 --upper-limit 200
update_rate 86400
system.label system
system.value 999999
EOC
### Now a different set of results
$writer->write(1234567891, 'fnord', [
'graph_title CPU usage!', # this line is different
'graph_order system user nice idle iowait irq softirq',
'graph_args --base 1000 -r --lower-limit 0 --upper-limit 200',
'update_rate 86400',
'system.label system',
'system.value 999998'
]);
$data = read_file($data_file);
is_string($data, <<EOC, 'Spool file was appended to');
timestamp 1234567890
multigraph fnord
graph_title CPU usage
graph_order system user nice idle iowait irq softirq
graph_args --base 1000 -r --lower-limit 0 --upper-limit 200
update_rate 86400
system.label system
system.value 999999
timestamp 1234567891
multigraph fnord
graph_title CPU usage!
graph_order system user nice idle iowait irq softirq
graph_args --base 1000 -r --lower-limit 0 --upper-limit 200
update_rate 86400
system.label system
system.value 999998
EOC
}
# writing different types of value.
# values can also include a timestamp: http://munin-monitoring.org/wiki/protocol-multifetch
{
my @tests = (
# timestamp, value, expected
[ 1234567890, '999999', '999999', 'Integer without timestamp' ],
[ 1234567890, '2134567890:999999', '2134567890:999999', 'Integer with timestamp' ],
[ 1234567890, 'U', 'U', 'Unknown without timestamp' ],
[ 1234567890, '2134567890:U', '2134567890:U', 'Unknown with timestamp' ],
[ 1234567890, '-2', '-2', 'Negative without timestamp' ],
[ 1234567890, '2134567890:-2', '2134567890:-2', 'Negative with timestamp' ],
[ 1234567890, '3.141', '3.141', 'Float without timestamp' ],
[ 1234567890, '2134567890:3.141', '2134567890:3.141', 'Float with timestamp' ],
[ 1234567890, '1.05e-34', '1.05e-34', 'E-notation without timestamp' ],
[ 1234567890, '2134567890:1.05e-34', '2134567890:1.05e-34', 'E-notation with timestamp' ],
);
foreach (@tests) {
my ($timestamp, $value, $expected, $msg) = @$_;
my $dir = tempdir( CLEANUP => 1 );
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
$writer->write($timestamp, 'fnord', [
"system.value $value",
]);
my $data_file = "$dir/munin-daemon.fnord.1234483200" . "." . Munin::Node::SpoolWriter::DEFAULT_TIME;
unless ( -r $data_file) {
fail("$msg: File not created");
next
}
my $data = read_file($data_file);
like($data, qr(^system\.value $expected\n$)m, $msg);
}
}
# writing multigraph results
{
my $dir = tempdir( CLEANUP => 1 );
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
$writer->write(1234567890, 'fnord', [
'multigraph fnord',
'graph_title CPU usage',
'system.label system',
'system.value 999999',
'multigraph fnord.one',
'graph_title subfnord',
'subsystem.label subsystem',
'subsystem.value 123',
]);
my $data_file = "$dir/munin-daemon.fnord.1234483200" . "." . Munin::Node::SpoolWriter::DEFAULT_TIME;
ok( -r $data_file, 'spool file is readable') or last;
my $data = read_file($data_file);
is_string($data, <<EOC, 'Data was written correctly');
timestamp 1234567890
multigraph fnord
graph_title CPU usage
system.label system
system.value 999999
multigraph fnord.one
graph_title subfnord
subsystem.label subsystem
subsystem.value 123
EOC
}
### cleanup
{
my $dir = tempdir( CLEANUP => 1 );
my $writer = Munin::Node::SpoolWriter->new(spooldir => $dir);
# one timestamp before the cutoff, one after.
my $stale = time - (Munin::Node::SpoolWriter::MAXIMUM_AGE * Munin::Node::SpoolWriter::DEFAULT_TIME) - 100;
my $fresh = time - (Munin::Node::SpoolWriter::MAXIMUM_AGE * Munin::Node::SpoolWriter::DEFAULT_TIME) + 100;
my $interval = Munin::Node::SpoolWriter::DEFAULT_TIME;
touch("$dir/munin-daemon.stale.$stale.$interval");
utime time, $stale, "$dir/munin-daemon.stale.$stale.$interval";
touch("$dir/munin-daemon.fresh.$fresh.$interval");
utime time, $fresh, "$dir/munin-daemon.stale.$fresh.$interval";
touch("$dir/cruft");
ok( -r "$dir/munin-daemon.stale.$stale.$interval", 'created a stale file');
ok( -r "$dir/munin-daemon.fresh.$fresh.$interval", 'created a fresh file');
ok( -r "$dir/cruft", 'created a cruft file');
$writer->cleanup;
ok(! -r "$dir/munin-daemon.stale.$stale.$interval", 'stale file is gone');
ok( -r "$dir/munin-daemon.fresh.$fresh.$interval", 'fresh file is still there');
ok( -r "$dir/cruft", 'cruft file is still there');
}