ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AWS EC2配置CloudWatch

2022-07-12 16:35:38  阅读:228  来源: 互联网

标签:CloudWatch aws AWS agent amazon EC2 file config 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

标签:CloudWatch,aws,AWS,agent,amazon,EC2,file,config,cloudwatch
来源: https://www.cnblogs.com/Don/p/16470536.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有