Percona Toolkit by example – pt-stalk

pt-stalk recipes: Gather forensic data about MySQL when a server problem occurs

It happens to us all from time to time: a server issue arises that leaves you scratching your head. That’s when Percona Toolkit’s pt-stalk comes into play, helping you diagnose the problem by capturing diagnostic data that helps you pinpoint what’s causing the havoc hitting your database.

From the documentation (http://www.percona.com/doc/percona-toolkit/pt-stalk.html):

pt-stalk watches for a trigger condition to become true, and then collects data to help in diagnosing problems. It is designed to run as a daemon with root privileges, so that you can diagnose intermittent problems that you cannot observe directly. You can also use it to execute a custom command, or to gather the data on demand without waiting for the trigger to happen.

There are some common options that you can use in all examples, so I recommend you to read the documentation if you have any specific questions.

Be prepared! It’s wise to have pt-stalk running 24/7, because problems such as MySQL lockups or spikes of activity typically leave no evidence to use in root cause analysis. By continuously running pt-stalk, you’ll have the data it gathers when the trouble occurs.

Let’s look at some specific “pt-stalk recipes.”

Just collect the information:

pt-stalk will collect the information and will exit after that.

$ pt-stalk --no-stalk  -- --defaults-file=./my.default.cnf

Every hour for one day:

Collect the information every one hour (–sleep=3600) 24 times (–iterations=24) without wait for any condition (–threshold=0) and run in background (–daemonize).

$ pt-stalk --sleep=3600 --threshold=0 --iterations=24 \
  --daemonize -- --defaults-file=./my.default.cnf

A host has more than 10 connections:

Collect the all information when the server (–match have more than 10 (–threshold 10) connections opened. You can use any variable from the “show processlist” command, in this case, I’m using the “Host” variable.

$ pt-stalk --function processlist --variable Host\
  --match --threshold 10 -- --defaults-file=./my.default.cnf

More than one variable:

In some cases, you want to check more than one variable, in those cases, you will have to write a small scrip to do this.

The script:
The script must contain a shell function called “trg_plugin” and that function must return a number, this number will be the one that pt-stalk will use to match against the –threshold option.

$ cat  pt-stalk-function 
# This function will count the commands that come form localhost ($4 in this case) 
# and with execution time ($7) greater than 5000.
# The  $EXT_ARGV is the connection options that you will send it to the pt-stalk.
#  $2    $3         $4          $5   $6        $7     $8      $9
#| Id  | User     | Host      | db | Command | Time | State | Info             |

trg_plugin() {
	mysqladmin $EXT_ARGV processlist |  awk -F\| 'BEGIN{counter=0} 
                     { if ($4 ~ /localhost/ && $7 > 5000 ) counter++ ; } END {print counter}'

The pt-stalk command:
Collect all information when the function called trg_plugin inside the script ./pt-stalk-function (–function ../pt-stalk-function) return more than 100 (–threshold 100)

$ pt-stalk --function ./pt-stalk-function \
  --threshold 100  --  --defaults-file=./my.default.cnf

Custom collector with plugins:

Plugins are useful to collect information that it is not included in the pt-stalk by default. For example, if you want to collect pid status information from /proc/[mysqlpid]/status you can use plugins for this.

The plugin:
The script in this case contain a shell function called “before_collect” and pt-stalk will run this function before collect the information (you can collect after, before and after, etc, please check the documentation for more information)

$ cat  pt-stalk-pidplugin
# This plugin will collect the /proc/[mysqlpid]/status from all mysqld pids running in 
# this box and it will store the information in a file in the default directory and 
# with the default prefix and adding the pid number and -status at the end. 
# For example: 2013_01_02_22_58_55-2082-status

        for __p in $(pidof mysqld); do 
            cat /proc/${__p}/status > ${OPT_DEST}/${prefix}-${__p}-status ; 

The pt-stalk command:
Before collect the information, it will run the plugin ./pt-stalk-pidplugin (–plugin ./pt-stalk-pidplugin)

$ pt-stalk --plugin ./pt-stalk-pidplugin \
  --threshold 10  -- --defaults-file=./my.default.cnf

Have any comments or questions? Just let me know below!

The post Percona Toolkit by example – pt-stalk appeared first on MySQL Performance Blog.

Powered by WordPress | Theme: Aeros 2.0 by TheBuckmaker.com