Skip to content

Instantly share code, notes, and snippets.

@popowa
Last active August 10, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save popowa/1cb7887eef3996f8394c7cf7f6f3f39c to your computer and use it in GitHub Desktop.
Save popowa/1cb7887eef3996f8394c7cf7f6f3f39c to your computer and use it in GitHub Desktop.
DatadogからEC2に紐づくセキュリティグループを取り出して、Excelで開く事を前提としたCSVを作ります
# -*- 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