diff --git a/databases/postgresql/README.md b/databases/postgresql/README.md new file mode 100644 index 0000000..e96d886 --- /dev/null +++ b/databases/postgresql/README.md @@ -0,0 +1,45 @@ +PostgreSQL DataDog monitors +====================== + +How to use this module +---------------------- + +``` +module "datadog-monitors-postgresql" { + source = "git::ssh://git@bitbucket.org/morea/terraform.feature.datadog.git//databases/postgresql?ref={revision}" + + environment = "${var.environment}" + message = "${module.datadog-message-alerting.alerting-message}" +} + +``` +Purpose +------- +Creates DataDog monitors with the following checks : + +* PostgreSQL not enough available connections +* PostgreSQL too many locks + +Inputs +------ + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| delay | Delay in seconds for the metric evaluation | string | `15` | no | +| environment | Environment | string | - | yes | +| filter_tags_use_defaults | Use default filter tags convention | string | `true` | no | +| filter_tags_custom | Tags used for custom filtering when filter_tags_use_defaults is false | string | `*` | no | +| postgresql_connection_threshold_critical | Maximum critical acceptable percent of connections | string | `80` | no | +| postgresql_connection_threshold_warning | Maximum warning acceptable percent of connections | string | `70` | no | +| postgresql_connection_silenced | Groups to mute PostgreSQL connection monitor | map | `` | no | +| postgresql_connection_message | Custom message for PostgreSQL connection monitor | string | `` | no | +| postgresql_thread_threshold_critical | Maximum critical acceptable number of threads | string | `500` | no | +| postgresql_thread_threshold_warning | Maximum warning acceptable number of threads | string | `400` | no | +| postgresql_thread_silenced | Groups to mute PostgreSQL threads monitor | map | `` | no | +| postgresql_thread_message | Custom message for PostgreSQL thread monitor | string | `` | no | +| message | Message sent when a monitor is triggered | string | - | yes | + +Related documentation +--------------------- + +DataDog documentation: [https://docs.datadoghq.com/integrations/postgres/](https://docs.datadoghq.com/integrations/postgres/) diff --git a/databases/postgresql/inputs.tf b/databases/postgresql/inputs.tf new file mode 100644 index 0000000..9c884f5 --- /dev/null +++ b/databases/postgresql/inputs.tf @@ -0,0 +1,77 @@ +variable "environment" { + description = "Environment" + type = "string" +} + +# Global DataDog +variable "delay" { + description = "Delay in seconds for the metric evaluation" + default = 15 +} + +variable "message" { + description = "Message sent when an alert is triggered" +} + +variable "filter_tags_use_defaults" { + description = "Use default filter tags convention" + default = "true" +} + +variable "filter_tags_custom" { + description = "Tags used for custom filtering when filter_tags_use_defaults is false" + default = "*" +} + +# PostgreSQL specific +################################## +### PostgreSQL connections ### +################################## + +variable "postgresql_connection_threshold_critical" { + default = 80 + description = "Maximum critical acceptable percent of connections" +} + +variable "postgresql_connection_threshold_warning" { + default = 70 + description = "Maximum warning acceptable percent of connections" +} + +variable "postgresql_connection_silenced" { + description = "Groups to mute PostgreSQL connection monitor" + type = "map" + default = {} +} + +variable "postgresql_connection_message" { + description = "Custom message for PostgreSQL connection monitor" + type = "string" + default = "" +} + +############################ +### PostgreSQL locks ### +############################ + +variable "postgresql_lock_threshold_critical" { + default = 99 + description = "Maximum critical acceptable number of locks" +} + +variable "postgresql_lock_threshold_warning" { + default = 70 + description = "Maximum warning acceptable number of locks" +} + +variable "posgresql_lock_silenced" { + description = "Groups to mute PostgreSQL lock monitor" + type = "map" + default = {} +} + +variable "postgresql_lock_message" { + description = "Custom message for PostgreSQL lock monitor" + type = "string" + default = "" +} diff --git a/databases/postgresql/mon-postgresql.tf b/databases/postgresql/mon-postgresql.tf new file mode 100644 index 0000000..d2778d7 --- /dev/null +++ b/databases/postgresql/mon-postgresql.tf @@ -0,0 +1,67 @@ +data "template_file" "filter" { + template = "$${filter}" + + vars { + filter = "${var.filter_tags_use_defaults == "true" ? format("dd_monitoring:enabled,dd_postgres:enabled,db_env:%s", var.environment) : "${var.filter_tags_custom}"}" + } +} + +resource "datadog_monitor" "postgresql_connection_too_high" { + name = "[${var.environment}] PostgreSQL Connections {{#is_alert}}{{{comparator}}} {{threshold}}% ({{value}}%){{/is_alert}}{{#is_warning}}{{{comparator}}} {{warn_threshold}}% ({{value}}%){{/is_warning}}" + message = "${coalesce(var.postgresql_connection_message, var.message)}" + type = "metric alert" + + query = < ${var.postgresql_connection_threshold_critical} + EOF + + evaluation_delay = "${var.delay}" + new_host_delay = "${var.delay}" + + thresholds { + warning = "${var.postgresql_connection_threshold_warning}" + critical = "${var.postgresql_connection_threshold_critical}" + } + + notify_no_data = true + renotify_interval = 0 + require_full_window = true + timeout_h = 0 + include_tags = true + + silenced = "${var.postgresql_connection_silenced}" + + tags = ["env:${var.environment}", "resource:postgresql"] +} + +resource "datadog_monitor" "postgresql_too_many_locks" { + name = "[${var.environment}] PostgreSQL too many locks {{#is_alert}}{{{comparator}}} {{threshold}} ({{value}}){{/is_alert}}{{#is_warning}}{{{comparator}}} {{warn_threshold}} ({{value}}){{/is_warning}}" + message = "${coalesce(var.postgresql_lock_message, var.message)}" + type = "metric alert" + + query = < ${var.postgresql_lock_threshold_critical} + EOF + + evaluation_delay = "${var.delay}" + new_host_delay = "${var.delay}" + + thresholds { + warning = "${var.postgresql_lock_threshold_warning}" + critical = "${var.postgresql_lock_threshold_critical}" + } + + notify_no_data = true + renotify_interval = 0 + require_full_window = true + timeout_h = 0 + include_tags = true + + silenced = "${var.posgresql_lock_silenced}" + + tags = ["env:${var.environment}", "resource:postgresql"] +}