-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstatus.go
386 lines (317 loc) · 10.8 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
/*
* VC5 load balancer. Copyright (C) 2021-present David Coles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package vc5
import (
"fmt"
"net/netip"
"sort"
"time"
"github.com/davidcoles/cue"
)
type servicemap map[netip.Addr][]serv
type serv struct {
Name string `json:"name,omitempty"`
Description string `json:"description"`
Address netip.Addr `json:"address"`
Port uint16 `json:"port"`
Protocol protocol `json:"protocol"`
Required uint8 `json:"required"`
Available uint8 `json:"available"`
Stats Stats `json:"stats"`
Destinations []dest `json:"destinations,omitempty"`
Up bool `json:"up"`
For uint64 `json:"for"`
Last uint64 `json:"last"`
Sticky bool `json:"sticky"`
Scheduler string `json:"scheduler"`
}
type dest struct {
Address netip.Addr `json:"address"`
Port uint16 `json:"port"`
Stats Stats `json:"stats"`
Weight uint8 `json:"weight"`
Disabled bool `json:"disabled"`
Up bool `json:"up"`
For uint64 `json:"for"`
Took uint64 `json:"took"`
When uint64 `json:"when"`
Last uint64 `json:"last"`
Diagnostic string `json:"diagnostic"`
MAC string `json:"mac"`
}
type state struct {
up bool
time time.Time
}
func (s *state) Up() bool { return s.up }
func (s *state) Time() time.Time { return s.time }
type Stats struct {
IngressOctets uint64 `json:"ingress_octets"`
IngressPackets uint64 `json:"ingress_packets"`
EgressOctets uint64 `json:"egress_octets"`
EgressPackets uint64 `json:"egress_packets"`
Flows uint64 `json:"flows"`
Current uint64 `json:"current"`
IngressOctetsPerSecond uint64 `json:"ingress_octets_per_second"`
IngressPacketsPerSecond uint64 `json:"ingress_packets_per_second"`
EgressOctetsPerSecond uint64 `json:"egress_octets_per_second"`
EgressPacketsPerSecond uint64 `json:"egress_packets_per_second"`
FlowsPerSecond uint64 `json:"flows_per_second"`
time time.Time
MAC string `json:"mac,omitempty"`
}
type Summary struct {
Uptime uint64 `json:"uptime"`
Latency uint64 `json:"latency_ns"`
Current uint64 `json:"current"`
Dropped uint64 `json:"dropped"`
DroppedPerSecond uint64 `json:"dropped_per_second"`
Blocked uint64 `json:"blocked"`
BlockedPerSecond uint64 `json:"blocked_per_second"`
NotQueued uint64 `json:"notqueued"`
NotQueuedPerSecond uint64 `json:"notqueued_per_second"`
TooBig uint64 `json:"toobig"`
TooBigPerSecond uint64 `json:"toobig_per_second"`
IngressOctets uint64 `json:"ingress_octets"`
IngressOctetsPerSecond uint64 `json:"ingress_octets_per_second"`
IngressPackets uint64 `json:"ingress_packets"`
IngressPacketsPerSecond uint64 `json:"ingress_packets_per_second"`
Flows uint64 `json:"flows"`
FlowsPerSecond uint64 `json:"flows_per_second"`
EgressOctets uint64 `json:"egress_octets"`
EgressOctetsPerSecond uint64 `json:"egress_octets_per_second"`
EgressPackets uint64 `json:"egress_packets"`
EgressPacketsPerSecond uint64 `json:"egress_packets_per_second"`
DSR bool `json:"dsr"`
VC5 bool `json:"vc5"`
time time.Time
}
func (s *Stats) add(x Stats) {
s.IngressOctets += x.IngressOctets
s.IngressPackets += x.IngressPackets
s.Flows += x.Flows
s.Current += x.Current
s.IngressOctetsPerSecond += x.IngressOctetsPerSecond
s.IngressPacketsPerSecond += x.IngressPacketsPerSecond
s.FlowsPerSecond += x.FlowsPerSecond
s.EgressOctets += x.EgressOctets
s.EgressPackets += x.EgressPackets
s.EgressOctetsPerSecond += x.EgressOctetsPerSecond
s.EgressPacketsPerSecond += x.EgressPacketsPerSecond
}
type vipstats struct {
VIP netip.Addr `json:"vip"`
Up bool `json:"up"`
Stats Stats `json:"stats"`
For uint64 `json:"for"`
}
func vipStatus(in servicemap, state map[netip.Addr]state) (out []vipstats) {
for vip, list := range in {
var stats Stats
for _, s := range list {
stats.add(s.Stats)
}
r, ok := state[vip]
if !ok {
r.time = time.Now()
}
out = append(out, vipstats{VIP: vip, Stats: stats, Up: r.up, For: uint64(time.Now().Sub(r.time) / time.Second)})
}
sort.SliceStable(out, func(i, j int) bool {
return out[i].VIP.Compare(out[j].VIP) < 0
})
return
}
func vipState(services []cue.Service, old map[netip.Addr]state, priorities map[netip.Addr]priority, logs Logger, mature bool) map[netip.Addr]state {
F := "vip"
rib := map[netip.Addr]bool{}
new := map[netip.Addr]state{}
for _, v := range cue.HealthyVIPs(services) {
rib[v] = true
}
for _, v := range cue.AllVIPs(services) {
up, _ := rib[v]
severity := priorityToSeverity(priorities[v])
if o, ok := old[v]; ok {
if o.up != up {
new[v] = state{up: up, time: time.Now()}
text := fmt.Sprintf("VIP %s went %s", v, upDown(up))
if mature {
logs.Alert(severity, F, "state", KV{"service.ip": v, "service.state": upDown(up)}, text)
}
} else {
new[v] = o
}
} else {
new[v] = state{up: rib[v], time: time.Now()}
if mature {
logs.Event(DEBUG, F, "added", KV{"service.ip": v, "service.state": upDown(up)})
}
}
if mature {
logs.State(F, "state", KV{"service.ip": v, "service.state": upDown(up)})
}
}
for vip, _ := range old {
if _, exists := new[vip]; !exists {
if mature {
logs.Event(DEBUG, F, "removed", KV{"service.ip": vip})
}
}
}
return new
}
func upDown(b bool) string {
if b {
return "up"
}
return "down"
}
func priorityToSeverity(p priority) uint8 {
switch p {
case CRITICAL:
return ERR
case HIGH:
return WARNING
case MEDIUM:
return NOTICE
case LOW:
return INFO
}
return ERR
}
func (s *Summary) Update(n Summary, start time.Time) {
o := *s
*s = n
s.Uptime = uint64(time.Now().Sub(start) / time.Second)
s.time = time.Now()
if o.time.Unix() != 0 {
diff := uint64(s.time.Sub(o.time) / time.Millisecond)
if diff != 0 {
s.DroppedPerSecond = (1000 * (s.Dropped - o.Dropped)) / diff
s.BlockedPerSecond = (1000 * (s.Blocked - o.Blocked)) / diff
s.NotQueuedPerSecond = (1000 * (s.NotQueued - o.NotQueued)) / diff
s.TooBigPerSecond = (1000 * (s.TooBig - o.TooBig)) / diff
s.IngressPacketsPerSecond = (1000 * (s.IngressPackets - o.IngressPackets)) / diff
s.IngressOctetsPerSecond = (1000 * (s.IngressOctets - o.IngressOctets)) / diff
s.EgressPacketsPerSecond = (1000 * (s.EgressPackets - o.EgressPackets)) / diff
s.EgressOctetsPerSecond = (1000 * (s.EgressOctets - o.EgressOctets)) / diff
s.FlowsPerSecond = (1000 * (s.Flows - o.Flows)) / diff
}
}
}
type Instance struct {
Service Service
Destination Destination
}
type Manifest cue.Service // so we don't have to explicitly pull cue into the balancer - not keen on the name though
func (s Manifest) Service() Service { return s.Instance().Service }
func (s Manifest) Instance() Instance { return instance(cue.Service(s), cue.Destination{}) }
type Balancer interface {
Stats() (Summary, map[Instance]Stats)
//Summary() Summary
Configure([]Manifest) error
}
func calculateRate(s Stats, o Stats) Stats {
s.time = time.Now()
if o.time.Unix() != 0 {
diff := uint64(s.time.Sub(o.time) / time.Millisecond)
if diff != 0 {
s.EgressPacketsPerSecond = (1000 * (s.EgressPackets - o.EgressPackets)) / diff
s.EgressOctetsPerSecond = (1000 * (s.EgressOctets - o.EgressOctets)) / diff
s.IngressPacketsPerSecond = (1000 * (s.IngressPackets - o.IngressPackets)) / diff
s.IngressOctetsPerSecond = (1000 * (s.IngressOctets - o.IngressOctets)) / diff
s.FlowsPerSecond = (1000 * (s.Flows - o.Flows)) / diff
}
}
return s
}
func serviceStatus(config *Config, balancer Balancer, director *cue.Director, old map[Instance]Stats) (Summary, servicemap, map[Instance]Stats) {
var current uint64
stats := map[Instance]Stats{}
status := map[netip.Addr][]serv{}
summary, allstats := balancer.Stats()
//summary := balancer.Summary()
for _, svc := range director.Status() {
cnf, _ := config.Services[Manifest(svc).Service()]
//key := serviceInstance(svc)
key := Manifest(svc).Instance()
lbs := map[Destination]Stats{}
for k, v := range allstats {
if k.Service == key.Service {
lbs[k.Destination] = v
}
}
var sum Stats
for _, s := range lbs {
sum.add(s)
}
serv := serv{
Name: cnf.Name,
Description: cnf.Description,
Address: svc.Address,
Port: svc.Port,
Protocol: protocol(svc.Protocol),
Required: svc.Required,
Available: svc.Available(),
Up: svc.Up,
For: uint64(time.Now().Sub(svc.When) / time.Second),
Sticky: svc.Sticky,
Scheduler: svc.Scheduler,
Stats: calculateRate(sum, old[key]),
}
for _, dst := range svc.Destinations {
foo := lbs[destination(dst)]
key := instance(svc, dst)
dest := dest{
Address: dst.Address,
Port: dst.Port,
Disabled: dst.Disabled,
Up: dst.Status.OK,
For: uint64(time.Now().Sub(dst.Status.When) / time.Second),
Took: uint64(dst.Status.Took / time.Millisecond),
Diagnostic: dst.Status.Diagnostic,
Weight: dst.Weight,
Stats: calculateRate(lbs[destination(dst)], old[key]),
MAC: lbs[destination(dst)].MAC,
}
current += foo.Current
stats[key] = dest.Stats
serv.Destinations = append(serv.Destinations, dest)
}
stats[key] = serv.Stats
sort.SliceStable(serv.Destinations, func(i, j int) bool {
return serv.Destinations[i].Address.Compare(serv.Destinations[j].Address) < 0
})
status[svc.Address] = append(status[svc.Address], serv)
}
summary.Current = current
return summary, status, stats
}
func destination(d cue.Destination) Destination { return Destination{Address: d.Address, Port: d.Port} }
func instance(s cue.Service, d cue.Destination) (i Instance) {
i.Service = Service{Address: s.Address, Port: s.Port, Protocol: Protocol(s.Protocol)}
i.Destination = Destination{Address: d.Address, Port: d.Port}
return
}
//func manifests(c []cue.Service) (m []Manifest) {
// for _, s := range x {
// m = append(m, s)
// }
// return
//}