shirou_gopsutil/net/net_freebsd.go

129 lines
3.2 KiB
Go
Raw Normal View History

2021-12-23 05:54:41 +08:00
//go:build freebsd
2014-05-12 11:03:20 +08:00
// +build freebsd
2014-12-30 21:09:05 +08:00
package net
2014-05-12 11:03:20 +08:00
import (
2017-12-31 14:25:49 +08:00
"context"
2014-09-19 23:15:35 +08:00
"strconv"
2014-05-18 22:23:47 +08:00
"strings"
"github.com/shirou/gopsutil/v3/internal/common"
2014-05-12 11:03:20 +08:00
)
func IOCounters(pernic bool) ([]IOCountersStat, error) {
2017-12-31 14:25:49 +08:00
return IOCountersWithContext(context.Background(), pernic)
}
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
out, err := invoke.CommandWithContext(ctx, "netstat", "-ibdnW")
2014-05-18 22:23:47 +08:00
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
ret := make([]IOCountersStat, 0, len(lines)-1)
exists := make([]string, 0, len(ret))
2014-05-18 22:23:47 +08:00
for _, line := range lines {
values := strings.Fields(line)
if len(values) < 1 || values[0] == "Name" {
continue
}
if common.StringsHas(exists, values[0]) {
// skip if already get
continue
}
exists = append(exists, values[0])
if len(values) < 12 {
continue
}
2014-05-18 22:23:47 +08:00
base := 1
2019-03-18 01:52:26 +08:00
// sometimes Address is omitted
2014-05-18 22:23:47 +08:00
if len(values) < 13 {
base = 0
}
2014-09-19 23:15:35 +08:00
parsed := make([]uint64, 0, 8)
vv := []string{
values[base+3], // PacketsRecv
values[base+4], // Errin
values[base+5], // Dropin
values[base+6], // BytesRecvn
values[base+7], // PacketSent
values[base+8], // Errout
values[base+9], // BytesSent
values[base+11], // Dropout
}
for _, target := range vv {
if target == "-" {
parsed = append(parsed, 0)
continue
}
t, err := strconv.ParseUint(target, 10, 64)
if err != nil {
return nil, err
}
parsed = append(parsed, t)
}
n := IOCountersStat{
2014-05-18 22:23:47 +08:00
Name: values[0],
2014-09-19 23:15:35 +08:00
PacketsRecv: parsed[0],
Errin: parsed[1],
Dropin: parsed[2],
BytesRecv: parsed[3],
PacketsSent: parsed[4],
Errout: parsed[5],
BytesSent: parsed[6],
Dropout: parsed[7],
2014-05-18 22:23:47 +08:00
}
ret = append(ret, n)
}
2015-01-01 20:29:25 +08:00
if pernic == false {
return getIOCountersAll(ret)
2015-01-01 20:29:25 +08:00
}
2014-05-18 22:23:47 +08:00
return ret, nil
2014-05-12 11:03:20 +08:00
}
2015-11-20 05:19:43 +08:00
2022-01-31 04:48:09 +08:00
// IOCountersByFile exists just for compatibility with Linux.
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
2017-12-31 14:25:49 +08:00
return IOCountersByFileWithContext(context.Background(), pernic, filename)
}
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic)
}
func FilterCounters() ([]FilterStat, error) {
2017-12-31 14:25:49 +08:00
return FilterCountersWithContext(context.Background())
}
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
return nil, common.ErrNotImplementedError
}
func ConntrackStats(percpu bool) ([]ConntrackStat, error) {
return ConntrackStatsWithContext(context.Background(), percpu)
}
func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) {
return nil, common.ErrNotImplementedError
}
// ProtoCounters returns network statistics for the entire system
2015-11-20 05:19:43 +08:00
// If protocols is empty then all protocols are returned, otherwise
// just the protocols in the list are returned.
// Not Implemented for FreeBSD
func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
2017-12-31 14:25:49 +08:00
return ProtoCountersWithContext(context.Background(), protocols)
}
func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
return nil, common.ErrNotImplementedError
2015-11-20 05:19:43 +08:00
}