Last active
August 10, 2016 11:45
-
-
Save popowa/1cb7887eef3996f8394c7cf7f6f3f39c to your computer and use it in GitHub Desktop.
DatadogからEC2に紐づくセキュリティグループを取り出して、Excelで開く事を前提としたCSVを作ります
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
import re | |
import io | |
from datadog import initialize, api | |
import csv | |
options = { | |
'api_key': 'hogehoge', | |
'app_key': 'foobar' | |
} | |
header = ['name', 'hostname', 'security group'] | |
initialize(**options) | |
hosts = api.Infrastructure.search(q='hosts:') | |
with io.open('sample.csv', 'w', encoding='utf-16') as f: | |
writer = csv.writer(f) | |
writer.writerow(header) | |
for i in range(len(hosts['results']['hosts'])): | |
body = [] | |
tags = api.Tag.get(hosts['results']['hosts'][i]) | |
matching = [ s for s in tags['tags'] if "name:" in s] | |
body.append(matching[0]) | |
body.append(hosts['results']['hosts'][i]) | |
for n in tags['tags']: | |
if n.startswith('security-group:'): | |
body.append(n) | |
writer.writerows([body]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment