Skip to content

Instantly share code, notes, and snippets.

@moqada
Created July 8, 2015 12:17
Show Gist options
  • Save moqada/ac3fbc8931ce7588061d to your computer and use it in GitHub Desktop.
Save moqada/ac3fbc8931ce7588061d to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import argparse
import collections
import json
def get_name(ins):
for t in ins['Tags']:
if t['Key'] == 'Name':
return t['Value']
return None
def get_instances(data, status='running'):
return [(get_name(i), i['Placement']['AvailabilityZone'], i['InstanceType'])
for d in data for i in d['Instances'] if i['State']['Name'] == status]
def main(path):
with open(path) as fh:
data = json.loads(fh.read())
ins = get_instances(data['Reservations'])
c = collections.Counter([i[1:] for i in ins])
print('==Summary==')
for i in c.items():
print(i)
print('\n\n==Detail==')
t = ''
for i in sorted(ins, key=lambda x: x[-1]):
if t != i[-1]:
print('--- {} ---'.format(i[-1]))
t = i[-1]
print(i)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('path')
args = parser.parse_args()
main(args.path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment