Files
Matt Heon d9c388e2fe Change to using gopsutil for cross-OS process ops
Instead of trying to write out own code to do basic process
operations (e.g. checking if a PID is still running in a multi-OS
friendly manner), use shirou/gopsutil, a multi-platform library
that should abstract all the complexity away. Unlike our previous
approach on Windows, this one should actually work.

Signed-off-by: Matt Heon <mheon@redhat.com>
2023-10-31 10:14:06 -04:00

89 lines
2.1 KiB
Go

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
}