Cloudwatch is one of the managed service by Amazon Web service for monitoring the resources such as EC2, RDS, EBS etc in AWS. This helps IT administrator to monitor each and every resources automatically. Cloudwatch the provides the metrices for CPU utilization, network bandwidth, etc. Cloudwatch will send you alerts whenever any of the metrics crosses its limit.

cloudwatch

Here I will showing how to monitor Ec2 instance Disk space and Memory utilization.

By default these two metrics are not available in AWS. We need to provide the access to cloudwatch user to get the information about the metrics.

Requirements

Access to AWS management console
Root access to Ec2 instance- Here we are choosing both centos and ubuntu machine.

Configuration

First we need to create IAM user for cloudwatch and ec2 acess.
Login to amazon management console and select IAM service.
From IAM services create a user “monuser” and select programmatic access to the cloud through the form of an Secret Key / Access Key. Download the Secret Key / Access Key.

Once you created the user, create a policy with following access.

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

 

The json format of policy will be like this.

{
“Version”: “2012-10-17”,
“Statement”: [
{
“Sid”: “VisualEditor0”,
“Effect”: “Allow”,
“Action”: [
“cloudwatch:PutMetricData”,
“ec2:DescribeTags”,
“cloudwatch:GetMetricStatistics”,
“cloudwatch:ListMetrics”
],
“Resource”: “*”
}
]
}

After the policy creation login to first ec2 instance.

Execute the following commands.

cd /opt

Download the monitoring script from AWS.

wget http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip

unzip CloudWatchMonitoringScripts-1.2.1.zip

cd aws-scripts-mon

mv awscreds.template awscreds.conf

vi awscreds.conf

 

add the secret and access key in the awscred.conf file

AWSAccessKeyId=xxxxxxxxxxxxxxxxx
AWSSecretKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

save and exit

We have to intall the perl dependency package for the script to work.

For centos machine use this.

sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA

For Ubuntu/debian use this

apt-get install libwww-perl libdatetime-perl

Once you installed add the cron entry for periodic update about the metrices to cloudwatch.

vi /etc/cron.d/cloudwatch-monitor

 

*/2 * * * * root /opt/aws-scripts-mon/mon-put-instance-data.pl –mem-util –mem-used –mem-avail –disk-space-util –disk-space-used –disk-space-avail –disk-path=/ –from-cron

chmod 0644 /etc/cron.d/cloudwatch-monitor

 

This cron job will run every 2 mins, sending memory utilization, the amount of memory being currently used, and the disk space utilization metrics to Cloudwatch.

Log into your AWS console, navigating to Cloudwatch and ensuring the correct region is selected.
Choose browse metrics.
You should now see Linux System under custom namespaces
Choose instance ID, and you will see a list of the new memory and disk metrics against your Instance ID.
Clicking on a metric will graph the data. If you’ve just set this up, there won’t be much data to graph.

Now that data from instances is being sent to Cloudwatch, there are a number of things we can do with it. For instance, we can create responses to defined threshold, which could include posting messages to a Slack channel or modifying a cloud scaling group on an instance. Setting up some of the aforementioned items are quite labour-intensive, but there are a lot of useful things you can extract from the extra data.

© 2018, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points
Categories: AWS

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.