-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2_counter.rb
42 lines (37 loc) · 1.43 KB
/
ec2_counter.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
require File.dirname(__FILE__) + '/aws_counter'
SECTION = ''
SERVICE_ID = ''
counter = Aws::Counter.new
counter.cli("aws ec2 describe-instances --filters Name=tag:Name,Values=#{SECTION}-#{SERVICE_ID}-*").map(:per_phase) do |result, data|
result['Reservations'].collect { |ins| ins['Instances'][0] }.each do |instance|
if /\w+-\w+-(?<phase>\w+)-.+/ =~ instance['Tags'].find {|tag| tag['Key'].downcase == 'name' }['Value']
data[phase] += 1
end
end
data['total'] = data.values.inject { |a, e| a + e }
end.save('instance_count_per_phase.csv')
counter.map do |result, data|
result['Reservations'].collect { |ins| ins['Instances'][0] }.each do |instance|
type = instance['InstanceType']
if data[type].nil?
data[type] = 1
else
data[type] += 1
end
end
end.save('instance_count_per_type.csv')
counter.map do |result, data|
tmp_data = data.dup
types = []
instances = result['Reservations'].collect { |ins| ins['Instances'][0] }
instances.each { |instance| types << instance['InstanceType'] }
types.each do |type|
Aws::Counter::PHASE_LIST.each { |phase| tmp_data["[#{phase}]#{type}"] = 0 }
end
instances.each do |instance|
if /\w+-\w+-(?<phase>\w+)-.+/ =~ instance['Tags'].find {|tag| tag['Key'].downcase == 'name' }['Value']
tmp_data["[#{phase}]#{instance['InstanceType']}"] += 1
end
end
tmp_data.each { |k, v| data[k] = v if v > 0 }
end.save('instance_count_per_phase_and_type.csv')