LoginSignup
0
0

More than 5 years have passed since last update.

Datadogのモニタの一括登録などをしてみた

Last updated at Posted at 2018-08-08

テスト環境で手動登録したやつをexport(json)してほかのorganizationにimportしてみた備忘録です。
同僚のpythonできる方がなんか作ってくれたのをapikeyを引数にする感じに改造してみました。

単体のモニタをimportするスクリプト
# -*- coding: utf-8 -*-
# Make sure you replace the API and/or APP key below
# with the ones for your account
'''
Usage:

    python .\import_monitor.py --apikey "apikey" --appkey "appkey" [template.json]

'''
from datadog import initialize, api
import json
import sys
import argparse

parser = argparse.ArgumentParser()

parser.add_argument('--apikey', action='store', help="optional") 
parser.add_argument('--appkey', action='store', help="optional") 
parser.add_argument("jsonfile", help="please set me")
args = parser.parse_args() 

apikey = args.apikey
appkey = args.appkey
jsonfile = args.jsonfile

options = {
    'api_key':apikey,
    'app_key':appkey
}

initialize(**options)

#arg conf
argvs = sys.argv
argc = len(argvs)

# Create a new monitor
if (argc != 6):
    print('bad')
    quit()

f = open(jsonfile, "r")
data = json.load(f)
api.Monitor.create(
    type = data["type"],
    query = data["query"],
    name = data["name"],
    message = data["message"],
    tags = data["tags"],
    options = data["options"]
)
一括処理
$ ls -1 ./path_to_file/*.json > jsonfiles.txt
$ cat jsonfiles.txt | while read line
> do
> python import_monitor.py --apikey "apikey" --appkey "appkey" "$line"
> done

更新はid指定するだけで大体同じ感じでした。
あと、スケジュールダウンタイムとかも。モニタの画面からできるのであんま需要なさそうですかね。

$ cat schedule_downtime_all.py 
from datadog import initialize, api
import time
import argparse
parser = argparse.ArgumentParser()

parser.add_argument('--apikey', action='store', help="optional") 
parser.add_argument('--appkey', action='store', help="optional") 
parser.add_argument('--start', action='store', help="optional") 
parser.add_argument('--end', action='store', help="optional") 
parser.add_argument('--orgname', action='store', help="optional") 
parser.add_argument('--notifies', action='store', help="optional")

args = parser.parse_args() 

apikey = args.apikey
appkey = args.appkey

options = {
    'api_key':apikey,
    'app_key':appkey
}

initialize(**options)

# set vars from options(timezone=Asia/Tokyo).
start_ts = args.start
end_ts = args.end
org_name = args.orgname
notifies = args.notifies
message = "%s mainte schedule created. %s" % (org_name, notifies)
timezone = 'Asia/Tokyo'

# Schedule downtime
api.Downtime.create(
    scope='*',
    start=start_ts,
    end=end_ts,
    message=message,
    timezone=timezone
)

$ python schedule_downtime_all.py --apikey "apikey" --appkey "appkey" \
 --start "$(date +%s --date '2018-08-03 20:00')" --end "$(date +%s --date '2018-08-04 20:00')" \
 --orgname "Test" --notifies "@mailaddr1 @mailaddr2"

dogコマンドで一括muteする場合は以下のような感じでendない永遠ミュートに。

dog --config ~/.dogrc_hoge monitor mute_all | jq '.'

なんかpipでdatadogいれるとdogコマンドが入っていました。
http://htnosm.hatenablog.com/entry/2017/03/15/090000
ただまあスケジュールダウンタイム登録はtimezone指定できなかったりオプション足らない感じでした。(のでpythonスクリプト一応つくった)

引数よりos.environとかつかったほうがいいんすかね。(よくわかっていない)
https://note.nkmk.me/python-os-environ-getenv/

参考:
https://docs.datadoghq.com/api/?lang=python#monitors
https://qiita.com/stkdev/items/e262dada7b68ea91aa0c
https://qiita.com/dodo5522/items/6ec2b6d83287add6c185
http://www.yamamo10.jp/yamamoto/comp/Python/dictionary/index.php
https://www.sejuku.net/blog/40791
http://htnosm.hatenablog.com/entry/2017/03/24/090000
https://docs.datadoghq.com/ja/guides/monitors/#%E9%81%B8%E6%8A%9E%E3%81%97%E3%81%9Fmonitor%E3%82%92%E8%A8%AD%E5%AE%9A%E5%A4%89%E6%9B%B4%E3%81%99%E3%82%8B

0
0
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
0