How to Add Custom Metrics in Amazon CloudWatch?
Audio : Listen to This Blog.
Amazon CloudWatch is an Amazon Web Services utility allowing monitoring of various components like EC2 instances, EBS volumes and the Elastic Load Balancer. For EC2 instances, we can monitor CPUUtilization, DiskReadBytes, DiskReadOps, DiskWriteBytes, NetworkIn and NetworkOut. More often than not, end-users would want to monitor more parameters than the ones available. eg. Free Memory, Free Swap and so on.
Amazon CloudWatch provides custom metrics to help circumvent the problem. One can simply define a custom metric based on each one’s need and continuously feed it with data using a simple bash or python script running a while loop. Let’s take an example of Free Memory.
The aim is to define a custom metric for Free Memory and continuously feed data to the metric from the machine that needs to be monitored.
- Install install/setup the AWS cloud-watch Command line fromhttp://aws.amazon.com/developertools/2534
- Setup the API as you would for any AWS Command line tool.
- export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/jre/
- export AWS_CLOUDWATCH_HOME=/opt/cloudwatch/ or the location where you have unzipped the utility
- export PATH=$AWS_CLOUDWATCH_HOME/bin:$PATH;
- To define a new metric eg. FreeMemory,
ubuntu@domU-12-31-32-0B-01-A7:~$ mon-put-data -m “FreeMemory” –namespace Clogeny –dimensions “instance=i-f23233,servertype=MongoDB” –value 100 -u Bytes - Now this command will create a FreeMemory metric in another 20 minutes. The namespace and dimensions can be customized as per your needs. For now we have chosen a dummy value (but will eventually contain valid data) and the unit we choose is Bytes.
There is more information about options and explanations about the Cloud Watch API athttp://docs.amazonwebservices.com/AmazonCloudWatch/latest/DeveloperGuide/index.html?CLIReference.html - Once the metric is created, we need to continuously feed data to this metric. /proc/meminfo contains information about the current memory status of the system and can be used as the data source
Here is a simple python script that will feed FreeMemory with the required data
import commands
ret, cmdout = commands.getstatusoutput(“cat /proc/meminfo | grep -e MemFree”)
free_mem = str(int(cmdout.split()[1]) * 1024)
# This simply fetches the Free memory from /proc/meminfo and converts it to bytes.
ret,cmdout= commands.getstatusoutput(“mon-put-data -m “FreeMemory” –namespace Clogeny –dimensions “instance=i-f23233,servertype=MongoDB” –value ” + free_mem + ” – u Bytes”)
# On executing command, the data will be populated on the CloudWatch Dashboard.
# Run these above commands in a loop, and you have your own little agent providing Free memory metric for your machine.
After running the tool for a while, you can see a similar graph in the AWS CloudWatch Console
Deleting a custom Metric
A custom metric cannot be explicitly deleted. If the metric remains unused for 2 weeks, it gets automatically deleted.
Costing
$0.50 per metric per month
Summary
You can see how easy it is to add a custom metric. In this example we have shown how to add a FreeMemory metric. There are several other useful metrics such FreeSwap, ProcessAvailability, DiskSpace, etc that can also be added. MSys Technologies as a leading AWS cloud services provider, can help you do that.