-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstats.go
61 lines (54 loc) · 1.44 KB
/
stats.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
package main
import (
"bufio"
"bytes"
"runtime"
"runtime/debug"
"strconv"
"strings"
"golang.zx2c4.com/wireguard/device"
)
func stats(dev *device.Device) func() (any, error) {
return func() (any, error) {
var buf bytes.Buffer
if err := dev.IpcGetOperation(&buf); err != nil {
logger.Errorf("Get device config: %v", err)
return nil, err
}
stats := struct {
Endpoint string
LastHandshakeTimestamp int64
ReceivedBytes int64
SentBytes int64
NumGoroutine int
Version string
}{
NumGoroutine: runtime.NumGoroutine(),
Version: version(),
}
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
line := scanner.Text()
if prefix := "endpoint="; strings.HasPrefix(line, prefix) {
stats.Endpoint = strings.TrimPrefix(line, prefix)
}
if prefix := "last_handshake_time_sec="; strings.HasPrefix(line, prefix) {
stats.LastHandshakeTimestamp, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
}
if prefix := "rx_bytes="; strings.HasPrefix(line, prefix) {
stats.ReceivedBytes, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
}
if prefix := "tx_bytes="; strings.HasPrefix(line, prefix) {
stats.SentBytes, _ = strconv.ParseInt(strings.TrimPrefix(line, prefix), 10, 64)
}
}
return stats, nil
}
}
func version() string {
info, ok := debug.ReadBuildInfo()
if ok {
return info.Main.Version
}
return "(devel)"
}