您的位置:首页 > 数据库 > MySQL

使用pt-stalk分析MySQL的性能波动 (转)

2016-12-29 12:14 417 查看
简介
在MySQL服务器出现短暂(5~30秒)的性能波动的时候,一般的性能监控工具都很难抓住故障现场,也就很难收集对应较细粒度的诊断信息。另外,如果这种波动出现的频率很低,例如几天才一次,我们也很难人为的抓住现场,收集数据。这正是pt-stalk所解决的问题。

参数

–function:设置触发条件,包括status、processlist、自定义脚本,详细见触发条件部分。

–dest:设置collect信息的存储目录,默认/var/lib/pt-stalk/。



另外设置–dest /u01/mysql选项到mysql目录之后,pt-stalk在清理超过期限的日志时,会暴力的将该目录下所有修改时间超过一定日期的文件全部删掉,

因此在设置dest目录时必须慎重(如果设置最好是一个独立的目录,而不是跟mysql或者其他设置在同一个目录)。–dest默认值是

查看pt-stalk的源码:



# Delete collect files which more than --retention-time days old.
find "$dir" -warn -type f -mtime +$retention_time -exec rm -f '{}' \;
–iterations:该参数指定pt-stalk在收集几次故障现场后就退出。默认pt-stalk会一直运行。

–run-time:触发收集后,该参数指定收集多长时间的数据。默认是30秒,比如show processlist会连续收集30次。

–sleep:为防止一直触发收集数据,该参数指定在某次触发后,必须sleep一段时候才继续观察并触发收集。默认是300秒

–interval:默认情况pt-stalk会每隔一秒检查一次状态数据,判断是否需要触发收集。该参数指定间隔时间,默认是1秒。

–cycles:默认情况pt-stalk只有连续观察到五次状态值满足触发条件时,才触发收集。该参数控制,需要连续几次满足条件,收集被触发,默认是5次。

–verbose:设置log的输出级别,默认是2;第一次运行可以设置为3,方便观察情况。

LEVEL PRINTS

===== =====================================

0 Errors

1 Warnings

2 Matching triggers and collection info

3 Non-matching triggers

–plugin:和–function参数类似,可以指定一个包含before_collect、after_collect等函数的shell脚本。

pt-stalk的触发条件

三种触发条件,通过参数function设置:

status

–function status –variable Threads_connected –threshold
2500,表示MySQL状态值Threads_connected超过2500时触发数据收集。常用的触发条件还可以使用Threads_running等。

processlist

–function processlist –variable State –match statistics –threshold 10,表示,show processlist中State列的值为statistics的线程数超过10则触发收集。

自定义脚本

包含 trg_plugin函数的shell脚本, trg_plugin 函数必须返回一个int值,比如下面的脚本,因为slow log的status是一个累加值,不能分析差值,因此写小脚本来获取差值,对slow突然出现的尖刺进行分析。

function trg_plugin(){
current_slow=`mysql -uroot -pxxx -P5002
-S/opt/tmp/mysql5002.sock -e"show global status like 'Slow_queries'" -B
-N|awk '{print $2}'`
current_timestamp=`date '+%s'`
#when first execute,the last_timestamp is empty
if [ ! -e Slow_queries ];then
echo "$current_timestamp $current_slow">Slow_queries
echo "0"
exit 0
fi
last_timestamp=`cat Slow_queries|awk '{print $1}'`
last_slow=`cat Slow_queries|awk '{print $2}'`
echo "$current_timestamp $current_slow">Slow_queries
let diff_timestamp=$current_timestamp-$last_timestamp
let diff_slow=$current_slow-$last_slow
#echo "$diff_timestamp $diff_slow"
if [ $diff_timestamp -gt 11 ];then
echo "0"
else
echo $diff_slow
fi
}
实际例子

pt-stalk --daemonize
--function=slow_log_status.sh --variable=my_custemed_condition --cycles=1
--threshold=2 --interval=10 --socket=/opt/tmp/mysql5002.sock
--verbose=3
收集结果

可以看到收集的信息非常的全面,包括大量的系统状态(IO、CPU、网卡)等以及大量MySQL信息(processlist、innodb status、variables等)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: