Carpe Diem

備忘録

FluentdをDatadogで監視

概要

fluentdを使う上でbuffer溢れは気になるポイントです。
monitor_agentを導入すればcurlで確認できますが、瞬間的な値であるためできれば継続した値が得られると便利です。
zabbixやsensuと組み合わせる方法もありますが、今回は一番楽そうなDataDogというSaaSを利用します。
DataDogは14日間トライアルがあり、以降はフリーミアムになります。
無料枠だと5ホストまで、かつデータ保持期間が1日となります。

環境

  • Ubuntu 14.04
  • fluentd 0.12.12
  • Nginx 1.4.6

サーバの用意

Vagrantfileを編集します。ログの送信側と受信側で2つ用意しておきます。
subscriberが受信側、publisherが送信側です。

  config.vm.define :subscriber do |fluentd|
    fluentd.vm.network :private_network, ip: "192.168.33.100"
  end

  config.vm.define :publisher do |fluentd|
    fluentd.vm.network :private_network, ip: "192.168.33.101"
  end

Fluentdのインストール

どちらにもインストールします。

$ curl -L https://td-toolbelt.herokuapp.com/sh/install-ubuntu-trusty-td-agent2.sh | sh

Subscriberの設定

fluentdの設定

/etc/td-agent/td-agent.confを以下のように編集します。初期設定を全て消してから追記してください。

<source>
  @type forward
  port 24224
</source>

<match nginx.access>
  @type file
  path /var/log/td-agent/access_log
</match>

簡単のためローカルに保存しています。

設定後は再起動

$ sudo service td-agent restart

Publisherの設定

Nginxのインストール

$ sudo apt-get install nginx

Nginxログのパーミッション設定

$ sudo chmod o+rx /var/log/nginx/
$ sudo chmod o+r /var/log/nginx/access.log

fluentdの設定

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

<source>
  @type monitor_agent
  port 24220
</source>

<match nginx.access>
  @id plg1
  @type forward
  buffer_type memory
  buffer_chunk_limit 10k
  buffer_queue_limit 64
  flush_interval 60s
  <server>
    host 192.168.33.100
    port 24224
  </server>
</match>

1つ目の@type tailのディレクティブはログの読み込みです。
2つ目の@type monitor_agentがfluentdのbufferの監視を可能にします。
3つ目の@type forwardはsubscriber側にログを転送する設定です。この中の@idは後でDataDogの設定をするときに使います。ちなみにflushが早いとDataDogのagentの処理(10sec毎)より先にbufferから出してしまうため、今回は動作検証しやすいよう長めに60sとしています。またbuffer_chunk_limitも小さめにしています。

設定後は再起動

$ sudo service td-agent restart

monitor_agentが入っていると以下のコマンドでチェックできます。

$ curl -s http://localhost:24220/api/plugins.json

結果

{
  "plugins": [
    {
      "retry_count": null,
      "output_plugin": false,
      "config": {
        "tag": "nginx.access",
        "format": "nginx",
        "pos_file": "/var/log/td-agent/nginx.access.log.pos",
        "path": "/var/log/nginx/access.log",
        "@type": "tail"
      },
      "type": "tail",
      "plugin_category": "input",
      "plugin_id": "object:3ff2d6182550"
    },
    {
      "retry_count": null,
      "output_plugin": false,
      "config": {
        "port": "24220",
        "@type": "monitor_agent"
      },
      "type": "monitor_agent",
      "plugin_category": "input",
      "plugin_id": "object:3ff2d618acf0"
    },
    {
      "retry_count": 0,
      "buffer_total_queued_size": 0,
      "buffer_queue_length": 0,
      "output_plugin": true,
      "config": {
        "flush_interval": "60s",
        "buffer_queue_limit": "64",
        "buffer_chunk_limit": "10k",
        "buffer_type": "memory",
        "@type": "forward",
        "@id": "plg1"
      },
      "type": "forward",
      "plugin_category": "output",
      "plugin_id": "plg1"
    }
  ]
}

type指定もできます。

$ curl -s http://localhost:24220/api/plugins.json?type=forward

結果

{
  "plugins": [
    {
      "retry_count": 0,
      "buffer_total_queued_size": 0,
      "buffer_queue_length": 0,
      "output_plugin": true,
      "config": {
        "flush_interval": "60s",
        "buffer_queue_limit": "64",
        "buffer_chunk_limit": "10k",
        "buffer_type": "memory",
        "@type": "forward",
        "@id": "plg1"
      },
      "type": "forward",
      "plugin_category": "output",
      "plugin_id": "plg1"
    }
  ]
}

監視する項目はこの内以下の3つです。

項目 説明
retry_count リトライ回数
buffer_total_queued_size bufferに溜まっているqueueの合計サイズ
buffer_queue_length bufferに溜まっているqueueの数

DataDogの導入

Sign In

DataDog

こちらからSign Inしてください。

DataDogのagentのインストール

今回はforwardのbufferを監視したいため、publisherにインストールします。 publisherssh後以下を実行

DD_API_KEY=ea703e7aa1efda0064eaa507d9e8ab7e bash -c "$(curl -L https://raw.githubusercontent.com/DataDog/dd-agent/master/packaging/datadog-agent/source/install_agent.sh)"

DD_API_KEYはダミーです。

fluentd監視の設定

$ cd /etc/dd-agent/conf.d/
$ sudo cp fluentd.yaml.example fluentd.yaml

以下のように編集します。

init_config:

instances:
    -  monitor_agent_url: http://localhost:24220/api/plugins.json
       plugin_ids:
         - plg1

再起動

$ sudo service datadog-agent restart

確認

$ sudo dd-agent info
2015-12-01 04:49:48,275 | WARNING | dd.collector | utils.deprecations(deprecations.py:11) | Using this command is deprecated and will be removed in a future version, for more information see https://github.com/DataDog/dd-agent/wiki/Deprecation-notice--(old-command-line-tools)
===================
Collector (v 5.6.2)
===================

  Status date: 2015-12-01 04:49:36 (11s ago)
  Pid: 2712
  Platform: Linux-3.13.0-68-generic-x86_64-with-debian-jessie-sid
  Python Version: 2.7.10
  Logs: <stderr>, /var/log/datadog/collector.log, syslog:/dev/log

  Clocks
  ======

    NTP offset: -0.0122 s
    System UTC time: 2015-12-01 04:49:48.513606

  Paths
  =====

    conf.d: /etc/dd-agent/conf.d
    checks.d: /opt/datadog-agent/agent/checks.d

  Hostnames
  =========

    socket-hostname: vagrant-ubuntu-trusty-64
    hostname: vagrant-ubuntu-trusty-64
    socket-fqdn: vagrant-ubuntu-trusty-64

  Checks
  ======

    ntp
    ---
      - Collected 0 metrics, 0 events & 1 service check

    disk
    ----
      - instance #0 [OK]
      - Collected 32 metrics, 0 events & 1 service check

    network
    -------
      - instance #0 [OK]
      - Collected 21 metrics, 0 events & 1 service check

    fluentd
    -------
      - instance #0 [OK]
      - Collected 3 metrics, 0 events & 2 service checks

  Emitters
  ========

    - http_emitter [OK]

DataDogのダッシュボードの設定

左メニューのMetricsをクリック

f:id:quoll00:20151201145010p:plain

Graphを選択。先ほどの設定がきちんとできていればfluentdの項目が出てきます。

f:id:quoll00:20151201145119p:plain

今回確認する3つを選択。

f:id:quoll00:20151201145139p:plain

これで各項目の推移を確認できるようになりました。

動作確認

abなどを使って故意にbufferを溜めます。

$ ab -n 1000 -c 10 http://localhost/

グラフを確認します。

f:id:quoll00:20151201152605p:plain

確かにqueueが溜まっていることが確認できました。

ソース