forked from cloudnautique/cowcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (148 loc) · 3.61 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
package main
import (
"github.com/Sirupsen/logrus"
"github.com/davecgh/go-spew/spew"
"github.com/miekg/dns"
"net/http"
"time"
)
var VERSION = "v0.0.0-dev"
var checkSlice = []CheckInterface{}
// Generics
// CheckInterface is a interface for Checks
type CheckInterface interface {
eval() bool
fail() bool
getStatus() bool
getName() string
}
type Check struct {
name string
description string
lastEval time.Time
lastFail time.Time
currentStatus bool
}
func (c *Check) eval() bool {
return true
}
func (c *Check) fail() bool {
logrus.Errorf("Check %s has failed", c.name)
c.lastFail = time.Now()
c.currentStatus = false
return true
}
func (c *Check) getStatus() bool {
return c.currentStatus
}
func (c *Check) getName() string {
return c.name
}
// Implemented checks
//CheckDNS is a check for the Kubernetes API
type CheckDNS struct {
Check
}
func NewCheckDNS() *CheckDNS {
return &CheckDNS{
Check{
name: "CheckDNS",
description: "A check for the DNS Service",
currentStatus: true,
},
}
}
func (c *CheckDNS) eval() bool {
logrus.Infof("Evaluating check %s", c.name)
logrus.WithFields(logrus.Fields{"before_eval": "true"}).Debug(spew.Sdump(c))
c.lastEval = time.Now()
// borrowing from https://godoc.org/github.com/miekg/dns#example-MX
config, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
dnsClient := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion("rancher-metadata.rancher.internal.", dns.TypeA)
m.RecursionDesired = true
r, _, err := dnsClient.Exchange(m, config.Servers[0]+":"+config.Port)
if err != nil {
logrus.Error(err)
c.fail()
return true
}
if r.Rcode != dns.RcodeSuccess {
logrus.Error(err)
c.fail()
return true
}
logrus.WithFields(logrus.Fields{"before_eval": "false"}).Debug(spew.Sdump(c))
c.currentStatus = true
return true
}
// CheckMetadata is a check for the Metadata Service
type CheckMetadata struct {
Check
}
func NewCheckMetadata() *CheckMetadata {
return &CheckMetadata{
Check{
name: "CheckMetadata",
description: "A check for the CheckMetadata Service",
currentStatus: true,
},
}
}
func (c *CheckMetadata) eval() bool {
logrus.Infof("Evaluating check %s", c.name)
logrus.WithFields(logrus.Fields{"before_eval": "true"}).Debug(spew.Sdump(c))
c.lastEval = time.Now()
httpClient := http.Client{Timeout: time.Duration(2 * time.Second)}
resp, err := httpClient.Get("http://169.254.169.250")
if err != nil {
logrus.Error("Fail")
c.fail()
return true
}
defer resp.Body.Close()
logrus.WithFields(logrus.Fields{"before_eval": "false"}).Debug(spew.Sdump(c))
return true
}
// HTTP Server
func checkState(w http.ResponseWriter, r *http.Request) {
health := true
for _, check := range checkSlice {
logrus.Debugf("checkState - Reading state of check %s", check.getName())
if check.getStatus() == false {
health = false
}
}
if health {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Everything OK"))
} else {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Failed"))
}
}
func evalChecks(checks []CheckInterface) {
for _, check := range checks {
check.eval()
}
}
func checkPoller(checks []CheckInterface) {
evalChecks(checks) // call once for instant first tick
t := time.NewTicker(2 * time.Second)
for _ = range t.C {
evalChecks(checks)
}
}
func main() {
logrus.SetLevel(logrus.WarnLevel)
logrus.Warn("Starting cowcheck...")
checkSlice = append(checkSlice, NewCheckDNS(), NewCheckMetadata())
go checkPoller(checkSlice)
http.HandleFunc("/", checkState)
http.HandleFunc("/health", checkState)
err := http.ListenAndServe(":5050", nil)
if err != nil {
logrus.Error(err)
}
}