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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
#!/usr/bin/env perl
BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use Time::HiRes qw(sleep);
use PerconaTest;
use DSNParser;
use Sandbox;
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');
if ( !$dbh ) {
plan skip_all => 'Cannot connect to sandbox master';
}
my $cnf = "/tmp/12345/my.sandbox.cnf";
my $pid_file = "/tmp/pt-stalk.pid.$PID";
my $log_file = "/tmp/pt-stalk.log.$PID";
my $dest = "/tmp/pt-stalk.collect.$PID";
my $int_file = "/tmp/pt-stalk-after-interval-sleep";
my $pid;
sub cleanup {
diag(`rm $pid_file $log_file $int_file 2>/dev/null`);
diag(`rm -rf $dest 2>/dev/null`);
}
sub wait_n_cycles {
my ($n) = @_;
PerconaTest::wait_until(
sub {
return 0 unless -f "$dest/after_interval_sleep";
my $n_cycles = `wc -l "$dest/after_interval_sleep" | awk '{print \$1}'`;
$n_cycles ||= '';
chomp($n_cycles);
return ($n_cycles || 0) >= $n;
},
1.5,
15
);
}
# ###########################################################################
# Test that it won't run if can't connect to MySQL.
# ###########################################################################
my $retval = system("$trunk/bin/pt-stalk -- --no-defaults --protocol socket --socket /dev/null >$log_file 2>&1");
my $output = `cat $log_file`;
like(
$output,
qr/Cannot connect to MySQL/,
"Cannot connect to MySQL"
);
is(
$retval >> 8,
1,
"Exit 1"
);
# ###########################################################################
# Test that it runs and dies normally.
# ###########################################################################
cleanup();
# As of v2.1.9 when --verbose was added, non-matching checks are not
# printed by default. So we use the --plugin to tell us when the tool
# has completed a cycle.
$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --dest $dest --plugin $trunk/t/pt-stalk/samples/plugin002.sh -- --defaults-file=$cnf");
is(
$retval >> 8,
0,
"Parent exit 0"
);
PerconaTest::wait_for_files($pid_file, $log_file);
ok(
-f $pid_file,
"Creates PID file"
);
ok(
-f $log_file,
"Creates log file"
);
sleep 1;
ok(
-d $dest,
"Creates --dest (collect) dir"
);
chomp($pid = `cat $pid_file 2>/dev/null`);
$retval = system("kill -0 $pid");
is(
$retval >> 0,
0,
"pt-stalk is running"
);
wait_n_cycles(2);
PerconaTest::kill_program(pid_file => $pid_file);
$output = `cat $log_file 2>/dev/null`;
unlike(
$output,
qr/Check results: status\(Threads_running\)=\d+, matched=no, cycles_true=0/,
"Non-matching results not logged because --verbose=2"
) or diag(`cat $log_file 2>/dev/null`, `cat $dest/*-output 2>/dev/null`);
PerconaTest::wait_until(sub { !-f $pid_file });
ok(
! -f $pid_file,
"Removes PID file"
);
$output = `cat $log_file 2>/dev/null`;
like(
$output,
qr/Caught signal, exiting/,
"Caught signal logged"
) or diag(`cat $log_file 2>/dev/null`, `cat $dest/*-output 2>/dev/null`);
# #############################################################################
# --verbose 3 (non-matching results)
# #############################################################################
cleanup();
$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --variable Threads_running --dest $dest --verbose 3 -- --defaults-file=$cnf");
PerconaTest::wait_for_files($pid_file, $log_file);
PerconaTest::wait_for_sh("grep -q 'Check results' $log_file >/dev/null");
PerconaTest::kill_program(pid_file => $pid_file);
$output = `cat $log_file 2>/dev/null`;
like(
$output,
qr/Check results: status\(Threads_running\)=\d+, matched=no, cycles_true=0/,
"Matching results logged with --verbose 3"
) or diag(`cat $dest/*-output 2>/dev/null`);
# #############################################################################
# --verbose 1 (just errors and warnings)
# #############################################################################
cleanup();
$retval = system("$trunk/bin/pt-stalk --daemonize --pid $pid_file --log $log_file --dest $dest --verbose 1 --plugin $trunk/t/pt-stalk/samples/plugin002.sh -- --defaults-file=$cnf");
PerconaTest::wait_for_files($pid_file, $log_file);
wait_n_cycles(2);
PerconaTest::kill_program(pid_file => $pid_file);
$output = `cat $log_file 2>/dev/null`;
like(
$output,
qr/Caught signal, exiting/,
"Warning logged (--verbose 1)"
);
unlike(
$output,
qr/Start|Collect|Check/i,
"No run info log (--verbose 1)"
);
# ###########################################################################
# Test collect.
# ###########################################################################
cleanup();
# We'll have to watch Uptime since it's the only status var that's going
# to be predictable.
my (undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'");
my $threshold = $uptime + 2;
$retval = system("$trunk/bin/pt-stalk --iterations 1 --dest $dest --variable Uptime --threshold $threshold --cycles 2 --run-time 2 --pid $pid_file -- --defaults-file=$cnf >$log_file 2>&1");
PerconaTest::wait_until(sub { !-f $pid_file });
$output = `cat $dest/*-trigger 2>/dev/null`;
like(
$output,
qr/Check results: status\(Uptime\)=\d+, matched=yes, cycles_true=2/,
"Collect triggered"
)
or diag(
'output', $output,
'log file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
'dest', `ls -l $dest/ 2>/dev/null`,
'df', `cat $dest/*-df 2>/dev/null`,
);
# There is some nondeterminism here. Sometimes it'll run for 2 samples because
# the samples may not be precisely 1 second apart.
chomp($output = `cat $dest/*-df 2>/dev/null | grep -c '^TS'`);
ok(
$output >= 1 && $output <= 3,
"Collect ran for --run-time"
)
or diag(
'output', $output,
'log file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
'dest', `ls -l $dest/ 2>/dev/null`,
'df', `cat $dest/*-df 2>/dev/null`,
);
ok(
PerconaTest::not_running("pt-stalk --iterations 1"),
"pt-stalk is not running"
);
$output = `cat $dest/*-trigger 2>/dev/null`;
like(
$output,
qr/pt-stalk ran with --function=status --variable=Uptime --threshold=$threshold/,
"Trigger file logs how pt-stalk was ran"
);
chomp($output = `cat $log_file 2>/dev/null | grep 'Collect [0-9] PID'`);
like(
$output,
qr/Collect 1 PID \d+/,
"Collector PID logged"
)
or diag(
'output', $output,
'log file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
);
# ###########################################################################
# Triggered but --no-collect.
# ###########################################################################
cleanup();
(undef, $uptime) = $dbh->selectrow_array("SHOW STATUS LIKE 'Uptime'");
$threshold = $uptime + 2;
$retval = system("$trunk/bin/pt-stalk --no-collect --iterations 1 --dest $dest --variable Uptime --threshold $threshold --cycles 1 --run-time 1 --pid $pid_file -- --defaults-file=$cnf >$log_file 2>&1");
PerconaTest::wait_until(sub { !-f $pid_file });
$output = `cat $log_file 2>/dev/null`;
like(
$output,
qr/Collect 1 triggered/,
"Collect triggered"
);
ok(
! -f "$dest/*",
"No files collected"
) or diag(`ls -l $dest/ 2>/dev/null`);
ok(
PerconaTest::not_running("pt-stalk --no-collect"),
"pt-stalk is not running"
);
# #############################################################################
# --config
# #############################################################################
cleanup();
diag(`cp $ENV{HOME}/.pt-stalk.conf $ENV{HOME}/.pt-stalk.conf.original 2>/dev/null`);
diag(`cp $trunk/t/pt-stalk/samples/config001.conf $ENV{HOME}/.pt-stalk.conf`);
system "$trunk/bin/pt-stalk --dest $dest --pid $pid_file >$log_file 2>&1 &";
PerconaTest::wait_for_files($pid_file);
sleep 1;
chomp($pid = `cat $pid_file`);
$retval = system("kill $pid 2>/dev/null");
is(
$retval >> 0,
0,
"Killed pt-stalk"
);
$output = `cat $log_file 2>/dev/null`;
like(
$output,
qr/Check results: status\(Aborted_connects\)=|variable=Aborted_connects/,
"Read default config file"
);
diag(`rm $ENV{HOME}/.pt-stalk.conf`);
diag(`cp $ENV{HOME}/.pt-stalk.conf.original $ENV{HOME}/.pt-stalk.conf 2>/dev/null`);
# #############################################################################
# Don't stalk, just collect.
# #############################################################################
cleanup();
# As of 2.2, --no-stalk means just that: don't stalk, just collect, so
# we have to specify --iterations=1 else the tool will continue to run,
# whereas in 2.1 --no-stalk implied/forced "collect once and exit".
$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --prefix nostalk --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1");
PerconaTest::wait_until(sub { !-f $pid_file });
$output = `cat $dest/nostalk-trigger 2>/dev/null`;
like(
$output,
qr/Not stalking/,
"Not stalking, collect triggered"
)
or diag(
'dest', `ls -l $dest/ 2>/dev/null`,
'log_file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
);
chomp($output = `grep -c '^TS' $dest/nostalk-df 2>/dev/null`);
is(
$output,
2,
"Not stalking, collect ran for --run-time"
)
or diag(
'dest', `ls -l $dest/ 2>/dev/null`,
'log_file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
);
my $vmstat = `which vmstat 2>/dev/null`;
SKIP: {
skip "vmstat is not installed", 1 unless $vmstat;
chomp(my $n=`awk '/[ ]*[0-9]/ { n += 1 } END { print n }' "$dest/nostalk-vmstat"`);
is(
$n,
"2",
"vmstat ran for --run-time seconds (bug 955860)"
);
};
is(
`cat $dest/nostalk-hostname 2>/dev/null`,
`hostname`,
"Not stalking, collect gathered data"
)
or diag(
'dest', `ls -l $dest/ 2>/dev/null`,
'log_file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
);
ok(
PerconaTest::not_running("pt-stalk --no-stalk"),
"Not stalking, pt-stalk is not running"
)
or diag(
'dest', `ls -l $dest/ 2>/dev/null`,
'log_file', `cat $log_file 2>/dev/null`,
'collector', `cat $dest/*-output 2>/dev/null`,
);
# ############################################################################
# bad "find" usage in purge_samples gives
# https://bugs.launchpad.net/percona-toolkit/+bug/942114
# ############################################################################
use File::Temp qw( tempdir );
my $tempdir = tempdir( CLEANUP => 1 );
my $script = <<"EOT";
. $trunk/bin/pt-stalk
purge_samples $tempdir 10000 2>&1
EOT
$output = `$script`;
unlike(
$output,
qr/\Qfind: warning: you have specified the -depth option/,
"Bug 942114: no bad find usage"
);
# #############################################################################
# Done.
# #############################################################################
cleanup();
diag(`rm -rf $dest 2>/dev/null`);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;
|