Setup custom CloudWatch metrics on EC2 instance

17 / Jan / 2014 by abhishek.tomar 0 comments

AWS provides its user the Cloud Monitoring service which is used to keep a check on the resources being used.

It can be a great tool for developers and system administrators for cases like:

  • Monitoring data/graphs
  • Setting up alarms
  • Identify trends
  • Take actions based on the state of cloud environment

But AWS does not provide the memory and the disk utilization metrics out of the box.These metrics can be monitored using third party scripts that are are written in Perl and are verified by AWS.

To install the scripts a few very basic and simple steps need to be followed, which are:

Prerequisites:  

We’ll have to install the following packages to get these scripts to work.

  1. For Debian Flavored Servers,

    [shell]sudo apt-get install unzip libwww-perl libcrypt-ssleay-perl[/shell]

  1. For Red Hat Flavored Servers,

    [shell]cd /
    yum install perl-Digest-SHA1
    perl -MCPAN -e shell ## Type ‘yes’ at every prompt, this will drop you at cpan prompt.
    cpan[1]> install Bundle::LWP5_837 LWP ## Here also type ‘yes’ at every prompt.[/shell]

Now we will download and configure the CloudWatch Monitoring Scripts on EC2 Linux system.

Following are the steps which we need to follow:

[shell]cd /opt
wget http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip
unzip CloudWatchMonitoringScripts-v1.1.0.zip
rm CloudWatchMonitoringScripts-v1.1.0.zip
cd aws-scripts-mon
open the awscreds.template file and enter your accesskeyid and secret key[/shell]

Above mentioned commands will set up the the required monitoring for us. Next step for us would be to send memory and disk usage details to CloudWatch, which would enable us to access information and setup alarms from AWS CloudWatch console.

The command below runs every minute and pushes data to CloudWatch. And to enable 1 minute granularity in CloudWatch we would also need to enable “Detailed Monitoring”.

[shell]*/1 * * * * /opt/aws-scripts-mon/mon-put-instance-data.pl –mem-util –disk-space-util  –swap-util –disk-path=/ –disk-path=/data –aws-credential-file=/opt/mon-scripts/awscreds.template[/shell]

In the above command we are monitoring two EBS volumes, one is Root volume and another volume which we have mounted on /data directory. It will push data every minute to CloudWatch.

Now you can set the alarm on your memory and disk utilization as per your application’s requirement.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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