-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
75 lines (71 loc) · 1.43 KB
/
Copy pathapp.go
File metadata and controls
75 lines (71 loc) · 1.43 KB
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
package main
import (
"sort"
"time"
)
type appRate struct {
Name string
D, U float64
}
func appDeltas(prev, cur map[string][2]int64, interval float64) (map[string][2]int64, []appRate) {
ab := map[string][2]int64{}
var rates []appRate
for name, cv := range cur {
pv, ok := prev[name]
if !ok {
pv = cv
}
db := cv[0] - pv[0]
if db < 0 {
db = 0
}
ub := cv[1] - pv[1]
if ub < 0 {
ub = 0
}
if db+ub > 0 {
ab[name] = [2]int64{db, ub}
rates = append(rates, appRate{name, float64(db) / interval, float64(ub) / interval})
}
}
sort.Slice(rates, func(i, j int) bool { return rates[i].D+rates[i].U > rates[j].D+rates[j].U })
return ab, rates
}
func runLogger(iface string, interval float64) {
iface = pickIface(iface)
pin, pout, ok := ifaceBytes(iface)
if !ok {
return
}
pproc := procBytes()
log := NewUsageLog()
cfg := LoadConfig()
det := NewDetector(cfg.SpikeFloorKBps, cfg.SpikeMult)
for {
time.Sleep(time.Duration(interval * float64(time.Second)))
cin, cout, ok := ifaceBytes(iface)
if !ok {
continue
}
din := cin - pin
if din < 0 {
din = 0
}
dout := cout - pout
if dout < 0 {
dout = 0
}
pin, pout = cin, cout
log.Add(din, dout)
cproc := procBytes()
ab, rates := appDeltas(pproc, cproc, interval)
pproc = cproc
log.AddApps(ab)
now := time.Now().Unix()
for _, r := range rates {
if det.Update(r.Name, r.D+r.U, now) {
log.LogSpike(r.Name, r.D, r.U)
}
}
}
}