Merge lp:~percona-toolkit-dev/percona-toolkit/collect-without-stalking into lp:percona-toolkit/2.0

Proposed by Daniel Nichter
Status: Merged
Approved by: Daniel Nichter
Approved revision: 185
Merged at revision: 193
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/collect-without-stalking
Merge into: lp:percona-toolkit/2.0
Diff against target: 226 lines (+106/-23)
3 files modified
bin/pt-stalk (+54/-20)
lib/PerconaTest.pm (+10/-0)
t/pt-stalk/pt-stalk.t (+42/-3)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/collect-without-stalking
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+95605@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-stalk'
2--- bin/pt-stalk 2012-03-02 16:07:29 +0000
3+++ bin/pt-stalk 2012-03-02 16:13:21 +0000
4@@ -1032,25 +1032,34 @@
5 # Run the trigger which returns the value of whatever is being
6 # checked. When the value is > --threshold for at least --cycle
7 # consecutive times, start collecting.
8- local value=$($TRIGGER_FUNCTION $OPT_VARIABLE)
9- local trg_exit_status=$?
10-
11- if [ -z "$value" ]; then
12- # No value. Maybe we failed to connect to MySQL?
13- warn "Detected value is empty; something failed? Trigger exit status: $trg_exit_status"
14- matched=""
15- cycles_true=0
16- elif [ $value -gt $OPT_THRESHOLD ]; then
17- matched="yes"
18- cycles_true=$(($cycles_true + 1))
19- else
20- matched=""
21- cycles_true=0
22+ if [ "$OPT_STALK" ]; then
23+ local value=$($TRIGGER_FUNCTION $OPT_VARIABLE)
24+ local trg_exit_status=$?
25+
26+ if [ -z "$value" ]; then
27+ # No value. Maybe we failed to connect to MySQL?
28+ warn "Detected value is empty; something failed? Trigger exit status: $trg_exit_status"
29+ matched=""
30+ cycles_true=0
31+ elif [ $value -gt $OPT_THRESHOLD ]; then
32+ matched="yes"
33+ cycles_true=$(($cycles_true + 1))
34+ else
35+ matched=""
36+ cycles_true=0
37+ fi
38+
39+ local msg="Check results: $OPT_VARIABLE=$value, matched=${matched:-no}, cycles_true=$cycles_true"
40+ log "$msg"
41+ elif [ "$OPT_COLLECT" ]; then
42+ # Make the next if condition true.
43+ matched=1
44+ cycles_true=$OPT_CYCLES
45+
46+ local msg="Not stalking; collect triggered immediately"
47+ log "$msg"
48 fi
49
50- local msg="Check results: $OPT_VARIABLE=$value, matched=${matched:-no}, cycles_true=$cycles_true"
51- log "$msg"
52-
53 if [ "$matched" -a $cycles_true -ge $OPT_CYCLES ]; then
54 # ##################################################################
55 # Start collecting, maybe.
56@@ -1085,7 +1094,6 @@
57 log "pt-stalk ran with $RAN_WITH" >> "$OPT_DEST/$prefix-trigger"
58 last_prefix="$prefix"
59
60-
61 # Fork and background the collect subroutine which will
62 # run for --run-time seconds. We (the parent) sleep
63 # while its collecting (hopefully --sleep is longer than
64@@ -1166,6 +1174,14 @@
65 option_error "Invalid --function value: $OPT_FUNCTION"
66 fi
67
68+ if [ -z "$OPT_STALK" -a "$OPT_COLLECT" ]; then
69+ # Not stalking; do immediate collect once.
70+ OPT_ITERATIONS=1
71+ OPT_CYCLES=0
72+ OPT_SLEEP=0
73+ OPT_INTERVAL=0
74+ fi
75+
76 usage_or_errors "$0"
77 po_status=$?
78 rm_tmpdir
79@@ -1206,7 +1222,7 @@
80 fi
81 fi
82
83- if [ "$OPT_DAEMONIZE" ]; then
84+ if [ "$OPT_STALK" -a "$OPT_DAEMONIZE" ]; then
85 # Check access to the --log file.
86 touch "$OPT_LOG" || die "Cannot write to --log $OPT_LOG"
87
88@@ -1226,7 +1242,7 @@
89 # use $! to get the PID of the child we just forked.
90 echo "$!" > "$OPT_PID"
91 else
92- make_pid_file "$OPT_PID" $$
93+ [ "$OPT_STALK" ] && make_pid_file "$OPT_PID" $$
94 main "$@"
95 fi
96 fi
97@@ -1360,6 +1376,8 @@
98 Collect system information. You can negate this option to make the tool watch
99 the system but not actually gather any diagnostic data.
100
101+See also L<"--stalk">.
102+
103 =item --collect-gdb
104
105 Collect GDB stacktraces. This is achieved by attaching to MySQL and printing
106@@ -1583,6 +1601,22 @@
107 It also prevents filling up the disk or gathering too much data to analyze
108 reasonably.
109
110+=item --stalk
111+
112+default: yes; negatable: yes
113+
114+Watch the server and wait for the trigger to occur. You can negate this option
115+to make the tool immediately gather any diagnostic data once and exit. This is
116+useful if a problem is already happening, but pt-stalk is not running, so
117+you only want to collect diagnostic data.
118+
119+If this option is negate, L<"--daemonize">, L<"--log">, L<"--pid">, and other
120+stalking-related options have no effect; the tool simply collects diagnostic
121+data and exits. Safeguard options, like L<"--disk-bytes-free"> and
122+L<"--disk-pct-free">, are still respected.
123+
124+See also L<"--collect">.
125+
126 =item --threshold
127
128 type: int; default: 25
129
130=== modified file 'lib/PerconaTest.pm'
131--- lib/PerconaTest.pm 2012-02-14 19:48:24 +0000
132+++ lib/PerconaTest.pm 2012-03-02 16:13:21 +0000
133@@ -294,6 +294,16 @@
134 );
135 }
136
137+sub wait_for_sh {
138+ my ($cmd) = @_;
139+ return wait_until(
140+ sub {
141+ my $retval = system("$cmd 2>/dev/null");
142+ return $retval >> 8 == 0 ? 1 : 0;
143+ }
144+ );
145+};
146+
147 sub _read {
148 my ( $fh ) = @_;
149 return <$fh>;
150
151=== modified file 't/pt-stalk/pt-stalk.t'
152--- t/pt-stalk/pt-stalk.t 2012-01-30 17:05:35 +0000
153+++ t/pt-stalk/pt-stalk.t 2012-03-02 16:13:21 +0000
154@@ -24,7 +24,7 @@
155 plan skip_all => 'Cannot connect to sandbox master';
156 }
157 else {
158- plan tests => 21;
159+ plan tests => 25;
160 }
161
162 my $cnf = "/tmp/12345/my.sandbox.cnf";
163@@ -155,7 +155,7 @@
164 "Collect ran for --run-time"
165 );
166
167-$output = `ps x | grep -v grep | grep 'pt-stalk pt-stalk --iterations 1 --dest $dest'`;
168+$output = `ps x | grep -v grep | grep 'pt-stalk --iterations 1'`;
169 is(
170 $output,
171 "",
172@@ -202,7 +202,7 @@
173 "No files collected"
174 );
175
176-$output = `ps x | grep -v grep | grep 'pt-stalk pt-stalk --iterations 1 --dest $dest'`;
177+$output = `ps x | grep -v grep | grep 'pt-stalk --no-collect'`;
178 is(
179 $output,
180 "",
181@@ -238,6 +238,45 @@
182 diag(`cp $ENV{HOME}/.pt-stalk.conf.original $ENV{HOME}/.pt-stalk.conf 2>/dev/null`);
183
184 # #############################################################################
185+# Don't stalk, just collect.
186+# #############################################################################
187+diag(`rm $pid_file 2>/dev/null`);
188+diag(`rm $log_file 2>/dev/null`);
189+diag(`rm $dest/* 2>/dev/null`);
190+
191+$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --prefix nostalk -- --defaults-file=$cnf >$log_file 2>&1");
192+
193+PerconaTest::wait_for_files("$dest/nostalk-trigger");
194+$output = `cat $dest/nostalk-trigger`;
195+like(
196+ $output,
197+ qr/Not stalking/,
198+ "Not stalking, collect triggered"
199+);
200+
201+PerconaTest::wait_for_files("$dest/nostalk-df");
202+PerconaTest::wait_for_sh("test \$(grep -c '^TS' $dest/nostalk-df) -ge 2");
203+chomp($output = `grep -c '^TS' $dest/nostalk-df`);
204+is(
205+ $output,
206+ 2,
207+ "Not stalking, collect ran for --run-time"
208+);
209+
210+is(
211+ `cat $dest/nostalk-hostname`,
212+ `hostname`,
213+ "Not stalking, collect gathered data"
214+);
215+
216+$output = `ps x | grep -v grep | grep 'pt-stalk --no-stalk'`;
217+is(
218+ $output,
219+ "",
220+ "Not stalking, pt-stalk is not running"
221+);
222+
223+# #############################################################################
224 # Done.
225 # #############################################################################
226 diag(`rm $pid_file 2>/dev/null`);

Subscribers

People subscribed via source and target branches