Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added -r parameter to choose alternate regions. #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions ec2-check-reserved-instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,38 @@
import sys
import os
import boto
import boto.ec2
from pprint import pprint
import argparse
from argparse import RawTextHelpFormatter
from collections import defaultdict

AWS_REGIONS = ['ap-northeast-1',
'ap-southeast-1',
'ap-southeast-2',
'eu-west-1',
'sa-east-1',
'us-east-1',
'us-west-1',
'us-west-2']

# You can uncomment and set these, or set the env variables AWSAccessKeyId & AWSSecretKey
# AWS_ACCESS_KEY_ID="aaaaaaaaaaaaaaaaaaaa"
# AWS_SECRET_ACCESS_KEY="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"

regiontxt = "If not specified then region us-east-1 is used.\nAvailable regions:\n\n %s" % ( "\n ".join(AWS_REGIONS))

parser = argparse.ArgumentParser(description='List a summary of AWS EC2 reservations', epilog=regiontxt, formatter_class=RawTextHelpFormatter)
parser.add_argument('-r', '--region', help="EC2 region name", required=False)
parser.add_argument('-n', '--names', help="Include names or instance IDs of instances that fit non-reservations", required=False, action='store_true')
args = parser.parse_args()

if args.region is not None:
if not args.region in AWS_REGIONS:
print "Unknown region: %s" % ( args.region )
sys.exit(-1)


try:
AWS_ACCESS_KEY_ID
except NameError:
Expand All @@ -19,11 +45,15 @@
print "Please set env variable"
sys.exit(1)

if args.region:
ec2_conn = boto.ec2.connect_to_region(args.region, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
else:
ec2_conn = boto.connect_ec2(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

ec2_conn = boto.connect_ec2(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
reservations = ec2_conn.get_all_instances()

running_instances = {}
instance_ids = defaultdict(list)
for reservation in reservations:
for instance in reservation.instances:
if instance.state != "running":
Expand All @@ -38,6 +68,10 @@
instance_type = instance.instance_type
running_instances[ (instance_type, az ) ] = running_instances.get( (instance_type, az ) , 0 ) + 1

if "Name" in instance.tags and len(instance.tags['Name']) > 0:
instance_ids[ (instance_type, az ) ].append(instance.tags['Name'])
else:
instance_ids[ (instance_type, az ) ].append(instance.id)

# pprint( running_instances )

Expand All @@ -53,7 +87,6 @@

# pprint( reserved_instances )


# 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])
Expand All @@ -78,10 +111,20 @@
if unreserved_instances == {}:
print "Congratulations, you have no unreserved instances"
else:
ids=""
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] )
if args.names:
ids = ', '.join(sorted(instance_ids[unreserved_instance]))
print "Instance not reserved:\t(%s)\t%s\t%s\t%s" % ( unreserved_instances[ unreserved_instance ], unreserved_instance[0], unreserved_instance[1], ids )

if running_instances.values():
qty_running_instances = reduce( lambda x, y: x+y, running_instances.values() )
else:
qty_running_instances = 0

qty_running_instances = reduce( lambda x, y: x+y, running_instances.values() )
qty_reserved_instances = reduce( lambda x, y: x+y, reserved_instances.values() )
if reserved_instances.values():
qty_reserved_instances = reduce( lambda x, y: x+y, reserved_instances.values() )
else:
qty_reserved_instances = 0

print "\n(%s) running on-demand instances\n(%s) reservations" % ( qty_running_instances, qty_reserved_instances )