2022-10-25 14:21:37 +08:00
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-StatusCheckFailed_System" {
|
2023-05-23 13:10:16 +08:00
|
|
|
alarm_name = "${var.settings.StatusCheckFailed_System.ecccode}-EC2_${var.ec2-instance-id}-StatusCheckFailed_System"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.StatusCheckFailed_System.comparison_operator
|
|
|
|
evaluation_periods = var.settings.StatusCheckFailed_System.evaluation_periods
|
2022-10-25 14:21:37 +08:00
|
|
|
metric_name = "StatusCheckFailed_System"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.StatusCheckFailed_System.period
|
|
|
|
statistic = var.settings.StatusCheckFailed_System.statistic
|
|
|
|
threshold = var.settings.StatusCheckFailed_System.threshold
|
2022-10-25 14:21:37 +08:00
|
|
|
alarm_description = "EC2:StatusCheckFailed_System"
|
|
|
|
namespace = "AWS/EC2"
|
|
|
|
insufficient_data_actions = []
|
2022-10-26 11:13:56 +08:00
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.StatusCheckFailed_System.action]
|
|
|
|
ok_actions = [var.settings.StatusCheckFailed_System.action]
|
2022-10-25 14:21:37 +08:00
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-StatusCheckFailed_Instance" {
|
2023-05-23 13:10:16 +08:00
|
|
|
alarm_name = "${var.settings.StatusCheckFailed_Instance.ecccode}-EC2_${var.ec2-instance-id}-StatusCheckFailed_Instance"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.StatusCheckFailed_Instance.comparison_operator
|
|
|
|
evaluation_periods = var.settings.StatusCheckFailed_Instance.evaluation_periods
|
2022-10-25 14:21:37 +08:00
|
|
|
metric_name = "StatusCheckFailed_Instance"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.StatusCheckFailed_Instance.period
|
|
|
|
statistic = var.settings.StatusCheckFailed_Instance.statistic
|
|
|
|
threshold = var.settings.StatusCheckFailed_Instance.threshold
|
2022-10-25 14:21:37 +08:00
|
|
|
alarm_description = "EC2:StatusCheckFailed_Instance"
|
|
|
|
namespace = "AWS/EC2"
|
|
|
|
insufficient_data_actions = []
|
2022-10-26 11:13:56 +08:00
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.StatusCheckFailed_Instance.action]
|
|
|
|
ok_actions = [var.settings.StatusCheckFailed_Instance.action]
|
2022-10-25 14:21:37 +08:00
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-CPUUtilization" {
|
2023-05-23 13:10:16 +08:00
|
|
|
alarm_name = "${var.settings.CPUUtilization.ecccode}-EC2_${var.ec2-instance-id}-CPUUtilization"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.CPUUtilization.comparison_operator
|
|
|
|
evaluation_periods = var.settings.CPUUtilization.evaluation_periods
|
2022-10-25 14:21:37 +08:00
|
|
|
metric_name = "CPUUtilization"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.CPUUtilization.period
|
|
|
|
statistic = var.settings.CPUUtilization.statistic
|
|
|
|
threshold = var.settings.CPUUtilization.threshold
|
2022-10-25 14:21:37 +08:00
|
|
|
alarm_description = "EC2:CPUUtilization"
|
|
|
|
namespace = "AWS/EC2"
|
|
|
|
insufficient_data_actions = []
|
2022-10-26 11:13:56 +08:00
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.CPUUtilization.action]
|
|
|
|
ok_actions = [var.settings.CPUUtilization.action]
|
2022-10-25 14:21:37 +08:00
|
|
|
treat_missing_data = "notBreaching"
|
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
2022-11-02 18:05:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# cwagent metrics
|
|
|
|
data "aws_instance" "ec2-instance" {
|
|
|
|
instance_id = var.ec2-instance-id
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:04:44 +08:00
|
|
|
# get instance OS
|
2023-05-23 13:10:16 +08:00
|
|
|
/*
|
2022-11-10 14:04:44 +08:00
|
|
|
data "external" "ec2-os" {
|
|
|
|
program = ["bash", "${path.module}/get-os-platform.sh"]
|
|
|
|
query = {
|
|
|
|
input = var.ec2-instance-id
|
2023-05-23 13:10:16 +08:00
|
|
|
asrolearn = var.asrolearn
|
2022-11-10 14:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-23 13:10:16 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
module "ec2_os" {
|
|
|
|
source = "../../util/terraform-aws-cli"
|
|
|
|
assume_role_arn = var.asrolearn
|
|
|
|
role_session_name = "terraform-ec2-detect-os"
|
|
|
|
aws_cli_commands = ["ec2", "describe-instances", "--instance-ids", var.ec2-instance-id]
|
|
|
|
aws_cli_query = "Reservations[].Instances[].PlatformDetails"
|
|
|
|
}
|
2022-11-10 14:04:44 +08:00
|
|
|
|
|
|
|
# Linux specific checks
|
2022-11-16 23:21:48 +08:00
|
|
|
# default cw agent uses mem_used_percent metric
|
2022-11-02 18:05:26 +08:00
|
|
|
|
2022-11-16 23:21:48 +08:00
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-mem_used_percent" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Linux" ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" ? 0 : 1
|
|
|
|
alarm_name = "${var.settings.mem_used_percent.ecccode}-EC2_${var.ec2-instance-id}-mem_used_percent"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.mem_used_percent.comparison_operator
|
|
|
|
evaluation_periods = var.settings.mem_used_percent.evaluation_periods
|
2022-11-16 23:21:48 +08:00
|
|
|
metric_name = "mem_used_percent"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.mem_used_percent.period
|
|
|
|
statistic = var.settings.mem_used_percent.statistic
|
|
|
|
threshold = var.settings.mem_used_percent.threshold
|
2022-11-16 23:21:48 +08:00
|
|
|
alarm_description = "EC2:mem_used_percent"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.mem_used_percent.action]
|
|
|
|
ok_actions = [var.settings.mem_used_percent.action]
|
2022-11-16 23:21:48 +08:00
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
ImageId = data.aws_instance.ec2-instance.ami
|
|
|
|
InstanceType = data.aws_instance.ec2-instance.instance_type
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
|
|
|
}
|
|
|
|
|
|
|
|
data "external" "cw-dimensions" {
|
|
|
|
program = ["bash", "${path.module}/get-cwagent-dimensions.sh"]
|
2022-11-03 11:39:31 +08:00
|
|
|
query = {
|
2023-05-23 13:10:16 +08:00
|
|
|
input = var.ec2-instance-id
|
|
|
|
asrolearn = var.asrolearn
|
2022-11-03 11:39:31 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-14 09:41:12 +08:00
|
|
|
|
2022-11-16 23:21:48 +08:00
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-swap_used_percent" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Linux" ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" ? 0 : 1
|
|
|
|
alarm_name = "${var.settings.swap_used_percent.ecccode}-EC2_${var.ec2-instance-id}-swap_used_percent"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.swap_used_percent.comparison_operator
|
|
|
|
evaluation_periods = var.settings.swap_used_percent.evaluation_periods
|
2022-11-16 23:21:48 +08:00
|
|
|
metric_name = "swap_used_percent"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.swap_used_percent.period
|
|
|
|
statistic = var.settings.swap_used_percent.statistic
|
|
|
|
threshold = var.settings.swap_used_percent.threshold
|
2022-11-16 23:21:48 +08:00
|
|
|
alarm_description = "EC2:swap_used_percent"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.swap_used_percent.action]
|
|
|
|
ok_actions = [var.settings.swap_used_percent.action]
|
2022-11-16 23:21:48 +08:00
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
ImageId = data.aws_instance.ec2-instance.ami
|
|
|
|
InstanceType = data.aws_instance.ec2-instance.instance_type
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
2022-11-14 09:41:12 +08:00
|
|
|
}
|
2022-11-03 11:39:31 +08:00
|
|
|
|
2022-11-16 23:21:48 +08:00
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-disk_used_percent" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Linux" && data.external.cw-dimensions.result != null ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" && data.external.cw-dimensions.result != null ? 0 : 1
|
|
|
|
alarm_name = "${var.settings.disk_used_percent.ecccode}-EC2_${var.ec2-instance-id}-disk_used_percent"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.disk_used_percent.comparison_operator
|
|
|
|
evaluation_periods = var.settings.disk_used_percent.evaluation_periods
|
2022-11-16 23:21:48 +08:00
|
|
|
metric_name = "disk_used_percent"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.disk_used_percent.period
|
|
|
|
statistic = var.settings.disk_used_percent.statistic
|
|
|
|
threshold = var.settings.disk_used_percent.threshold
|
2022-11-16 23:21:48 +08:00
|
|
|
alarm_description = "EC2:disk_used_percent"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.disk_used_percent.action]
|
|
|
|
ok_actions = [var.settings.disk_used_percent.action]
|
2022-11-16 23:21:48 +08:00
|
|
|
dimensions = data.external.cw-dimensions.result
|
|
|
|
|
2022-11-02 18:05:26 +08:00
|
|
|
tags = var.default-tags
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-disk_inodes_free" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Linux" && data.external.cw-dimensions.result != null ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" && data.external.cw-dimensions.result != null ? 0 : 1
|
|
|
|
alarm_name = "${var.settings.disk_inodes_free.ecccode}-EC2_${var.ec2-instance-id}-disk_inodes_free"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.disk_inodes_free.comparison_operator
|
|
|
|
evaluation_periods = var.settings.disk_inodes_free.evaluation_periods
|
2022-11-02 18:05:26 +08:00
|
|
|
metric_name = "disk_inodes_free"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.disk_inodes_free.period
|
|
|
|
statistic = var.settings.disk_inodes_free.statistic
|
|
|
|
threshold = var.settings.disk_inodes_free.threshold
|
2022-11-02 18:05:26 +08:00
|
|
|
alarm_description = "EC2:disk_inodes_free"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.disk_inodes_free.action]
|
|
|
|
ok_actions = [var.settings.disk_inodes_free.action]
|
2022-11-14 09:41:12 +08:00
|
|
|
dimensions = data.external.cw-dimensions.result
|
|
|
|
/*
|
2022-11-02 18:05:26 +08:00
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
ImageId = data.aws_instance.ec2-instance.ami
|
|
|
|
InstanceType = data.aws_instance.ec2-instance.instance_type
|
2022-11-03 11:39:31 +08:00
|
|
|
device = data.external.disk-device.result.device
|
|
|
|
fstype = data.external.disk-device.result.fstype
|
|
|
|
path = "/"
|
2022-11-02 18:05:26 +08:00
|
|
|
}
|
2022-11-14 09:41:12 +08:00
|
|
|
*/
|
2022-11-02 18:05:26 +08:00
|
|
|
tags = var.default-tags
|
2022-11-03 21:11:15 +08:00
|
|
|
}
|
|
|
|
|
2022-11-16 23:21:48 +08:00
|
|
|
# process metric not published by default cw agent config
|
2022-11-03 21:11:15 +08:00
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-processes_total" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Linux" ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" ? 0 : 1
|
|
|
|
alarm_name = "${var.settings.processes_total.ecccode}-EC2_${var.ec2-instance-id}-processes_total"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.processes_total.comparison_operator
|
|
|
|
evaluation_periods = var.settings.processes_total.evaluation_periods
|
2022-11-03 21:11:15 +08:00
|
|
|
metric_name = "processes_total"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.processes_total.period
|
|
|
|
statistic = var.settings.processes_total.statistic
|
|
|
|
threshold = var.settings.processes_total.threshold
|
2022-11-03 21:11:15 +08:00
|
|
|
alarm_description = "EC2:processes_total"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.processes_total.action]
|
|
|
|
ok_actions = [var.settings.processes_total.action]
|
2022-11-03 21:11:15 +08:00
|
|
|
dimensions = {
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
ImageId = data.aws_instance.ec2-instance.ami
|
|
|
|
InstanceType = data.aws_instance.ec2-instance.instance_type
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
2022-11-10 14:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Windows specific checks
|
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-MemoryCommittedPct" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Windows" ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" ? 1 : 0
|
|
|
|
alarm_name = "${var.settings.MemoryCommittedPct.ecccode}-EC2_${var.ec2-instance-id}-MemoryCommittedPct"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.MemoryCommittedPct.comparison_operator
|
|
|
|
evaluation_periods = var.settings.MemoryCommittedPct.evaluation_periods
|
2022-11-10 14:04:44 +08:00
|
|
|
metric_name = "Memory % Committed Bytes In Use"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.MemoryCommittedPct.period
|
|
|
|
statistic = var.settings.MemoryCommittedPct.statistic
|
|
|
|
threshold = var.settings.MemoryCommittedPct.threshold
|
2022-11-10 14:04:44 +08:00
|
|
|
alarm_description = "EC2:MemoryCommittedBytes"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.MemoryCommittedPct.action]
|
|
|
|
ok_actions = [var.settings.MemoryCommittedPct.action]
|
2022-11-10 14:04:44 +08:00
|
|
|
dimensions = {
|
|
|
|
objectname = "Memory"
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
ImageId = data.aws_instance.ec2-instance.ami
|
|
|
|
InstanceType = data.aws_instance.ec2-instance.instance_type
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
|
|
|
}
|
|
|
|
|
2023-01-05 23:35:31 +08:00
|
|
|
resource "aws_cloudwatch_metric_alarm" "ec2-LogicalDiskFreePct" {
|
2023-05-23 13:10:16 +08:00
|
|
|
# count = data.external.ec2-os.result.os == "Windows" ? 1 : 0
|
|
|
|
count = flatten(module.ec2_os.result)[0] == "Windows" ? 1 : 0
|
|
|
|
alarm_name = "${var.settings.LogicalDiskFreePct.ecccode}-EC2_${var.ec2-instance-id}-LogicalDiskFreePct"
|
2023-01-05 23:35:31 +08:00
|
|
|
comparison_operator = var.settings.LogicalDiskFreePct.comparison_operator
|
|
|
|
evaluation_periods = var.settings.LogicalDiskFreePct.evaluation_periods
|
2022-11-10 14:04:44 +08:00
|
|
|
metric_name = "LogicalDisk % Free Space"
|
2023-01-05 23:35:31 +08:00
|
|
|
period = var.settings.LogicalDiskFreePct.period
|
|
|
|
statistic = var.settings.LogicalDiskFreePct.statistic
|
|
|
|
threshold = var.settings.LogicalDiskFreePct.threshold
|
2022-11-10 14:04:44 +08:00
|
|
|
alarm_description = "EC2:OsDiskFreePct"
|
|
|
|
namespace = "CWAgent"
|
|
|
|
insufficient_data_actions = []
|
|
|
|
actions_enabled = var.actions-enabled
|
2023-01-05 23:35:31 +08:00
|
|
|
alarm_actions = [var.settings.LogicalDiskFreePct.action]
|
|
|
|
ok_actions = [var.settings.LogicalDiskFreePct.action]
|
2022-11-10 14:04:44 +08:00
|
|
|
dimensions = {
|
|
|
|
instance = "C:"
|
|
|
|
objectname = "LogicalDisk"
|
|
|
|
InstanceId = var.ec2-instance-id
|
|
|
|
ImageId = data.aws_instance.ec2-instance.ami
|
|
|
|
InstanceType = data.aws_instance.ec2-instance.instance_type
|
|
|
|
}
|
|
|
|
tags = var.default-tags
|
2022-10-25 14:21:37 +08:00
|
|
|
}
|