メモベースです
Apacheからログファイルをtailするとローテートとか色々考えないといけない事が非常にめんどくさかったので色々試行錯誤してみた。
必要な設定は予めしておく
- apache
- fluentd
- selinuxとかセキュリティ周り
Apache LogFormatをこんな感じのJSONにする
LogFormat "{\"remotehost\":\"%h\",\"ident\":\"%l\",\"user\":\"%u\",\"datetime\":\"%t\",\"request\":\"%r\",\"status\":\"%>s\",\"byte\":\"%b\",\"referer\":\"%{Referer}i\",\"user_agent\":\"%{User-Agent}i\",\"time_taken\":%D}" combinedjson
CustomLog "|/opt/td-agent/bin/fluent-cat -p 24224 test" combinedjson
Fluentd側はこんな感じで適当に
<source>
type forward
port 24224
bind 0.0.0.0
tag test
<parse>
type json
</parse>
</source>
<match test>
type stdout
</match>
そうすると、動いてくれた。
2022-04-04 16:36:26.937969132 +0900 test: {"remotehost":"192.168.56.1","ident":"-","user":"-","datetime":"[04/Apr/2022:16:36:26 +0900]","request":"GET /index.html HTTP/1.1","status":"304","byte":"-","referer":"-","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36","time_taken":179}
後は好きなようにいじればOK…と、
これで煩わしいファイル管理をしなくて良くなる。
stdoutに出力する事も考えましたが、errorや他のログと分離させるのが面倒なのでポートやsocketを分けて利用する方が使いやすいかなと思った。