1 Star 0 Fork 11

徐凯文/script

forked from mo-shan/script 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
slow_tool.py 3.35 KB
一键复制 编辑 原始数据 按行查看 历史
moshan 提交于 2019-11-28 10:52 . add slow_tool
#!/usr/bin/python
#coding : utf-8
#File Name :
#Author : moshan
#Mail : mo_shan@yeah.net
#Created Time: 2019-11-28 10:52:05
#Function :
#########################################################################
# anemometer collection script to gather and digest slow query logs
# this is a quick draft script so please give feedback!
#
# basic usage would be to add this to cron like this:
# */5 * * * * anemometer_collect.sh --interval 15 --history-db-host anemometer-db.example.com
#
# This will have to run as a user which has write privileges to the mysql slow log
#
# Additionally there are two sets of permissions to worry about: The local mysql instance, and the remote digest storage instance
# These are handled through defaults files, just create a file in the: my.cnf format such as:
# [client]
# user=
# password=
#
# use --defaults-file for permissions to the local mysql instance
# and use --history-defaults-file for permissions to the remote digest storage instance
#
#
slow_log= instance_port=3306
digest='/opt/script/pt-query-digest'
IPADDR=$(ip addr show em1 | grep "inet " | awk '{print $2}' | sed 's/\/.*//g')
NOW=$(date +"%F_%H:%M")
history_db_host=slow.db-mljr.com
history_db_port=3308
history_db_name='slow_query_log'
help () {
cat <<EOF
Usage: $0 --interval <seconds>
Options:
--slow-log-file The slow log file location
--instance-port The mysql instance port number
--history-db-host Hostname of anemometer database server
--history-db-port Port of anemometer database server
--history-db-name Database name of anemometer database server (Default slow_query_log)
EOF
}
while test $# -gt 0
do
case $1 in
"--pt-query-digest"|"-d")
digest=$2
shift
;;
"--help")
help
exit 0
;;
"--history-db-host")
history_db_host=$2
shift
;;
"--history-db-port")
history_db_port=$2
shift
;;
"--history-db-name")
history_db_name=$2
shift
;;
"--slow-log-file"|"-s")
slow_log=$2
shift
;;
"--instance-port"|"-p")
instance_port=$2
shift
;;
*)
echo >&2 "Invalid argument: $1"
;;
esac
shift
done
if [ ! -e "${digest}" ];
then
echo "Error: cannot find digest script at: ${digest}"
exit 1
fi
if [ ! -e "${slow_log}" ];
then
echo "Error: cannot find slow log file at: ${slow_log}"
exit 1
fi
qcount=$(grep 'Query_time' $slow_log | wc -l)
if [ -z "$qcount" ] || [ $qcount -eq 0 ]; then
exit 1
fi
echo " Processing log and saving to ${history_db_host}:${history_db_port}/${history_db_name}"
dirpath=/opt/data/mysql_slow_log
mkdir -p $dirpath/$(date +%F)
find $dirpath -mtime +15 -exec rm -rf {} \;
# process the log
"${digest}" --user=anemometer --password=user_anemometer \
--review h="${history_db_host}",P="${history_db_port}",D="$history_db_name",t=global_query_review \
--history h="${history_db_host}",P="${history_db_port}",D="$history_db_name",t=global_query_review_history \
--no-report --limit=0\% \
--filter="\$event->{Bytes} = length(\$event->{arg}) and \$event->{hostname}=\"$IPADDR:$instance_port\" " \
"${slow_log}"
/bin/cp -a -f $slow_log $dirpath/$(date +%F)/slow.log-${instance_port}_${NOW}
> $slow_log
#*/5 * * * * /opt/script/anemometer_collect.sh -s /data/mysql/3321/slow.log -p 3321
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kevinx_xu/script.git
git@gitee.com:kevinx_xu/script.git
kevinx_xu
script
script
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385