Cloudwatch 客户端安装
采用AWS的amazon linux 2 标准AMI或基于这个AMI做定制的镜像安装的EC2实例默认已经集成了Cloudwatch客户端。
手工安装客户端可以参考官方文档:
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html
Cloudwatch配置样例
可以通过如下语句以向导方式生成一个配置模版:
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
可以在这个模版基础上做修改,下面是一个定制后的配置样例:
{
"agent": {
"metrics_collection_interval": 10,
"run_as_user": "root"
},
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"cpu": {
"resources": [
"*"
],
"measurement": [
{
"name": "cpu_usage_idle",
"unit": "Percent"
},
{
"name": "cpu_usage_iowait",
"unit": "Percent"
},
{
"name": "cpu_usage_system",
"unit": "Percent"
},
{
"name": "cpu_usage_user",
"unit": "Percent"
},
{
"name": "cpu_usage_nice",
"unit": "Percent"
},
"cpu_usage_guest"
],
"totalcpu": false,
"metrics_collection_interval": 10
},
"netstat": {
"measurement": [
"tcp_established",
"tcp_syn_sent",
"tcp_close"
],
"metrics_collection_interval": 60
},
"disk": {
"measurement": [
"used_percent",
"disk_inodes_free"
],
"resources": [
"/",
"/home/t4",
"/emr",
"/mnt",
"/mnt1",
"/var/log"
],
"metrics_collection_interval": 60,
"drop_device": true
},
"processes": {
"measurement": [
"running",
"sleeping",
"dead"
]
},
"mem": {
"measurement": [
"mem_used_percent",
"mem_used",
"mem_cached",
"mem_total"
],
"metrics_collection_interval": 10
}
}
}
}
配置推送到 ssm的parameter store
可以将上述配置保存成文件,然后推送到ssm的parameter store,方便各个机器共享配置文件,实现统一配置管理。
aws ssm put-parameter --name "parameter name" --type "String" --value file://configuration_file_pathname
配置生效
在EC2上执行如下语句将parameter store上的配置文件下载到本地,并重启服务生效:
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c ssm:AmazonCloudWatch-linux