AWS Custom CloudWatch Metrics for Memory and Storage

AWS CloudWatch provides many default metrics to monitor EC2 instance like CPU Utilization, Network In and Out, Disk I/O, etc, However by default it does not provide Memory Utilization and Storage i.e. EBS Storage Utilization. To get Memory and Storage Utilization details into AWS CloudWatch we need to setup Custom CloudWatch metrics. In this post, we are going to setup both Memory and Storage CloudWatch monitoring on Linux machine.

Steps To Monitoring Memory and Disk Metrics For Amazon EC2 Linux Instances in Cloud Watch.
1. Connect to your instance over SSH.

2. In SSH session, install packages as follows.
sudo apt-get update
sudo apt-get install unzip
sudo apt-get install libwww-perl libdatetime-perl

3. Create a folder where you want to store the monitoring scripts and run the following command to download python script.

curl http://awscloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip -O

4. Run the following commands to install the monitoring scripts which downloaded in above command.

unzip CloudWatchMonitoringScripts-1.2.1.zip
rm CloudWatchMonitoringScripts-1.2.1.zip
cd aws-scripts-mon

5. Ensure that the scripts have permission to perform CloudWatch operations using one of the following options:

If you associated an IAM role (instance profile) with your instance, verify that it grants permissions to perform the following operations:

  • cloudwatch:PutMetricData
  • cloudwatch:GetMetricStatistics
  • cloudwatch:ListMetrics
  • ec2:DescribeTags

6. After providing permission, specify AWS credentials in a credentials file.

First, copy the awscreds.template file to awscreds.conf as follows:
cp awscreds.template awscreds.conf

7. Open the aws.creds.conf file and add AWS Access Key and Secret Access Key into the file.

AWSAccessKeyId=my-access-key-id
AWSSecretKey=my-secret-access-key

8. To collect all available memory metrics and Root path (/) storage utilization. And send them to CloudWatch run the following command.

/mon-put-instance-data.pl --mem-util --mem-used-incl-cache-buff --mem-used --mem-avail --disk-space-util --disk-path=/

9. For writing a cronjob for above, open crontab with below command.

crontab -e

Add the below line at the end of file to send memory and storage data to AWS CloudWatch every five minutes.

*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-space-util --disk-path=/ --from-cron

10. Save the cronjob file and close it.

Leave a Reply

Your email address will not be published. Required fields are marked *