Skip to content

Instantly share code, notes, and snippets.

@iitenkida7
Created July 26, 2017 16:12
Show Gist options
  • Save iitenkida7/09312a5eaf6ec6b4395249de4b551e83 to your computer and use it in GitHub Desktop.
Save iitenkida7/09312a5eaf6ec6b4395249de4b551e83 to your computer and use it in GitHub Desktop.
スロークエリ解析サンプル
#!/bin/bash
#以下のサイトの丸パクリです。感謝
#http://fukata.org/2009/04/28/mysql-slow-query-output-one-line/
###################################################################
#
# 当スクリプトでは、以下のことを行います。
# mysql側から出力されるslow-queryを1行に出力し
# sortコマンドなどと併用し、スロークエリの解析を補助する。
#
# 実行例:
#
# sh analytics_mysql_query.sh /var/log/slow_query.log |sort -nr -k3
#
###################################################################
# 解析するログファイル
LOG_FILE=${1?'log file empty.'}
# trコマンドのオプション
TR_OPTION='\r\n\v'
# 区切り文字
DELIM='\t'
# ログファイルの存在・読込チェック
if [ -d $LOG_FILE ] || [ ! -s $LOG_FILE ] ; then
echo "${LOG_FILE} is directory or empty."
elif [ ! -r $LOG_FILE ] && [ -f $LOG_FILE ]; then
echo "${LOG_FILE} is not readable."
fi
analytics() {
// 現在出力中のログが1行に属するか
at_line=0
while read line; do
case $line in
# 「#」から始まるログ
'#'*)
if [ $at_line = 0 ]; then
echo ''
echo -ne "${line}${DELIM} " |tr -d ${TR_OPTION}
elif [ $at_line = 1 ]; then
sql=0
echo -ne "${DELIM}${line} " |tr -d ${TR_OPTION}
fi
;;
# その他
*)
echo -ne ' '$line |tr -d ${TR_OPTION}
sql=1
;;
esac
done
echo ''
}
analytics < $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment