UPD: refactored cw dimension detection

This commit is contained in:
xpk 2022-11-14 09:41:12 +08:00
parent 14ef1770c2
commit d30103f29e
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
2 changed files with 21 additions and 0 deletions

View File

@ -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'

View File

@ -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]