locals {
  mq_critical_disk_free = 10 * 1024 * 1024
  mq_warning_disk_free  = 15 * 1024 * 1024
}

resource "datadog_monitor" "mq_cpu_utilization" {
  name    = "${var.environment}-${var.service_name} broker {{broker.name}} CPU utilization at {{value}}%. {{#is_alert}}Threshold exceeded: Set at {{threshold}}%.{{/is_alert}}{{#is_warning}}Warning threshold: Set at {{warn_threshold}}%.{{/is_warning}}"
  type    = "metric alert"
  message = <<EOT
{{#is_warning}}Warning: CPU Utilization for {{broker.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_alert}}Critical Alter: CPU Utilization for {{broker.name}} is too high. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_recovery}}Recovery: CPU Utilization for {{broker.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOT

  query = "max(last_1h):max:aws.amazonmq.system_cpu_utilization{broker:uat-core-notifications} by {broker} > 90"

  monitor_thresholds {
    critical = 90
    warning  = 80
  }

  evaluation_delay = 1800 // minimum recommended value for AWS metrics 900

  notify_no_data    = true
  no_data_timeframe = 120
  renotify_interval = 300

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  # ignore any changes in silenced value; using silenced is deprecated in favor of downtimes
  lifecycle {
    #ignore_changes = [silenced]
  }

  tags = local.common_tags
}

resource "datadog_monitor" "mq_disk_free" {
  name    = "${var.environment}-${var.service_name} broker {{broker.name}} disk space usage at {{value}}. {{#is_alert}}Threshold exceeded: Set at {{threshold}}%.{{/is_alert}}{{#is_warning}}Warning threshold: Set at {{warn_threshold}}%.{{/is_warning}}"
  type    = "metric alert"
  message = <<EOT
{{#is_alert}}Critical Alter: Free disk space value for {{broker.name}} is too low. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_warning}}Warning: Disk space usage for {{broker.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_recovery}}Recovery: Disk space usage for {{broker.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOT

  query = "min(last_1h):min:aws.amazonmq.rabbit_mqdisk_free{broker:uat-core-notifications} by {broker} < ${local.mq_critical_disk_free}"

  monitor_thresholds {
    critical = local.mq_critical_disk_free
    warning  = local.mq_warning_disk_free
  }

  evaluation_delay = 1800 // minimum recommended value for AWS metrics 900

  notify_no_data    = true
  no_data_timeframe = 120
  renotify_interval = 300

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  # ignore any changes in silenced value; using silenced is deprecated in favor of downtimes
  lifecycle {
    ##ignore_changes = [silenced]
  }

  tags = local.common_tags
}

resource "datadog_monitor" "mq_memory_usage" {
  name    = "${var.environment}-${var.service_name} broker {{broker.name}} memory usage at {{value}}%. {{#is_alert}}Threshold exceeded: Set at {{threshold}}%.{{/is_alert}}{{#is_warning}}Warning threshold: Set at {{warn_threshold}}%.{{/is_warning}}"
  type    = "metric alert"
  message = <<EOT
{{#is_alert}}Critical Alter: Free memory value for {{broker.name}} is too low. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_warning}}Warning: Memory Usage for {{broker.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_recovery}}Recovery: Memory Usage for {{broker.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOT

  query = "max(last_1h):max:aws.amazonmq.rabbit_mqmem_used{broker:uat-core-notifications} by {broker} / max:aws.amazonmq.rabbit_mqmem_limit{broker:uat-core-notifications} by {broker} * 100 > 70"

  monitor_thresholds {
    critical = 70
    warning  = 50
  }

  evaluation_delay = 1800 // minimum recommended value for AWS metrics 900

  notify_no_data    = true
  no_data_timeframe = 120
  renotify_interval = 300

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  # ignore any changes in silenced value; using silenced is deprecated in favor of downtimes
  lifecycle {
    ##ignore_changes = [silenced]
  }

  tags = local.common_tags
}
