forked from epheph/ec2-check-reserved-instances
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2-check-reserved-instances.py
executable file
·113 lines (91 loc) · 4.25 KB
/
ec2-check-reserved-instances.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
import sys
import os
from pprint import pprint
import boto
import boto.ec2
from boto.sdb.db.property import StringProperty, IntegerProperty
from boto.manage import propget
# You can uncomment and set these, or set the env variables AWSAccessKeyId & AWSSecretKey
# AWS_ACCESS_KEY_ID="aaaaaaaaaaaaaaaaaaaa"
# AWS_SECRET_ACCESS_KEY="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
#try:
# AWS_ACCESS_KEY_ID
#except NameError:
# try:
# AWS_ACCESS_KEY_ID=os.environ['AWSAccessKeyId']
# AWS_SECRET_ACCESS_KEY=os.environ['AWSSecretKey']
# except KeyError:
# print "Please set env variable"
# sys.exit(1)
DETAILS=False
#ec2_conn = boto.connect_ec2(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
def get_region():
prop = StringProperty(name='region', verbose_name='EC2 Region',
choices=boto.ec2.regions)
return propget.get(prop, choices=boto.ec2.regions)
def remove_zero(adict):
""" prune dict """
if type(adict) != dict:
return
for k,v in adict.items():
if v == 0:
adict.pop(k)
region = get_region()
ec2_conn = boto.ec2.connect_to_region(region.name)
reservations = ec2_conn.get_all_instances()
running_instances = {}
for reservation in reservations:
for instance in reservation.instances:
if instance.state != "running":
if DETAILS: sys.stderr.write("Disqualifying instance %s: not running\n" % ( instance.id ) )
elif instance.spot_instance_request_id:
if DETAILS: sys.stderr.write("Disqualifying instance %s: spot\n" % ( instance.id ) )
else:
if instance.vpc_id:
print "Does not support vpc yet, please be careful when trusting these results"
else:
az = instance.placement
instance_type = instance.instance_type
running_instances[ (instance_type, az ) ] = running_instances.get( (instance_type, az ) , 0 ) + 1
#prune running_instances
remove_zero(running_instances)
# pprint( running_instances )
reserved_instances = {}
for reserved_instance in ec2_conn.get_all_reserved_instances():
if reserved_instance.state != "active":
if DETAILS: sys.stderr.write( "Excluding reserved instances %s: no longer active\n" % ( reserved_instance.id ) )
else:
az = reserved_instance.availability_zone
instance_type = reserved_instance.instance_type
reserved_instances[( instance_type, az) ] = reserved_instances.get ( (instance_type, az ), 0 ) + reserved_instance.instance_count
#prune reserved_instances
remove_zero(reserved_instances)
# pprint( reserved_instances )
if not running_instances and not reserved_instances:
print "You does use this region."
sys.exit(0)
# this dict will have a positive number if there are unused reservations
# and negative number if an instance is on demand
instance_diff = dict([(x, reserved_instances[x] - running_instances.get(x, 0 )) for x in reserved_instances])
# instance_diff only has the keys that were present in reserved_instances. There's probably a cooler way to add a filtered dict here
for placement_key in running_instances:
if not placement_key in reserved_instances:
instance_diff[placement_key] = -running_instances[placement_key]
# pprint ( instance_diff )
unused_reservations = dict((key,value) for key, value in instance_diff.iteritems() if value > 0)
if unused_reservations == {}:
print "Congratulations, you have no unused reservations"
else:
for unused_reservation in unused_reservations:
print "UNUSED RESERVATION!\t(%s)\t%s\t%s" % ( unused_reservations[ unused_reservation ], unused_reservation[0], unused_reservation[1] )
print ""
unreserved_instances = dict((key,-value) for key, value in instance_diff.iteritems() if value < 0)
if unreserved_instances == {}:
print "Congratulations, you have no unreserved instances"
else:
for unreserved_instance in unreserved_instances:
print "Instance not reserved:\t(%s)\t%s\t%s" % ( unreserved_instances[ unreserved_instance ], unreserved_instance[0], unreserved_instance[1] )
qty_running_instances = reduce( lambda x, y: x+y, running_instances.values() )
qty_reserved_instances = reduce( lambda x, y: x+y, reserved_instances.values() )
print "\n(%s) running on-demand instances\n(%s) reservations" % ( qty_running_instances, qty_reserved_instances )