-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
88 lines (83 loc) · 2.06 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
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
package stats
import (
"bufio"
"context"
"os"
"path/filepath"
"strings"
)
type InterfaceStats struct {
PacketsReceived int64 // in packets
Link int // link status
PacketsSent int64 // out packets
NumCRCErr int // input CRC errors
NumOverflows int // packet overflows
NumSoftOverflows int // software overflow
NumFramingErr int // framing errors
NumBufferingErr int // buffering errors
NumOutputErr int // output errors
Promiscuous int // number of promiscuous opens
Mbps int // megabits per sec
Addr string
}
func ReadInterfaceStats(ctx context.Context, opts ...Option) (*InterfaceStats, error) {
cfg := newConfig(opts...)
file := filepath.Join(cfg.rootdir, "stats")
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
var stats InterfaceStats
scanner := bufio.NewScanner(f)
for scanner.Scan() {
s := strings.TrimSpace(scanner.Text())
a := strings.SplitN(s, ":", 2)
if len(a) != 2 {
continue
}
var p intParser
v := strings.TrimSpace(a[1])
switch a[0] {
case "in":
stats.PacketsReceived = p.ParseInt64(v, 10)
case "link":
stats.Link = p.ParseInt(v, 10)
case "out":
stats.PacketsSent = p.ParseInt64(v, 10)
case "crc":
stats.NumCRCErr = p.ParseInt(v, 10)
case "overflows":
stats.NumOverflows = p.ParseInt(v, 10)
case "soft overflows":
stats.NumSoftOverflows = p.ParseInt(v, 10)
case "framing errs":
stats.NumFramingErr = p.ParseInt(v, 10)
case "buffer errs":
stats.NumBufferingErr = p.ParseInt(v, 10)
case "output errs":
stats.NumOutputErr = p.ParseInt(v, 10)
case "prom":
stats.Promiscuous = p.ParseInt(v, 10)
case "mbps":
stats.Mbps = p.ParseInt(v, 10)
case "addr":
stats.Addr = v
}
if err := p.Err(); err != nil {
return nil, err
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return &stats, nil
}
type TCPStats struct {
MaxConn int
MaxSegment int
ActiveOpens int
PassiveOpens int
EstablishedResets int
CurrentEstablished int
}