LoginSignup
0
1

More than 5 years have passed since last update.

itamae|webサーバーにfluentd(td−agent)をインストールして、Nginxのログを収集するように設定変更するレシピ

Last updated at Posted at 2016-09-14

概要

以前の投稿で構築したのCentOS7のWebサーバから、fluentdを使ってNginxのログを収集するように設定を追加する

構築ネットワーク

▪️ 192.168.56.101
centos7-admin(管理サーバー)
itamae実行サーバーとfluentdでのログ集積サーバー

▪️ 192.168.56.102
centos7-web(webサーバー)
Nginxをセットアップしてあるwebサーバー、fluentdでログ送信サーバーの設定を追加する

前準備

  • nginxのログのフォーマットを変更する

    log_format     main  '$remote_addr $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for" '
                         '$request_time $upstream_response_time';
    
  • nginxのログの名前、パスを確実に指定していく

    access_log /var/log/nginx/80.access.log  main;
    error_log  /var/log/nginx/80.error.log;
    
    access_log /var/log/nginx/443.access.log  main;
    error_log  /var/log/nginx/443.error.log;
    
  • nginxのログディレクトリを他のユーザーアカウントで読めるように変更

    chmod 655 /var/log/nginx
    

本編

  • fluentdはrubygem版ではなく、td−agent版でインストール
    実行ユーザーがtd−agentになるので、データの読み込みパーミッションに注意

  • nginxのログ読み込みは、pluginのtail−exを使用
    以前、検証した際に、 標準のtailで満たせなかった要件がtail−exで満たせたという記憶があり、(どういう要件かは失念)tail−exが使い勝手がいい気がするので使用することにする

  • pluginのhostnameでデータにもhostnameを埋め込む
    fluent取得先での利用方針がまで固まっておらず、tagだけより使い勝手を向上させるためにhostnameを使用して、tagだけでなく、データにもhostnameを追加する

  • confファイルの記載方針としてincludeを使用する
    1つのファイルに全て記載してファイルが大きくなると、編集ミスのリスクが増えるので、設定単位で1ファイルづつに分割し、includeしていくようにする

  • LAN内通信のみを前提とするので、通信方法を特に凝らない
    LAN内の収集サーバーへの通信のみなので、暗号化など考えず平文通信のままでOKとする


本稿で使用したレシピはgithubに上げてある

リポジトリの追加

treasuredataオフィシャルのリポジトリの追加

td.rb
execute "Add td repo" do
    action  :run
    command "rpm --import https://packages.treasuredata.com/GPG-KEY-td-agent"
end

template "/etc/yum.repos.d/td.repo" do
    action :create
    owner  "root"
    group  "root"
    mode   "644"
    source "files/td.repo"
end

(管理サーバーから実行)

cd ~/itamae_cookbooks/repos

itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx td.rb

fluentd(td-agent)の追加

  1. td-agentの追加
  2. pluginの追加
  3. 設定ファイル設置
  4. td-agentの自動起動設定/開始
fluent.rb
package "td-agent" do
    action :install
end

[
    "fluent-plugin-hostname",
    "fluent-plugin-tail-ex",
].each{| plugin |
    execute plugin do
        action  :run
        command "/sbin/td-agent-gem install #{plugin}"
    end
}

[
    "source.d",
    "match.d",
].each{| dir |
    directory "/etc/td-agent/#{dir}" do
        action :create
        owner  "td-agent"
        group  "td-agent"
        mode   "777"
    end
}

[
    ["80.access.conf",             "/source.d/"],
    ["80.error.conf",              "/source.d/"],
    ["443.access.conf",            "/source.d/"],
    ["443.error.conf",             "/source.d/"],
    ["tcp.conf",                   "/source.d/"],
    ["socket.conf",                "/source.d/"],
    ["debug.conf",                 "/match.d/" ],
    ["hostname.conf",              "/match.d/" ],
    ["forward_centos7-admin.conf", "/match.d/" ],
    ["td-agent.conf",              "/"        ],
].each{| file_ary |
    template "/etc/td-agent#{file_ary[1]}#{file_ary[0]}" do
        action  :create
        owner   "td-agent"
        group   "td-agent"
        mode    "644"
        source  "files#{file_ary[1]}#{file_ary[0]}"
    end
}

service "td-agent" do
    action [:enable, :restart]
end

(管理サーバーから実行)

cd ~/itamae_cookbooks/fluent

itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx fluent.rb

確認作業

(webサーバーで)nginxの起動確認


ps axwww|grep nginx|grep -v grep

#=>  911 ?        Ss     0:00 nginx: master process /usr/sbin/nginx
#=>  912 ?        S      0:00 nginx: worker process
#=>  913 ?        S      0:00 nginx: worker process
#=>  914 ?        S      0:00 nginx: worker process
#=>  915 ?        S      0:00 nginx: worker process

(webサーバーで)td-agentの起動確認


ps axwww|grep td-agent|grep -v grep

#=>   1031 ?        Sl     0:00 /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --log /var/log/td-agent/td-agent.log --use-v1-config --group td-agent --daemon /var/run/td-agent/td-agent.pid
#=>   1034 ?        Sl     0:02 /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent --log /var/log/td-agent/td-agent.log --use-v1-config --group td-agent --daemon /var/run/td-agent/td-agent.pid

(webサーバーで)td-agentの設定確認

td-agent.logを確認する

cat /var/log/td-agent/td-agent.log

# fluentd(td-agent)の起動ログ
2016-09-15 01:29:16 +0900 [info]: starting fluentd-0.12.26

.....
.....

# 追加したプラグインの読み込み状態
2016-09-15 01:29:16 +0900 [info]: gem 'fluent-plugin-hostname' version '0.0.2'
.....

2016-09-15 01:29:16 +0900 [info]: gem 'fluent-plugin-tail-ex' version '0.1.1'

# 読み込んだ設定ファイルレコード
2016-09-15 01:29:17 +0900 [info]: using configuration file: <ROOT>
  <source>
.....
.....
  </match>
</ROOT>

# tail(tajil−ex)しているファイル
2016-09-15 01:29:17 +0900 [info]: following tail of /var/log/nginx/80.access.log
2016-09-15 01:29:17 +0900 [info]: following tail of /var/log/nginx/80.error.log
2016-09-15 01:29:17 +0900 [info]: following tail of /var/log/nginx/443.access.log
2016-09-15 01:29:17 +0900 [info]: following tail of /var/log/nginx/443.error.log

ブラウザでアクセスして、収集サーバー側のfluentdにデータが送られてくることを確認

とりあえず、現時点での収集サーバー側での活用方法に関する仕様が未定のため、収集サーバーでは、最低限

  • 全IPからポート24224(fluentd既定)からを無条件に受け入れる設定

    <source>
    @type forward
    port 24224
    bind "0.0.0.0"
    </source>
    
  • tagが *.nginx.** のデータについて標準出力に表示する設定

    <match *.nginx.**>
    @type stdout
    </match>
    

の2つのみ設定して verbose level traceで起動しながら標準出力を監視する

# verbose level trace
ta-agent -vv

#=> 2016-09-15 01:54:40 +0900 addhost.nginx.80.access.centos7-web: {"remote":"192.168.56.1","user":"-","method":"GET","path":"/","code":"200","size":"612","referer":"-","agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0","http_x_forwarded_for":"-","request_time":"0.000","upstream_response_time":"-","hostname":"centos7-web"}
.....
#=> 2016-09-15 01:54:50 +0900 addhost.nginx.80.access.centos7-web: {"remote":"192.168.56.1","user":"-","method":"GET","path":"/auth/","code":"200","size":"15","referer":"-","agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0","http_x_forwarded_for":"-","request_time":"0.000","upstream_response_time":"-","hostname":"centos7-web"}
.....
#=> 2016-09-15 01:56:10 +0900 addhost.nginx.443.access.centos7-web: {"remote":"192.168.56.1","user":"ldusr01","method":"GET","path":"/","code":"200","size":"19","referer":"-","agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0","http_x_forwarded_for":"-","request_time":"0.026","upstream_response_time":"-","hostname":"centos7-web"}
0
1
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
0
1