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.
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.
Access to AWS management console
Root access to Ec2 instance- Here we are choosing both centos and ubuntu machine.
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.
The json format of policy will be like this.
After the policy creation login to first ec2 instance.
Execute the following commands.
Download the monitoring script from AWS.
mv awscreds.template awscreds.conf
add the secret and access key in the awscred.conf file
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.
*/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.