Merged in MON-105-monitors-for-postgresql (pull request #98)

MON-105 monitors for postgresql

Approved-by: Boris Rousseau <boris.rousseau@morea.fr>
Approved-by: Quentin Manfroi <quentin.manfroi@yahoo.fr>
Approved-by: Guillaume Kérivel <guillaume.kerivel@fr.clara.net>
This commit is contained in:
Guillaume Kérivel 2018-09-18 15:08:50 +00:00 committed by Quentin Manfroi
commit 6aee8a75eb
7 changed files with 353 additions and 1 deletions

View File

@ -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/)

View File

@ -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" {

View File

@ -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 | `<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 | `<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 | `<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 | `<list>` | no |
| postgresql_connection_message | Custom message for PostgreSQL connection monitor | string | `` | no |
| postgresql_connection_silenced | Groups to mute for PostgreSQL connection monitor | map | `<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 | `<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/)

View File

@ -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"
}

View File

@ -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}"
}

View File

@ -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 = <<EOF
"postgres.can_connect".over${module.filter-tags.service_check}.by("port","server").last(6).count_by_status()
EOF
thresholds = {
warning = "${var.postgresql_availability_threshold_warning}"
critical = 5
}
notify_no_data = true
no_data_timeframe = "${var.postgresql_availability_no_data_timeframe}"
new_host_delay = "${var.new_host_delay}"
renotify_interval = 0
notify_audit = false
timeout_h = 0
include_tags = true
locked = false
require_full_window = true
silenced = "${var.postgresql_availability_silenced}"
tags = ["env:${var.environment}", "type:database", "provider:postgres", "resource:postgresql", "team:claranet", "created-by:terraform", "${var.postgresql_availability_extra_tags}"]
}
resource "datadog_monitor" "postgresql_connection_too_high" {
count = "${var.postgresql_connection_enabled ? 1 : 0}"
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 = <<EOF
${var.postgresql_connection_time_aggregator}(${var.postgresql_connection_timeframe}):
avg:postgresql.percent_usage_connections${module.filter-tags.query_alert} by {server}
* 100 > ${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 = <<EOF
${var.postgresql_lock_time_aggregator}(${var.postgresql_lock_timeframe}):
default(avg:postgresql.locks${module.filter-tags.query_alert} by {server}, 0)
> ${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}"]
}

View File

@ -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}"
}