AWS cloudwatch agent setup
From Notes_Wiki
Home > Amazon web services > AWS cloudwatch agent setup
The following steps do not lead to working metric being captured in cloudwatch. One can use CentOS 8.x Zabbix instead of Cloudwatch to monitor memory, disk, etc. for various VMs / physical hosts
Create Role for cloudwatch agent
- Login into AWS management console
- Go to IAM
- Go to Roles -> Create Role
- Select type of trusted entity should be "AWS service"
- Under "Choose a use case" select "EC2" from Common use cases
- In permissions page search for and select "CloudWatchAgentServerPolicy"
- Click next.
- No need of any tags. Click next
- Give Role name such as "CloudWatchAgentServerRole" and similar description.
- Click create role
Refer:
Assign role to EC2 VM
- Log into AWS management console
- Go to EC2
- Right click on VM and go to Security -> "Modify IAM Role"
- Select the role created with CloudWatchAgentServicePolicy included
- Click Save.
Using CloudWatch agent on CentOS
Install Cloudwatch agent on CentOS VM
- Assuming required CloudWatch Agent role is already created and assigned to the VM
- Get root access using:
- sudo su -
- Based on OS and region get download link from: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html
- Install wget and vim using:
- dnf -y install wget vim
- wget the link of agent rpm file
- wget https://s3.amazonaws.com/amazoncloudwatch-agent/centos/amd64/latest/amazon-cloudwatch-agent.rpm
- Install agent
- dnf -y localinstall ./amazon-cloudwatch-agent.rpm
Refer:
Create CloudWatch agent configuration file on CentOS
- Run the agent configuration file wizard using:
- sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
-
- For OS select
- (1) Linux
- Between EC2 and on-premise choose
- (1) EC2
- Which user are you planning to run the agent. Choose
- (1) root
- Do you want to turn on StatsD daemon
- Choose : (1) yes
- Which port do you want StatsD daemon to listen to
- Default 8125
- What is the collect interval for StatsD deamon
- 1 (10s)
- What is the aggregate interval for StatsD daemon
- 4 (60s)
- Do you want to monitor metrics from CollectD?
- (2 no)
- Do you want to monitor any host metrics? e.g. CPU, memory, etc.
- 1 yes
- Do you want to monitor cpu metrics per core? Additional CloudWatch charges may apply.
- 2 no
- Do you want to add ec2 dimensions (ImageId, InstanceId, InstanceType, AutoScalingGroupName) into all of your metrics if the info is available?
- 1 yes
- Would you like to collect your metrics at high resolution (sub-minute resolution)? This enables sub-minute resolution for all metrics, but you can customize for specific metrics in the output json file.
- 4. 60 sec
- Which default metrics config do you want
- 1. Basic
- See the reference link for difference between Basic, Standard and Advanced
- Are you satisfied with the above config? Note
- it can be manually customized after the wizard completes to add additional items. : 1 yes
- Do you have any existing CloudWatch Log Agent (http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html) configuration file to import for migration?
- 2 no
- Do you want to monitor any log files?
- 2 no
- Do you want to store the config in the SSM parameter store?
- 2 no
- Edit the configuration file '/opt/aws/amazon-cloudwatch-agent/bin/config.json as root user and Optionally remove:
- "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
- under append_dimensions
Refer:
Run Cloudwatch agent on CentOS EC2 Linux VM
- To run the cloudwatch agent use:
- 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
- This creates service called "amazon-cloudwatch-agent". Later status can be checked using:
- systemctl status amazon-cloudwatch-agent
- If you modify config file then you must run
- 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
- For changes to take effect. Restarting agent with systemctl will not help in config file changes taking effect.
- Logs are avaialble at: /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log. To get more detailed logs edit config and under agent add
- "debug": true
- After this again fetch-config as specified above
Refer:
Looking at metrics in cloudwatch
Go to cloudwatch and try to look at metrics for:
- disk_used_percent
- mem_used_percent
for EC2 instance with matching instance ID.
Unfortunately metric are not visible.
Troubleshooting metric not visible at cloudwatch
- Adding namespace: using:
- "namespace": "CWAgent",
- under metrics in the json configuration file is also not helping
- Installing awscli and setting region and access keys using https://www.sbarjatiya.com/notes_wiki/index.php/Installing_AWS_command-line_tools is also not helping