使用AutoScaling策略进行CloudWatch自定义EC2内存指标和警报

时间:2018-02-26 10:25:23

标签: amazon-web-services amazon-ec2 autoscaling amazon-cloudwatch

我的目标是根据我的EC2实例上使用的内存测试AutoScaling。

为了监控我的EC2实例内存,我在我的EC2实例上安装了CloudWatch代理,并创建并配置了我的CloudWatch代理配置文件,如下所述: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html

{
        "metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
                "metrics_collected": {
                        "cpu": {
                                "measurement": [
                                        "cpu_usage_idle",
                                        "cpu_usage_iowait",
                                        "cpu_usage_user",
                                        "cpu_usage_system"
                                ],
                                "metrics_collection_interval": 60,
                                "totalcpu": false
                        },
                        "disk": {
                                "measurement": [
                                        "used_percent",
                                        "inodes_free"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "diskio": {
                                "measurement": [
                                        "io_time"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "swap": {
                                "measurement": [
                                        "swap_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        }
                }
        }
}

在EC2实例上启动CloudWatch代理后,自定义指标工作正常,我可以在CloudWatch上看到它们。

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:configuration-parameter-store-name -s

我使用此EC2实例创建了一个AMI,并使用此AMI创建了一个AutoScaling组。 然后,我根据自定义指标" mem_used_percent"设置警报。 (当> = 50时)并将其用作AutoScaling组策略(添加1个实例)。附图。

enter image description here

我连接到EC2实例并使用" Stress"增加使用的内存。引发警报,触发AutoScaling策略并创建新的EC2实例。

我看到的问题是我在特定的EC2实例上创建了一个警报,而不是整个AutoScaling组,并且一旦初始EC2实例终止,警报就不再有意义了。 我怎么能为AutoScaling Group配置相同的想法?

感谢。

2 个答案:

答案 0 :(得分:3)

来自AWS文档,

对于CloudWatch代理,您可以使用aggregation_dimensions

汇总自定义指标
"metrics": {
  "cpu":{...}
  "disk":{...}
  "aggregation_dimensions" : [["AutoScalingGroupName"], ["InstanceId", "InstanceType"]]
}

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-common-scenarios.html

对于仍在使用旧perl脚本的用户,请检查选项:

--aggregated
--auto-scaling

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html

答案 1 :(得分:0)

我采取了相同的方法但没有成功。

{
"metrics": {
    "metrics_collected": {
        "LogicalDisk": {
            "measurement": [
                "% Free Space"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "*"
            ]
        },
        "Memory": {
            "measurement": [
                "% Committed Bytes In Use"
            ],
            "metrics_collection_interval": 60
        },
        "Paging File": {
            "measurement": [
                "% Usage"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "*"
            ]
        },
        "PhysicalDisk": {
            "measurement": [
                "% Disk Time"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "*"
            ]
        },
        "Processor": {
            "measurement": [
                "% User Time",
                "% Idle Time",
                "% Interrupt Time"
            ],
            "metrics_collection_interval": 60,
            "resources": [
                "_Total"
            ]
        }
    },
    "append_dimensions": {
        "ImageId": "${aws:ImageId}",
        "InstanceId": "${aws:InstanceId}",
        "InstanceType": "${aws:InstanceType}",
        "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
    },
    "aggregation_dimensions" : [["AutoScalingGroupName"]]
}

}

当我查看cloudwatch管理控制台中的输出时,我看不到所有> CWAgent下的AutoScalingGroupName聚合