📜 ⬆️ ⬇️

AWS CloudWatch: Custom Metrics

Hi habravchane!

Recently changed jobs, but still absorbed in cloud technologies. And now I will have many more projects, and with them, I hope, interesting articles.

So, it was necessary to create metrics on the parameters that Cloud Watch can not count. You can measure a lot of things, but, for example, take the Load Average. This strange, but understandable to all parameter, explains the state of the system. In general, we can evaluate the health of the server by this state. Naturally, not always, but as an example, LA is perfect.
')
What do we need for this?

1. Amazon CloudWatch Command Line Tools
2. Several scripts

Let's start preparations on the server.
  1. Create a folder in the / opt folder - aws
  2. Unpack the command line utilities in the / opt / aws / mon folder
  3. The key and certificate pk - **. Pem and cert - **. Pem. Put in the folder / opt / aws / keys
  4. The necessary folder with Java symlink is directed to / usr / java / latest


First, let's understand how we will get the Load Average. I like this:
load_average=$(uptime | awk -F'load average:' '{ print $2 }' | awk '{ print $2 }') load_average=${load_average%%','} 

In this variable, we will save LA in 5 minutes (second).

Next, we need a TimeStamp in a specific format:
 timestamp=$(date -u +%Y-%m-%dT%H:%M:%S.000Z) 


Well and the last - registration of value of the metrics:
 mon-put-data --metric-name "LoadAverage" --namespace "CustomMetric" --timestamp $timestamp --value $load_average 

--metric-name "LoadAverage" - the name of the metric
--namespace "CustomMetric" - where this metric will be located

So, the final script with variables, perpetual loop and logging looks like this:
/opt/aws/cw_scaler.sh
 #!/bin/bash export AWS_CLOUDWATCH_HOME=/opt/aws/mon export AWS_CLOUDWATCH_URL=https://monitoring.amazonaws.com export PATH=$AWS_CLOUDWATCH_HOME/bin:$PATH export JAVA_HOME=/usr/java/latest export TOOLS_HOME=/opt/aws export EC2_PRIVATE_KEY=$TOOLS_HOME/keys/pk-GWO6MOXPTCZA5EY7**********RSFJ.pem export EC2_CERT=$TOOLS_HOME/keys/cert-GWO6MOXPTCZA5EY7**********RSFJ.pem while [ true ]; do load_average=$(uptime | awk -F'load average:' '{ print $2 }' | awk '{ print $2 }') load_average=${load_average%%','} timestamp=$(date -u +%Y-%m-%dT%H:%M:%S.000Z) mon-put-data --metric-name "LoadAverage" --namespace "CustomMetric" --timestamp $timestamp --value $load_average echo "$timestamp: Load Average $load_average" >>$TOOLS_HOME/cw_scaler.log echo "" >>$TOOLS_HOME/cw_scaler.log sleep 14 done 


Let's not forget about the init script to start / stop our minidemon:
/etc/init.d/cw_scaler-init
 #!/bin/bash1 #chkconfig: 2345 55 25 # source function library . /etc/rc.d/init.d/functions #Set environement export TOOLS_HOME=/opt/aws start() { $TOOLS_HOME/cw_scaler.sh& } stop() { kill $(ps ax | grep '/opt/aws/cw_scaler.sh' | grep -v "grep" | awk '{print $1}') } case "$1" in start) echo "Starting Cloud Watch scaler." start ;; stop) echo "Stopping Cloud Watch scaler." stop ;; *) echo $"Usage: cw_scaler.sh {start|stop}" exit 1 ;; esac 


Well, actually, what did we get at the end? After 5-10 minutes, you will see the following metrics type in the Cloud Watch panel: CustomMetric, and in it - LoadAverage:


Yes, gentlemen, everything is very simple, and you can shoot any kind of data that can be transferred in numerical equivalent and on the basis of them already build autoscaling.

Shl. If you noticed, this is my first article in the corporate blog EPAM Systems. Please love and respect! =)

Source: https://habr.com/ru/post/147046/


All Articles