LoginSignup
3
8

More than 3 years have passed since last update.

DatadogのモニタをTerraformで管理してみる

Last updated at Posted at 2018-09-21

タイトルどおりの備忘録です。

既存のモニタをimportするためには管理上の名前ととってくるためのidとAPIキーAPPキーが要ります。
https://www.terraform.io/docs/providers/datadog/r/monitor.html#import

とりあえずdogコマンド入れてidと名前の一覧をとってくる


# pip install datadog
# vi ~/.dogrc
[Connection]
apikey = ************apikey*************
appkey = ************appkey*******************

# dog --config ~/.dogrc --raw monitor show_all |jq '.'|egrep -a '\"id|\"name'|egrep -v -a '***要らないID***|null|message'

terraformをダウンロードしてパスを通す

wget https://releases.hashicorp.com/terraform/0.11.8/terraform_0.11.8_linux_amd64.zip
unzip terraform_0.11.8_linux_amd64.zip
ln -s ./terraform /usr/local/bin/
which terraform
terraform -v
  • tfvarsを用意する
    • datadogのキーを設定する
    • importしたあとで共通化できそうな変数があれば追記する
# vi terraform.tfvars
datadog_api_key="************apikey*************"
datadog_app_key="************appkey*******************"
environment="Test"
notifies1="******メールの宛先1******"
notifies2="******メールの宛先2******"
notifies3=""
notifies4=""
detect_alert_count1="1"
  • tfにリソース名を書いた枠を設定する
# vi main.tf
# Variables
variable "datadog_api_key" {}
variable "datadog_app_key" {}

# Configure the Datadog provider
provider "datadog" {
  api_key = "${var.datadog_api_key}"
  app_key = "${var.datadog_app_key}"
}
# vi monitor_resource.tf
# monitor_resouce.tf
## host resouce monitors
resource "datadog_monitor" "rs_cpu_usage" {
  # (resource arguments)
}

#resource "datadog_monitor" "rs_memory_usage" {
#  # (resource arguments)
#}

#resource "datadog_monitor" "rs_disk_usage" {
#  # (resource arguments)
#}
  • 既存のモニタをimportする
# terraform import datadog_monitor.rs_cpu_usage 535xxxx 

※import時に-var-fileが使えないようだったので複数環境もってたらいちいちterraform.tfvars上書きしながら作業するかんじでした

  • tfとtfvarsに既存設定を反映する
monitor_resouce.tf
# monitor_resouce.tf
## host resouce monitors

variable "environment" {}
variable "notifies1" {}
variable "notifies2" {}
variable "notifies3" {}
variable "notifies4" {}
variable "detect_alert_count1" {}

resource "datadog_monitor" "rs_cpu_usage" {
  name = "[${var.environment}] CPU Usage is very high on {{host.name}}"
  type = "metric alert"
  message = "${var.notifies1} ${var.notifies2} ${var.notifies3}"
  query = "avg(last_5m):avg:azure.vm.percentage_cpu{type:microsoft.compute/virtualmachines} > 90"
  thresholds {
    "%" = "${var.detect_alert_count1}"
    warning = "80.0"
    critical = "90.0"
  }
  include_tags = "false"
  no_data_timeframe = "10"
  notify_no_data = "true"
  require_full_window = "false"
}
#~略~

共通化できるものと固有の設定を切り分けて反映する
このimportしてtemplateと変数ファイルに反映して、をモニタ数分繰り返す。

  • テストする

terraform plan -var-file terraform.tfvars

※つかう-var-fileの名前がterraform.tfvarsだけなら特に指定しなくても動きます

  • 更新する

追加したい任意の値を適宜tf,tfvarsに書いてplan,applyします

terraform apply -var-file terraform.tfvars

terraformは開発者目線だとテスト書かなくていいの最高という意見を聞いたことがあります。
以上

  • dogコマンドでとりだしたモニタのデータをcsvにする

モニタ一覧がほしいという需要があったので追記。

## ヘッダをいれておく
# echo name,type,thresholds_warning,thresholds_critical,query,message,creator > monitors_20190621.csv

## dogコマンドでとってきてcsvにしてヘッダ入れたファイルに追記
# dog --config ~/.dogrc --raw monitor show_all |jq -r '.[]| [.name, .type, .options.thresholds.warning, .options.thresholds.critical, .query, .message, .creator.email]|@csv' >> monitors_20190621.csv

## 文字コードがutf8だとwindowsで読めないので変換する
# iconv -f UTF-8 -t SJIS /tmp/monitors_freesia_20190621.csv > /tmp/monitors_20190621-sjis.csv
3
8
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
3
8