CloudWatch のメトリクスを Elasticsearch に突っ込むスクリプトを Python の標準ライブラリだけ使って書いたけど、 Elasticsearch クライアントを使えばよかったことに今さら気付いた。
pip install elasticsearch
でインストールしたら、スクリプトはこんな風に書き直すことができた。
# -*- coding: utf-8 -*- import os import sys import json from elasticsearch import Elasticsearch ELASTICSEARCH_URL = "localhost:9200" METRICS_ROOT_DIR = "/var/log/perform/my-app-name" INSTANCES = [ "rds", ] METRICS = [ "CPUUtilization", "DatabaseConnections", "DiskQueueDepth", "FreeableMemory", "FreeStorageSpace", "ReadIOPS", "WriteIOPS", "ReadLatency", "WriteLatency", "NetworkReceiveThroughput", "NetworkTransmitThroughput", ] es = Elasticsearch(ELASTICSEARCH_URL) def post_datapoint(index_name, type_name, datapoint): # Elasticsearch クライアントを使ってインデックスにデータを登録 es.index(index=index_name, doc_type=type_name, id=datapoint["Timestamp"], body=datapoint) def post_instance_metrics(dir_path, instance): for metric in METRICS: file_path = os.path.join(dir_path, metric + ".json") with open(file_path) as f: data = json.load(f) for datapoint in data["Datapoints"]: post_datapoint(instance, metric, datapoint) for instance in INSTANCES: metrics_dir = os.path.join(METRICS_ROOT_DIR, instance) post_instance_metrics(metrics_dir, instance)
スッキリ。 awscli は pip でインストールしたのに、Elasticsearch は標準ライブラリだけ使うというのも、 今考えればおかしな話だ。