-
Notifications
You must be signed in to change notification settings - Fork 12
/
healthcheck.go
263 lines (223 loc) · 5.37 KB
/
healthcheck.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package flywheel
import (
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Status keeps track of the status
type Status uint
// Different state of the systems
const (
STOPPED Status = iota
STARTING
STARTED
STOPPING
UNHEALTHY
)
// String - Working with integer statuses is mostly better, but it's
// occasionally necessary to output the status name.
func (n Status) String() string {
switch n {
case STOPPED:
return "STOPPED"
case STARTING:
return "STARTING"
case STARTED:
return "STARTED"
case STOPPING:
return "STOPPING"
case UNHEALTHY:
return "UNHEALTHY"
default:
return "INVALIDSTATUS"
}
}
// HealthWatcher - Check the status of the instances. Currently checks if they are "ready"; all
// stopped or all started. Will need to be extended to determine actual status.
func (fw *Flywheel) HealthWatcher(out chan<- Status) {
out <- fw.CheckAll()
ticker := time.NewTicker(fw.hcInterval)
for {
select {
case <-ticker.C:
out <- fw.CheckAll()
}
}
}
// CheckAll - check asg/instance state
// TODO - add more information what is unhealthy
func (fw *Flywheel) CheckAll() Status {
health := make(map[string]int)
err := fw.checkInstances(health)
if err != nil {
log.Print(err)
return UNHEALTHY
}
err = fw.checkStoppedAutoScalingGroups(health)
if err != nil {
log.Print(err)
return UNHEALTHY
}
_, terminated := health["terminated"]
_, starting := health["pending"]
_, stopping := health["stopping"]
_, shutting := health["shutting-down"]
_, running := health["running"]
_, stopped := health["stopped"]
switch {
case starting && (stopping || shutting):
log.Print("Unhealthy: Mix of starting and stopping resources")
return UNHEALTHY
case running && stopped:
log.Print("Unhealthy: Mix of running and stopped resources")
return UNHEALTHY
case terminated:
log.Print("Instance terminated, manual intervention required")
return UNHEALTHY
case starting:
return STARTING
case stopping, shutting:
return STOPPING
case running:
return STARTED
case stopped:
return STOPPED
default:
log.Printf("Unhealthy: %v", health)
return UNHEALTHY
}
}
func (fw *Flywheel) checkInstances(health map[string]int) error {
if len(fw.config.Instances) == 0 {
return nil
}
resp, err := fw.ec2.DescribeInstances(
&ec2.DescribeInstancesInput{
InstanceIds: fw.config.AwsInstances(),
},
)
if err != nil {
log.Print(err)
return err
}
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
state := *instance.State.Name
health[state] = health[state] + 1
}
}
return nil
}
func (fw *Flywheel) checkStoppedAutoScalingGroups(health map[string]int) error {
var err error
var awsGroupNames []*string
if len(fw.config.AutoScaling.Stop) == 0 {
return nil
}
for _, groupName := range fw.config.AutoScaling.Stop {
awsGroupNames = append(awsGroupNames, &groupName)
}
resp, err := fw.autoscaling.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: awsGroupNames,
},
)
if err != nil {
return err
}
for _, group := range resp.AutoScalingGroups {
running := true
instanceIds := []*string{}
for _, instance := range group.Instances {
instanceIds = append(instanceIds, instance.InstanceId)
}
iResp, err := fw.ec2.DescribeInstances(
&ec2.DescribeInstancesInput{
InstanceIds: instanceIds,
},
)
if err != nil {
return err
}
for _, reservation := range iResp.Reservations {
for _, instance := range reservation.Instances {
state := *instance.State.Name
health[state] = health[state] + 1
running = running && *instance.State.Name == "running"
}
}
// if all instances are running and ASG is suspended
// resume the group
if running && len(group.SuspendedProcesses) > 0 {
for _, instance := range group.Instances {
fw.autoscaling.SetInstanceHealth(
&autoscaling.SetInstanceHealthInput{
InstanceId: instance.InstanceId,
HealthStatus: aws.String("Healthy"),
},
)
}
_, err = fw.autoscaling.ResumeProcesses(
&autoscaling.ScalingProcessQuery{
AutoScalingGroupName: group.AutoScalingGroupName,
},
)
if err != nil {
return err
}
}
}
return nil
}
// NOT USED ?
func (fw *Flywheel) checkTerminatedAutoScalingGroups(health map[string]int) error {
var err error
var awsGroupNames []*string
for groupName := range fw.config.AutoScaling.Terminate {
awsGroupNames = append(awsGroupNames, &groupName)
}
resp, err := fw.autoscaling.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: awsGroupNames,
},
)
if err != nil {
log.Print(err)
return err
}
for _, group := range resp.AutoScalingGroups {
if *group.MaxSize == 0 {
if len(group.Instances) == 0 {
health["stopped"]++
} else {
health["stopping"]++
}
continue
}
healthy := true
for _, instance := range group.Instances {
if *instance.HealthStatus != "Healthy" {
healthy = false
break
}
}
if healthy {
health["running"]++
if len(group.SuspendedProcesses) > 0 {
_, err = fw.autoscaling.ResumeProcesses(
&autoscaling.ScalingProcessQuery{
AutoScalingGroupName: group.AutoScalingGroupName,
},
)
if err != nil {
return err
}
}
} else {
health["starting"]++
}
}
return nil
}