-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.go
More file actions
211 lines (193 loc) · 4.36 KB
/
Copy pathstats.go
File metadata and controls
211 lines (193 loc) · 4.36 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
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
package main
import (
"bufio"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/labstack/echo/v4"
)
// A sample is one instant of machine load. cpu/mem are percentages; rx/tx are
// bytes per second across all real interfaces.
type sample struct {
T int64 `json:"t"`
CPU float64 `json:"cpu"`
Mem float64 `json:"mem"`
Rx float64 `json:"rx"`
Tx float64 `json:"tx"`
}
const (
statsInterval = 5 * time.Second
statsSlots = 60 // 5s × 60 = last 5 minutes
)
// statsRing is a fixed-size circular buffer of the last statsSlots samples — the
// ring keeps memory flat while always holding the most recent window.
type statsRing struct {
mu sync.Mutex
buf [statsSlots]sample
head int // next write position
n int // filled slots (<= statsSlots)
}
func newStatsRing() *statsRing { return &statsRing{} }
func (r *statsRing) add(s sample) {
r.mu.Lock()
r.buf[r.head] = s
r.head = (r.head + 1) % statsSlots
if r.n < statsSlots {
r.n++
}
r.mu.Unlock()
}
// snapshot returns the samples oldest-first.
func (r *statsRing) snapshot() []sample {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]sample, 0, r.n)
start := (r.head - r.n + statsSlots) % statsSlots
for i := 0; i < r.n; i++ {
out = append(out, r.buf[(start+i)%statsSlots])
}
return out
}
// collect samples machine load every statsInterval for the life of the daemon.
// On platforms without /proc it simply records nothing.
func (r *statsRing) collect() {
prevTotal, prevIdle := readCPU()
prevRx, prevTx := readNet()
last := time.Now()
for range time.Tick(statsInterval) {
now := time.Now()
secs := now.Sub(last).Seconds()
last = now
total, idle := readCPU()
cpu := 0.0
if dt := total - prevTotal; dt > 0 {
cpu = 100 * float64((total-prevTotal)-(idle-prevIdle)) / float64(dt)
}
prevTotal, prevIdle = total, idle
rxNow, txNow := readNet()
rx, tx := 0.0, 0.0
if secs > 0 {
rx = float64(rxNow-prevRx) / secs
tx = float64(txNow-prevTx) / secs
}
prevRx, prevTx = rxNow, txNow
r.add(sample{
T: now.UnixMilli(),
CPU: clampPct(cpu),
Mem: clampPct(readMem()),
Rx: maxF(rx, 0),
Tx: maxF(tx, 0),
})
}
}
func (d *Daemon) apiStats(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]any{
"samples": d.stats.snapshot(),
"interval": int(statsInterval.Seconds()),
})
}
// readCPU returns cumulative total and idle jiffies from /proc/stat.
func readCPU() (total, idle uint64) {
f, err := os.Open("/proc/stat")
if err != nil {
return 0, 0
}
defer f.Close()
sc := bufio.NewScanner(f)
if !sc.Scan() {
return 0, 0
}
fields := strings.Fields(sc.Text())
if len(fields) < 5 || fields[0] != "cpu" {
return 0, 0
}
for i, v := range fields[1:] {
n, _ := strconv.ParseUint(v, 10, 64)
total += n
if i == 3 || i == 4 { // idle + iowait
idle += n
}
}
return total, idle
}
// readMem returns used memory as a percentage of total (/proc/meminfo).
func readMem() float64 {
f, err := os.Open("/proc/meminfo")
if err != nil {
return 0
}
defer f.Close()
var total, avail float64
sc := bufio.NewScanner(f)
for sc.Scan() {
fields := strings.Fields(sc.Text())
if len(fields) < 2 {
continue
}
v, _ := strconv.ParseFloat(fields[1], 64)
switch fields[0] {
case "MemTotal:":
total = v
case "MemAvailable:":
avail = v
}
}
if sc.Err() != nil {
return 0
}
if total == 0 {
return 0
}
return 100 * (total - avail) / total
}
// readNet returns total received and transmitted bytes across real interfaces
// (loopback and virtual bridges excluded) from /proc/net/dev.
func readNet() (rx, tx uint64) {
f, err := os.Open("/proc/net/dev")
if err != nil {
return 0, 0
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
line := sc.Text()
colon := strings.IndexByte(line, ':')
if colon < 0 {
continue
}
name := strings.TrimSpace(line[:colon])
if name == "lo" || strings.HasPrefix(name, "veth") || strings.HasPrefix(name, "docker") || strings.HasPrefix(name, "br-") {
continue
}
fields := strings.Fields(line[colon+1:])
if len(fields) < 9 {
continue
}
r, _ := strconv.ParseUint(fields[0], 10, 64)
t, _ := strconv.ParseUint(fields[8], 10, 64)
rx += r
tx += t
}
if sc.Err() != nil {
return 0, 0
}
return rx, tx
}
func clampPct(v float64) float64 {
if v < 0 {
return 0
}
if v > 100 {
return 100
}
return v
}
func maxF(a, b float64) float64 {
if a > b {
return a
}
return b
}