Skip to content

Instantly share code, notes, and snippets.

@tkuchiki
Last active August 29, 2015 14:06
Show Gist options
  • Save tkuchiki/585a7015dfda793b8c04 to your computer and use it in GitHub Desktop.
Save tkuchiki/585a7015dfda793b8c04 to your computer and use it in GitHub Desktop.
ltsv 形式の nginx ログを response time ごとに色分けで出力
#!/bin/bash
usage() {
echo "Usage: $(basename $0) -v|--value VALUE -k|--key KEY [--min MIN] [--max MAX] [-l|--log LOG]
-v, --value find value (grep pattern)
-k, --key find key (grep pattern)
-l, --log log file path
--min min response time
--max max response time
"
exit 0
}
[ $# = 0 ] && usage
OPT=$(getopt -q -o v:k:l:h -l min:,max:,key:,value:,log:,help -- "${@}")
eval set -- "${OPT}"
while :; do
case "${1}" in
-v | --value) VALUE="${2}"; shift 2 ;;
-k | --key) KEY="${2}"; shift 2 ;;
-l | --log) FILE="${2}"; shift 2 ;;
--min) MIN="${2}"; shift 2 ;;
--max) MAX="${2}"; shift 2 ;;
-h | --help) usage ;;
--) shift; break ;;
*) echo "Internal error!" 1>&2; exit 1 ;;
esac
done
MIN=${MIN:-0.1}
MAX=${MAX:-0.5}
FILE="${FILE:-/var/log/aggregated/nginx/$(ls -t1 /var/log/aggregated/nginx/ | head -n 1)}"
grep "${KEY}:""${VALUE}" ${FILE} | awk -v min=$MIN -v max=$MAX '
BEGIN{
OFS="\t";
red="\033[31m";
yellow="\033[33m";
clear="\033[0m";
}
{
res_time=$10;
status=$7;
sub("upstream_response_time:", "", res_time);
sub("status:", "", status);
if (res_time < min && status < 500) {
print $3,$10,$1,$7,$5;
} else if (res_time >= min && res_time < max && status < 500) {
print yellow $3,$10,$1,$7,$5 clear;
} else {
print red $3,$10,$1,$7,$5 clear;
}
}'
log_format ltsv 'host:$remote_addr\t'
'user:$remote_user\t'
'time:$time_iso8601\t'
'method:$request_method\t'
'uri:$request_uri\t'
'protocol:$server_protocol\t'
'status:$status\t'
'size:$body_bytes_sent\t'
'request_time:$request_time\t'
'upstream_response_time:$upstream_response_time\t'
'upstream_addr:$upstream_addr\t'
'referer:$http_referer\t'
'user_agent:$http_user_agent\t';
#!/bin/bash
usage() {
echo "Usage: $(basename $0) [--min MIN] [--max MAX] [-l|--log LOG]
-l, --log log file path
--min min response time
--max max response time
"
exit 0
}
OPT=$(getopt -q -o l:h -l min:,max:,log:,help -- "${@}")
eval set -- "${OPT}"
while :; do
case "${1}" in
-l | --log) FILE="${2}"; shift 2 ;;
--min) MIN="${2}"; shift 2 ;;
--max) MAX="${2}"; shift 2 ;;
-h | --help) usage ;;
--) shift; break ;;
*) echo "Internal error!" 1>&2; exit 1 ;;
esac
done
MIN=${MIN:-0.1}
MAX=${MAX:-0.5}
FILE="${FILE:-/var/log/aggregated/nginx/$(ls -t1 /var/log/aggregated/nginx/ | head -n 1)}"
tail -F $FILE | awk -v min=$MIN -v max=$MAX '
BEGIN{
OFS="\t";
red="\033[31m";
yellow="\033[33m";
clear="\033[0m";
}
{
res_time=$10;
sub("upstream_response_time:", "", res_time);
if (res_time < 0.1) {
print $3,$10,$1,$7,$5;
} else if (res_time >= 0.1 && res_time < 0.5) {
print yellow $3,$10,$1,$7,$5 clear;
} else {
print red $3,$10,$1,$7,$5 clear;
}
}'
#!/bin/bash
usage() {
echo "Usage: $(basename $0) -s|--start START [-e|--end END] [--min MIN] [--max MAX] [-l|--log LOG]
-s, --start start at
-e, --end end at
-l, --log log file path
--min min response time
--max max response time
"
exit 0
}
[ $# = 0 ] && usage
OPT=$(getopt -q -o s:e:l:h -l min:,max:,start:,end:,log:,help -- "${@}")
eval set -- "${OPT}"
while :; do
case "${1}" in
-s | --start) START="${2}"; shift 2 ;;
-e | --end) END="${2}"; shift 2 ;;
-l | --log) FILE="${2}"; shift 2 ;;
--min) MIN="${2}"; shift 2 ;;
--max) MAX="${2}"; shift 2 ;;
-h | --help) usage ;;
--) shift; break ;;
*) echo "Internal error!" 1>&2; exit 1 ;;
esac
done
END=${END:-$(date "+%Y-%m-%dT%H:%M:%S")}
START_TIME=`date -d "${START}" "+%Y%m%d%H%M%S"`;
END_TIME=`date -d "${END}" "+%Y%m%d%H%M%S"`;
MIN=${MIN:-0.1}
MAX=${MAX:-0.5}
FILE="${FILE:-/var/log/aggregated/nginx/$(ls -t1 /var/log/aggregated/nginx/ | head -n 1)}"
grep "${KEY}:""${VALUE}" ${FILE} | awk -v start=$START_TIME -v end=$END_TIME -v min=$MIN -v max=$MAX '
BEGIN{
OFS="\t";
red="\033[31m";
yellow="\033[33m";
clear="\033[0m";
}
{
time=$4;
gsub(/time|[:T+\-]|09:00/, "", time);
res_time=$12;
api_status=$10;
sub("upstream_response_time:", "", res_time);
sub("api_status:", "", api_status);
if (start <= time && end >= time) {
if (res_time < min && api_status < 500) {
print bold $3,$10,$1,$7,$5 normal;
} else if (res_time >= min && res_time < max && api_status < 500) {
print yellow $3,$10,$1,$7,$5 clear;
} else {
print red $3,$10,$1,$7,$5 clear;
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment