Calling CloudWatch API (using in blackbird AWS plugin)

It may be a bit of a mundane article, but let's use boto to hit the CloudWatch API to get various metrics. This mechanism is also used in various AWS plugins of blackbird, and let's deepen our understanding of NameSpace and such conceptual things in CloudWatch together.

CloudWatch Outline

The basic concept of CloudWatch is

There is. Then, it is in no particular order, but I will follow in order from the one that is easy to understand.

Metrics

Metrics is one of the most straightforward concepts, Zabbix-wise Item. It is an image that one item contains various values in chronological order. For example, in the case of RDS, the value of how much Swap was swapped is entered in Bytes in Metrics called Swap Usage.

Namespaces

Namespaces is a lot of the above Metrics, which is kind and quantitative. In Zabbix terms, it corresponds to the concept of Host (or also Host Groups).

It feels like there is a Metric called Swap Usage or a Metric called Database Connections in the Host called db001.

Dimensions

Dimensions is a bit fluffy and confusing (at least I thought so). In Zabbix, it will be Application (If HostGroup is Namespaces, Host will be Dimensions).

Periods

From the point of view of API to get Metric, it corresponds to 1min, 5min in Avg (1min) or Avg (5min) from the viewpoint of CloudWatch UI, depending on which interval data is acquired. It's the Period of Time Period.

Time stamps

It is a time stamp. Time stamps are set for each metric in Namespaces to indicate what time metric is and how much. Anyone who sometimes sends data with a time stamp in Zabbix Sender can think of it as equivalent to that time stamp.

Statistics

In Japanese, it is considered to be statistics, but I wonder if it is closer to Method personally than statistics, and how to retrieve Data. This may or may not be available with Metric.

For example, if the unit of Metric is Bytes, Sum calculates the total Bytes per unit time, Average indicates the average per unit time, and Count indicates the frequency of occurrence.

In Zabbix, I think that it corresponds to the calculation at the time of saving, but the big difference is that you can put only raw data and call arbitrary Statistics when fetching.

Units

As the name implies, it is a unit. It can be bytes, bytes / second,%, or the number of times (simply a number).

How to get CloudWatch metrics

Now that you know the general concept, let's actually use the CloudWatch API to get Metrics. First of all

pip install boto

Installation of boto. boto is a Python binding library for calling AWS APIs, although it may not be necessary to explain. Even blackbird uses boto internally.

Get Connection for CloudWatch

The class name is boto.ec2.cloudwatch.CloudWatchConnection, but the image is an API Token Object-like image.

import datetime

import boto.ec2.cloudwatch

conn = boto.ec2.cloudwatch.connect_to_region(
    region_name='ap-northeast-1',
    aws_access_key_id='YOUR_AWS_ACCESS_KEY_ID',
    aws_secret_access_key='YOUR_AWS_SECRET_ACCESS_KEY'
)

If you give the correct credentials here, you will get a Connection instance. So, when I try to get the Request Count for 15 minutes from the latest ELB and Sum every 5 minutes using the Connection instance


end_time = datetime.datetime.utcnow()
start_time = end_time - datetime.timedelta(minutes=15)

metrics = conn.get_metric_statistics(
    #period specifies the number of seconds as an int
    period=300,
    #start_time is the beginning of the time stamp period datetime.Specified by datetime object
    start_time=start_time,
    #end_time is start_Like time
    #datetime the end of the time stamp period.Specified by datetime object
    end_time=end_time,
    #metric_As the name suggests, specify the Metric name with str.
    metric_name='RequestCount',
    #Specify Namespace with str
    namespace='AWS/ELB',
    statistics='Sum',
    #Dimensions specify key and value with dictionary
    dimensions={
        'LoadBalancerName': 'YOUR_ELB_NAME',
        'AvailabilityZone': 'ap-northeast-1a'
    },
    #There are some units that are meaningless even if specified in Metric.
    unit=None
)

print metrics

Such a value will be returned.

[
    {
        u'Timestamp': datetime.datetime(2014, 12, 7, 10, 48),
        u'Sum': 143426.0,
        u'Unit': u'Count'
    },
    {
        u'Timestamp': datetime.datetime(2014, 12, 7, 10, 38),
        u'Sum': 135535.0,
        u'Unit': u'Count'
    },
    {
        u'Timestamp': datetime.datetime(2014, 12, 7, 10, 43),
        u'Sum': 139088.0,
        u'Unit': u'Count'
    }
]

A dictionary with Timestamp as key, datetime.datetime object as value, specified Statistics as key, Metrics value as value, Unit as key, and the name of the specified (or defualt) Unit as value. The list of is returned (it is obvious from the above structure).

So, blackbird sends data to zabbix server based on this.

Recommended Posts

Calling CloudWatch API (using in blackbird AWS plugin)
Mouse operation using Windows API in Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Get mail using Gmail API in Java
Tweet using the Twitter API in Python
Get Youtube data in Python using Youtube Data API
I tried using AWS Rekognition's Detect Labels API
Try using the BitFlyer Ligntning API in Python
Get image URL using Flickr API in Python
Let's judge emotions using Emotion API in Python
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Upload JPG file using Google Drive API in Python
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
Get LEAD data using Marketo's REST API in Python
Try to delete tweets in bulk using Twitter API
OpenVINO using Inference Engine Python API in PC environment
Using the National Diet Library Search API in Python