Last active
December 28, 2016 11:23
-
-
Save mmm/ee84cabc22a0ff79b038605beaf3b97d to your computer and use it in GitHub Desktop.
checked used -vs- reserved instances in ec2
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
#!/usr/bin/ruby | |
# reports used -vs- reserved instances in ec2 | |
# | |
# needs `{apt-get,brew} install awscli` with an `AWS_PROFILE` configured | |
require 'json' | |
def reserved_instances | |
JSON(`aws --profile=#{AWS_PROFILE} ec2 describe-reserved-instances`)["ReservedInstances"] | |
end | |
def active_reserved_instances | |
reserved_instances.select{ |instance| instance["State"] == "active" } | |
end | |
def active_reserved_instance_types | |
instance_types = Hash.new(0) | |
active_reserved_instances.each do |instance| | |
az = instance["AvailabilityZone"] | |
type = instance["InstanceType"] | |
count = instance["InstanceCount"] | |
unless instance_types.has_key?(az) then | |
instance_types[az] = Hash.new(0) | |
end | |
instance_types[az][type] += count | |
end | |
instance_types | |
end | |
def current_reservations | |
JSON(`aws --profile=#{AWS_PROFILE} ec2 describe-instances`)["Reservations"] | |
end | |
def current_instance_types | |
instance_types = Hash.new(0) | |
current_reservations.each do |reservation| | |
reservation["Instances"].each do |instance| | |
pg = instance["Placement"] | |
az = pg["AvailabilityZone"] | |
#az = instance["Placement"]["AvailabilityZone"] | |
type = instance["InstanceType"] | |
unless instance_types.has_key?(az) then | |
instance_types[az] = Hash.new(0) | |
end | |
instance_types[az][type] += 1 | |
end | |
end | |
instance_types | |
end | |
def sorted(hash) | |
Hash[hash.sort_by(&:first)] | |
end | |
%w(us-west-2a us-west-2b us-west-2c).each do |az| | |
puts "for az #{az}:" | |
puts "reserved: #{sorted(active_reserved_instance_types[az])}" | |
puts " used: #{sorted(current_instance_types[az])}" | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment