How to Collect Memory Disk Usage By Using CloudWatch Agent
Table of Contents
- Introduction
- Install the CloudWatch Agent on Amazon Linux EC2
- Run the CloudWatch Agent Wizard
- CloudWatch Agent Config File Path
- Install collectd for the CloudWatch Agent
- CloudWatch Agent File
- Run the CloudWatch Agent on EC2 Linux
- Conclusion
Introduction
This blog provides a quick guide on how to set up CloudWatch Agent manually on an EC2 Linux instance. CloudWatch Agent enables you to collect system or custom metrics from Amazon EC2 instances across operating systems. You can install CloudWatch Agent on AWS Cloud or on-premises servers, Linux, or Windows as well.
Install the CloudWatch Agent on Amazon Linux EC2
sudo yum install amazon-cloudwatch-agent
You can import the CloudWatch agent configuration if you already have one. That will be useful if you want to reuse the same CloudWatch agent config over and over again. The following step uses the default wizard to create a new config file.
Run the CloudWatch Agent Wizard
Once you run the following command on a Linux EC2 instance, you'll get started with the CloudWatch Agent wizard.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
CloudWatch Agent Config File Path
After you finish the CloudWatch agent setup wizard, it will create a JSON config file at the following location.
/opt/aws/amazon-cloudwatch-agent/bin/config.json
Install collectd for the CloudWatch Agent
If you wish to use collectd to collect metrics, you should install it via the following commands.
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install -y collectd
CloudWatch Agent File
This CloudWatch agent config file is used to specify proxy and region information.
cd /opt/aws/amazon-cloudwatch-agent/etc
nano amazon-cloudwatch-agent.toml
Run the CloudWatch Agent on EC2 Linux
The following is the command to start the CloudWatch agent with the config file you either created from the wizard or from your previous config file.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-conf
ig -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
Conclusion
At this point, if everything works out, you should start seeing memory or disk usage metrics. Go to CloudWatch > Metrics, click All metrics, then click CWAgent. Under CWAgent, you should see a list of metrics you set up from the CloudWatch Agent, with data being sent from your EC2 instance.