Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Last active March 27, 2019 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akiatoji/bdf29afbabefe8a2c3cedefd1c33203a to your computer and use it in GitHub Desktop.
Save akiatoji/bdf29afbabefe8a2c3cedefd1c33203a to your computer and use it in GitHub Desktop.
Add CloudWatch Memory/Disk monitoring to EC2

AWS EC2 Memory and Disk monitoring/alert

AWS kind of sucks when it comes to monitoring Memory and Disk usage on EC2, as in they don't provide it out of the box. AWS instead gives you a set of perl scripts to do this via CloudWatch custom metrics.

Details are here:

Monitoring Memory and Disk Metrics for Amazon EC2 Linux Instances

The following is the actual steps used to get Disk/Memory stats into CloudWatch

Install CloudWatchMonitoringScripts

First, create an IAM user who has permissions to the following:

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

Create an AccessKey/SecretKey under this user. Then do the steps below per instance.

This is on AMI Linux

> sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https -y
> sudo yum -y install perl-Digest-SHA  # not documented, was needed on at least 1 AMI EC2
> curl https://aws-cloudwatch.s3.amazonaws.com/downloads/Clou dWatchMonitoringScripts-1.2.2.zip -O
> unzip CloudWatchMonitoringScripts-1.2.2.zip
> cd aws-scripts-mon
> cp awscreds.template awscreds.conf
> vi awscreds.conf

Add AccessKey/SecretKey to awscreds.conf. Then add something like this to crontab:

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

Then go into CloudWatch and create dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment