ようへいの日々精進XP

よかろうもん

(超メモ)私的な Datadog の Agent Check の書き方

超メモ。

Agent Check のポイント(ドキュメントより抜粋)

参考

概要

  • Datadog Agent のプラグインという位置づけ
  • Python で実装する
  • デフォルトで 15 秒間隔(メインチェックの実行ループに組み込まれている)

Agent Check の実装

  • AgentCheck クラスを継承し check() 関数を利用する

メトリクスの送信

以下の関数を利用することが出来る。

- self.gauge( ... ) # Sample a gauge metric
- self.increment( ... ) # Increment a counter metric
- self.decrement( ... ) # Decrement a counter metric
- self.histogram( ... ) # Sample a histogram metric
- self.rate( ... ) # Sample a point, with the rate calculated at the end of the check
- self.count( ... ) # Sample a raw count metric
- self.monotonic_count( ... ) # Sample an increasing counter metric

各関数は以下の引数を渡す。

  • metric : The name of the metric(意訳:メトリクス名)
  • value : The value for the metric (defaults to 1 on increment, -1 on decrement)(意訳:メトリクスの値)
  • tags : (optional) A list of tags to associate with this metric.(意訳:タグ)
  • hostname : (optional) A hostname to associate with this metric. Defaults to the current host.(意訳:ホスト名)
  • device_name : (optional) A device name to associate with this metric.(意訳:デバイス名)

イベントの送信

  • self.event 関数を呼び出すことでイベントに送信することが出来る
  • イベントに関する payload は以下を設定することが出来る
{
    "timestamp": int, the epoch timestamp for the event,
    "event_type": string, the event name,
    "api_key": string, the api key for your account,
    "msg_title": string, the title of the event,
    "msg_text": string, the text body of the event,
    "aggregation_key": string, a key to use for aggregating events,
    "alert_type": (optional) string, one of ('error', 'warning', 'success', 'info');
        defaults to 'info',
    "source_type_name": (optional) string, the source type name,
    "host": (optional) string, the name of the host,
    "tags": (optional) list, a list of tags to associate with this event
}

以下、例。

import time

from checks import AgentCheck
class HelloCheck(AgentCheck):
    def check(self, instance):
        self.event({
            'timestamp': int(time.time()),
            'event_type': 'http_check',
            'msg_title': 'URL timeout',
            'msg_text': 'URL timeout',
            'aggregation_key': 'hello.world'
        })

aggregation_key って何するんだろうって思ったら、指定したキー(上記の例では hello.world)でイベント集約する為に利用するとのこと。

関数の呼び出し

設定

  • 各 Agent Check には設定ファイルが必要
  • 設定ファイルは /etc/dd-agent/conf.d/ 以下に置かれ、YAML 形式で記述する
  • 設定ファイルは Agent Check スクリプトと同じ名前にする必要がある(例:hello-world.py なら hello-world.yml)

設定ファイルの内容は以下の通り。

init_config:

instances:
    [{}]
  • init_config は Agent Check が実行される際に利用するグローバルな設定を定義する、設定した値は self.init_configget メソッドで取得することが出来る
  • instances は Check を実行するリスト、設定した値は instanceget メソッドで取得することが出いる

ドキュメントを見ながら Hello World

/etc/dd-agent/conf.d/hello-world.yaml を書く

init_config:

instances:
    [{}]

/opt/datadog-agent/agent/checks.d/hello-world.py を書く

from checks import AgentCheck
class HelloCheck(AgentCheck):
    def check(self, instance):
        self.gauge('hello.world', 1)

datadog-agent の再起動

$ sudo service datadog-agent restart

デバッグする

$ sudo -u dd-agent dd-agent check hello-world
2015-11-14 21:18:32,259 | WARNING | dd.collector | checks.disk(disk.py:74) | Using `use_mount` in datadog.conf has been deprecated in favor of `use_mount` in disk.yaml
2015-11-14 21:18:32,263 | INFO | dd.collector | config(config.py:881) | initialized checks.d checks: ['hello-world', 'network', 'process', 'ntp', 'disk']
2015-11-14 21:18:32,264 | INFO | dd.collector | config(config.py:882) | initialization failed checks.d checks: []
2015-11-14 21:18:32,264 | INFO | dd.collector | checks.collector(collector.py:529) | Running check hello-world
Metrics:
[('hello.world',
  1447503512,
  1,
  {'hostname': 'vagrant-ubuntu-trusty-64', 'type': 'gauge'})]
Events:
[]
Service Checks:
[]
Service Metadata:
[{}]
    hello-world
    -----------
      - instance #0 [OK]
      - Collected 1 metric, 0 events & 0 service checks

メトリクスを確認

f:id:inokara:20151114212747p:plain


Hello World を卒業して...

ログインユーザー数を監視する Agent Check 簡易版

github.com

設定ファイルを以下のように準備する。

init_config:
    threshold_user: 3
instances:
    [{}]

試しに dd-agent check を実行。

$ sudo -u dd-agent dd-agent check check_user
2015-11-15 00:15:10,473 | WARNING | dd.collector | checks.disk(disk.py:74) | Using `use_mount` in datadog.conf has been deprecated in favor of `use_mount` in disk.yaml
2015-11-15 00:15:10,477 | INFO | dd.collector | config(config.py:881) | initialized checks.d checks: ['hello-world', 'network', 'process', 'ntp', 'disk', 'check_user']
2015-11-15 00:15:10,477 | INFO | dd.collector | config(config.py:882) | initialization failed checks.d checks: []
2015-11-15 00:15:10,478 | INFO | dd.collector | checks.collector(collector.py:529) | Running check check_user
Metrics: 
[('login.user.check',
  1447514110,
  '4',
  {'hostname': 'vagrant-ubuntu-trusty-64', 'type': 'gauge'})]
Events: 
[{'aggregation_key': 'login.user.check',
  'alert_type': 'error',
  'api_key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'event_type': 'login_user_check',
  'msg_text': '4 users still login.(Threshhold : 3 users)',
  'msg_title': 'Still Login user count check',
  'timestamp': 1447514110}]
Service Checks: 
[]
Service Metadata: 
[{}]
    check_user
    ----------
      - instance #0 [OK]
      - Collected 1 metric, 1 event & 0 service checks

datadog-agent を再起動してみる。

f:id:inokara:20151115001739p:plain

おお。


ということで...

ドキュメント が読みやすくて意外に簡単に Agent Check を書くことが出来て嬉しい。

以上。