-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.rb
122 lines (99 loc) · 2.77 KB
/
analyze.rb
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
114
115
116
117
118
119
120
121
122
require 'bundler/setup'
Bundler.require
require 'optparse'
require 'json'
$auth_data = YAML.load_file 'auth.yml'
$account = false
$region = false
OptionParser.new do |opts|
opts.on('-a', '--account ACCOUNT', 'The AWS account to use') do |a|
$account = a
end
opts.on('-r', '--region REGION', 'The AWS region to use') do |r|
$region = r
end
end.parse!
regions = $auth_data.values.collect{|v| v.keys}.flatten.uniq.sort
usage = "Usage: bundle exec ruby #{$0} --account=#{$auth_data.keys.sort.join(',')} --region=#{regions.join(',')}"
if $account == false or $region == false
puts usage
exit 1
end
if $auth_data[$account] == nil
puts "Error: Could not find credentials for account \"#{$account}\""
puts usage
exit 2
end
if $auth_data[$account][$region] == nil
puts "Error: Could not find credentials for region \"#{$region}\" in account \"#{$account}\""
puts usage
exit 3
end
$auth = $auth_data[$account][$region]
AWS.config( :access_key_id=>$auth['access_key_id'], :secret_access_key=>$auth['secret_access_key'], :region=>$region )
ec2 = AWS.ec2
instances = ec2.instances.select{|instance| instance.status == :running}
reserved = ec2.reserved_instances.select{|ri| ri.state == 'active'}
puts "Collecting running instances"
running1 = []
running2 = []
instances.each do |instance|
print "."
running1 << instance.instance_type + "/" + instance.availability_zone
running2 << instance.instance_type + "/" + instance.availability_zone
end
puts
puts
puts "Collecting purchased reserved instances"
purchased1 = []
purchased2 = []
reserved.each do |ri|
ri.instance_count.times do
print "."
purchased1 << ri.instance_type + "/" + ri.availability_zone
purchased2 << ri.instance_type + "/" + ri.availability_zone
end
end
puts
puts
#########
puts "Purchased RIs that are not in use by any running EC2 instances:"
running1.each do |r|
# find the index of the first element that matches the running instance
i = purchased1.index r
if !i.nil?
purchased1.delete_at i
end
end
purchased1.sort.each do |p|
puts " #{p}"
end
puts
#########
puts "Running instances that are not yet reserved:"
purchased2.each do |p|
i = running2.index p
if !i.nil?
running2.delete_at i
end
end
running2.sort.each do |r|
puts " #{r}"
end
puts
#########
puts "Running Instances:"
puts
instances.sort{|x,y| x.instance_type <=> y.instance_type}.each do |instance|
puts " #{instance.instance_type}\t#{instance.availability_zone}\t#{instance.launch_time}\t#{instance.vpc_id}\t#{instance.tags['Name']}"
end
puts
puts
puts "Purchased Reserved Instances"
puts
reserved.sort{|x,y| x.instance_type <=> y.instance_type}.each do |ri|
ri.instance_count.times do
puts " #{ri.instance_type}\t#{ri.availability_zone}\t#{ri.offering_type}\t#{ri.product_description}"
end
end
puts