From d30103f29e3a7029ec58627130413efcd52f244a Mon Sep 17 00:00:00 2001 From: xpk Date: Mon, 14 Nov 2022 09:41:12 +0800 Subject: [PATCH] UPD: refactored cw dimension detection --- .../Monitoring.EC2/get-cwagent-dimensions.sh | 6 ++++++ .../ManagementGovernance/Monitoring.EC2/main.tf | 15 +++++++++++++++ 2 files changed, 21 insertions(+) create mode 100755 modules/ManagementGovernance/Monitoring.EC2/get-cwagent-dimensions.sh diff --git a/modules/ManagementGovernance/Monitoring.EC2/get-cwagent-dimensions.sh b/modules/ManagementGovernance/Monitoring.EC2/get-cwagent-dimensions.sh new file mode 100755 index 0000000..16de537 --- /dev/null +++ b/modules/ManagementGovernance/Monitoring.EC2/get-cwagent-dimensions.sh @@ -0,0 +1,6 @@ +#!/bin/bash +eval "$(jq -r '@sh "id=\(.input)"')" + +aws cloudwatch list-metrics --namespace CWAgent --metric-name disk_inodes_free \ +--dimensions Name=InstanceId,Value=$id Name=path,Value=/ | \ +jq '.Metrics[] | .Dimensions[] | {(.Name):(.Value)}' | jq -s 'add' diff --git a/modules/ManagementGovernance/Monitoring.EC2/main.tf b/modules/ManagementGovernance/Monitoring.EC2/main.tf index bcc7e0e..f453332 100644 --- a/modules/ManagementGovernance/Monitoring.EC2/main.tf +++ b/modules/ManagementGovernance/Monitoring.EC2/main.tf @@ -149,12 +149,21 @@ resource "aws_cloudwatch_metric_alarm" "ec2-swap_free" { # get device dimension from cw metrics +/* data "external" "disk-device" { program = ["bash", "${path.module}/get-cwagent-device.sh"] query = { input = var.ec2-instance-id } } +*/ + +data "external" "cw-dimensions" { + program = ["bash", "${path.module}/get-cwagent-dimensions.sh"] + query = { + input = var.ec2-instance-id + } +} resource "aws_cloudwatch_metric_alarm" "ec2-disk_free" { count = data.external.ec2-os.result.os == "Linux" ? 1 : 0 @@ -171,6 +180,8 @@ resource "aws_cloudwatch_metric_alarm" "ec2-disk_free" { actions_enabled = var.actions-enabled alarm_actions = [var.sns-targets.alarm-actions-urgent] ok_actions = [var.sns-targets.alarm-actions-urgent] + dimensions = data.external.cw-dimensions.result + /* dimensions = { InstanceId = var.ec2-instance-id ImageId = data.aws_instance.ec2-instance.ami @@ -179,6 +190,7 @@ resource "aws_cloudwatch_metric_alarm" "ec2-disk_free" { fstype = data.external.disk-device.result.fstype path = "/" } + */ tags = var.default-tags lifecycle { ignore_changes = [tags] @@ -201,6 +213,8 @@ resource "aws_cloudwatch_metric_alarm" "ec2-disk_inodes_free" { actions_enabled = var.actions-enabled alarm_actions = [var.sns-targets.alarm-actions-urgent] ok_actions = [var.sns-targets.alarm-actions-urgent] + dimensions = data.external.cw-dimensions.result + /* dimensions = { InstanceId = var.ec2-instance-id ImageId = data.aws_instance.ec2-instance.ami @@ -209,6 +223,7 @@ resource "aws_cloudwatch_metric_alarm" "ec2-disk_inodes_free" { fstype = data.external.disk-device.result.fstype path = "/" } + */ tags = var.default-tags lifecycle { ignore_changes = [tags]