尝试使用Terraform来管理Datadog的监控.
这是根据标题所写的备忘录。
为了导入现有的监控,需要提供管理上的名称、以及获取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
※在导入时似乎无法使用-var-file,所以如果有多个环境,就需要在操作时一边覆盖terraform.tfvars进行工作。
- tfとtfvarsに既存設定を反映する
# 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"
}
#~略~
将可共通化的部分与特定的设置分开,并进行反映。
将此导入到模板和变量文件中,并循环重复适用于每个监视器。
- テストする
按照terraform.tfvars中的变量文件进行计划的Terraform。
如果只有一个名为terraform.tfvars的-var-file文件,则无需特别指定即可运行。
- 更新する
请将想要添加的任意数值适时写入tf、tfvars文件,然后执行plan和apply命令。
应用terraform -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