AWS CloudWatch: Custom Metrics

    Hi habravchane!

    Recently changed his job, but still absorbed in cloud technology. And now I will have much more projects, and with them the articles, I hope, are interesting.

    So, it was necessary to create metrics for parameters that Cloud Watch can’t count. You can measure a lot of things, but, for example, take the Load Average. This strange but understandable parameter explains the state of the system. In general, we can evaluate server health by this condition. Naturally, not always, but as an example, LA is ideal.

    What do we need for this?

    1. Amazon CloudWatch Command Line Tools
    2. Several scripts

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

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

    In this variable we save LA for 5 minutes (second).

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

    Well, the last thing is registering the metric value:
    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:
    export AWS_CLOUDWATCH_HOME=/opt/aws/mon
    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 }')
    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

    Do not forget about the init script to start / stop our mini-daemon:
    #chkconfig: 2345 55 25
    # source function library
    . /etc/rc.d/init.d/functions
    #Set environement
    export TOOLS_HOME=/opt/aws
        kill $(ps ax | grep '/opt/aws/' | grep -v "grep"  | awk '{print $1}')
    case "$1" in
    		echo "Starting Cloud Watch scaler."
    		echo "Stopping  Cloud Watch scaler."
    		echo $"Usage: {start|stop}"
    		exit 1

    Well with actually what we got in the end? After 5-10 minutes, you will see the type of metrics in the Cloud Watch panel: CustomMetric, and LoadAverage in it:

    Yes, gentlemen, everything is very simple, and you can take any kind of data that can be transferred in numerical terms and based on them build autoscaling.

    Threat. If you notice, this is my first article on the EPAM Systems corporate blog. Please love and respect! =)

    Also popular now: