Datadog API をイイカンジに叩く為に Dogshell を使う


まえがき

Datadog を運用していると、CLI でイイカンジにガッと API を叩きたいなーという気持ちになる時があります。
例えば、特定のメトリクスに依存している Monitor が存在しないかをさっくり調査したい、とか。
どうやら Datadog は Dogshell という CLI ツールを公式で用意してくれているようです、やりましたね! 使っていきましょう!

環境

$ python --version
Python 3.6.4
$ pip --version
pip 18.0

Python 2.7系 でも動いたので、バージョンはあまり気にしなくても良さそうです。

インストール

pip でいけます。

$ pip install datadog

色々出る。

$ dog -v
dog 0.22.0

やりましたね!!!

使う

Dogshell のユーザガイドを流し見すると、以下のコマンドが使えそうです。

dog metric
dog event
dog status_check
dog monitor
dog downtime
dog timeboard
dog screenboard
dog host
dog tag
dog search
dog comment

Datadog の画面を普段から見ている人なら、ふんふん、なるほど、と、何が出来そうかざっくり想像出来ると思います。

設定

早速使っていきたいところですが、 Dogshell を使うには API Key と Application Key などの Config 設定が必要です。
https://app.datadoghq.com/account/settings#api で API Key と Application Key を発行しておきましょう。
AWS CLI みたいに dog config 的な対話的な何かが用意されている気がするんですが、されていません( dog [--config CONFIG] というオプションは存在するようですが、こう、やっぱり対話的に全部のコンフィグを一発で設定したいですよね?)。
なので、ここはいきなりコマンドを叩きます。 put 系の操作を行って何か問題が起こっても嫌なので、参照系のコマンドを適当に先述のコマンド一覧から探しましょう。

$ dog metric --help
usage: dog metric [-h] {post} ...

optional arguments:
  -h, --help  show this help message and exit

Verbs:
  {post}
    post      Post metrics

post しかなくて駄目そう!!!!

$ dog event --help
usage: dog event [-h] {post,show,stream} ...

optional arguments:
  -h, --help          show this help message and exit

Verbs:
  {post,show,stream}
    post              Post events.
    show              Show event details.
    stream            Retrieve events from the Event Stream

show は良さげですね。適当に実行しましょう。すると、 ~/.dogrc が存在しない場合は対話的に Config を要求されます。
~/.dogrc に Config を作る為に、先述の API Key と Application Key を訊かれるので、入力しましょう。

$ dog event show foo
/Users/hieki/.dogrc does not exist. Would you like to create it? [Y/n] Y
What is your api key? (Get it here: https://app.datadoghq.com/account/settings#api)xxxxxxxxxxxxxxxxxxxx
What is your application key? (Generate one here: https://app.datadoghq.com/account/settings#api) xxxxxxxxxxxxxxxxxxxx
Wrote /Users/hieki/.dogrc

~/.dogrc は作成されましたが、 foo という event は存在しないので、怒られます。

ERROR: ERROR: No event matches that event_id.

config の設定はこれで です。

試してみる

ひとまず私が調査したかったのは、最近利用を停止することになった NewRelic のメトリクスを使っている Datadog Monitor が存在しないか、という点でした。 dog monitor コマンドを使えば良さそうな気配がありますね。help を見てみましょう。

$ dog monitor --help
usage: dog monitor [-h] [--string_ids]
                   {post,update,show,show_all,delete,mute_all,unmute_all,mute,unmute}
                   ...

optional arguments:
  -h, --help            show this help message and exit
  --string_ids          Represent monitor IDs as strings instead of ints in
                        JSON

Verbs:
  {post,update,show,show_all,delete,mute_all,unmute_all,mute,unmute}
    post                Create a monitor
    update              Update existing monitor
    show                Show a monitor definition
    show_all            Show a list of all monitors
    delete              Delete a monitor
    mute_all            Globally mute monitors (downtime over *)
    unmute_all          Globally unmute monitors (cancel downtime over *)
    mute                Mute a monitor
    unmute              Unmute a monitor

show_all!!! これこれ!!!! という気持ちになりますね。
そして mute_all というヤバそうなコマンドも見つけます。絶対に触りたくない。
というわけで show_all を叩きましょう。

$ dog monitor show_all
1111111 無です   無 {'notify_audit': False, 'locked': False, 'timeout_h': 0, 'silenced': {}, 'include_tags': False, 'no_data_timeframe': None, 'require_full_window': True, 'new_host_delay': 300, 'notify_no_data': False, 'renotify_interval': 0, 'escalation_message': '', 'thresholds': {'critical': 50000.0, 'warning': 5000.0}}   33733   sum(last_5m):avg:aws.events.invocations{*}.as_count() > 50000   query alert
2222222 無2です  無2    {'notify_audit': False, 'locked': False, 'timeout_h': 0, 'silenced': {}, 'include_tags': False, 'no_data_timeframe': None, 'require_full_window': True, 'new_host_delay': 300, 'notify_no_data': False, 'renotify_interval': 0, 'escalation_message': '', 'thresholds': {'critical': 50000.0, 'warning': 5000.0}}   33733   sum(last_5m):avg:aws.events.invocations{*}.as_count() > 50000   query alert

これは適当に作ったサンプルの Monitor ですが、こんな感じの乱雑なものがドバっと出てきます。目で読むのは厳しいので適当に grep したりしてめぼしいものを探しましょう。
今回は new_relic が含まれる Monitor なので、

$ dog monitor show_all | grep new_relic

$

という感じです。無さそうですね、オッケー、NewRelic 依存の Monitor は残って無さそうです

あとがき

Dogshell には他にも色々なコマンドがあるので、 すべての機能を網羅することは出来ませんが、最低限の使い方をご紹介しました。
みなさんも Dogshell で良い Datadog ライフを満喫しましょう!