resource "datadog_monitor" "offline_agent_monitor" {
  count = var.offline_agent_monitor_enabled ? 1 : 0
  name  = "${var.environment}-${var.service_name} Jenkins offline agents monitor"
  type  = "query alert"
  message = templatefile("${path.module}/templates/notifications.tftpl", {
    message                        = "Jenkins agents are offline on ${var.environment}-${var.service_name}."
    notification_endpoints         = var.notification_endpoints
    alert_notification_endpoints   = var.alert_notification_endpoints
    no_data_notification_endpoints = var.no_data_notification_endpoints
  })
  escalation_message = "Jenkins agents are still offline on ${var.environment}-${var.service_name}. Escalation to ${var.escalation_notification_endpoints}"

  query = "max(last_10m):max:jenkins.node.offline{cluster_name:${var.environment}-${var.service_name}} > 0"

  monitor_thresholds {
    critical = 0
  }

  include_tags             = false
  renotify_interval        = 60
  require_full_window      = false
  on_missing_data          = "show_and_notify_no_data"
  notification_preset_name = "hide_query"

  tags = local.tags
}
