LoginSignup
2
1

More than 1 year has passed since last update.

ELBのアクセスログを手動で解析する

Last updated at Posted at 2017-08-21

前提

ELB

以下が設定されていること

  • アクセスログ
    • S3 の場所 log-bucket-name / prefix

ARNの番号を取得

  • ARN
    • arn:aws:elasticloadbalancing:ap-northeast-1:account-id:loadbalancer/app/名前/XXXXX

AWS Credentials

以下のファイルが設定されていること

~/.aws/credentials

download

1日分

aws s3 cp s3://[log-bucket-name]/[prefix]/AWSLogs/[account-id]/elasticloadbalancing/ap-northeast-1/[YYYY]/[MM]/[DD] . --recursive

ひと月分

aws s3 cp s3://[log-bucket-name]/[prefix]/AWSLogs/[account-id]/elasticloadbalancing/ap-northeast-1/[YYYY]/[MM]/ . --recursive

ひと月分は多いぜ、って時は

  • 01日〜09日

    aws s3 cp s3://[log-bucket-name]/[prefix]/AWSLogs/[account-id]/elasticloadbalancing/ap-northeast-1/[YYYY]/[MM]/ . --recursive --exclude "*" --include "*_YYYYMM0*"
    
  • 10日〜19日

    aws s3 cp s3://[log-bucket-name]/[prefix]/AWSLogs/[account-id]/elasticloadbalancing/ap-northeast-1/[YYYY]/[MM]/ . --recursive --exclude "*" --include "*_YYYYMM1*"
    
  • 20日〜29日

    aws s3 cp s3://[log-bucket-name]/[prefix]/AWSLogs/[account-id]/elasticloadbalancing/ap-northeast-1/[YYYY]/[MM]/ . --recursive --exclude "*" --include "*_YYYYMM2*"
    

展開

find . -name "*gz" -exec gunzip {} {} \;

解析例①:検索

find . -name "*\.log" | xargs grep "検索文字列" > グレップ.txt

解析例②:特定の1時間に来たリクエストを、エンドポイントごとに集計

2020/01/01の9時台(UTC)に来たリクエストの場合

ag "https 2020-01-01T09:" *.log > 2020010109.csv
# リクエストごとに集計して、少ない順にソート
# path parameterの場合は、別途sedとか必要
cat 2020010109.csv | awk -F "\"" '{print $2}' | awk -F "?" '{print $1}' | sort | uniq -c | sort -n

解析例③:とあるパスの平均レスポンスタイムを取得

2020/01/01の9時台(UTC)に来たリクエストの場合

ag "https 2020-01-01T09:" *.log > 2020010109.csv

# リクエスト総数
REQSUM=`ag "とあるパス" 2020010109.csv | wc -l`
# レスポンス合計
RESSUM=`ag "とあるパス" 2020010109.csv | awk  '{print $7}' | awk '{sum=sum+$1}END{print sum}'`
# 平均(小数第3位まで)
echo "scale=3; ${RESSUM} / ${REQSUM}" | bc

解析例④:URLエンコードされてるので、デコードしたい

全部

find . -name "*\.log" | while read -r fname; do echo -e ${REPLY//%/\\x}; done

1ファイル

while read; do echo -e ${REPLY//%/\\x}; done < ファイル名.log
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1