Last active
August 29, 2015 14:24
-
-
Save megatk/e89f2dd061abf8a6e001 to your computer and use it in GitHub Desktop.
Apache のログをグルーピングして出力するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# 名称 | |
# groupbyApacheLog - Apacheログを指定項目でグルーピング | |
# | |
# 書式 | |
# groupbyApacheLog [-s] [-tz] target LogFile | |
# | |
# -s | |
# 静的ファイル(.js .css .html .jpg .gif .png)含める | |
# デフォルトでは除外 | |
# | |
# -tz | |
# tar.gzファイルを解凍する | |
# | |
# target | |
# どの項目をグルーピングするか? | |
# host …… リモートホスト名 | |
# ymd[his] …… 日時 | |
# request …… リクエストパス | |
# referer …… リファラー | |
# useragent …… ユーザーエージェント | |
# | |
# Logfile | |
# 出力フォーマットはcombined形式固定 | |
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined | |
# | |
# 依存関係 | |
# | |
# | |
# 解説 | |
# Apacheのログファイルを指定方法でグルーピングする | |
# 件数とグルーピング項目を出力(並びは件数降順) | |
# | |
# 戻り値 | |
# 0 正常終了 | |
# 1 ユーザーの指定間違い | |
# | |
# | |
######################################################################################### | |
CMDNAME=`basename $0` # 実行コマンド名 | |
USAGE="Usage: $CMDNAME [-s] [-tz] target LogFile" # 書式(エラーメッセージ用) | |
BASETPATH=/tmp/ | |
LOGFILE= # Apacheのファイル | |
STATIC=FALSE # 静的ファイルを除外するかどうか | |
GREPSTATIC= | |
OPTSTATIC= | |
TZ=FALSE # tar.gzを解凍するか | |
TMP=${BASETPATH}groupby.$$ # 圧縮の解凍用一時ファイル | |
trap `rm -f ${BASETPATH}*.$$; exit 1` 1 2 3 15 # 異常終了の際は一時ファイルを消す | |
# オプションの処理 | |
while : | |
do | |
case $1 in | |
-s) STATIC=TRUE | |
shift | |
;; | |
-tz) TZ=TRUE | |
shift | |
;; | |
--) shift | |
break | |
;; | |
-*) echo "$USAGE" 1>&2 | |
exit 1 | |
;; | |
*) break | |
;; | |
esac | |
done | |
# 引数が正しく指定されているか | |
if [ $# -ne 1 -a $# -ne 2 ]; then | |
echo "Error: args must only one or two" 1>&2 | |
exit 1 | |
fi | |
# ログファイルが読み込めるか | |
if [ -r "$2" ]; then | |
LOGFILE="$2" | |
else | |
# 標準入力が使えるか | |
if [ -p /dev/stdin ] ; then | |
LOGFILE='-' | |
else | |
echo "Error: Logfile cannot read" 1>&2 | |
exit 1 | |
fi | |
fi | |
# 圧縮ファイルオプション | |
if [ $TZ = TRUE ]; then | |
# ログファイルが圧縮されているか | |
IS_THAW=$(file -bi $LOGFILE | grep 'gzip' | sed -e "s/;.*//") | |
if [ $IS_THAW ]; then | |
tar -O -xzvf $LOGFILE > $TMP | |
LOGFILE=$TMP | |
fi | |
fi | |
# grep による抽出 | |
if [ $STATIC = FALSE ]; then | |
GREPSTATIC='.js \|.css \|.html \|.jpg \|.gif \|.png ' | |
OPTSTATIC='-v' | |
fi | |
# グルーピングの実処理 | |
case $1 in | |
host) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk '{print $1}' | sort | uniq -c | sort -nr | |
;; | |
ymd) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'\\[|\\]|:' '{print $2}' | sort | uniq -c | sort -nr | |
;; | |
ymdh) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'\\[|\\]|:' '{print $2,$3}' | sort | uniq -c | sort -nr | |
;; | |
ymdhi) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'\\[|\\]|:' '{printf("%s %s:%s\n",$2,$3,$4)}' | sort | uniq -c | sort -nr | |
;; | |
ymdhis) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'\\[|\\]|:' '{sub(/ .*/, "", $5); printf("%s %s:%s:%s\n",$2,$3,$4,$5)}' | sort | uniq -c | sort -nr | |
;; | |
request) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'"' '{print $2}' | sort | uniq -c | sort -nr | |
;; | |
referer) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'"' '{print $4}' | sort | uniq -c | sort -nr | |
;; | |
useragent ) | |
grep $OPTSTATIC "`echo ${GREPSTATIC:-''}`" $LOGFILE | awk -F'"' '{print $6}' | sort | uniq -c | sort -nr | |
;; | |
* ) | |
echo "Error: target not incorrect" 1>&2 | |
exit 1 | |
;; | |
esac | |
rm -f ${BASETPATH}*.$$ | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment