aws 使用cloudwatch agent增强监控EC2内存和磁盘

sam 2022年02月24日 355次浏览

1. cloudwatch agent代理下载

# 建议先进入/tmp,这样就可以在10天后让系统自动删除了
cd /tmp
# 下载
wget https://s3.cn-north-1.amazonaws.com.cn/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

2. 安装

rpm -U ./amazon-cloudwatch-agent.rpm

3. EC2控制台创建角色并附加

CloudWatchAgentServerRole

4. 添加路径

mkdir /usr/share/collectd/

5. touch文件

touch /usr/share/collectd/types.db

6. 初始化生成配置参数(可选)

# 会在这里:/opt/aws/amazon-cloudwatch-agent/bin/config.json 生成文件
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

7.初始化生成配置参数(实际文件路径)

{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "root"
    },
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "",
                        "log_group_name": ".",
                        "log_stream_name": "{instance_id}"
                    }
                ]
            }
        }
    },
    "metrics": {
        "append_dimensions": {
            "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            "collectd": {
                "metrics_aggregation_interval": 60
            },
            "disk": {
                "measurement": [
                    "used_percent"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            },
            "statsd": {
                "metrics_aggregation_interval": 60,
                "metrics_collection_interval": 10,
                "service_address": ":8125"
            }
        }
    }
}

8. 注册为系统服务进行启动

amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json

9. 确认服务端口是否启用

# 上述变量service_address设置的参数,程序会监听8125端口
netstat -lnput|grep 8125