Skip to content

Instantly share code, notes, and snippets.

@darkn3rd
Last active June 23, 2024 20:19
Show Gist options
  • Save darkn3rd/250399339f10d189689945a9bd58045e to your computer and use it in GitHub Desktop.
Save darkn3rd/250399339f10d189689945a9bd58045e to your computer and use it in GitHub Desktop.
Datadog example for monitors
# Run Script to fetch monitors
# --------------------------------------
# WORKSPACE=$(terraform workspace show)
# [[ -d "./tfvars" ]] && VAR_DEFS_PATH="./tfvars"
# [[ -d ".././tfvars" ]] && VAR_DEFS_PATH=".././tfvars"
# [[ -z "$VAR_DEFS_PATH" ]] && { echo 'VAR_DEFS_PATH not specified. Aborting' >&2 ; exit 1; }
# DD_API_KEY="$(awk -F'"' '/dd_api_key/{ print $2 }' $VAR_DEFS_PATH/$WORKSPACE.secret.tfvars)"
# DD_APP_KEY="$(awk -F'"' '/dd_app_key/{ print $2 }' $VAR_DEFS_PATH/$WORKSPACE.secret.tfvars)"
# [[ -z "$DD_API_KEY" ]] && { echo 'DD_API_KEY not specified. Aborting' >&2 ; exit 1; }
# [[ -z "$DD_APP_KEY" ]] && { echo 'DD_APP_KEY not specified. Aborting' >&2 ; exit 1; }
# MONITORS_PATH=${MONITORS_PATH:-"$VAR_DEFS_PATH/$WORKSPACE.monitors.json"}
# curl --silent --request GET "https://api.datadoghq.com/api/v1/monitor" \
# --header "Accept: application/json" \
# --header "DD-API-KEY: ${DD_API_KEY}" \
# --header "DD-APPLICATION-KEY: ${DD_APP_KEY}" \
# | jq > $MONITORS_PATH
# --------------------------------------
locals {
workspace = trimprefix(var.org_workspace, "dd-org-")
monitors = jsondecode(file("${path.module}/tfvars/${local.workspace}.monitors.json"))
}
resource "datadog_monitor" "monitor" {
for_each = { for monitor in local.monitors : monitor.name => monitor }
monitors = jsondecode(file("${path.module}/tfvars/${local.workspace}.monitors.json"))
name = each.value.name
type = each.value.type
message = each.value.message
escalation_message = try(each.value.options.escalation_message, null) # invalid for metrics
query = each.value.query
monitor_thresholds {
ok = try(each.value.options.thresholds.ok, null)
warning = try(each.value.options.thresholds.warning, null)
critical = try(each.value.options.thresholds.critical, null)
critical_recovery = try(each.value.options.thresholds.critical_recovery, null)
}
include_tags = each.value.options.include_tags
tags = each.value.tags
priority = each.value.priority
new_group_delay = try(each.value.options.new_group_delay, null)
new_host_delay = try(each.value.options.new_host_delay, null) # provider defaults to 300 regardless of null
no_data_timeframe = try(each.value.options.no_data_timeframe, null)
notification_preset_name = try(each.value.options.notification_preset_name, null)
notify_audit = try(each.value.options.notify_audit, null)
notify_by = try(each.value.options.notify_by, null)
notify_no_data = try(each.value.options.notify_no_data, null)
on_missing_data = try(each.value.options.on_missing_data, null)
renotify_interval = try(each.value.options.renotify_interval, null)
renotify_occurrences = try(each.value.options.renotify_occurrences, null)
renotify_statuses = try(each.value.options.renotify_statuses, null)
require_full_window = try(each.value.options.require_full_window, false) # null = true, so defaulting to false
restricted_roles = try(each.value.restricted_roles, null)
enable_logs_sample = try(each.value.options.enable_logs_sample, null)
dynamic "monitor_threshold_windows" {
for_each = try(each.value.options.threshold_windows, null) != null ? [each.value.options.threshold_windows] : []
content {
recovery_window = monitor_threshold_windows.value.recovery_window
trigger_window = monitor_threshold_windows.value.trigger_window
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment