td-agent-bitでapacheのエラーログを検知してslackに通知させる
td-agent-bitでapacheのエラーログが出力されるとslackに通知が送られるように、raspberry piに設定をしました。(ずいぶんと不恰好な実装になってしまいましたが。)なぜ、td-agentではなくて、td-agent-bitなのかというとサーバ内でのメモリの消費をできる限り抑えたかったからです。td-agent-bitを利用した設定に関する日本語の記事はほとんど見かけない(td-agentはよく見るけど)ので、誰かの役に立つことを祈って残しておきます。
fluentdとfluent-bitの違いはこちらを参照
Fluentd & Fluent Bit |
td-agent-bitのインストールと設定
Raspberry Pi · Fluent Bit v0.11 Documentation
- td-agent-bit.confでは、apacheのerror.logをみて、ログレベルがerror, crit, alert, emergの時はoutput.txtに出力させるように設定をしています。
- FILTERのMatchは、basic regular expressionの正規表現がサポートされているようで、|を使ったOR表現は利用できなかった。 結構ハマったので注意。
POSIX BRE does not support any other features. Even alternation is not supported.
http://www.regular-expressions.info/posix.html
# make directory
mkdir -p /home/pi/script/apache_error_log_check
# server gpg key
wget -qO - http://packages.fluentbit.io/fluentbit.key | sudo apt-key add -
# update your sources lists
echo "deb http://packages.fluentbit.io/raspbian jessie main" >> /etc/apt/sources.list
# update repository database
apt-get update
# install td-agent-bit
apt-get install td-agent-bit
# td-agent configuration for apache log
vi /etc/td-agent-bit/td-agent-bit.conf
----------------------------------------------------------------------------------------------
[SERVICE]
Flush 5
Daemon Off
Log_Level info
Parsers_File parsers.conf
# error level
[INPUT]
Name tail
Path /var/log/apache2/error.log
Tag apache2.error_log
Parser apache_error
[FILTER]
Name grep
Match apache2.error_log
Regex level error
[OUTPUT]
Name file
Match apache2.error_log
Path /home/pi/script/apache_error_log_check/output.txt
# crit level
[INPUT]
Name tail
Path /var/log/apache2/error.log
Tag apache2.crit_log
Parser apache_error
[FILTER]
Name grep
Match apache2.crit_log
Regex level crit
[OUTPUT]
Name file
Match apache2.crit_log
Path /home/pi/script/apache_error_log_check/output.txt
# alert level
[INPUT]
Name tail
Path /var/log/apache2/error.log
Tag apache2.alert_log
Parser apache_error
[FILTER]
Name grep
Match apache2.alert_log
Regex level alert
[OUTPUT]
Name file
Match apache2.alert_log
Path /home/pi/script/apache_error_log_check/output.txt
# emerg level
[INPUT]
Name tail
Path /var/log/apache2/error.log
Tag apache2.emerg_log
Parser apache_error
[FILTER]
Name grep
Match apache2.emerg_log
Regex level emerg
[OUTPUT]
Name file
Match apache2.emerg_log
Path /home/pi/script/apache_error_log_check/output.txt
----------------------------------------------------------------------------------------------
# enable td-agent daemon
systemctl list-unit-files | grep td-agent
systemctl enable td-agent-bit
systemctl list-unit-files | grep td-agent
# start td-agent-bit
service td-agent-bit start
service td-agent-bit statusこの設定だけでは、output.txtにerror, crit, alert, emergレベルのログが出力されるだけなのでslackに通知するにはまだ設定が足りません。本当は、td-agent-bitのHTTP outputを使いたくてtd-agent-bit.confに設定を試みたのですが、どうしてもPOSTメソッドで正しい形式でデータを送る設定をすることができなかったので、苦肉の策でoutput.txtのハッシュ値を調べて、変化があったらslackに通知するhellscriptを書きました。
HTTP · Fluent Bit v0.11 Documentation
上記のファイルを、cronで1分ごとに実行するように設定すればapacheのエラーログを検知できるようになります。ただ、これなら無理してtd-agent-bitを使わなくてもshellscriptだけでもいけるような。。。
# set cron job mkdir -pv /home/pi/script/apache_error_log_check cd /home/pi/script/apache_error_log_check && pwd # put apache-error-log-check.sh script vi apache-error-log-check.sh chmod 744 apache-error-log-check.sh # edit crontab crontab -e ---------------------------------------------------------------------------------------------- # apache error log detection * * * * * sh /home/pi/script/apache_error_log_check/apache-error-log-check.sh ---------------------------------------------------------------------------------------------- # test slack alert notification curl http://localhost/-------------------------------------------------------------------------------