Saturday, November 30, 2013

AWS CLI memorandum - Getting CPU Metrics on CloudWatch


This is just memorandum to extract CPUUtilization with AWS CLI because I'm considering using Zabbix or Sensu to monitor the instances on AWS and thinking of how to extract each metrics, such as CPU usage, Memory usage, or Traffic.
Writing plugins will be helpful but the simplest way and using functions which they offer are easier than writing plugins, I believe.

Listing metrics of AWS/EC2

$ aws cloudwatch list-metrics --namespace AWS/EC2 --dimensions Name=InstanceType,Value=t1.micro
{
    "Metrics": [
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "NetworkOut"
        }, 
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "DiskWriteOps"
        }, 
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "DiskWriteBytes"
        }, 
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "CPUUtilization"
        }, 
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "DiskReadOps"
        }, 
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "DiskReadBytes"
        }, 
        {
            "Namespace": "AWS/EC2", 
            "Dimensions": [
                {
                    "Name": "InstanceType", 
                    "Value": "t1.micro"
                }
            ], 
            "MetricName": "NetworkIn"
        }
    ]
}

Getting metrics of CPUUtilization

$ aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-xxx \
--statistics Average \
--start-time `date -u '+%FT%TZ' -d '10 mins ago'` \
--end-time `date -u '+%FT%TZ'` \
--period 60
{
    "Datapoints": [
        {
            "Timestamp": "2013-11-30T11:13:00Z", 
            "Average": 5.6659999999999995, 
            "Unit": "Percent"
        }, 
        {
            "Timestamp": "2013-11-30T11:18:00Z", 
            "Average": 6.6659999999999995, 
            "Unit": "Percent"
        }
    ], 
    "Label": "CPUUtilization"
}

Extracting average with jq command

$ aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-xxx \
--statistics Average \
--start-time `date -u '+%FT%TZ' -d '10 mins ago'` \
--end-time `date -u '+%FT%TZ'` \
--period 60 | jq '.Datapoints[0] | .Average'
6.6659999999999995

10 comments:

  1. did you paste twice on that last one? btw, do you know you are the only one online posting this info? that i could find quick.

    ReplyDelete
    Replies
    1. That was just typo, so I fixed it. Thx!

      Delete
    2. please, post more if you have experienced, very valuable, thanks, cheers, over

      Delete
  2. There are many game soft rte. children. It makes lot of attraction for them. The specified game help or the player. Theming and body lead them in attraction style and mode.

    ReplyDelete
  3. I simply wanted to write down a quick word to say thanks to you for
    those wonderful tips and hints you are showing on this site.


    AWS Training in Chennai


    AWS Training in Bangalore


    AWS Training in Bangalore

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I feel really happy to have seen your webpage and look forward to so
    many more entertaining times reading here. Thanks once more for all
    the details.

    aws training in chennai

    ReplyDelete
  6. You’ll be informed that your PC must be rebooted for Check Disk to complete, so make sure you have closed all of your applications first. Going Here to know more.

    ReplyDelete
  7. Really nice blog post.provided a helpful information.I hope that you will post more updates like this
    AWS Online Training

    ReplyDelete

iJAWS@Doorkeeper