This repository has been archived by the owner on Jun 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_autoscaling_desired_capacity.rb
executable file
·68 lines (54 loc) · 1.93 KB
/
check_autoscaling_desired_capacity.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
#!/usr/bin/env ruby
require 'aws-sdk'
require 'optparse'
require 'ostruct'
require 'pp'
options = OpenStruct.new
OptionParser.new do |opts|
opts.banner = "Usage: check_autoscaling_desired_capacity.rb [options]"
opts.on("-a", "--access_key access_key", "AWS access key") do |a|
options.access_key = a
end
opts.on("-s", "--secret_key secret_key", "AWS secret key") do |s|
options.secret_key = s
end
opts.on("-g", "--autoscaling_group autoscaling_group", "Autoscaling group to inspect") do |g|
options.autoscaling_group = g
end
opts.on("-r", "--region region", "Region to look at") do |r|
options.region = r
end
opts.on("-d", "--debug", "Print debug output") do |d|
options.debug = true
end
end.parse!
# Validate options
missing_options = %w{access_key secret_key autoscaling_group region}.select do |param|
options.send(param.to_sym) == nil
end
if missing_options.length > 0
puts "Missing options, use --help for help: #{missing_options.join(", ")}"
exit 1
end
# Auth with AWS
AWS.config({
:access_key_id => options.access_key,
:secret_access_key => options.secret_key,
:region => options.region
})
# Look for scaling group, and get launch config
as = AWS::AutoScaling.new
as_group = as.groups[options.autoscaling_group]
lc = as_group.launch_configuration
# Look at desired capacity, and the number of instances launched;
# if they're mismatched, then we're not scaling because the spot price
# is too high, or we're scaling down, or scaling up.
instances_launched = as_group.auto_scaling_instances.length
desired_capacity = as_group.desired_capacity
if instances_launched != desired_capacity
puts "CRITICAL: desired capacity for #{options.autoscaling_group} is #{desired_capacity}, but #{instances_launched} instances running"
exit 2
else
puts "OK: desired capacity for #{options.autoscaling_group} is #{desired_capacity}, #{instances_launched} instances running"
exit 0
end