1
0

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 1 year has passed since last update.

Log AnalyticsにApacheのアクセスログを保管する

Posted at

試したこと

Apacheのアクセスログを長期保管や解析のためにLogAnalyticsに保管してみた。

手順

アクセスログをJSON化する

Log Analyticsに取り込む際にデフォルトのフォーマットでは扱いづらいので、JSON化する。
またローテーションにおいて循環ログ(access.logを使い回すなど)は推奨されないので、日付を付与したログファイル名でローテーションをかける。

/etc/httpd/conf/httpd.conf
LogFormat "{\"Timestamp\":\"%{%Y-%m-%dT%H:%M:%S}t.%{msec_frac}t\", \"RemoteIP\":\"%a\", \"Host\":\"%V\", \"Port\":\"%p\", \"Request\":\"%U\", \"Query\":\"%q\", \"Size\":\"%B\", \"Method\":\"%m\", \"Status\":\"%s\", \"UserAgent\":\"%{User-agent}i\", \"Referer\":\"%{Referer}i\"}" json
CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_%Y%m%d.json 86400" json

ログ出力のサンプルはこんな感じとなる。

access_20221229.json
{"Timestamp":"2022-12-29T14:28:59.769", "RemoteIP":"172.31.254.9", "Host":"10.162.2.102", "Port":"80", "Request":"/index.php", "Query":"", "Size":"25", "Method":"GET", "Status":"200", "UserAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54", "Referer":"-"}
{"Timestamp":"2022-12-29T14:29:48.686", "RemoteIP":"172.31.254.9", "Host":"centos.localdomain", "Port":"80", "Request":"-", "Query":"", "Size":"0", "Method":"-", "Status":"408", "UserAgent":"-", "Referer":"-"}

Log Analyticsにカスタムログ設定をする

あとで書く

Log Analyticsのクエリを作成する

標準では"RawData"にまるっと一行が入る。
image.png

このままでは検索や解析がしづらいので、パースするクエリを書く
※ローカル時間をUTC時間に変換するために -9h するのがミソ

httpd_accesslog_json_CL
| extend d = parse_json(RawData)
| extend Timestamp = todatetime(d.Timestamp) - 9h
| extend RemoteIP = d.RemoteIP
| extend Host = d.Host
| extend Port = d.Port
| extend Request = d.Request
| extend Query = d.Query
| extend Size = tolong(d.Size)
| extend Method = d.Method
| extend Status = d.Status
| extend UserAgent = d.UserAgent
| extend Referer = d.Referer
| project Timestamp, RemoteIP, Host, Port, Request, Query, Size, Method, Status, UserAgent, Referer

以下のように整形されて出力できる。
image.png

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?