Monitoring Memory and Disk Metrics for Amazon EC2 Linux Instances

From DevOps Notebook

Requirements

In order for script to work either IAM role or awscreds.conf file needs to be configure.
Required IAM permissions are:

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

So policy would look like:

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

Required packages

Amazon Linux

  • login with ec2-user
$ sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64

Ubuntu

  • login with ubuntu user
$ sudo apt-get update && \
  sudo apt-get install libwww-perl libdatetime-perl unzip

Install Monitoring Scripts

$ cd /opt/ && sudo curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
$ sudo unzip CloudWatchMonitoringScripts-1.2.2.zip && \
  sudo rm CloudWatchMonitoringScripts-1.2.2.zip && \
  cd aws-scripts-mon

Setup crontab

Crontab needs to be setup in order to push data periodically to cloudwatch
Bellow cron is example, you should add paremeters for metrics you need,
which you can have by running:

 $ /opt/aws-scripts-mon/mon-put-instance-data.pl --help 

Crontab:

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