-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
240 lines (197 loc) · 6.13 KB
/
main.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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strconv"
"sync"
"syscall"
"time"
"github.com/Luzifer/rconfig"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
)
var (
cfg = struct {
CheckDefinitionsFile string `flag:"check-definitions-file,c" default:"/etc/elb-instance-status.yml" description:"File or URL containing checks to perform for instance health"`
UnhealthyThreshold int64 `flag:"unhealthy-threshold" default:"5" description:"How often does a check have to fail to mark the machine unhealthy"`
CheckInterval time.Duration `flag:"check-interval" default:"1m" description:"How often to execute checks (do not set below 10s!)"`
ConfigRefreshInterval time.Duration `flag:"config-refresh" default:"10m" description:"How often to update checks from definitions file / url"`
Verbose bool `flag:"verbose,v" default:"false" description:"Attach stdout of the executed commands"`
Listen string `flag:"listen" default:":3000" description:"IP/Port to listen on for ELB health checks"`
VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"`
}{}
version = "dev"
checks map[string]checkCommand
checkResults = map[string]*checkResult{}
checkResultsLock sync.RWMutex
lastResultRegistered time.Time
)
type checkCommand struct {
Name string `yaml:"name"`
Command string `yaml:"command"`
WarnOnly bool `yaml:"warn-only"`
}
type checkResult struct {
Check checkCommand
IsSuccess bool
Streak int64
}
func init() {
rconfig.Parse(&cfg)
if cfg.VersionAndExit {
fmt.Printf("elb-instance-status %s\n", version)
os.Exit(0)
}
}
func loadChecks() error {
var rawChecks []byte
if _, err := os.Stat(cfg.CheckDefinitionsFile); err == nil {
// We got a local file, read it
rawChecks, err = ioutil.ReadFile(cfg.CheckDefinitionsFile)
if err != nil {
return err
}
} else {
// Check whether we got an URL
if _, err := url.Parse(cfg.CheckDefinitionsFile); err != nil {
return errors.New("Definitions file is neither a local file nor a URL")
}
// We got an URL, fetch and read it
resp, err := http.Get(cfg.CheckDefinitionsFile)
if err != nil {
return err
}
defer resp.Body.Close()
rawChecks, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
}
tmpResult := map[string]checkCommand{}
err := yaml.Unmarshal(rawChecks, &tmpResult)
if err == nil {
checks = tmpResult
}
return err
}
func main() {
if err := loadChecks(); err != nil {
log.Fatalf("Unable to read definitions file: %s", err)
}
c := cron.New()
c.AddFunc("@every "+cfg.CheckInterval.String(), spawnChecks)
c.AddFunc("@every "+cfg.ConfigRefreshInterval.String(), func() {
if err := loadChecks(); err != nil {
log.Printf("Unable to refresh checks: %s", err)
}
})
c.Start()
spawnChecks()
r := mux.NewRouter()
r.HandleFunc("/status", handleELBHealthCheck)
r.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(cfg.Listen, r)
}
func spawnChecks() {
ctx, _ := context.WithTimeout(context.Background(), cfg.CheckInterval-time.Second)
for id := range checks {
go executeAndRegisterCheck(ctx, id)
}
}
func executeAndRegisterCheck(ctx context.Context, checkID string) {
check := checks[checkID]
start := time.Now()
cmd := exec.Command("/bin/bash", "-e", "-o", "pipefail", "-c", check.Command)
// Enable process groups in to order to be able to kill a whole group
// instead of a single process
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Stderr = newPrefixedLogger(os.Stderr, checkID+":STDERR")
if cfg.Verbose {
cmd.Stdout = newPrefixedLogger(os.Stderr, checkID+":STDOUT")
}
err := cmd.Start()
if err == nil {
cmdDone := make(chan error)
go func(cmdDone chan error, cmd *exec.Cmd) { cmdDone <- cmd.Wait() }(cmdDone, cmd)
loop := true
for loop {
select {
case err = <-cmdDone:
loop = false
case <-ctx.Done():
log.Printf("Execution of check '%s' was killed through context timeout.", checkID)
// Kill the process group to make sure that all child processes are killed too
// and to avoid deadlocks.
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
time.Sleep(100 * time.Millisecond)
}
}
}
success := err == nil
checkResultsLock.Lock()
if _, ok := checkResults[checkID]; !ok {
checkResults[checkID] = &checkResult{
Check: check,
}
}
if success == checkResults[checkID].IsSuccess {
checkResults[checkID].Streak++
} else {
checkResults[checkID].IsSuccess = success
checkResults[checkID].Streak = 1
}
if !success {
log.Printf("Check %q failed, streak now at %d, error was: %s", checkID, checkResults[checkID].Streak, err)
}
lastResultRegistered = time.Now()
if success {
checkPassing.WithLabelValues(checkID).Set(1)
} else {
checkPassing.WithLabelValues(checkID).Set(0)
}
checkExecutionTime.WithLabelValues(checkID).Observe(float64(time.Since(start).Nanoseconds()) / float64(time.Microsecond))
checkResultsLock.Unlock()
}
func handleELBHealthCheck(res http.ResponseWriter, r *http.Request) {
healthy := true
start := time.Now()
buf := bytes.NewBuffer([]byte{})
checkResultsLock.RLock()
for _, cr := range checkResults {
state := ""
switch {
case cr.IsSuccess:
state = "PASS"
case !cr.IsSuccess && cr.Check.WarnOnly:
state = "WARN"
case !cr.IsSuccess && !cr.Check.WarnOnly && cr.Streak < cfg.UnhealthyThreshold:
state = "CRIT"
case !cr.IsSuccess && !cr.Check.WarnOnly && cr.Streak >= cfg.UnhealthyThreshold:
state = "CRIT"
healthy = false
}
fmt.Fprintf(buf, "[%s] %s\n", state, cr.Check.Name)
}
checkResultsLock.RUnlock()
res.Header().Set("X-Collection-Parsed-In", strconv.FormatInt(time.Since(start).Nanoseconds()/int64(time.Microsecond), 10)+"ms")
res.Header().Set("X-Last-Result-Registered-At", lastResultRegistered.Format(time.RFC1123))
if healthy {
currentStatusCode.Set(http.StatusOK)
res.WriteHeader(http.StatusOK)
} else {
currentStatusCode.Set(http.StatusInternalServerError)
res.WriteHeader(http.StatusInternalServerError)
}
io.Copy(res, buf)
}