Cloudwatch service of AWS is very useful for the developers as well as for server admins because it helps them to find the bottleneck in their application architecture. AWS provide lots of pre-defined metrics to detect the issues regarding system and predifined services like CPU Limit, Disk Usage, Network Data etc. But if you want to monitor your system on the basis of other specifications then you can achieve it with the help of custom metrics.
To create a custom metric for your system you have to use the aws cli because it helps you to put data on the cloudwatch panel. You can download the aws cli for your OS distribution from here https://aws.amazon.com/cli/ and configure it by “aws configure” command which ask you for following parameters-:
AWS Access Key ID [None]: ACCESS KEY ID OF YOUR IAM USER
AWS Secret Access Key [None]: SECRET KEY OF YOUR IAM USER
Default region name [None]: REGION NAME for ex-: us-west-2
Default output format [None]: ENTER
After installing the aws cli on your system, now you need to grab the value which you want to send for creating the metrics for example we want to create a custom metrics for FREE RAM of the server, To grab the value of FREE RAM you can use the following bash script-:
SAVE this script with script.sh
#START OF SCRIPT
MEMFREE=$(egrep -o "MemFree:\s*([0-9]*)" /proc/meminfo | egrep -o "[0-9]*")
BUFFERS=$(egrep -o "Buffers:\s*([0-9]*)" /proc/meminfo | egrep -o "[0-9]*")
CACHED=$(egrep -o "\bCached:\s*([0-9]*)" /proc/meminfo | egrep -o "[0-9]*")
FREEMEM=`expr $MEMFREE + $BUFFERS + $CACHED`
/usr/local/bin/aws cloudwatch put-metric-data --namespace "MyNameSpace" --metric-name "FREE RAM" --value $FREEMEM --region us-west-2
#END OF SCRIPT
In the script we have included the aws cli command as well which is used to put the data on AWS Cloudwatch panel, lets understand some of there parameters which are as follows-:
- namespace -: It resembles the unique namespace of your metric to differentiate with other custom or predefined metrics for ex-: –namespace “MyNameSpace”
- metric-name -: It defines the metric name which we used to store our metric data for ex-: –metric-name “FREE RAM”
- value -: it resembles the exact value of our metric at particular time period for ex-: –value 56
- timestamp -: it helps to map the timestamp with the values of metrics for ex-: –timestamp 2016-06-14T12:00:00
- region -: It define the region in which our server is running for ex-: –region us-west-2
Now create a crontab entry of script.sh in your server for every minute so that it will send data to cloudwatch periodically every minute.
* * * * * ./script.sh
Now go to the cloudwatch panel and observe the graph of your metrics.
Now you can automate various tasks on the basis of your own custom metrics for ex-:
- You can create an alarm on cloudwatch custom metrics to detect any abnormal activity.
- You can also create an alarm to autoscale your EC2 instances on the basis of your own custom metrics.
Cloudwatch custom metrics help you to manage your server’s need according to your own created parameters and it helps you to analyze the logs more efficiently in graphical way.