NEW: cwagent external datasource and ec2 monitoring

This commit is contained in:
KF 2022-11-03 11:39:31 +08:00
parent 282df3c08b
commit a0cf9362d4
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
3 changed files with 38 additions and 12 deletions

View File

@ -0,0 +1,21 @@
#!/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[] | select ((.Name=="device") or (.Name=="fstype")) | { (.Name): (.Value)}' | \
jq -s 'add // {"device":"unknown", "fstype":"unknown"}'
exit 0
DEVICE=$(aws cloudwatch list-metrics --namespace CWAgent --metric-name disk_inodes_free \
--dimensions Name=InstanceId,Value=$id Name=path,Value=/ \
--query 'Metrics[].Dimensions[?Name==`device`].Value' --output text)
FSTYPE=$(aws cloudwatch list-metrics --namespace CWAgent --metric-name disk_inodes_free \
--dimensions Name=InstanceId,Value=$id Name=path,Value=/ \
--query 'Metrics[].Dimensions[?Name==`fstype`].Value' --output text)
jq -n --arg device "$DEVICE" --arg fstype "$FSTYPE" '{"device":$device,"fstype":$fstype}'

View File

@ -147,6 +147,15 @@ 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
}
}
resource "aws_cloudwatch_metric_alarm" "ec2-disk_free" {
alarm_name = "${var.cw-alarm-prefix}:EC2:disk_free:${var.ec2-instance-id}"
comparison_operator = "LessThanThreshold"
@ -161,15 +170,14 @@ resource "aws_cloudwatch_metric_alarm" "ec2-disk_free" {
actions_enabled = var.actions-enabled
alarm_actions = [var.alarm-actions-urgent]
ok_actions = [var.alarm-actions-urgent]
treat_missing_data = "notBreaching"
dimensions = {
InstanceId = var.ec2-instance-id
ImageId = data.aws_instance.ec2-instance.ami
InstanceType = data.aws_instance.ec2-instance.instance_type
# The following will not work. AWS expects the device name from OS, not from AWS perspective
# device = one(data.aws_instance.ec2-instance.root_block_device[*].device_name)
# device = ""
fstype = "xfs"
#device = length(data.external.disk-device.result.device) > 0 ? data.external.disk-device.result.device : "unknown"
#fstype = length(data.external.disk-device.result.fstype) > 0 ? data.external.disk-device.result.fstype : "unknown"
device = data.external.disk-device.result.device
fstype = data.external.disk-device.result.fstype
path = "/"
}
tags = var.default-tags
@ -193,16 +201,13 @@ resource "aws_cloudwatch_metric_alarm" "ec2-disk_inodes_free" {
actions_enabled = var.actions-enabled
alarm_actions = [var.alarm-actions-urgent]
ok_actions = [var.alarm-actions-urgent]
treat_missing_data = "notBreaching"
dimensions = {
InstanceId = var.ec2-instance-id
ImageId = data.aws_instance.ec2-instance.ami
InstanceType = data.aws_instance.ec2-instance.instance_type
# The following will not work. AWS expects the device name from OS, not from AWS perspective
# device = one(data.aws_instance.ec2-instance.root_block_device[*].device_name)
# device = ""
fstype = "xfs"
path = "/"
device = data.external.disk-device.result.device
fstype = data.external.disk-device.result.fstype
path = "/"
}
tags = var.default-tags
lifecycle {

View File

@ -2,5 +2,5 @@
eval "$(jq -r '@sh "lb=\(.input)"')"
RESULTS=$(aws elbv2 describe-target-groups --load-balancer-arn $lb --query TargetGroups[*].TargetGroupArn --output text --no-cli-pager | sed 's/\t/\n/g' | sort | xargs)
jq -n --arg result "$RESULTS" '{"result":$result}' | tee -a /tmp/log.txt
jq -n --arg result "$RESULTS" '{"result":$result}'