~percona-toolkit-dev/percona-toolkit/pt-stalk-iter-1-bug-1070434

8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
1
#!/usr/bin/env bash
2
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
3
# This program is part of Percona Toolkit: http://www.percona.com/software/
4
# See "COPYRIGHT, LICENSE, AND WARRANTY" at the end of this file for legal
5
# notices and disclaimers.
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
6
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
7
usage() {
8
   if [ "${OPT_ERR}" ]; then
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
9
      echo "Error: $OPT_ERR" >&2
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
10
   fi
11
   echo "Usage: pt-sift FILE|PREFIX|DIRECTORY" >&2
12
   echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
13
   exit 1
14
}
15
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
16
# ###########################################################################
17
# tmpdir package
18
# This package is a copy without comments from the original.  The original
19
# with comments and its test file can be found in the Bazaar repository at,
20
#   lib/bash/tmpdir.sh
21
#   t/lib/bash/tmpdir.sh
22
# See https://launchpad.net/percona-toolkit for more information.
23
# ###########################################################################
24
94.2.184 by Baron Schwartz
Fix a bazillion tests with ANSI sql_mode, and get rid of a bunch of MySQLDump usage.
25
26
set -u
27
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
28
PT_TMPDIR=""
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
29
30
mk_tmpdir() {
255.1.1 by Daniel Nichter
Use 6 X with mktemp to make it work on some platforms and update all tools.
31
   local dir="${1:-""}"
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
32
33
   if [ -n "$dir" ]; then
34
      if [ ! -d "$dir" ]; then
255.1.1 by Daniel Nichter
Use 6 X with mktemp to make it work on some platforms and update all tools.
35
         mkdir "$dir" || die "Cannot make tmpdir $dir"
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
36
      fi
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
37
      PT_TMPDIR="$dir"
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
38
   else
255.1.1 by Daniel Nichter
Use 6 X with mktemp to make it work on some platforms and update all tools.
39
      local tool="${0##*/}"
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
40
      local pid="$$"
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
41
      PT_TMPDIR=`mktemp -d -t "${tool}.${pid}.XXXXXX"` \
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
42
         || die "Cannot make secure tmpdir"
43
   fi
44
}
45
46
rm_tmpdir() {
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
47
   if [ -n "$PT_TMPDIR" ] && [ -d "$PT_TMPDIR" ]; then
48
      rm -rf "$PT_TMPDIR"
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
49
   fi
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
50
   PT_TMPDIR=""
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
51
}
52
53
# ###########################################################################
54
# End tmpdir package
55
# ###########################################################################
56
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
57
# ###########################################################################
58
# Global variables
59
# ###########################################################################
60
61
TOOL="pt-sift"
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
62
if [ -d "/var/lib/pt-stalk" ]; then
63
   BASEDIR="/var/lib/pt-stalk"
64
else
65
   BASEDIR="$PWD"
66
fi
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
67
PREFIX=""
68
69
# ###########################################################################
70
# Subroutines
71
# ###########################################################################
72
73
sigtrap() {
74
   echo "Caught signal, exiting" >&2
75
   rm_tmpdir
76
   exit 0
77
}
78
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
79
# Show current help and settings
80
print_help() {
81
   cat <<-HELP
82
   You can control this program with key presses.
83
                  ---  COMMANDS  ---
84
      1  Default action: summarize files
85
      0  Minimal action: list files
86
      *  View all the files in less
87
      d  Invoke 'diskstats' on the disk performance data
88
      i  View the first INNODB STATUS sample in 'less'
109.1.1 by Daniel Nichter
Change mext to pt-mext.
89
      m  Invoke 'pt-mext' to show the SHOW STATUS counters side by side
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
90
      n  Summarize the 'netstat -antp' status data
91
                  --- NAVIGATION ---
92
      j  Select the next timestamp
93
      k  Select the previous timestamp
94
      q  Quit the program
95
	HELP
96
}
97
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
98
# ###########################################################################
99
# Main program loop, called below if tool is ran from the command line.
100
# ###########################################################################
101
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
102
main() {
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
103
   trap sigtrap SIGHUP SIGINT SIGTERM
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
104
105
   # If there's a command-line arg, figure out if it's a file, directory, or
106
   # prefix.  The outcome of this block of code should be that BASEDIR is the
107
   # directory where the files live, without a trailing slash; and PREFIX is
108
   # either empty or a timestamp, such as "2011_02_08_16_58_07".
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
109
   if [ $# -gt 1 ]; then
110
      OPT_ERR="Specify only one PREFIX or DIR"
111
      usage
112
   fi
113
114
   if [ $# -eq 1 ]; then
115
      if [ -d "$1" ]; then
116
         BASEDIR="$1"
117
         PREFIX=""
118
      elif [ -f "$1" -o -f "$1-df" -o -f "$1df" ]; then
119
         BASEDIR="$(dirname "$1")"
120
         PREFIX="$(echo "$1" | perl -ne '$_ =~ m/([\d_]+)/; print $1;')"
121
      else
122
         echo "Error: $1 is not a directory, and there are no pt-stalk files in the curent working directory ($BASEDIR) with a $1 prefix." >&2
123
         echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
124
         exit 1
125
      fi
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
126
   fi
127
128
   # If the programs we need don't exist, try to get them.
102 by Daniel
Fix pt-sift to fetch and eval programs correctly. Lists these programs in the sys reqs pod section.
129
   # Percona Toolkit tools:
119 by Daniel Nichter
Merge lp:~percona-toolkit-dev/percona-toolkit/pt-align r257..260 (branch was accidently created from pt-table-checksum-2.0 branch instead of 2.0 branch).
130
   for prog in pt-diskstats pt-pmp pt-mext pt-align; do
102 by Daniel
Fix pt-sift to fetch and eval programs correctly. Lists these programs in the sys reqs pod section.
131
      # A var can't be named "PR_pt-pmp" so we chop of "pt-" to get
132
      # the program's basename, resulting in "PR_pmp".
133
      prog_base=${prog#"pt-"}
134
      if which "$prog" >/dev/null 2>&1 ; then
135
         eval "PR_$prog_base"="$(which "$prog")"
136
      elif [ -f "$prog" -a -x "$prog" ]; then
137
         eval "PR_$prog_base"="./$prog"
138
      elif [ -f "${BASEDIR}/$prog" -a -x "${BASEDIR}/$prog" ]; then
139
         eval "PR_$prog_base"="${BASEDIR}/$prog"
140
      elif which "curl" >/dev/null 2>&1; then
141
         echo "Fetching $prog" >&2
110.1.1 by Daniel Nichter
Get tools from http://www.percona.com/get/ so curl works.
142
         curl "http://www.percona.com/get/$prog" > "$prog" && chmod +x "$prog"
102 by Daniel
Fix pt-sift to fetch and eval programs correctly. Lists these programs in the sys reqs pod section.
143
         eval "PR_$prog_base"="./$prog"
144
      else
145
         echo "Cannot find or fetch required program: $prog" >&2
146
         exit 1
147
      fi 
148
   done
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
149
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
150
   # Make a secure tmpdir.
151
   mk_tmpdir
152
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
153
   # We need to generate a list of timestamps, and ask the user to choose one if
154
   # there is no PREFIX yet.  NOTE: we rely on the "-df" files here.
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
155
   (
156
      cd "$BASEDIR"
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
157
      ls *-df 2>/dev/null | cut -d- -f1 | sort > "$PT_TMPDIR/pt-sift.prefixes" 
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
158
   )
159
   if [ ! -s "$PT_TMPDIR/pt-sift.prefixes" ]; then
160
      echo "Error: There are no pt-stalk files in $BASEDIR" >&2
161
      echo "For more information, 'man pt-sift' or 'perldoc $0'." >&2
162
      exit 1
163
   fi
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
164
   if [ -z "${PREFIX}" ]; then
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
165
      if [ "$(grep -c . $PT_TMPDIR/pt-sift.prefixes)" = "1" ]; then
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
166
         # If there is only one sample, we use it as the prefix.
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
167
         PREFIX="$(cat $PT_TMPDIR/pt-sift.prefixes)"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
168
      fi
169
   fi
170
   if [ -z "${PREFIX}" ]; then
171
      echo
172
      i=0
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
173
      cat $PT_TMPDIR/pt-sift.prefixes | while read line; do
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
174
         i=$(($i + 1))
175
         echo -n "  $line"
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
176
         if [ $i -eq 3 ]; then
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
177
            echo
178
            i=0
179
         fi
180
      done
181
      # We might have ended mid-line or we might have printed a newline; print a
182
      # newline if required to end the list of timestamp prefixes.
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
183
      awk 'BEGIN { i = 0 } { i++ } END { if ( i % 3 != 0 ) { print "" } }' $PT_TMPDIR/pt-sift.prefixes
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
184
      echo
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
185
      while [ -z "${PREFIX}" -o "$(grep -c "${PREFIX}" $PT_TMPDIR/pt-sift.prefixes)" -ne 1 ]; do
186
         DEFAULT="$(tail -1 $PT_TMPDIR/pt-sift.prefixes)"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
187
         read -e -p "Select a timestamp from the list [${DEFAULT}] " ARG
188
         ARG="${ARG:-${DEFAULT}}"
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
189
         if [ "$(grep -c "${ARG}" $PT_TMPDIR/pt-sift.prefixes)" -eq 1 ]; then
190
            PREFIX="$(grep "${ARG}" $PT_TMPDIR/pt-sift.prefixes)"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
191
         fi
192
      done
193
   fi
194
195
   KEY=""
196
   ACTION="DEFAULT"
197
   while [ "${KEY}" != "q" ]; do
198
199
      if [ "${ACTION}" != "INVALID" ]; then
200
         # Print the current host, timestamp and action.  Figure out if we're at
201
         # the first or last sample, to make it easy to navigate.
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
202
         PAGE="$(awk "/./{i++} /${PREFIX}/{c=i} END{print c, \"of\", i}" $PT_TMPDIR/pt-sift.prefixes)"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
203
         HOST="$(cat "${BASEDIR}/${PREFIX}-hostname" 2>/dev/null)"
204
         echo -e "======== ${HOST:-unknown} at \033[34m${PREFIX} \033[31m${ACTION}\033[0m (${PAGE}) ========"
205
      fi
206
207
      # Take an action based on the current $ACTION
208
      case "${ACTION}" in
209
210
         # Format a brief report: busiest device's disk stats, CPU stats
211
         DEFAULT)
212
            echo "--diskstats--"
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
213
            if [ -f "${BASEDIR}/${PREFIX}-diskstats" ]; then
214
215
               $PR_diskstats --group-by disk "${BASEDIR}/${PREFIX}-diskstats" \
216
                  | awk '
217
                     /ts/ { header = $0 }
218
                     /[0-9]/ {
219
                        io  = $3 + $9;
220
                        if ( io >= mio ) {
221
                           mio   = io;
222
                           mseen = $0;
223
                        }
224
                     }
225
                     END {
226
                        print header;
227
                        print mseen;
228
                     }'
229
230
               # Find out which device was the busiest.
231
               mdev="$($PR_diskstats --group-by disk "${BASEDIR}/${PREFIX}-diskstats" \
232
                  | awk '
233
                     /[0-9]/ {
234
                        io  = $3 + $9;
235
                        if ( io >= mio ) {
236
                           mio   = io;
237
                           mdev  = $2;
238
                        }
239
                     }
240
                     END {
241
                        print mdev;
242
                     }')"
243
244
               # Print the busy% for that device, rounded to the nearest N%, with
245
               # "." as a marker for a repeated value.
246
               $PR_diskstats --group-by sample "${BASEDIR}/${PREFIX}-diskstats" \
247
                  | awk "
248
                     BEGIN {
249
                        fuzz = 5;
250
                        printf \" ${mdev} \"
251
                     }
252
                     \$1 = \"${mdev}\" {
253
                        busy_rounded = fuzz * sprintf(\"%d\", substr(\$15, 1, length(\$15) - 1) / fuzz);
254
                        if ( printed == 1 && prev == busy_rounded ) {
255
                           printf \" .\";
256
                        }
257
                        else {
258
                           printf \" %d%%\", busy_rounded;
259
                           prev    = busy_rounded;
260
                           printed = 1;
261
                        }
262
                     }"
263
               echo
264
            else
265
               echo "    No diskstats file exists"
266
            fi
267
268
            echo "--vmstat--"
269
            if [ -f "${BASEDIR}/${PREFIX}-vmstat" ]; then
270
               tail -n 3 "${BASEDIR}/${PREFIX}-vmstat-overall" | $PR_align
271
272
               # Figure out which column is 'wa' and print this, similar to the
273
               # busy% for disks above.
274
               wa_col="$(awk '/swpd/{for(i=1;i<=NF;++i){if($i=="wa"){print i; exit}}}' "${BASEDIR}/${PREFIX}-vmstat")"
275
               awk "
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
276
                  BEGIN {
277
                     fuzz = 5;
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
278
                     printf \"wa\"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
279
                  }
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
280
                  /[0-9]/ {
281
                     wa_rounded = fuzz * sprintf(\"%d\", \$${wa_col} / fuzz);
282
                     if ( printed == 1 && prev == wa_rounded ) {
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
283
                        printf \" .\";
284
                     }
285
                     else {
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
286
                        printf \" %d%%\", wa_rounded;
287
                        prev    = wa_rounded;
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
288
                        printed = 1;
289
                     }
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
290
                  }" "${BASEDIR}/${PREFIX}-vmstat"
291
               echo
292
            else
293
               echo "    No vmstat file exists"
294
            fi
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
295
296
            echo "--innodb--"
297
            awk '
298
               /queries inside/ {
299
                  inside = $0;
300
               }
301
               /Main thread/ {
302
                  main_state = substr($0, index($0, ":") + 2);
303
               }
304
               /Pending normal/ {
305
                  pending_reads += substr($5, 1, length($5) - 1);
306
                  pending_reads += substr($NF, 1, length($NF) - 1);
307
               }
308
               /ibuf aio reads/ {
309
                  pending_reads += substr($4, 1, length($4) - 1);
310
                  pending_reads += substr($7, 1, length($7) - 1);
311
                  pending_reads += $NF;
312
               }
313
               /Pending flushes/ {
314
                  pending_flushes = substr($5, 1, length($5) - 1) + $NF;
315
               }
316
               /pending preads/ {
317
                  pending_reads += $1;
318
                  pending_writes += $4;
319
               }
320
               /pending log writes/ {
321
                  pending_writes += $1 + $5;
322
               }
323
               /Pending reads/ {
324
                  pending_reads += $NF;
325
               }
326
               /Pending writes/ {
327
                  pending_writes += substr($4, 1, length($4) - 1);
328
                  pending_writes += substr($7, 1, length($7) - 1);
329
                  pending_writes += $NF;
330
               }
331
               /Log sequence number/ {
332
                  if ( $NF == 5 ) {
333
                     lsn = ($4 * (2^32)) + $5;
334
                  }
335
                  else {
336
                     lsn = $4;
337
                  }
338
               }
339
               /Last checkpoint at/ {
340
                  if ( $NF == 5 ) {
341
                     chkp = ($4 * (2^32)) + $5;
342
                  }
343
                  else {
344
                     chkp = $4;
345
                  }
346
               }
347
               /END OF INNODB/ {
348
                  complete = 1;
349
               }
350
               /^TRANSACTIONS$/ {
351
                  tseen = 1;
352
               }
353
               /^---TRANSACTION/ {
354
                  if ( tseen == 1 ) {
355
                     if ( $2 ~ /,/ ) {
356
                        status = $3;
357
                        time   = $4;
358
                     }
359
                     else {
360
                        status = $4;
361
                        time   = $5;
362
                     }
363
                     txns[status]++;
364
                     if ( time > txntime[status] ) {
365
                        txntime[status] = time;
366
                     }
367
                  }
368
               }
369
               /LOCK WAIT/ {
370
                  if ( tseen == 1 ) {
371
                     txns["LOCK WAIT"]++;
372
                     if ( $3 > txntime["LOCK WAIT"] ) {
373
                        txntime["LOCK WAIT"] = $3;
374
                     }
375
                  }
376
               }
377
               END {
378
                  if ( complete != 1 ) {
379
                     print "    (innodb status is incomplete)";
380
                  }
381
                  printf "    txns:";
382
                  for ( i in txns ) {
383
                     printf " %dx%s (%ds)", txns[i], i, txntime[i];
384
                  }
385
                  print "";
386
                  if ( inside ) {
387
                     print "    " inside;
388
                  }
389
                  printf "    Main thread: %s, pending reads %d, writes %d, flush %d\n", main_state, pending_reads, pending_writes, pending_flushes;
390
                  printf "    Log: lsn = %d, chkp = %d, chkp age = %d\n", lsn, chkp, lsn - chkp;
391
               }
392
            ' "${BASEDIR}/${PREFIX}-innodbstatus1"
393
            echo "    Threads are waiting at:"
394
            awk '/has waited at/ { print $6, $7, $8 }' \
395
              "${BASEDIR}/${PREFIX}-innodbstatus1" | sort | uniq -c | sort -rn
396
            echo "    Threads are waiting on:"
397
            awk '/^[XS]-lock on.*latch/ { print }' \
398
              "${BASEDIR}/${PREFIX}-innodbstatus1" | sort | uniq -c | sort -rn
399
195.1.1 by baron at percona
Look for processlist or processlist1 for compatibility with pt-collect (fixes bug 930533)
400
            # This section checks for processlist or processlist1 for backwards
401
            # compatibility with the obsolete pt-collect tool.
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
402
            echo "--processlist--"
195.1.1 by baron at percona
Look for processlist or processlist1 for compatibility with pt-collect (fixes bug 930533)
403
            local PROCESSLIST_FILE="${BASEDIR}/${PREFIX}-processlist"
404
            if [ -e "${BASEDIR}/${PREFIX}-processlist1" ]; then
405
               PROCESSLIST_FILE="${BASEDIR}/${PREFIX}-processlist1"
406
            fi
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
407
            for word in State Command; do
408
               echo "    $word"
196.2.1 by baron at percona
Aggregate only the first set of processes from a file with multiple samples (fixes bug 945842)
409
               awk -F: -v column="$word" '
410
               BEGIN {
411
                  regex = "^ *" column
412
               }
413
               {
414
                  if ( $1 ~ regex ) {
415
                     print $2;
416
                  }
417
                  # Newer versions of pt-stalk gather several samples. We will
418
                  # analyze only the first sample.
419
                  if ( $0 ~ /^TS/ ) {
420
                     ts++;
421
                     if (ts > 1) {
422
                        exit
423
                     }
424
                  }
425
               }' "${PROCESSLIST_FILE}" \
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
426
                  | sort | uniq -c | sort -rn | head -n 5
427
            done
428
429
            echo "--stack traces--"
196.3.1 by baron at percona
Test for stacktrace file existence before calling pt-pmp (fixes bug 945836)
430
            if [ -e "${BASEDIR}/${PREFIX}-stacktrace" ]; then
431
               $PR_pmp -l 5 "${BASEDIR}/${PREFIX}-stacktrace" | head -n 5
432
            else
433
               echo "    No stack trace file exists"
434
            fi
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
435
436
            echo "--oprofile--"
437
            if [ ! -e "${BASEDIR}/${PREFIX}-opreport" ]; then
438
               echo "    No opreport file exists"
439
            fi
440
            test -e "${BASEDIR}/${PREFIX}-opreport" && awk '
441
               {
442
                  if ( $1 == "samples" ) {
443
                     go = 1;
444
                  }
445
                  if ( go == 1 ) {
446
                     print "    " $0;
447
                     if ( printed++ == 6 ) {
448
                        exit;
449
                     }
450
                  }
451
               } ' "${BASEDIR}/${PREFIX}-opreport"
452
            ;;
453
454
         LIST)
455
            ls -lh ${BASEDIR}/${PREFIX}-*
456
            ;;
457
458
         VIEW)
459
            echo "Viewing all files"
460
            less -i ${BASEDIR}/${PREFIX}-*
461
            echo "Press a key to continue or choose a different action"
462
            ;;
463
464
         DISKSTATS)
465
            echo "Starting $PR_diskstats"
466
            $PR_diskstats "${BASEDIR}/${PREFIX}-diskstats"
467
            echo "Press a key to continue or choose a different action"
468
            ;;
469
470
         INNODB)
471
            echo "Viewing InnoDB files"
472
            less -i "${BASEDIR}/${PREFIX}-innodbstatus1"
473
            echo "Press a key to continue or choose a different action"
474
            ;;
475
476
         MEXT)
477
            echo "Displaying the first 4 samples of SHOW STATUS counters"
109.1.1 by Daniel Nichter
Change mext to pt-mext.
478
            # Grab the first 4 samples by looking for blank lines.
479
            # I'll rewrite pt-mext and this will be simpler in future.
480
            # TODO: upgrade, if pt-mext is fixed :)
109.1.2 by Daniel Nichter
Use $PR_mext instead of pt-mext.
481
            awk '/---/{if(i++>12){exit}}{print}' "${BASEDIR}/${PREFIX}-mysqladmin" | $PR_mext -r -- cat - | less -S
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
482
            echo "Press a key to continue or choose a different action"
483
            ;;
484
485
         NETWORK)
486
            echo "Source of connections to port 3306"
487
            awk '
488
               /:3306/ {
489
                  print substr($5, 0, index($5, ":") - 1);
490
               }
491
               /TS/ {
492
                  if ( i++ > 1 ) {
493
                     # Stop after the first sample
494
                     exit;
495
                  }
496
               }' "${BASEDIR}/${PREFIX}-netstat" | sort | uniq -c | sort -rn
497
            echo "Status of connections to port 3306"
498
            awk '
499
               /:3306/ {
500
                  print $6;
501
               }
502
               /TS/ {
503
                  if ( i++ > 1 ) {
504
                     # Stop after the first sample
505
                     exit;
506
                  }
507
               }' "${BASEDIR}/${PREFIX}-netstat" | sort | uniq -c | sort -rn
508
            echo "Press a key to continue or choose a different action"
509
            ;;
510
511
         INVALID)
512
            ;;
513
514
      esac
515
516
      # Capture and handle the interactive key-strokes.
196.4.1 by baron at percona
Invoke pt-diskstats with proper new options (fixes bug 945834)
517
      tput sgr0
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
518
      KEY=""
196.4.1 by baron at percona
Invoke pt-diskstats with proper new options (fixes bug 945834)
519
      if ! read -n 1 -s KEY 2>/dev/null; then
520
         echo "Error while trying to read interactive keystroke command. Exiting."
521
         exit
522
      fi
523
      case "${KEY:-}" in
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
524
         j|k)
525
            PREFIX="$(awk "
526
               BEGIN {
527
                  printed = 0;
528
               }
529
               {
530
                  prev=curr;
531
                  curr=\$1;
532
                  if ( \"j\" == \"${KEY}\" && prev == \"${PREFIX}\" && curr ~ /./ ) {
533
                     print curr;
534
                     printed = 1;
535
                     exit;
536
                  }
537
                  if ( \"k\" == \"${KEY}\" && curr == \"${PREFIX}\" && prev ~ /./ ) {
538
                     print prev;
539
                     printed = 1;
540
                     exit;
541
                  }
542
               }
543
               END {
544
                  if ( printed == 0 ) {
545
                     print \"${PREFIX}\";
546
                  }
288.1.4 by fraserb at gmail
Change TMPDIR to PT_TMPDIR
547
               }" $PT_TMPDIR/pt-sift.prefixes)"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
548
            ;;
549
         1)
550
            ACTION="DEFAULT"
551
            ;;
552
         0)
553
            ACTION="LIST"
554
            ;;
555
         '*')
556
            ACTION="VIEW"
557
            ;;
558
         d)
559
            ACTION="DISKSTATS"
560
            ;;
561
         i)
562
            ACTION="INNODB"
563
            ;;
564
         m)
565
            ACTION="MEXT"
566
            ;;
567
         n)
568
            ACTION="NETWORK"
569
            ;;
570
         q)
571
            ;;
572
         '?')
573
            print_help
574
            echo "Press any key to continue"
575
            read -n 1 -s
576
            ;;
577
         *)
578
            echo "Unknown key '${KEY}'; press ? for help"
579
            ACTION="INVALID"
580
            ;;
581
      esac
582
   done
583
109.5.3 by Brian Fraser
pt-sift: Use mktemp through the tmpdir package.
584
   rm_tmpdir
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
585
}
586
587
# Execute the program if it was not included from another file.  This makes it
588
# possible to include without executing, and thus test.
283.2.1 by fraserb at gmail
Bug 912902: basename not guaranteed to work for Shell tools
589
if    [ "${0##*/}" = "$TOOL" ] \
590
   || [ "${0##*/}" = "bash" -a "$_" = "$0" ]; then
357.2.3 by Daniel Nichter
Use non-Bashism indirect reference in parse_options. Change $@ to ${@:-} and update parse_options in all tools.
591
    main "${@:-""}"
8 by Daniel Nichter
Add Aspersa tools, their tests, and util/test-bash-tool.
592
fi
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
593
594
# ############################################################################
595
# Documentation
596
# ############################################################################
597
:<<'DOCUMENTATION'
598
=pod
599
600
=head1 NAME
601
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
602
pt-sift - Browses files created by pt-stalk.
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
603
604
=head1 SYNOPSIS
605
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
606
Usage: pt-sift FILE|PREFIX|DIRECTORY
607
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
608
pt-sift browses files created by L<pt-stalk>.  If no options are given,
322.1.3 by Daniel Nichter
Suppress ls output when it fails. Handle missing diskstats and vmstat. Shorten synopsis.
609
the tool browses all pt-stalk files in C</var/lib/pt-stalk> if that directory
610
exists, else the current working directory is used.  If a FILE is given,
611
the tool browses files with the same prefix in the given file's directory.
612
If a PREFIX is given, the tool browses files in C</var/lib/pt-stalk>
613
(or the current working directory) with the same prefix.  If a DIRECTORY
614
is given, the tool browses all pt-stalk files in it.
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
615
59 by Daniel
Add RISKS section to Bash tools. Re-order all tools' DOWNLOADING section. Remove some unused options.
616
=head1 RISKS
617
618
The following section is included to inform users about the potential risks,
619
whether known or unknown, of using this tool.  The two main categories of risks
620
are those created by the nature of the tool (e.g. read-only tools vs. read-write
621
tools) and those created by bugs.
622
623
pt-sift is a read-only tool.  It should be very low-risk.
624
625
At the time of this release, we know of no bugs that could cause serious harm
626
to users.
627
628
The authoritative source for updated information is always the online issue
629
tracking system.  Issues that affect this tool will be marked as such.  You can
630
see a list of such issues at the following URL:
631
L<http://www.percona.com/bugs/pt-sift>.
632
633
See also L<"BUGS"> for more information on filing bugs and getting help.
634
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
635
=head1 DESCRIPTION
636
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
637
pt-sift downloads other tools that it might need, such as L<pt-diskstats>,
638
and then makes a list of the unique timestamp prefixes of all the files in
322.1.2 by Daniel Nichter
Clean up pt-sift: capture signals, verify and simplify arg processing, improve error messages and docs, exit if there are no pt-stalk files.
639
the directory, as written by the L<pt-stalk> tool.  If the user specified
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
640
a timestamp on the command line, then it begins with that sample of data;
641
otherwise it begins by showing a list of the timestamps and prompting for
642
a selection.  Thereafter, it displays a summary of the selected sample, and
643
the user can navigate and inspect with keystrokes.  The keystroke commands
644
you can use are as follows:
645
646
=over
647
648
=item d
649
650
Sets the action to start the L<pt-diskstats> tool on the sample's disk
651
performance statistics.
652
653
=item i
654
655
Sets the action to view the first INNODB STATUS sample in less.
656
657
=item m
658
659
Displays the first 4 samples of SHOW STATUS counters side by side with the
660
L<pt-mext> tool.
661
662
=item n
663
664
Summarizes the first sample of netstat data in two ways: by originating host,
665
and by connection state.
666
667
=item j
668
669
Select the next timestamp as the active sample.
670
671
=item k
672
673
Select the previous timestamp as the active sample.
674
675
=item q
676
677
Quit the program.
678
679
=item 1
680
681
Sets the action for each sample to the default, which is to view a summary
682
of the sample.
683
684
=item 0
685
686
Sets the action to just list the files in the sample.
687
688
=item *
689
217.1.1 by Daniel Nichter
Fix spellings errors.
690
Sets the action to view all of the sample's files in the less program.
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
691
692
=back
693
694
=head1 OPTIONS
695
696
This tool does not have any command-line options.
13 by Daniel Nichter
Re-brand standard POD sections (DOWNLOADING, ENVIRONMENT, etc). Change ABOUT MAATKIT to ABOUT PERCONA TOOLKIT.
697
698
=head1 ENVIRONMENT
699
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
700
This tool does not use any environment variables.
13 by Daniel Nichter
Re-brand standard POD sections (DOWNLOADING, ENVIRONMENT, etc). Change ABOUT MAATKIT to ABOUT PERCONA TOOLKIT.
701
702
=head1 SYSTEM REQUIREMENTS
703
102 by Daniel
Fix pt-sift to fetch and eval programs correctly. Lists these programs in the sys reqs pod section.
704
This tool requires Bash v3 and the following programs: pt-diskstats, pt-pmp,
119 by Daniel Nichter
Merge lp:~percona-toolkit-dev/percona-toolkit/pt-align r257..260 (branch was accidently created from pt-table-checksum-2.0 branch instead of 2.0 branch).
705
pt-mext, and pt-align.  If these programs are not in your PATH,
102 by Daniel
Fix pt-sift to fetch and eval programs correctly. Lists these programs in the sys reqs pod section.
706
they will be fetched from the Internet if curl is available.
13 by Daniel Nichter
Re-brand standard POD sections (DOWNLOADING, ENVIRONMENT, etc). Change ABOUT MAATKIT to ABOUT PERCONA TOOLKIT.
707
708
=head1 BUGS
709
14 by Daniel Nichter
Replace $TOOL with tool name.
710
For a list of known bugs, see L<http://www.percona.com/bugs/pt-sift>.
13 by Daniel Nichter
Re-brand standard POD sections (DOWNLOADING, ENVIRONMENT, etc). Change ABOUT MAATKIT to ABOUT PERCONA TOOLKIT.
711
712
Please report bugs at L<https://bugs.launchpad.net/percona-toolkit>.
713
Include the following information in your bug report:
714
715
=over
716
717
=item * Complete command-line used to run the tool
718
719
=item * Tool L<"--version">
720
721
=item * MySQL version of all servers involved
722
723
=item * Output from the tool including STDERR
724
725
=item * Input files (log/dump/config files, etc.)
726
727
=back
728
729
If possible, include debugging output by running the tool with C<PTDEBUG>;
730
see L<"ENVIRONMENT">.
731
51 by Daniel Nichter
Copy and format for POD docu from Aspersa User's Manual to forked Aspersa tools.
732
=head1 DOWNLOADING
733
734
Visit L<http://www.percona.com/software/percona-toolkit/> to download the
735
latest release of Percona Toolkit.  Or, get the latest release from the
736
command line:
737
738
   wget percona.com/get/percona-toolkit.tar.gz
739
740
   wget percona.com/get/percona-toolkit.rpm
741
742
   wget percona.com/get/percona-toolkit.deb
743
744
You can also get individual tools from the latest release:
745
746
   wget percona.com/get/TOOL
747
748
Replace C<TOOL> with the name of any tool.
749
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
750
=head1 AUTHORS
751
752
Baron Schwartz
753
13 by Daniel Nichter
Re-brand standard POD sections (DOWNLOADING, ENVIRONMENT, etc). Change ABOUT MAATKIT to ABOUT PERCONA TOOLKIT.
754
=head1 ABOUT PERCONA TOOLKIT
755
756
This tool is part of Percona Toolkit, a collection of advanced command-line
757
tools developed by Percona for MySQL support and consulting.  Percona Toolkit
758
was forked from two projects in June, 2011: Maatkit and Aspersa.  Those
759
projects were created by Baron Schwartz and developed primarily by him and
760
Daniel Nichter, both of whom are employed by Percona.  Visit
761
L<http://www.percona.com/software/> for more software developed by Percona.
762
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
763
=head1 COPYRIGHT, LICENSE, AND WARRANTY
764
131 by Daniel
Build percona-toolkit-2.0.2
765
This program is copyright 2010-2011 Baron Schwartz, 2011-2012 Percona Inc.
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
766
Feedback and improvements are welcome.
767
768
THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
769
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
770
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
771
772
This program is free software; you can redistribute it and/or modify it under
773
the terms of the GNU General Public License as published by the Free Software
774
Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
775
systems, you can issue `man perlgpl' or `man perlartistic' to read these
776
licenses.
777
778
You should have received a copy of the GNU General Public License along with
779
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
780
Place, Suite 330, Boston, MA  02111-1307  USA.
13 by Daniel Nichter
Re-brand standard POD sections (DOWNLOADING, ENVIRONMENT, etc). Change ABOUT MAATKIT to ABOUT PERCONA TOOLKIT.
781
782
=head1 VERSION
783
396.1.5 by Daniel Nichter
Build percona-toolkit-2.1.5
784
pt-sift 2.1.5
12 by Daniel Nichter
Remove duplicate copyright notices. Add POD and copyright for Aspersa tools. Fix checking for "pt-pmp" instead of "pmp", etc.
785
786
=cut
787
788
DOCUMENTATION