10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ログで遊ぶ

Posted at

個人的メモ。

こんなことをしなくてもいい世界が理想。
下手に色々調査出来ちゃうと「それでいいじゃん」ってなってしまうのでよろしくないかもしれない。

multilog編

tai64n形式のタイムスタンプを戻しながら監視する

tail -F /var/log/test/main/current | tai64nlocal

tai64n形式のタイムスタンプを戻しながら過去ログを掘る

tai64nlocal < /var/log/test/main/@4000000056de14e1323658e4.s | less

lotateしたログを全検索してついでにタイムスタンプを戻す

grep -h search_word /var/log/test/main/* | tai64nlocal

日ごとのエラー件数を出してみる(例外指定版)

grep -h NullPointerException /var/log/test/error/* | tai64nlocal |  awk '{sum[$1] += 1}; END{ for (i in sum) {print i "," sum[i]}}' | sort -nr

日ごとのエラー件数を出してみる(全件)

cat /var/log/test/error/* | tai64nlocal |  awk '{sum[$1] += 1}; END{ for (i in sum) {print i "," sum[i]}}' | sort -nr

ぶっちゃけ検索しないならwc -l使った方がいいと思う。

wc -l  /var/log/test/error/*

nginx(webサーバーアクセスログ)編

以下の設定を想定。

log_format  main '"$remote_addr" - "$remote_user" $time_local $request "$status" "$body_bytes_sent" "$request_time" "$http_referer" "$http_user_agent" "$http_x_forwarded_for" cookie="$http_cookie"';

ほどほどに見やすくログを見る

tail -F /var/log/nginx/access.log | awk '{print $4 "\t" $6 "\t" $9 "\t" $11 "\t" $10 "\t" $7}'

こんなログが出てくる。

08/Mar/2016:16:41:11   GET    200     "0.010" 5844    /assets/js/app.js

1分ごとのユニークなアクセスIP数を取る(多い時間順)

sed -e 's/:[0-9][0-9][\x20]+\+0900//g' -e 's/- - //g' /var/log/nginx/access.log | awk '{print $2, $1}' | sort | uniq | awk '{sum[$1] += 1}; END{ for (i in sum) {print i, sum[i]}}' | sort -nrk2

sedの2つめの-eから始めると秒単位で取れる(その際はログがいっぱい出るのでhead付けるなりファイルに移すこと)。

オチ

I/Oが伴うデータのやり取りするときにbashから直接コマンドブッ叩くの超楽。

とはいえ、気が付いたら複雑怪奇なコマンドチェーンの処理の山が出来て後で誰もメンテナンス出来なくなるのでとっさの要件以外でこういうことはしないように気を付けたい。

10
8
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
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?