AWS — CloudWatch Memory Utilization

Jupyter Data
3 min readJan 18, 2017

--

As you cannot setup an alarm based on memory utilization for Auto-scaling, you need to manually setup the system to measure the System Memory utilization and cron job it to your AWS Cloudwatch system as a custom Metric

Luckily there are sample Perl scripts which have been provided by AWS to do the above — http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html

$ sudo apt-get update$ sudo apt-get install unzip$ sudo apt-get install libwww-perl libdatetime-perl

Download the script and unzip it

$ curl http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip -O$ unzip CloudWatchMonitoringScripts-1.2.1.zip$ rm CloudWatchMonitoringScripts-1.2.1.zip$ cd aws-scripts-mon

You need the IAM Role associated with the instance have these following permissions:

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

Inorder to provide these permissions to these roles, you can use this policy generator here:

Once you generate the policy with these permissions, attach it to your Role under the AWS IAM section: https://console.aws.amazon.com/iam/home?region=your-region#/roles

To perform a simple test run without posting data to CloudWatch

$ ./mon-put-instance-data.pl --mem-util --verify --verbose

To collect all available memory metrics and send them to CloudWatch

$ ./mon-put-instance-data.pl --mem-util --mem-used --mem-avail

Now this metric that you just sent, should be available under cloudwatch

To cronjob this:

For some reason, crontab didn’t work for me. Im guessing something to do with permissions (Not IAM) but local system permissions. I got it working by

$ sudo nano /etc/cron.d/cloudwatch-monitor*/5 * * * * root /home/ubuntu/aws-scripts-mon/mon-put-instance-data.pl --mem-util --from-cron

Save and Exit.

$ sudo chmod +x /etc/cron.d/cloudwatch-monitor
$ sudo /etc/init.d/cron restart or sudo service cron restart

This should send the memory metrics to your cloudwatch every 5 mins, but if you dont receive any in cloud watch — you need to check your syslog

$ sudo tail -10 /var/log/syslog

If you see this error in your log from your cron:

(CRON) info (No MTA installed, discarding output)

Now if you go under Linux System metric in Cloud watch (custom namespace), you should see it.

In case of the following error in your logs and Cloudwatch doesnt pick up the metrics

Signature not yet current: 20130909T170846Z is still later than 20130909T170823Z (20130909T170323Z + 5 min.)

Run the following command to sync the clock

$ sudo ntpdate ntp.ubuntu.com

Didnt work for me: but documenting it nevertheless (Another way)

$ sudo crontab -e

And add this line:

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

Make sure to use absolute paths in your cron job for the script location. Relative paths with ~ dont work (Thanks to wjordan on stackoverflow)

Sign up to discover human stories that deepen your understanding of the world.

--

--

Jupyter Data
Jupyter Data

Written by Jupyter Data

Fastest way to Explore your Data

No responses yet

Write a response