0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python botoでCloudWatch Metricsの一覧とUnitの単位の対応表を取得する

Posted at

動機

CloudWatchのMetricsで、それぞれの単位ってなんだろうっていうのを調べたかったけど、良い一覧が無かったので作った。
bash + aws-cliだとめんどくさそうなので、Rubyならサクッと作れそうだけど、お勉強がてらPythonで。

動作環境

  • Python 3.4.2
  • boto 2.4.1

ソース

# !/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()

使い方

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

separatorは指定しないとタブ区切りになる。

実行結果

いるかどうか分からないけど、結果だけ知りたい方向けに。
(EC2,EBS,RDSのみ)

$ 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

botoの感想

  • 初めは公式じゃなくてコミュニティ製だった(ですよね?)からか、他のSDKとちょっと使用感が違う気がする
  • 逆に、実用重視っぽい感じがするので慣れれば他より使いやすいのかも?
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?