diff --git a/README.md b/README.md index ef5bd6f..ed6b246 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ The `//` is very important, it's a terraform specific syntax used to separate gi - [elasticsearch](https://bitbucket.org/morea/terraform.feature.datadog/src/master/database/elasticsearch/) - [mongodb](https://bitbucket.org/morea/terraform.feature.datadog/src/master/database/mongodb/) - [mysql](https://bitbucket.org/morea/terraform.feature.datadog/src/master/database/mysql/) + - [postgresql](https://bitbucket.org/morea/terraform.feature.datadog/src/master/database/postgresql/) - [redis](https://bitbucket.org/morea/terraform.feature.datadog/src/master/database/redis/) - [middleware](https://bitbucket.org/morea/terraform.feature.datadog/src/master/middleware/) - [apache](https://bitbucket.org/morea/terraform.feature.datadog/src/master/middleware/apache/) diff --git a/database/mysql/monitors-mysql.tf b/database/mysql/monitors-mysql.tf index 14814b3..82133a1 100644 --- a/database/mysql/monitors-mysql.tf +++ b/database/mysql/monitors-mysql.tf @@ -26,7 +26,7 @@ resource "datadog_monitor" "mysql_availability" { silenced = "${var.mysql_availability_silenced}" - tags = ["env:${var.environment}", "type:database", "provider:mysql", "resource:mysql", "team:claranet", "created-by:terraform", "${var.mysql_connection_extra_tags}"] + tags = ["env:${var.environment}", "type:database", "provider:mysql", "resource:mysql", "team:claranet", "created-by:terraform", "${var.mysql_availability_extra_tags}"] } resource "datadog_monitor" "mysql_connection" { diff --git a/database/postgresql/README.md b/database/postgresql/README.md new file mode 100644 index 0000000..21b4dc0 --- /dev/null +++ b/database/postgresql/README.md @@ -0,0 +1,67 @@ +# DATABASE POSTGRESQL DataDog monitors + +## How to use this module + +``` +module "datadog-monitors-database-postgresql" { + source = "git::ssh://git@bitbucket.org/morea/terraform.feature.datadog.git//database/postgresql?ref={revision}" + + environment = "${var.environment}" + message = "${module.datadog-message-alerting.alerting-message}" +} + +``` + +## Purpose + +Creates DataDog monitors with the following checks: + +- PostgreSQL Connections +- PostgreSQL server does not respond +- PostgreSQL too many locks + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| environment | Environment | string | - | yes | +| evaluation_delay | Delay in seconds for the metric evaluation | string | `15` | no | +| filter_tags_custom | Tags used for custom filtering when filter_tags_use_defaults is false | string | `*` | no | +| filter_tags_use_defaults | Use default filter tags convention | string | `true` | no | +| message | Message sent when an alert is triggered | string | - | yes | +| new_host_delay | Delay in seconds for the metric evaluation | string | `300` | no | +| posgresql_lock_silenced | Groups to mute for PostgreSQL lock monitor | map | `` | no | +| postgresql_availability_enabled | Flag to enable PostgreSQL availability monitor | string | `true` | no | +| postgresql_availability_extra_tags | Extra tags for PostgreSQL availability monitor | list | `` | no | +| postgresql_availability_message | Custom message for PostgreSQL availability monitor | string | `` | no | +| postgresql_availability_no_data_timeframe | PostgreSQL availability monitor no data timeframe | string | `10` | no | +| postgresql_availability_silenced | Groups to mute for PostgreSQL availability monitor | map | `` | no | +| postgresql_availability_threshold_warning | PostgreSQL availability monitor (warning threshold) | string | `3` | no | +| postgresql_connection_enabled | Flag to enable PostgreSQL connection monitor | string | `true` | no | +| postgresql_connection_extra_tags | Extra tags for PostgreSQL connection connects monitor | list | `` | no | +| postgresql_connection_message | Custom message for PostgreSQL connection monitor | string | `` | no | +| postgresql_connection_silenced | Groups to mute for PostgreSQL connection monitor | map | `` | 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_time_aggregator | Monitor time aggregator for PostgreSQL connection monitor [available values: min, max or avg] | string | `avg` | no | +| postgresql_connection_timeframe | Monitor timeframe for PostgreSQL connection monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | string | `last_15m` | no | +| postgresql_lock_enabled | Flag to enable PostgreSQL lock monitor | string | `true` | no | +| postgresql_lock_extra_tags | Extra tags for PostgreSQL lock connects monitor | list | `` | no | +| postgresql_lock_message | Custom message for PostgreSQL lock monitor | string | `` | no | +| postgresql_lock_threshold_critical | Maximum critical acceptable number of locks | string | `99` | no | +| postgresql_lock_threshold_warning | Maximum warning acceptable number of locks | string | `70` | no | +| postgresql_lock_time_aggregator | Monitor time aggregator for PostgreSQL lock monitor [available values: min, max or avg] | string | `min` | no | +| postgresql_lock_timeframe | Monitor timeframe for PostgreSQL lock monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`] | string | `last_5m` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| postgresql_availability_id | id for monitor postgresql_availability | +| postgresql_connection_too_high_id | id for monitor postgresql_connection_too_high | +| postgresql_too_many_locks_id | id for monitor postgresql_too_many_locks | + +Related documentation +--------------------- + +DataDog documentation: [https://docs.datadoghq.com/integrations/postgres/](https://docs.datadoghq.com/integrations/postgres/) diff --git a/database/postgresql/inputs.tf b/database/postgresql/inputs.tf new file mode 100644 index 0000000..9386a5e --- /dev/null +++ b/database/postgresql/inputs.tf @@ -0,0 +1,170 @@ +variable "environment" { + description = "Environment" + type = "string" +} + +# Global DataDog +variable "evaluation_delay" { + description = "Delay in seconds for the metric evaluation" + default = 15 +} + +variable "new_host_delay" { + description = "Delay in seconds for the metric evaluation" + default = 300 +} + +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 availability ### +################################## + +variable "postgresql_availability_silenced" { + description = "Groups to mute for PostgreSQL availability monitor" + type = "map" + default = {} +} + +variable "postgresql_availability_enabled" { + description = "Flag to enable PostgreSQL availability monitor" + type = "string" + default = "true" +} + +variable "postgresql_availability_extra_tags" { + description = "Extra tags for PostgreSQL availability monitor" + type = "list" + default = [] +} + +variable "postgresql_availability_message" { + description = "Custom message for PostgreSQL availability monitor" + type = "string" + default = "" +} + +variable "postgresql_availability_threshold_warning" { + description = "PostgreSQL availability monitor (warning threshold)" + type = "string" + default = 3 +} + +variable "postgresql_availability_no_data_timeframe" { + description = "PostgreSQL availability monitor no data timeframe" + type = "string" + default = 10 +} + +################################## +### 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 for PostgreSQL connection monitor" + type = "map" + default = {} +} + +variable "postgresql_connection_enabled" { + description = "Flag to enable PostgreSQL connection monitor" + type = "string" + default = "true" +} + +variable "postgresql_connection_extra_tags" { + description = "Extra tags for PostgreSQL connection connects monitor" + type = "list" + default = [] +} + +variable "postgresql_connection_message" { + description = "Custom message for PostgreSQL connection monitor" + type = "string" + default = "" +} + +variable "postgresql_connection_time_aggregator" { + description = "Monitor time aggregator for PostgreSQL connection monitor [available values: min, max or avg]" + type = "string" + default = "avg" +} + +variable "postgresql_connection_timeframe" { + description = "Monitor timeframe for PostgreSQL connection monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`]" + type = "string" + default = "last_15m" +} + +############################ +### 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 for PostgreSQL lock monitor" + type = "map" + default = {} +} + +variable "postgresql_lock_enabled" { + description = "Flag to enable PostgreSQL lock monitor" + type = "string" + default = "true" +} + +variable "postgresql_lock_extra_tags" { + description = "Extra tags for PostgreSQL lock connects monitor" + type = "list" + default = [] +} + +variable "postgresql_lock_message" { + description = "Custom message for PostgreSQL lock monitor" + type = "string" + default = "" +} + +variable "postgresql_lock_time_aggregator" { + description = "Monitor time aggregator for PostgreSQL lock monitor [available values: min, max or avg]" + type = "string" + default = "min" +} + +variable "postgresql_lock_timeframe" { + description = "Monitor timeframe for PostgreSQL lock monitor [available values: `last_#m` (1, 5, 10, 15, or 30), `last_#h` (1, 2, or 4), or `last_1d`]" + type = "string" + default = "last_5m" +} diff --git a/database/postgresql/modules.tf b/database/postgresql/modules.tf new file mode 100644 index 0000000..19fcbdb --- /dev/null +++ b/database/postgresql/modules.tf @@ -0,0 +1,8 @@ +module "filter-tags" { + source = "../../common/filter-tags" + + environment = "${var.environment}" + resource = "postgres" + filter_tags_use_defaults = "${var.filter_tags_use_defaults}" + filter_tags_custom = "${var.filter_tags_custom}" +} diff --git a/database/postgresql/monitors-postgresql.tf b/database/postgresql/monitors-postgresql.tf new file mode 100644 index 0000000..a37ef7a --- /dev/null +++ b/database/postgresql/monitors-postgresql.tf @@ -0,0 +1,92 @@ +resource "datadog_monitor" "postgresql_availability" { + count = "${var.postgresql_availability_enabled ? 1 : 0}" + name = "[${var.environment}] PostgreSQL server does not respond" + message = "${coalesce(var.postgresql_availability_message, var.message)}" + + type = "service check" + + query = < ${var.postgresql_connection_threshold_critical} + EOF + + evaluation_delay = "${var.evaluation_delay}" + new_host_delay = "${var.new_host_delay}" + + thresholds { + warning = "${var.postgresql_connection_threshold_warning}" + critical = "${var.postgresql_connection_threshold_critical}" + } + + notify_no_data = false + renotify_interval = 0 + require_full_window = true + timeout_h = 0 + include_tags = true + + silenced = "${var.postgresql_connection_silenced}" + + tags = ["env:${var.environment}", "type:database", "provider:postgres", "resource:postgresql", "team:claranet", "created-by:terraform", "${var.postgresql_connection_extra_tags}"] +} + +resource "datadog_monitor" "postgresql_too_many_locks" { + count = "${var.postgresql_lock_enabled ? 1 : 0}" + 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.evaluation_delay}" + new_host_delay = "${var.new_host_delay}" + + thresholds { + warning = "${var.postgresql_lock_threshold_warning}" + critical = "${var.postgresql_lock_threshold_critical}" + } + + notify_no_data = false + renotify_interval = 0 + require_full_window = true + timeout_h = 0 + include_tags = true + + silenced = "${var.posgresql_lock_silenced}" + + tags = ["env:${var.environment}", "type:database", "provider:postgres", "resource:postgresql", "team:claranet", "created-by:terraform", "${var.postgresql_lock_extra_tags}"] +} diff --git a/database/postgresql/outputs.tf b/database/postgresql/outputs.tf new file mode 100644 index 0000000..24c26d1 --- /dev/null +++ b/database/postgresql/outputs.tf @@ -0,0 +1,14 @@ +output "postgresql_availability_id" { + description = "id for monitor postgresql_availability" + value = "${datadog_monitor.postgresql_availability.*.id}" +} + +output "postgresql_connection_too_high_id" { + description = "id for monitor postgresql_connection_too_high" + value = "${datadog_monitor.postgresql_connection_too_high.*.id}" +} + +output "postgresql_too_many_locks_id" { + description = "id for monitor postgresql_too_many_locks" + value = "${datadog_monitor.postgresql_too_many_locks.*.id}" +}