AWS EC2配置CloudWatch
1.创建IAM角色并关联到EC2
2.安装cloudwatch代理
3.生成代理配置文件
4.启动代理
Linux
sudo mkdir -p /usr/share/collectd
cd /usr/share/collectd
sudo touch types.db
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
tail -f /opt/aws/amazon-cloudwatch-agent/logs/configuration-validation.log
Windows(powershell下执行)
& "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m ec2 -s -c file:"C:\Program Files\Amazon\AmazonCloudWatchAgent\config.json"
5.创建报警规则
常用命令:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard //配置向导,自动生成配置文件 sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop //停止agent sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start //启动agent sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status //查询agent状态 sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json //重新加载配置文件 |
修改agent配置参数:
sudo vi /opt/aws/amazon-cloudwatch-agent/bin/config.json
重启agent使配置生效:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
当前配置config.json示例:
{ "agent" : { "metrics_collection_interval" : 60 , "run_as_user" : "root" }, "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" : [ "/" , "/mnt" ] }, "mem" : { "measurement" : [ "mem_used_percent" ], "metrics_collection_interval" : 60 }, "statsd" : { "metrics_aggregation_interval" : 60 , "metrics_collection_interval" : 10 , "service_address" : ":8125" } } } } |
amazon-cloudwatch-agent-ctl参数说明:
usage: amazon-cloudwatch-agent-ctl -a stop|start|status|fetch-config|append-config|remove-config [-m ec2|onPremise|auto] [-c default |all|ssm:<parameter-store-name>|file:<file-path>] [-o default |all|ssm:<parameter-store-name>|file:<file-path>] [-s] e.g. 1 . apply a SSM parameter store config on EC2 instance and restart the agent afterwards: amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-Config.json -s 2 . append a local json config file on onPremise host and restart the agent afterwards: amazon-cloudwatch-agent-ctl -a append-config -m onPremise -c file:/tmp/config.json -s 3 . query agent status: amazon-cloudwatch-agent-ctl -a status -a: action stop: stop the agent process. start: start the agent process. status: get the status of the agent process. fetch-config: apply config for agent, followed by -c or -o or both. Target config can be based on location (ssm parameter store name, file name), or 'default' . append-config: append json config with the existing json configs if any, followed by -c. Target config can be based on the location (ssm parameter store name, file name), or 'default' . remove-config: remove config for agent, followed by -c or -o or both. Target config can be based on the location (ssm parameter store name, file name), or 'all' . -m: mode ec2: indicate this is on ec2 host. onPremise: indicate this is on onPremise host. auto: use ec2 metadata to determine the environment, may not be accurate if ec2 metadata is not available for some reason on EC2. -c: amazon-cloudwatch-agent configuration default : default configuration for quick trial. ssm:<parameter-store-name>: ssm parameter store name. file:<file-path>: file path on the host. all: all existing configs. Only apply to remove-config action. -o: cwagent-otel-collector configuration default : default configuration for quick trial. ssm:<parameter-store-name>: ssm parameter store name. file:<file-path>: file path on the host. all: all existing configs. Only apply to remove-config action. -s: optionally restart after configuring the agent configuration this parameter is used for 'fetch-config' , 'append-config' , 'remove-config' action only. |
参考来源:https://docs.amazonaws.cn/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html