Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto

Motivation

I wanted to find out what each unit was in CloudWatch Metrics, but I didn't have a good list, so I made one. bash + ʻaws-cli seems to be annoying, so it seems that you can make it quickly with Ruby, but if you study, with Python`.

Operating environment

Source

#!/usr/bin/env python

import sys
import datetime
import re
from boto.ec2 import cloudwatch

end    = datetime.datetime.utcnow()
start  = end - datetime.timedelta(minutes=10)

cw = cloudwatch.connect_to_region('ap-northeast-1')

def main():

    if len(sys.argv) not in [2,3]:
        print('Usage: python %s <namespace> [separator]' % sys.argv[0])
        quit()

    namespace = sys.argv[1]
    separator = "\t" if len(sys.argv) == 2 else sys.argv[2]
    results   = []

    for metric in cw.list_metrics(namespace=namespace):
        for data in metric.query(start_time=start, end_time=end, statistics='Average'):
            results.append(separator.join([namespace, metric.name, data['Unit']]))

    results = list(set(results))
    for ret in results:
        print(ret)

if __name__ == '__main__':
	main()

How to use

python <script-file-name> <namespace> [separator]

If separator is not specified, it will be tab-delimited.

Execution result

For those who don't know if they are there, but just want to know the results. (EC2, EBS, RDS only)

$ python get-metrics-unit-type.py AWS/EC2
AWS/EC2	CPUCreditUsage	Count
AWS/EC2	CPUCreditBalance	Count
AWS/EC2	StatusCheckFailed	Count
AWS/EC2	DiskReadOps	Count
AWS/EC2	StatusCheckFailed_System	Count
AWS/EC2	DiskWriteBytes	Bytes
AWS/EC2	NetworkOut	Bytes
AWS/EC2	DiskReadBytes	Bytes
AWS/EC2	NetworkIn	Bytes
AWS/EC2	DiskWriteOps	Count
AWS/EC2	StatusCheckFailed_Instance	Count
AWS/EC2	CPUUtilization	Percent

$ python get-metrics-unit-type.py AWS/EBS
AWS/EBS	VolumeWriteBytes	Bytes
AWS/EBS	VolumeQueueLength	Count
AWS/EBS	VolumeReadOps	Count
AWS/EBS	VolumeIdleTime	Seconds
AWS/EBS	VolumeTotalWriteTime	Seconds
AWS/EBS	VolumeWriteOps	Count

$ python get-metrics-unit-type.py AWS/RDS
AWS/RDS	FreeableMemory	Bytes
AWS/RDS	NetworkTransmitThroughput	Bytes/Second
AWS/RDS	DatabaseConnections	Count
AWS/RDS	WriteIOPS	Count/Second
AWS/RDS	ReadIOPS	Count/Second
AWS/RDS	ReadLatency	Seconds
AWS/RDS	WriteThroughput	Bytes/Second
AWS/RDS	ReadThroughput	Bytes/Second
AWS/RDS	FreeStorageSpace	Bytes
AWS/RDS	WriteLatency	Seconds
AWS/RDS	DiskQueueDepth	Count
AWS/RDS	CPUUtilization	Percent
AWS/RDS	CPUCreditUsage	Count
AWS/RDS	NetworkReceiveThroughput	Bytes/Second
AWS/RDS	CPUCreditBalance	Count
AWS/RDS	BinLogDiskUsage	Bytes
AWS/RDS	SwapUsage	Bytes

impression of boto

――At first, it wasn't official but made by the community (isn't it?), So I feel that the usability is a little different from other SDKs. ――On the contrary, it seems to be practically important, so if you get used to it, it may be easier to use than others.

Recommended Posts

Get a list of CloudWatch Metrics and a correspondence table for Unit units with Python boto
Get a list of IAM users with Boto3
Python: Get a list of methods for an object
Get a list of purchased DMM eBooks with Python + Selenium
Get a list of files in a folder with python without a path
[Python] Get a list of folders only
Get a correspondence table of OTU names and their strains (phylum, family, genus, etc.) with QIIME2
For Windows: Get a list of directories and files under a specific directory.
Get a list of packages installed in your current environment with python
I measured the speed of list comprehension, for and while with python2.7.
[Python] Create a list of date and time (datetime type) for a certain period
Get a list of articles posted by users with Python 3 Qiita API v2
Get the stock price of a Japanese company with Python and make a graph
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
List of Python libraries for data scientists and data engineers
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
Get the value of a specific key in a list from the dictionary type in the list with Python
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
[Python] Create a date and time list for a specified period
Installation procedure for Python and Ansible with a specific version
Get the number of specific elements in a python list
[Python] How to delete rows and columns in a table (list of drop method options)
Since Python 1.5 of Discord, I can't get a list of members
Recursively get the Excel list in a specific folder with python and write it to Excel.
Get rid of dirty data with Python and regular expressions
Detect objects of a specific color and size with Python
How to get a list of built-in exceptions in python
Python: Create a dictionary from a list of keys and values
Sample of HTTP GET and JSON parsing with python of pepper
Library for specifying a name server and dig with python
Turn an array of strings with a for statement (Python3)
python memo: enumerate () -get index and element of list at the same time and turn for statement
Extract bigquery dataset and table list with python and output as CSV
Build and test a CI environment for multiple versions of Python
Try to get a list of breaking news threads in Python.
Create a striped illusion with gamma correction for Python3 and openCV3
I tried to create a list of prime numbers with python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
The story of making a standard driver for db with python.
I tried to get a list of AMI Names using Boto3
Try to get CloudWatch metrics with re: dash python data source
Introductory table of contents for python3
Coexistence of Python2 and 3 with CircleCI (1.0)
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Get images of OpenStreetMap and Geographical Survey Institute maps with Python + py-staticmaps
A collection of tips for speeding up learning and reasoning with PyTorch
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Receive a list of the results of parallel processing in Python with starmap
Get images of OpenStreetMap and Geographical Survey Institute maps with Python + staticmap
Get and estimate the shape of the head using Dlib and OpenCV with python
Explanation of creating an application for displaying images and drawing with Python
Display a list of alphabets in Python 3
A memo with Python2.7 and Python3 on CentOS
Connect a lot of Python or and and
Get a token for conoha in python
For those who want to learn Excel VBA and get started with Python
Until you get daily data for multiple years of Japanese stocks and save it in a single CSV (Python)