So how do you deploy custom monitors in AWS and push them to CloudWatch:
By default CloudWatch monitors the below four metrics and if you want to monitor beyond these four metrics then you have to deploy custom monitors, below we will go though the depoyment of custom metric and push the metrics to CloudWatch monitoring dashboard.
CloudWatch default metrics for EC2 Instances:
1. CPU
2. Disk IO
3. Network bytes in/out
4. Status check
Download the latest version of monitoring scripts from the below URL:
http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip
[root@shaikprod ~]# sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64 -y
By default CloudWatch monitors the below four metrics and if you want to monitor beyond these four metrics then you have to deploy custom monitors, below we will go though the depoyment of custom metric and push the metrics to CloudWatch monitoring dashboard.
CloudWatch default metrics for EC2 Instances:
1. CPU
2. Disk IO
3. Network bytes in/out
4. Status check
Download the latest version of monitoring scripts from the below URL:
http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip
Above script provides following metrics:
- Memory Utilization – Memory allocated by applications and the operating system, exclusive of caches and buffers, in percentages.
- Memory Used – Memory allocated by applications and the operating system, exclusive of caches and buffers, in megabytes.
- Memory Available – System memory available for applications and the operating system, in megabytes.
- Disk Space Utilization – Disk space usage as percentages.
- Disk Space Used – Disk space usage in gigabytes.
- Disk Space Available – Available disk space in gigabytes.
- Swap Space Utilization – Swap space usage as a percentage.
- Swap Space Used – Swap space usage in megabytes.
Install the required packages
[root@shaikprod ~]# sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64 -y
Complete!
[root@shaikprod ~]#
Download the monitoring scripts:
[root@shaikprod ~]# curl
https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24225 100 24225 0 0 241k 0 --:--:-- --:--:-- --:--:-- 241k
Install the monitoring scripts:
unzip CloudWatchMonitoringScripts-1.2.2.zip && \
rm CloudWatchMonitoringScripts-1.2.2.zip && \
cd aws-scripts-mon
[root@shaikprod ~]# unzip CloudWatchMonitoringScripts-1.2.2.zip && \
> rm CloudWatchMonitoringScripts-1.2.2.zip && \
> cd aws-scripts-mon
Archive: CloudWatchMonitoringScripts-1.2.2.zip
extracting: aws-scripts-mon/awscreds.template
inflating: aws-scripts-mon/AwsSignatureV4.pm
inflating: aws-scripts-mon/CloudWatchClient.pm
inflating: aws-scripts-mon/LICENSE.txt
inflating: aws-scripts-mon/mon-get-instance-stats.pl
inflating: aws-scripts-mon/mon-put-instance-data.pl
inflating: aws-scripts-mon/NOTICE.txt
rm: remove regular file ‘CloudWatchMonitoringScripts-1.2.2.zip’? y
Below files will be deployed by the above install:
[root@shaikprod aws-scripts-mon]# ls -lrt
total 96
-rw-r--r-- 1 root root 138 Mar 26 2018 NOTICE.txt
-rwxr-xr-x 1 root root 18144 Mar 26 2018 mon-put-instance-data.pl
-rwxr-xr-x 1 root root 9739 Mar 26 2018 mon-get-instance-stats.pl
-rw-r--r-- 1 root root 9124 Mar 26 2018 LICENSE.txt
-r--r--r-- 1 root root 22519 Mar 26 2018 CloudWatchClient.pm
-r--r--r-- 1 root root 17021 Mar 26 2018 AwsSignatureV4.pm
-rw-r--r-- 1 root root 30 Mar 26 2018 awscreds.template
If you view the perl scripts, it mentions the usage and provides examples on how to push the metrics to CloudWatch
[root@shaikprod aws-scripts-mon]# grep -A5 "Usage:" *.pl
mon-get-instance-stats.pl:Usage: mon-get-instance-stats.pl [options]
mon-get-instance-stats.pl-
mon-get-instance-stats.pl- Queries Amazon CloudWatch for statistics on CPU, memory, swap, and
mon-get-instance-stats.pl- disk space utilization within a given time interval. This data is
mon-get-instance-stats.pl- provided for the Amazon EC2 instance on which this script is executed.
mon-get-instance-stats.pl-
--
mon-put-instance-data.pl:Usage: mon-put-instance-data.pl [options]
mon-put-instance-data.pl-
mon-put-instance-data.pl- Collects memory, swap, and disk space utilization on an Amazon EC2
mon-put-instance-data.pl- instance and sends this data as custom metrics to Amazon CloudWatch.
mon-put-instance-data.pl-
Verify the script execution:
[root@shaikprod aws-scripts-mon]# ./mon-put-instance-data.pl --mem-util --verify --verbose
MemoryUtilization: 9.47831427948723 (Percent)
Using IAM role <admin-role>
Endpoint: https://monitoring.us-east-2.amazonaws.com
Payload: {"MetricData":[{"Timestamp":1559456122,"Dimensions":[{"Value":"i-00e1a0eaccb314f6f","Name":"InstanceId"}],"Value":9.47831427948723,"Unit":"Percent","MetricName":"MemoryUtilization"}],"Namespace":"System/Linux","__type":"com.amazonaws.cloudwatch.v2010_08_01#PutMetricDataInput"}
Verification completed successfully. No actual metrics sent to CloudWatch.
Now publish the metrics to CloudWatch:
[root@shaikprod aws-scripts-mon]# ./mon-put-instance-data.pl --mem-util --mem-used --mem-avail --aggregated=only
Successfully reported metrics to CloudWatch. Reference Id: 1bb50b87-84fe-11e9-89db-9fb70138859a
You can add the above metric execution to crontab so that the metrics are continuously published to CludWatch.