LoginSignup
21
22

More than 5 years have passed since last update.

FluentdでApacheのエラーログを収集する正規表現

Last updated at Posted at 2014-09-12

Apache のエラーログを in_tail プラグインで収集する設定サンプルを紹介します。

fluent.conf (td-agent.conf)

fluentd < v0.10.53 OR td-agent < v1.1.21

<source>
  type   tail
  path   /var/log/httpd/error_log
  format /^(\[(?<time>[^\]]*)\] \[(?<level>[^\]]+)\] (\[client (?<host>[^\]]*)\] )?(?<message>.*)|(?<message>.*))$/
  time_format %a %b %d %H:%M:%S %Y
  tag    apache.error
  pos_file /var/log/td-agent/apache_error.pos
</source>

# 送り先を Fluentd の標準ログへ出力します
<match apache.error>
  type stdout
</match>

fluentd >= v0.10.53 (2014/08/21) OR td-agent v1.1.21 (or later) OR td-agent v2

<source>
  type   tail
  path   /var/log/httpd/error_log
  format apache_error
  tag    apache.error
  pos_file /var/log/td-agent/apache_error.pos
</source>

# 送り先を Fluentd の標準ログへ出力します
<match apache.error>
  type stdout
</match>

Apache の error_log サンプル

こちらは CentOS 6 にインストールした Apache 2 から出力されるエラーログです。

$ sudo tail -f /var/log/httpd/error_log
[Tue Aug 05 15:48:43 2014] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Tue Aug 05 15:48:43 2014] [notice] Digest: generating secret for digest authentication ...
[Tue Aug 05 15:48:43 2014] [notice] Digest: done
[Tue Aug 05 15:48:43 2014] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Tue Aug 05 15:48:56 2014] [error] [client ::1] Directory index forbidden by Options directive: /var/www/html/

取り込まれた error_log のサンプル

type stdoutと設定したので、 構造化されたログは Fluentd の標準出力へ次のように出力されます。

実際には out_forward プラグインを用いて、ログの集約をする Fluentdインスタンス(Aggregator)へ送り、適宜ログの保存や活用を行いましょう。

$ sudo tail -f /var/log/td-agent/td-agent.log
2014-08-05 15:48:43 +0900 apache.error: {"level":"notice","message":"suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)"}
2014-08-05 15:48:43 +0900 apache.error: {"level":"notice","message":"Digest: generating secret for digest authentication ..."}
2014-08-05 15:48:43 +0900 apache.error: {"level":"notice","message":"Digest: done"}
2014-08-05 15:48:43 +0900 apache.error: {"level":"notice","message":"Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations"}
2014-08-05 15:48:56 +0900 apache.error: {"level":"error","host":"::1","message":"Directory index forbidden by Options directive: /var/www/html/"}
21
22
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
21
22