ようへいの日々精進XP

よかろうもん

Datadog の Monitor 定義を Ruby DSL で管理する Barkdog を使ってみた

はじめに

f:id:inokara:20150728220409p:plain

Datadog にはモニタリングしているメトリクスに対して、設定したしきい値で通知を飛ばす Monitor という機能が備わっている。

この Monitor の設定は WebUI 又は提供される API 経由で設定を行うことが出来るが、この設定を RubyDSL で書けるようにした Barkdog というツールが紹介されていたので使ってみた。

github.com

Barkdog は KumogataPiculet 等の Codenize.tools の各種ツールをメンテナンスされている @sgwr_dts さんがメンテナンスされている。


触ってみた

Datadog で Docker コンテナをモニタリングしている環境で試す

f:id:inokara:20150728221808p:plain f:id:inokara:20150728221819p:plain

インストール

試す環境は以下の通り。

# MacOS X Yosemite
% uname -a
Darwin xxxxxxxxxxxxx.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64

# Ruby はちょっと古い
% ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]

インストールは gem install で一発。

% gem install barkdog --no-ri --no-rdoc -V

ヘルプ

まずはヘルプを見てみる。

% barkdog -h
Usage: barkdog [options]
        --api-key API_KEY
        --app-key APP_KEY
    -a, --apply
    -f, --file FILE
        --dry-run
    -e, --export
    -o, --output FILE
        --no-color
        --debug
    -h, --help

先述の Piculet 等のオプションとよく似ている(と思う)ので、Codenize.tools の各種ツールを普段から使っていると更に導入しやすいのではないだろうか。(あくまでも個人的な感想です)

API キーと Application キーを環境変数に登録

事前に Datadog のダッシュボード(Integrations → APIs)から API キーと Application キーを取得しておく。

export BARKDOG_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export BARKDOG_APP_KEY=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

barkdog コマンドの引数として API キーと Application キーを渡しても OK 牧場。

barkdog \
  --api-key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
  --app-key yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

export

既存の設定を export する場合には -e オプションを付けて実行する。

f:id:inokara:20150728220942p:plain

例として上図のような 2 つの Monitor 定義を export した結果は以下の通り。

% barkdog -e
# Export Datadog monitors
monitor "{{host.name}}  Docker CPU system が怪しい", :type=>"metric alert" do
  query "avg(last_5m):avg:docker.cpu.system{*} by {host} > 0.01"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    silenced({})
  end
end

monitor "{{host.name}}  Docker コンテナ数が怪しい", :type=>"metric alert" do
  query "avg(last_5m):sum:docker.containers.running{*} by {host} < 8"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    renotify_interval 0
    silenced({})
    timeout_h 0
  end
end

この場合だと標準出力に出力されるだけなので -o Barkfile で Barkfile に出力する。

 barkdog -e -o Barkfile
Export Datadog monitors to `Barkfile`

Barkfile の中身は以下のような内容になっている。

% cat Barkfile
monitor "{{host.name}} の Docker CPU system が怪しい", :type=>"metric alert" do
  query "avg(last_5m):avg:docker.cpu.system{*} by {host} > 0.01"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    silenced({})
  end
end

monitor "{{host.name}} の Docker コンテナ数が怪しい", :type=>"metric alert" do
  query "avg(last_5m):sum:docker.containers.running{*} by {host} < 8"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    renotify_interval 0
    silenced({})
    timeout_h 0
  end
end

各パラメータについては Datadog の API リファレンスに記載されているので割愛するが、主なパラメータについては以下のような定義となっている。

パラメータ 定義内容 ざくっと意味
query avg(last_5m):sum:docker.containers.running{*} by {host} < 8 過去 5 分の Docker container 数/host 平均数が 8 以下であれば...
message @slack-datadog-notification @slack-datadog-notification に定義されている通知先に通知する
notify_no_data true メトリクスデータが飛んで来なくなったら通知を飛ばす(デフォルトは false
no_data_timeframe 10 10 分間メトリクスデータ飛んで来なかったら通知を飛ばす(超意訳なので間違ってたらすいません)
notify_audit false Monitor の定義が変更されたら通知しない

apply

Monitor の定義を追加したいので Barkfile に以下を追加する。(※しきい値0.01 とあえて超小さくして設定する)

monitor "{{host.name}} の Docker CPU user が怪しい", :type=>"metric alert" do
  query "avg(last_5m):avg:docker.cpu.user{*} by {host} > 0.01"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    silenced({})
  end
end

追加したら -a オプション(apply)に --dry-run を指定して適用前に確認することが出来る。

% barkdog -a --dry-run
Apply `Barkfile` to Datadog monitors (dry-run)
Create Monitor: {{host.name}} の Docker CPU user が怪しい (dry-run)
No change

特にエラーが出ないので適用する。

% barkdog -a
Apply `Barkfile` to Datadog monitors
Create Monitor: {{host.name}} の Docker CPU user が怪しい

適用を確認してみたいので export オプションで確認してみる。

% barkdog -e
# Export Datadog monitors
monitor "{{host.name}} の Docker CPU system が怪しい", :type=>"metric alert" do
  query "avg(last_5m):avg:docker.cpu.system{*} by {host} > 0.01"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    silenced({})
  end
end

monitor "{{host.name}} の Docker CPU user が怪しい", :type=>"metric alert" do
  query "avg(last_5m):avg:docker.cpu.user{*} by {host} > 0.01"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    silenced({})
  end
end

monitor "{{host.name}} の Docker コンテナ数が怪しい", :type=>"metric alert" do
  query "avg(last_5m):sum:docker.containers.running{*} by {host} < 8"
  message "@slack-datadog-notification"
  options do
    no_data_timeframe 10
    notify_audit false
    notify_no_data true
    renotify_interval 0
    silenced({})
    timeout_h 0
  end
end

暫くするとしきい値が低すぎるので通知が飛ぶ。(今回は通知を Slack に飛ばしている)

f:id:inokara:20150728222842p:plain


さいごに

導入を是非検討したい

Datadog で Monitor 機能を利用している(又は導入する予定)であれば、以下のような理由から Barkdog を設置ツールの一つとして検討するべきだと思う。

  • 設定をコードで管理出来る
  • Ruby DSL で理解し易いし JSON 書くよりも楽チン(あくまでも個人的見解)

気になった点

  • query の定義(特に Metric 名とか)がパッと思い出せない場合にはダッシュボードを確認する必要があった

おまけ

Datadog で Docker コンテナを監視するには dd-agent コンテナが提供されているのでそちらを利用すると楽チン。

docker run -d \
  --name dd-agent \
  -h `hostname` \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /proc/mounts:/host/proc/mounts:ro \
  -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
  -e API_KEY=${datadog-API-key} \
datadog/docker-dd-agent

事前に Datadog の API キーを取得しておく。