Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kim-YongJin/55ebad68ab16a86c9643a411a29f70a9 to your computer and use it in GitHub Desktop.
Save Kim-YongJin/55ebad68ab16a86c9643a411a29f70a9 to your computer and use it in GitHub Desktop.
[Only 2 step]Monitoring Memory and Disk Metrics for AWS EC2 Linux Instances

Step 1(permissions)

Ensure the permissions. It needs 4 permissions the following.

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

You can attach permittions using one of the following options.

  • Associate an IAM role with your instance
  • Specify your AWS credentials in a credentials file

Step 2(sh)

Run attached file or Copy and Paste the following on the target EC2 instance with SSH. You can change the mon-put-instance-data.pl options as you needs. Please have a look at this link. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html#using_put_script

# install dependencies
sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https -y
cd
# download a script
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
# unzip script
unzip CloudWatchMonitoringScripts-1.2.2.zip
# delete a downloaded script
rm CloudWatchMonitoringScripts-1.2.2.zip
# add to crontab
(crontab -l ;  echo "*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --swap-util --swap-used --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --from-cron") | crontab

Location of Metrics

The data will be published to CloudWatch each 5 minute. You can see it the following.

  • Memory: AWS CloudWatch -> Metrics -> Linux System -> InstanceId
  • Disk: AWS CloudWatch -> Metrics -> Linux System -> Filesystem, InstanceId, MountPath cloudwatch-metrics-ec2-memory-disk
sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https -y
cd
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
unzip CloudWatchMonitoringScripts-1.2.2.zip
rm CloudWatchMonitoringScripts-1.2.2.zip
(crontab -l ; echo "*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --swap-util --swap-used --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --from-cron") | crontab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment