事前準備
使ったfluentdのプラグイン
- fulentd-plugin-elasticsearch
- fluentd-plugin-slack
- fluentd-plugin-rewrite-tag-filter
- t-plugin-mysqlslowquery
slackのwebhookの設定
http://xxxx.slack.com/services/newの下の方にある「incomming WebHooks」のリンクからwebhook urlの情報を取得します
fluent.confの設定
fluent.conf
<source>
type tail
path log/rails_application_development.log
tag slack
format multiline
format_firstline /^., /
format1 /^., \[(?<time>[^\.]+).+\][ ]+(?<level>[^ ]+) -- :(?<message>.*)$/
time_format %Y-%m-%dT%H:%M:%S
</source>
<match rails.*>
host 192.***.**.***
port 9200
index_name famiphoto
type_name fluentd
type elasticsearch
</match>
<match slack>
type slack
webhook_url https://hooks.slack.com/services/***********/*********
channel general
username rails_dog
flush_interval 5s
icon_emoji :dog:
message_keys level,message
message "[%s] %s"
</match>
elasticsearchにログを入れる設定と同じくslackにapplicationログの内容を通知する設定。実際はslackに通知するのはエラーの場合だけでいいけど。
一旦はメモとして残す。
同じ要領でslow-logも通知するようにする。
mysqlのスローログを通知する設定(仮)
my.cnfにスローログをファイルに出す設定にしておいて、fluent.confを編集します。
fluent.conf
<source>
type mysql_slow_query
path log/mysql_query.log
tag mysql_slow_query_slack_notifier
</source>
<match mysql_slow_query_slack_notifier>
type slack
webhook_url https://hooks.slack.com/services/***********
channel general
username mysql_slowlog_ghost
icon_emoji :ghost:
color danger
message "Query_time:%s\r\nLock_time:%s\r\nRows_sent:%s\r\nRows_examined:%s"
message_keys Query_time,Lock_time,Rows_sent,Rows_examined
flush_interval 1m
</match>
こんな感じになります。color dangerを指定しているのでslack上で赤い表示がされます。