0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FluentdでInfluxDBにNginxのログを格納

Last updated at Posted at 2020-06-11

Fluentdでログの転送を行い,InfluxDBへログの格納を行う.

環境

仮想マシンのスペックは統一してある.

  • OS: Ubuntu 18.04.4 LTS
  • Kernel Version: 4.15.0-101-generic
  • RAM: 1[GB]
  • vCPU: 1[Core]
  • InfluxDB 1.8.0
  • Fluentd 1.10.2

構成図

cdsl-vm-1にNginxがインストールされている.このサーバのアクセスログをdmz-loggingにインストールしたInfluxDBへ格納する.また,格納したログをdmz-bastionにインストールしたChronografで可視化する.

image.png

[1] InfluxDB -> データベース

対象ホスト: dmz-logging

インストール

以下を見ながらインストールを行う.今回は1.8.0をインストールする.
https://docs.influxdata.com/influxdb/v1.8/introduction/install/

wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

sudo apt-get update && sudo apt-get install influxdb
sudo systemctl unmask influxdb.service
sudo systemctl start influxdb

初期設定

設定ファイルは /etc/influxdb 以下にある.

sudo vi /etc/influxdb/influxdb.conf

以下のドキュメントをもとにInfluxDBへアクセスしてみる.
https://docs.influxdata.com/influxdb/v1.8/introduction/get-started/

$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0
> 

データベース一覧を確認

> show databases
name: databases
name
----
_internal

データベース mydb を作成

> create database mydb

データベースが作成された確認

> show databases
name: databases
name
----
_internal
mydb

基本的なSQL構文は対応しているため,学習コストは高くないように見える.以下はサンプルのクエリである.

> INSERT temperature,machine=unit42,type=assembly external=25,internal=37
>
> SELECT * FROM "temperature"
name: temperature
-----------------
time		                        	 external	  internal	 machine	type
2015-10-21T19:28:08.385013942Z  25	        	37     		unit42  assembly
>
> SELECT * FROM /.*/ LIMIT 1
--
> SELECT * FROM "cpu_load_short"
--
> SELECT * FROM "cpu_load_short" WHERE "value" > 0.9

ログを記録するデータベースを作成

> create database nginx_access_log

InfluxDBとの接続にはcurlが使える.

$ curl -i -XPOST http://dmz-logging:8086/query --data-urlencode "q=show databases"

デフォルトでは認証が無効になっている.必要に応じて以下から設定を行う.
https://docs.influxdata.com/influxdb/v1.8/administration/authentication_and_authorization/

[2] Fluentd -> ログ転送

対象ホスト: cdsl-vm-1, dmz-vpn-edge

公式のガイドに従ってインストールする.
https://docs.fluentd.org/installation/before-install

準備

ここで1024が表示されたら,ファイルディスクリプタの上限を引き上げる.

$ ulimit -n
1024

以下を /etc/security/limits.conf へ追記する.

/etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

編集後は再起動を行う.その後,カーネルパラメータを /etc/sysctl.conf で変更する.

/etc/sysctl.conf
net.core.somaxconn = 1024
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 4096 12582912 16777216
net.ipv4.tcp_rmem = 4096 12582912 16777216
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535

編集後は sysctl -p を実行する.

インストール

以下のドキュメントに従ってインストールを行う.
https://docs.fluentd.org/installation/install-by-deb

Ubuntu 18.04(bionic)なので以下を実行する.

curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-bionic-td-agent3.sh | sh

起動させ,それを確認する.

sudo systemctl start td-agent.service
sudo systemctl status td-agent.service

サンプルログを飛ばして,それがログに記録されているかを確認する.

curl -X POST -d 'json={"json":"message"}' http://localhost:8888/debug.test
tail /var/log/td-agent/td-agent.log
...
2020-06-06 23:41:20.920424246 +0900 debug.test: {"json":"message"}

InfluxDB用のプラグインを導入

$ sudo /usr/sbin/td-agent-gem install fluent-plugin-influxdb
Fetching: influxdb-0.7.0.gem (100%)
Successfully installed influxdb-0.7.0
Fetching: fluent-plugin-influxdb-2.0.0.gem (100%)
Successfully installed fluent-plugin-influxdb-2.0.0
Parsing documentation for influxdb-0.7.0
Installing ri documentation for influxdb-0.7.0
Parsing documentation for fluent-plugin-influxdb-2.0.0
Installing ri documentation for fluent-plugin-influxdb-2.0.0
Done installing documentation for influxdb, fluent-plugin-influxdb after 0 seconds
2 gems installed

以下の記事を参考にAggregator Fluentdの設定を作成

cdsl-vm-1の設定

/etc/td-agent/td-agent.conf
<source>
  @type tail
  path /var/log/nginx/access.log
  pos_file /var/log/td-agent/nginx-access.log.pos
  format nginx
  tag nginx.access
  @label @ngx-log
</source>

<label @ngx-log>
  # FOR DEBUG
  # <match **>
  #   @type stdout
  #   @id output_stdout
  # </match>
  <match **>
    @type forward
    heartbeat_interval 1
    recover_wait 10 # default: 10s
    phi_threshold 16 # default: 16

    <server>
      host 192.168.200.5
      port 24224
    </server>
  </match>
</label>

dmz-vpn-edgeの設定

/etc/td-agent/td-agent.conf
<source>
    @type forward
    port 24224
    @label @gcp-ngx
</source>
<label @gcp-ngx>
    # <match **>
    #     @type stdout
    #     @id output_stdout
    # </match>
    <match nginx.access>
        @type influxdb
        host "dmz-logging.dmz.tak-cslab.org"
        port 8086
        dbname "nginx_access_log"
        measurement "cdsl-vm-1"
        use_ssl false
        time_precision "s"
        tag_keys ["nginx","cdsl-vm-1"]
        sequence_tag "_seq"
    </match>
</label>

デバッグするときは,DEBUG用の <match> のコメントを外すし,2台ともに以下のコマンドでFluentdを起動する.

sudo -u td-agent td-agent -c /etc/td-agent/td-agent.conf

正常にログが転送されていることをInfluxDBから確認する.

$ influx -precision rfc3339

> select * from "nginx_access_log"."autogen"."cdsl-vm-1" limit 10
name: cdsl-vm-1
time                _seq agent                                                                                                              code host method path  referer                       remote         size user
----                ---- -----                                                                                                              ---- ---- ------ ----  -------                       ------         ---- ----
1591589006000000000 0    Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)                                             503  -    GET    /dev/ https://ja.tak-cslab.org/dev/ 216.245.221.83 119  -
1591589020000000000 0    Mozilla/5.0 (compatible; Nimbostratus-Bot/v1.3.2; http://cloudsystemnetworks.com)                                  403  -    GET    /     -                             209.17.96.170  2669 -
1591589066000000000 0    Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)                                             503  -    GET    /dev/ https://ja.tak-cslab.org/dev/ 216.245.221.83 119  -
1591589120000000000 0    -                                                                                                                  403  -    GET    /     -                             74.82.47.5     2669 -
1591589126000000000 0    Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)                                             503  -    GET    /dev/ https://ja.tak-cslab.org/dev/ 216.245.221.83 119  -
1591589150000000000 0    Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)                                             301  -    HEAD   /     http://ja.tak-cslab.org/      216.245.221.83 0    -
1591589150000000000 1    Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)                                             200  -    HEAD   /     http://ja.tak-cslab.org/      216.245.221.83 0    -
1591589186000000000 0    Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/)                                             503  -    GET    /dev/ https://ja.tak-cslab.org/dev/ 216.245.221.83 119  -
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?