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
Step2.
Download and configure the "Memory" and "Disk/Volume" script
Create a folder cloudwatch
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
now run the following command to send data to cloudwatch (Disk data)
Disk Utility
To see the all information related CPU, Memory and Disk details in the last 24 hours
Step 4.
For help related to cronjob and other commands
Step 5.
To continuously send the data to CloudWatch. We will set a cronjob
Copy your instance id, Linux System > Filesystem, instanceId, MountPath => selct DiskSpaceUsed
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" -ysudo yum install zip unzip -yAmazon Linux 2 and Amazon Linux AMIsudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64 -y sudo yum install zip unzip -ySUSE Linux Enterprise Serversudo zypper install perl-Switch perl-DateTimesudo zypper install –y "perl(LWP::Protocol::https)"Debian Based Systems 
sudo apt-get updatesudo apt-get install unzip libwww-perl libdatetime-perlStep2.
Download and configure the "Memory" and "Disk/Volume" script
Create a folder cloudwatch
mkdir cloudwatchDownload the package into the cloudwatch directory
wget https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zipUnzip the downloaded script
unzip CloudWatchMonitoringScripts-1.2.2.zipRemove the download zip script compress file
rm CloudWatchMonitoringScripts-1.2.2.zipNow go inside the extracted script directory
cd aws-scripts-monCreate a copy of awscreds.template to awscreds.conf
cp awscreds.template awscreds.confNow 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 --verboseOriginal command
./mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --mem-used --mem-availThe memory commands All in One (this command is same as above command
./mon-put-instance-data.pl --mem-util --mem-used --mem-availDisk Utility
./mon-put-instance-data.pl --disk-space-util --disk-path=/dev/xvda1Disk Space Available
./mon-put-instance-data.pl --disk-space-avail --disk-path=/dev/xvda1Disk Space Used
./mon-put-instance-data.pl --disk-space-used --disk-path=/dev/xvda1Disk All in One command
./mon-put-instance-data.pl --disk-space-util --disk-space-used --disk-space-avail --disk-path=/dev/xvda1./mon-get-instance-stats.pl --recent-hours=24For help related to cronjob and other commands
./mon-put-instance-data.pl --helpTo 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 usageCopy your instance id, Linux System > Filesystem, instanceId, MountPath => selct DiskSpaceUsed

 
 
