LoginSignup
2
3

More than 5 years have passed since last update.

Fluentd ログ全体を処理しつつ特定のメッセージは別処理したい場合のtips

Posted at

はじめに

FluentdでログをCloudWatchへ転送しつつ、特定のメッセージ(緊急対応が必要なもの)が起きた場合に通知する時にどう書けばいいのかなと調べたので書きました。

環境

  • CentOS Linux release 7.3.1611 (Core)
  • td-agent-2.3.5-1.el7.x86_64

目的

下記のような目的があり、Fluentdのconf設定をしました。
- /var/log/app.log(WEBアプリケーションから出力したログ)をtailしてCloudWatch Logsへ格納したい。
- ただしメッセージ内にERROR、EMERGENCY、ALERT、CRITICALが含まれていた場合は別で処理したい。
- ログの確認はCloudWatchでおこないたい。

設定内容

td-agent.conf
<source>
    # アプリケーションログをtailしてtagをつける
    @type tail
    pos_file /tmp/app.pos
    path /var/log/app.log
    tag app.log
    format /^(?<message>.*)$/
</source>

<match app.log>
    @type copy
    <store>
        # CloudWatch Logsへの転送
        @type cloudwatch_logs
        log_group_name hogehoge_app_log
        log_stream_name app-log
        auto_create_stream true
        include_time_key true
        localtime true
    </store>
    <store>
      # copyしてラベルの付替え
      @type relabel
      @label @alert
    </store>
</match>

# 特定のラベルの処理
<label @alert>
    # filterで検索する
    <filter **.*>
        @type grep
        regexp1 message (ERROR|EMERGENCY|ALERT|CRITICAL)
    </filter>

    # filterで絞り込まれたログに対して処理を行う
    <match **.*>
        @type exec
        command /app/bin/hogehoge.sh
        format json
        buffer_path /tmp/alert_match.pos
        flush_interval 5s
    </match>
</label>

おわりに

簡単にですがCloudWatchに飛ばしつつERROR等特定のメッセージをどうにかするtipsでした。
メッセージが頻発すると辛いので、プラグインなどで抑制した方が良いかと思います。

2
3
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
2
3