Skip to main content

Monitor Memory and Disk Utilization on EC2 instances with Cloudwatch Custom metrics



As you know, in AWS we cannot monitor Hard Disk (Volume) and Memory (RAM) through CloudWatch but with few steps, we can monitor and set alarm on it according to our needs. So, in this post we will see, how can we accomplish that.

Step1.

Installation of dependencies (packages and binaries) in order to setup Memory and Disk utilization

RHEL 7
sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA --enablerepo="rhui-REGION-rhel-server-optional" -y
sudo yum install zip unzip -y
Amazon Linux 2 and Amazon Linux AMI
sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64 -y 
sudo yum install zip unzip -y
SUSE Linux Enterprise Server
sudo zypper install perl-Switch perl-DateTime
sudo zypper install –y "perl(LWP::Protocol::https)"
Debian Based Systems 
sudo apt-get update
sudo apt-get install unzip libwww-perl libdatetime-perl


Step2.

Download and configure the "Memory" and "Disk/Volume" script

Create a folder cloudwatch
mkdir cloudwatch
Download the package into the cloudwatch directory
wget https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip
Unzip the downloaded script
unzip CloudWatchMonitoringScripts-1.2.2.zip
Remove the download zip script compress file
rm CloudWatchMonitoringScripts-1.2.2.zip
Now go inside the extracted script directory
cd aws-scripts-mon
Create a copy of awscreds.template to awscreds.conf
cp awscreds.template awscreds.conf
Now edit the copy file and put AWSAccessId and AWSSecretKey
AWSAccessId=

AWSSecretKey=



Step 3.

Create a user cloudwatch user with Programmatic access

Go to IAM and create a user with following details -
Username - cloudwatchuser 
Access type - Programmatic access 
Permission - CloudWatchFullAccess
Copy the AWS Access ID and AWS Secret Key for next step to configure "awscred.conf"

Now, Paste here the AWSAccessId and AWSSecretKey copy in the last step into the awscreds.conf file

Varify Command
./mon-put-instance-data.pl --mem-util --verify --verbose
Original command
./mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --mem-used --mem-avail
The memory commands All in One (this command is same as above command
./mon-put-instance-data.pl --mem-util --mem-used --mem-avail

now run the following command to send data to cloudwatch (Disk data)

Disk Utility
./mon-put-instance-data.pl --disk-space-util --disk-path=/dev/xvda1
Disk Space Available
./mon-put-instance-data.pl --disk-space-avail --disk-path=/dev/xvda1
Disk Space Used
./mon-put-instance-data.pl --disk-space-used --disk-path=/dev/xvda1
Disk All in One command
./mon-put-instance-data.pl --disk-space-util --disk-space-used --disk-space-avail --disk-path=/dev/xvda1

To see the all information related CPU, Memory and Disk details in the last 24 hours
./mon-get-instance-stats.pl --recent-hours=24


Step 4.

For help related to cronjob and other commands
./mon-put-instance-data.pl --help


Step 5.
To continuously send the data to CloudWatch. We will set a cronjob
*/5 * * * * /home/gautam/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl --mem-util --disk-space-util --disk-path=/dev/xvda1 --from-cron

*/5 * * * * /home/gautam/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl --mem-used --disk-space-used --disk-path=/dev/xvda1 --from-cron

*/5 * * * * /home/gautam/cloudwatch/aws-scripts-mon/mon-put-instance-data.pl --mem-avail --disk-space-avail --disk-path=/dev/xvda1 --from-cron



Step 6.
Now we are setup alarm for disk usage

Copy your instance id, Linux System > Filesystem, instanceId, MountPath => selct DiskSpaceUsed

Popular posts from this blog

WordPress Site is not loading properly behind Google Cloud/AWS Load Balancer

Hello Guys, Today we are going to understand how can we fix a WordPress loading issue (CSS and JS loading issue) behind the Google Cloud Load Balancer or AWS Load Balancer. Generally, When we host a WordPress site directly with Google Cloud Compute Engine VM instance or AWS EC2 instance. It's loading perfectly fine. But once we added this WordPress server behind any Load Balancer either from GCP Load Balancer or AWS Load Balancer, you site will completely broken, means the CSS and JS of your site not loaded properly. The reason for this is - When you put a load balancer in front of WordPress, you need to modify wp-config.php to process the HTTP header HTTP_X_FORWARDED_PROTO to detect the protocol that the user is using and not the protocol the load balancer is using to connect to your backend. To fix this issue, we have to make following changes in the  wp-config.php  file and add the below code snippet on the top of  wp-config.php file - Google Cloud Platform Load Bal...

Git and GitHub Commands Mastery

1. Setup Git and GitHub Global Configuration  git config --global user.email "gautamthakur1983@gmail.com" git config --global user.name "Gautam Thakur" git config --global list git config --list 2. Git Lifecycle | Initilize, Status, Add, Commit git status git init git add git commit -m "Commit Message" git log git log --oneline 3. Git Difference between last commit changes and current version changes git diff 4. Git Compare between 2 different Git Commits git diff eac4c5b 82485b1 5. Git Statsh - To save some changes for temporary purpose ## Pop take out stash and clear but apply take out stash but not clear git stash (To save current changes for temporary) git stash pop (To take out all stash contents) git stash list git stash clear (To clear all the stash changes) git stash save "NAME"  git stash save "NAME1" (Working with multiple stash) git stash sapply 0 or 1 (0 for name, 1 for about) after that run git stash clear git stash clear 6...

How to Setup Kubernetes Cluster in Google Cloud Virtual Machine using "kubeadm"? | Ubuntu 20.04/22.04 LTS

  Hello Friends, In this post, we are going to setup Kubernetes Cluster on Virtual Machine in Google Cloud Platform using kubeadm tool. Hope this post will help you in Kubernetes learning Hand-On Labs (HOL). Requirements: Master Node: No. of VMs 1 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 6443 (API Server) | 2379 (ETCD) | 10251 (Scheduler) | 10252 (Controller Manager) 10250 (Kubelet), sudo access with admin access  Worker Node: No. of VMs 2 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 30000-32767 (Services) | 10250 (Kubelet),  sudo access with admin access  Disable Swap and comment fstab entry: First, Disable Swap and remove or comment the Swap entries from fstab file : sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab Next...