resource "datadog_monitor" "rds_memory_usage_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{dbinstanceidentifier.name}} free memory 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 = <<EOF
{{#is_alert}}Critical Alter: Free memory value for {{dbinstanceidentifier.name}} is too low. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_warning}}Warning: Memory Usage for {{dbinstanceidentifier.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_recovery}}Recovery: Memory Usage for {{dbinstanceidentifier.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOF

  query = "avg(last_1h):avg:aws.rds.freeable_memory{alias:${var.account_alias}} by {dbinstanceidentifier} < 536870912"

  monitor_thresholds {
    warning  = 1073741824
    critical = 536870912
  }

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

  notify_no_data    = false
  renotify_interval = 300
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}

resource "datadog_monitor" "rds_cpu_usage_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{dbinstanceidentifier.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 = <<EOF
{{#is_warning}}Warning: CPU Utilization for {{dbinstanceidentifier.name}} is approaching limits. Consider optimizing or scaling resources.{{/is_warning}}
{{#is_alert}}Critical Alter: CPU Utilization for {{dbinstanceidentifier.name}} is too high. Consider optimizing or scaling resources.{{/is_alert}}
{{#is_recovery}}Recovery: CPU Utilization for {{dbinstanceidentifier.name}} is back to normal. No further actions are required.{{/is_recovery}}

${var.slack_channel}
EOF

  query = "avg(last_2h):avg:aws.rds.cpuutilization{alias:${var.account_alias}} by {dbinstanceidentifier} > 95"

  monitor_thresholds {
    warning  = 90
    critical = 95
  }

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

  notify_no_data    = false
  renotify_interval = 300
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}

resource "datadog_monitor" "rds_replica_lag_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{dbinstanceidentifier.name}} replication lag at {{value}}%. {{#is_alert}}Threshold exceeded: Set at {{threshold}}%.{{/is_alert}}{{#is_warning}}Warning threshold: Set at {{warn_threshold}}%.{{/is_warning}}"
  type    = "query alert"
  message = <<EOF
{{#is_warning}}Replication lag is > 10 min on host: {{dbinstanceidentifier.name}}{{/is_warning}}
{{#is_alert}}Replication lag is > 20 min on host: {{dbinstanceidentifier.name}}{{/is_alert}}

${var.slack_channel}
EOF

  query = "max(last_1h):max:aws.rds.aurora_replica_lag_maximum{alias:${var.account_alias}} by {dbinstanceidentifier} > 1200000"

  monitor_thresholds {
    warning  = 600000
    critical = 1200000
  }

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

  new_group_delay   = 300
  notify_no_data    = false
  renotify_interval = 300
  renotify_statuses = ["alert"]
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}

resource "datadog_monitor" "rds_free_storage_space_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{dbinstanceidentifier.name}} free storage space 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 = <<EOF
{{#is_warning}}Free storage is space < 4GiB min on host: {{dbinstanceidentifier.name}}{{/is_warning}}
{{#is_alert}}Free storage is space < 2GiB on host: {{dbinstanceidentifier.name}}{{/is_alert}}

${var.slack_channel}
EOF

  query = "avg(last_1h):avg:aws.rds.free_storage_space{alias:${var.account_alias}} by {dbinstanceidentifier} < 2147483648"

  monitor_thresholds {
    warning  = 4294967296
    critical = 2147483648
  }

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

  notify_no_data    = false
  renotify_interval = 300
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}

resource "datadog_monitor" "rds_free_local_storage_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{dbinstanceidentifier.name}} free local storage space 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 = <<EOF
{{#is_warning}}Free local storage is < 4GiB min on host: {{dbinstanceidentifier.name}}{{/is_warning}}
{{#is_alert}}Free local storage is < 2GiB on host: {{dbinstanceidentifier.name}}{{/is_alert}}

${var.slack_channel}
EOF

  query = "avg(last_1h):avg:aws.rds.free_local_storage{alias:${var.account_alias}} by {dbinstanceidentifier} < 2147483648"

  monitor_thresholds {
    warning  = 4294967296
    critical = 2147483648
  }

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

  notify_no_data    = false
  renotify_interval = 300
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}

resource "datadog_monitor" "rds_write_iops_monitor" {
  name    = "${var.environment}-${var.service_name} instance {{dbinstanceidentifier.name}} write IOPS 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 = <<EOF
{{#is_warning}}Write IOPS is > 150000 on host: {{dbinstanceidentifier.name}}{{/is_warning}}
{{#is_alert}}Write IOPS is > 200000 on host: {{dbinstanceidentifier.name}}{{/is_alert}}

${var.slack_channel}
EOF

  query = "avg(last_2h):avg:aws.rds.write_iops{alias:${var.account_alias}} by {dbinstanceidentifier}.as_rate() > 200000"

  monitor_thresholds {
    warning  = 150000
    critical = 200000
  }

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

  notify_no_data    = false
  renotify_interval = 300
  restricted_roles  = [data.datadog_role.datadog_admin_role.id]

  notify_audit = false
  timeout_h    = 0
  include_tags = true

  tags = local.common_tags
}
