From 9557437195ec4efa7b503c9e27e9e03113bba565 Mon Sep 17 00:00:00 2001 From: Rafael Romero Carmona Date: Thu, 14 Jun 2018 18:17:04 +0200 Subject: [PATCH] MON-224 CloudSQL MySQL Network Connections monitor --- cloud/gcp/cloud-sql/mysql/README.md | 49 +++++++++++++ cloud/gcp/cloud-sql/mysql/inputs.tf | 73 +++++++++++++++++++ .../mysql/monitors-cloudsql-mysql.tf | 54 ++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 cloud/gcp/cloud-sql/mysql/README.md create mode 100644 cloud/gcp/cloud-sql/mysql/inputs.tf create mode 100644 cloud/gcp/cloud-sql/mysql/monitors-cloudsql-mysql.tf diff --git a/cloud/gcp/cloud-sql/mysql/README.md b/cloud/gcp/cloud-sql/mysql/README.md new file mode 100644 index 0000000..1d0cecd --- /dev/null +++ b/cloud/gcp/cloud-sql/mysql/README.md @@ -0,0 +1,49 @@ +GCP CloudSQL MySQL Monitors +============================== + +How to use this module +---------------------- + +``` +module "datadog-monitors-gcp-cloudsql-mysql" { + source = "git::ssh://git@bitbucket.org/morea/terraform.feature.datadog.git//cloud/gcp/clouds-sql/mysql?ref={revision}" + + project_id = "${var.gcp_project_id}" + environment = "${var.environment}" + message = "${module.datadog-message-alerting.alerting-message}" +} + +``` + +Purpose +------- +Creates DataDog monitors with the following checks : + +* CloudSQL MySQL Network Connections +* CloudSQL MySQL Replication Lag +* CloudSQL MySQL Failover Lag + +Useful links +------------ + +* [GCP Metrics for CloudSQL](https://cloud.google.com/monitoring/api/metrics_gcp#gcp-cloudsql) +* [Datadog Useful monitors for GCP CloudSQL](https://www.datadoghq.com/blog/monitor-google-cloud-sql/) +* [Max connections depends on the type of the instance](https://cloud.google.com/sql/docs/quotas#fixed-limits) + +Inputs +------ + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| delay | Delay in seconds for the metric evaluation | string | `900` | no | +| environment | Architecture environment | string | - | yes | +| 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 a monitor is triggered | string | - | yes | +| network_connections_hard_limit | Max number of connections for the CloudSQL Instance. Default value is the max value on https://cloud.google.com/sql/docs/quotas#fixed-limits for MySQL | string | `4000` | no | +| network_connections_message | Custom message for the Network Connections monitor | string | `` | no | +| network_connections_silenced | Groups to mute for GCP Cloud SQL Network Connections monitor | map | `` | no | +| network_connections_threshold_critical | Number of network connections (critical threshold) | string | `3600` | no | +| network_connections_threshold_warning | Number of network connections (warning threshold) | string | `3200` | no | +| network_connections_timeframe | Timeframe for the Network Connections monitor | string | `last_5m` | no | +| project_id | ID of the GCP Project | string | - | yes | diff --git a/cloud/gcp/cloud-sql/mysql/inputs.tf b/cloud/gcp/cloud-sql/mysql/inputs.tf new file mode 100644 index 0000000..b1fbc95 --- /dev/null +++ b/cloud/gcp/cloud-sql/mysql/inputs.tf @@ -0,0 +1,73 @@ +# +# Datadog global variables +# +variable "environment" { + description = "Architecture environment" + type = "string" +} + +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 = "*" +} + +variable "message" { + description = "Message sent when a monitor is triggered" +} + +variable "delay" { + description = "Delay in seconds for the metric evaluation" + default = 900 +} + +# +# Filter variables +# +variable "project_id" { + type = "string" + description = "ID of the GCP Project" +} + +# +# Network Connections +# +variable "network_connections_message" { + description = "Custom message for the Network Connections monitor" + type = "string" + default = "" +} + +variable "network_connections_timeframe" { + description = "Timeframe for the Network Connections monitor" + type = "string" + default = "last_5m" +} + +variable "network_connections_hard_limit" { + description = "Max number of connections for the CloudSQL Instance. Default value is the max value on https://cloud.google.com/sql/docs/quotas#fixed-limits for MySQL" + type = "string" + default = 4000 +} + +variable "network_connections_threshold_warning" { + description = "Number of network connections (warning threshold)" + type = "string" + default = 3200 +} + +variable "network_connections_threshold_critical" { + description = "Number of network connections (critical threshold)" + type = "string" + default = 3600 +} + +variable "network_connections_silenced" { + description = "Groups to mute for GCP Cloud SQL Network Connections monitor" + type = "map" + default = {} +} diff --git a/cloud/gcp/cloud-sql/mysql/monitors-cloudsql-mysql.tf b/cloud/gcp/cloud-sql/mysql/monitors-cloudsql-mysql.tf new file mode 100644 index 0000000..847de67 --- /dev/null +++ b/cloud/gcp/cloud-sql/mysql/monitors-cloudsql-mysql.tf @@ -0,0 +1,54 @@ +# +# FILTERS +# +data "template_file" "filter" { + template = "$${filter}" + + vars { + filter = "${var.filter_tags_use_defaults == "true" ? + format("project_id:%s",var.project_id) : + "${var.filter_tags_custom}"}" + } +} + +# +# MySQL Network Connections +# +resource "datadog_monitor" "network_connections" { + name = "[${var.environment}] Cloud SQL MySQL Network Connections (hard limit: ${var.network_connections_hard_limit}) {{#is_alert}}{{{comparator}}} {{threshold}}% ({{value}}%){{/is_alert}}{{#is_warning}}{{{comparator}}} {{warn_threshold}}% ({{value}}%){{/is_warning}}" + message = "${coalesce(var.network_connections_message, var.message)}" + + type = "metric alert" + + query = < ${var.network_connections_threshold_critical} +EOF + + thresholds { + warning = "${var.network_connections_threshold_warning}" + critical = "${var.network_connections_threshold_critical}" + } + + include_tags = true + notify_no_data = true + require_full_window = false + renotify_interval = 0 + notify_audit = false + timeout_h = 0 + include_tags = true + locked = false + evaluation_delay = "${var.delay}" + new_host_delay = "${var.delay}" + silenced = "${var.network_connections_silenced}" + + tags = [ + "team:gcp", + "provider:gcp", + "env:${var.environment}", + "resource:cloud-sql", + "engine:mysql", + ] +}