-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.go
431 lines (395 loc) · 10.6 KB
/
status.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package status
import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"fmt"
"html/template"
"log"
"math"
"net"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/dustin/go-humanize"
docker "github.com/fsouza/go-dockerclient"
"github.com/oxtoacart/bpool"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
)
const (
exprTempStr = "coretemp_core_[0-9]+"
exprHostStr = "Host\\w*(?::|\\(\\`)([0-9a-z\\.-]+)"
// host prefix & suffix. slightly stupid but needs to support:
// traefik v1 `traefik.frontend.rule`
// traefik v1 `traefik.<name>.frontend.rule`
// traefik v2 `traefik.http.routers.<name>.rule`
labelHostPrefix = "traefik."
labelHostSuffix = ".rule"
labelGroup = "xyz.senan.compose-status.group"
labelCheckMethod = "xyz.senan.compose-status.check.method"
labelCheckPort = "xyz.senan.compose-status.check.port"
labelCheckPath = "xyz.senan.compose-status.check.path"
labelCheckExpCode = "xyz.senan.compose-status.check.code"
labelProject = "com.docker.compose.project"
)
//go:embed tmpl.html
var homeTmpl string
//go:embed chart.js
var chartJS []byte
var (
exprTemp = regexp.MustCompile(exprTempStr)
exprHost = regexp.MustCompile(exprHostStr)
)
var funcMap = template.FuncMap{
"humanDate": humanize.Time,
"humanBytes": humanize.IBytes,
"humanDuration": func(d time.Duration) string {
switch {
case d.Milliseconds() < 1000:
return fmt.Sprintf("%.2f ms", float64(d)/float64(time.Millisecond))
case d.Seconds() < 60:
return fmt.Sprintf("%.0f seconds", d.Seconds())
case d.Minutes() < 60:
return fmt.Sprintf("%.0f minutes", d.Minutes())
case d.Hours() < 24:
return fmt.Sprintf("%.0f hours", d.Hours())
default:
return fmt.Sprintf("%.0f days", d.Hours()/24)
}
},
"js": func(v interface{}) template.JS {
out, _ := json.Marshal(v)
return template.JS(out)
},
}
type Container struct {
Name string
Status string
Link string
HTTP HTTPCheck
}
type Stats struct {
Load1 float64
Load5 float64
Load15 float64
MemUsed uint64
MemTotal uint64
CPU float64
CPUTemp float64
Uptime time.Duration
}
type hist []float64
func (h *hist) add(n float64) {
*h = append(*h, n)
*h = (*h)[1:len(*h)]
}
type Controller struct {
tmpl *template.Template
dockerNetworkName string
dockerClient *docker.Client
httpClient *http.Client
buffPool *bpool.BufferPool
scanInterval time.Duration
pageTitle string
showCredit bool
lastGroups map[string][]string
lastProjects map[string][]Container
lastStats Stats
histCPU hist
histTemp hist
*http.ServeMux
}
type ControllerOpt func(*Controller) error
func WithTitle(title string) ControllerOpt {
return func(c *Controller) error {
c.pageTitle = title
return nil
}
}
func WithScanInternal(dur time.Duration) ControllerOpt {
return func(c *Controller) error {
c.scanInterval = dur
return nil
}
}
func WithHistWindow(dur time.Duration) ControllerOpt {
return func(c *Controller) error {
c.histCPU = hist(make([]float64, dur/c.scanInterval))
c.histTemp = hist(make([]float64, dur/c.scanInterval))
return nil
}
}
func WithCredit(c *Controller) error {
c.showCredit = true
return nil
}
func NewController(dockerNetworkName string, options ...ControllerOpt) (*Controller, error) {
dockerClient, err := docker.NewClientFromEnv()
if err != nil {
return nil, fmt.Errorf("creating docker client: %w", err)
}
tmpl, err := template.
New("").
Funcs(funcMap).
Parse(homeTmpl)
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
cont := &Controller{
tmpl: tmpl,
dockerClient: dockerClient,
dockerNetworkName: dockerNetworkName,
httpClient: &http.Client{
Timeout: 25 * time.Millisecond,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
},
buffPool: bpool.NewBufferPool(64),
lastStats: Stats{},
ServeMux: http.NewServeMux(),
}
for _, option := range options {
if err := option(cont); err != nil {
return nil, fmt.Errorf("running option: %w", err)
}
}
cont.ServeMux.HandleFunc("/chart.js", cont.serveChartJS)
cont.ServeMux.HandleFunc("/", cont.serveHome)
return cont, nil
}
func parseLabelHost(label string) string {
match := exprHost.FindStringSubmatch(label)
if len(match) < 2 {
return ""
}
return match[1]
}
func parseLabelsLink(labels map[string]string) string {
for k, v := range labels {
prefix := strings.HasPrefix(k, labelHostPrefix)
suffix := strings.HasSuffix(k, labelHostSuffix)
if prefix && suffix {
return parseLabelHost(v)
}
}
return ""
}
func parseStatus(status string) string {
status = strings.ReplaceAll(status, "(healthy)", "")
status = strings.TrimSpace(status)
status = strings.ToLower(status)
return status
}
type HTTPCheck struct {
OK bool
Code int
Duration time.Duration
Timeout bool
}
func checkHTTP(httpClient *http.Client, dockerNetworkID string, dockerContainer docker.APIContainers) (*HTTPCheck, error) {
portRaw, ok := dockerContainer.Labels[labelCheckPort]
if !ok {
return nil, nil
}
port, _ := strconv.Atoi(portRaw)
var method string = http.MethodHead
if m, ok := dockerContainer.Labels[labelCheckMethod]; ok {
method = m
}
var path string = "/"
if p, ok := dockerContainer.Labels[labelCheckPath]; ok {
path = p
}
var expCode int = 200
if c, ok := dockerContainer.Labels[labelCheckExpCode]; ok {
expCode, _ = strconv.Atoi(c)
}
var ip string
for _, v := range dockerContainer.Networks.Networks {
if v.NetworkID != dockerNetworkID {
continue
}
ip = v.IPAddress
break
}
if ip == "" {
return nil, nil
}
url := fmt.Sprintf("http://%s:%d/%s", ip, port, strings.TrimPrefix(path, "/"))
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
start := time.Now()
res, err := httpClient.Do(req)
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
return &HTTPCheck{Timeout: true}, nil
}
if err != nil {
return nil, fmt.Errorf("make request: %w", err)
}
check := &HTTPCheck{
Code: res.StatusCode,
Duration: time.Since(start),
}
if (res.StatusCode >= 200 && res.StatusCode < 300) || res.StatusCode == expCode {
check.OK = true
}
return check, err
}
func averageTemp(cores []host.TemperatureStat) float64 {
var numCores int
var temp float64
for _, t := range cores {
if match := exprTemp.MatchString(t.SensorKey); match {
numCores++
temp += t.Temperature
}
}
if numCores == 0 {
return 0
}
return temp / float64(numCores)
}
func (c *Controller) Refresh() error {
dockerNetworks, err := c.dockerClient.ListNetworks()
if err != nil {
return fmt.Errorf("list docker networks: %w", err)
}
var dockerNetworkID string
for _, dn := range dockerNetworks {
if dn.Name == c.dockerNetworkName {
dockerNetworkID = dn.ID
break
}
}
if dockerNetworkID == "" {
return fmt.Errorf("can't find docker network %q", c.dockerNetworkName)
}
dockerContainers, err := c.dockerClient.ListContainers(
docker.ListContainersOptions{},
)
if err != nil {
return fmt.Errorf("listing containers: %w", err)
}
c.lastGroups = map[string][]string{}
c.lastProjects = map[string][]Container{}
groupedProjects := map[string]struct{}{}
// insert the current time for any container we see
for _, dockerContainer := range dockerContainers {
if len(dockerContainer.Names) == 0 {
return fmt.Errorf("%q does not have a name", dockerContainer.ID)
}
project, ok := dockerContainer.Labels[labelProject]
if !ok {
continue
}
if group, ok := dockerContainer.Labels[labelGroup]; ok {
c.lastGroups[group] = append(c.lastGroups[group], project)
groupedProjects[project] = struct{}{}
}
container := Container{
Name: dockerContainer.Names[0],
Status: parseStatus(dockerContainer.Status),
Link: parseLabelsLink(dockerContainer.Labels),
}
check, err := checkHTTP(c.httpClient, dockerNetworkID, dockerContainer)
if err != nil {
log.Printf("error getting http check for %q: %v\n", container.Name, err)
}
if check != nil {
container.HTTP = *check
}
c.lastProjects[project] = append(c.lastProjects[project], container)
}
for project := range c.lastProjects {
if _, ok := groupedProjects[project]; !ok {
// put the ungrouped projects into the "~" pseudo group
c.lastGroups["~"] = append(c.lastGroups["~"], project)
}
}
// not checking errors here becuase some of these return lists of
// warnings which i don't care about at the moment
if uptime, _ := host.Uptime(); uptime != 0 {
c.lastStats.Uptime = time.Duration(uptime * 1e+9)
}
if load, _ := load.Avg(); load != nil {
c.lastStats.Load1 = load.Load1
c.lastStats.Load5 = load.Load5
c.lastStats.Load15 = load.Load15
}
if memory, _ := mem.VirtualMemory(); memory != nil {
c.lastStats.MemUsed = memory.Used
c.lastStats.MemTotal = memory.Total
}
if cpus, _ := cpu.Percent(0, false); len(cpus) > 0 {
round := math.Round(cpus[0]*100) / 100
c.lastStats.CPU = round
c.histCPU.add(round)
}
if temps, _ := host.SensorsTemperatures(); len(temps) > 0 {
avg := averageTemp(temps)
c.lastStats.CPUTemp = avg
c.histTemp.add(avg)
}
return nil
}
func (c *Controller) Start() {
if err := c.Refresh(); err != nil {
log.Printf("error refreshing: %v\n", err)
}
ticker := time.NewTicker(c.scanInterval)
for range ticker.C {
if err := c.Refresh(); err != nil {
log.Printf("error refreshing: %v\n", err)
}
}
}
func (c *Controller) serveHome(w http.ResponseWriter, r *http.Request) {
tmplData := struct {
PageTitle string
ShowCredit bool
Groups map[string][]string
Projects map[string][]Container
Stats Stats
HistDataCPU []float64
HistDataTemp []float64
HistPeriod time.Duration
}{
c.pageTitle,
c.showCredit,
c.lastGroups,
c.lastProjects,
c.lastStats,
c.histCPU,
c.histTemp,
c.scanInterval,
}
for _, projects := range tmplData.Groups {
sort.Strings(projects)
}
// using a pool of buffers, we can write to one first to catch template
// errors, which avoids a superfluous write to the response writer
buff := c.buffPool.Get()
defer c.buffPool.Put(buff)
if err := c.tmpl.Execute(buff, tmplData); err != nil {
http.Error(w, fmt.Sprintf("error executing template: %v", err), 500)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if _, err := buff.WriteTo(w); err != nil {
log.Printf("error writing response buffer: %v\n", err)
}
}
func (c *Controller) serveChartJS(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "chart.js", time.Unix(0, 0), bytes.NewReader(chartJS))
}