2026-07-25 17:58:54 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-25 23:03:31 -07:00
|
|
|
// Split by direction of travel so each counter belongs to exactly one
|
|
|
|
|
// direction: the transmitting interface owns the tx fields and the receiving one
|
|
|
|
|
// the rx fields. Reading both sets off both interfaces double counts every drop.
|
2026-07-25 22:54:04 -07:00
|
|
|
var (
|
|
|
|
|
nicTxFields = []string{
|
|
|
|
|
"tx_errors", "tx_dropped", "tx_fifo_errors",
|
|
|
|
|
"tx_carrier_errors", "tx_aborted_errors", "tx_window_errors",
|
|
|
|
|
"collisions",
|
|
|
|
|
}
|
|
|
|
|
nicRxFields = []string{
|
|
|
|
|
"rx_errors", "rx_dropped", "rx_crc_errors", "rx_missed_errors",
|
|
|
|
|
"rx_length_errors", "rx_over_errors", "rx_frame_errors", "rx_fifo_errors",
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-07-25 17:58:54 -07:00
|
|
|
|
|
|
|
|
type nicCounters struct {
|
2026-07-25 22:54:04 -07:00
|
|
|
tx uint64
|
|
|
|
|
rx uint64
|
|
|
|
|
carrierDown uint64
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readUint(path string) (uint64, bool) {
|
|
|
|
|
b, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
v, err := strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|
|
|
|
|
return v, true
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 22:54:04 -07:00
|
|
|
func sumFields(base string, fields []string) uint64 {
|
|
|
|
|
var total uint64
|
|
|
|
|
for _, f := range fields {
|
2026-07-25 17:58:54 -07:00
|
|
|
if v, ok := readUint(base + "/statistics/" + f); ok {
|
2026-07-25 22:54:04 -07:00
|
|
|
total += v
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-07-25 22:54:04 -07:00
|
|
|
return total
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
2026-07-25 22:54:04 -07:00
|
|
|
func readNIC(ifname string) nicCounters {
|
|
|
|
|
base := "/sys/class/net/" + ifname
|
|
|
|
|
c := nicCounters{
|
|
|
|
|
tx: sumFields(base, nicTxFields),
|
|
|
|
|
rx: sumFields(base, nicRxFields),
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|
2026-07-25 22:54:04 -07:00
|
|
|
c.carrierDown, _ = readUint(base + "/carrier_down_count")
|
|
|
|
|
return c
|
2026-07-25 17:58:54 -07:00
|
|
|
}
|