github.com/gofiber/fiber/v2@v2.47.0/internal/gopsutil/net/net_freebsd.go (about) 1 //go:build freebsd 2 // +build freebsd 3 4 package net 5 6 import ( 7 "context" 8 "errors" 9 "os/exec" 10 "strconv" 11 "strings" 12 13 "github.com/gofiber/fiber/v2/internal/gopsutil/common" 14 ) 15 16 func IOCounters(pernic bool) ([]IOCountersStat, error) { 17 return IOCountersWithContext(context.Background(), pernic) 18 } 19 20 func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { 21 netstat, err := exec.LookPath("netstat") 22 if err != nil { 23 return nil, err 24 } 25 out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW") 26 if err != nil { 27 return nil, err 28 } 29 30 lines := strings.Split(string(out), "\n") 31 ret := make([]IOCountersStat, 0, len(lines)-1) 32 exists := make([]string, 0, len(ret)) 33 34 for _, line := range lines { 35 values := strings.Fields(line) 36 if len(values) < 1 || values[0] == "Name" { 37 continue 38 } 39 if common.StringsHas(exists, values[0]) { 40 // skip if already get 41 continue 42 } 43 exists = append(exists, values[0]) 44 45 if len(values) < 12 { 46 continue 47 } 48 base := 1 49 // sometimes Address is omitted 50 if len(values) < 13 { 51 base = 0 52 } 53 54 parsed := make([]uint64, 0, 8) 55 vv := []string{ 56 values[base+3], // PacketsRecv 57 values[base+4], // Errin 58 values[base+5], // Dropin 59 values[base+6], // BytesRecvn 60 values[base+7], // PacketSent 61 values[base+8], // Errout 62 values[base+9], // BytesSent 63 values[base+11], // Dropout 64 } 65 for _, target := range vv { 66 if target == "-" { 67 parsed = append(parsed, 0) 68 continue 69 } 70 71 t, err := strconv.ParseUint(target, 10, 64) 72 if err != nil { 73 return nil, err 74 } 75 parsed = append(parsed, t) 76 } 77 78 n := IOCountersStat{ 79 Name: values[0], 80 PacketsRecv: parsed[0], 81 Errin: parsed[1], 82 Dropin: parsed[2], 83 BytesRecv: parsed[3], 84 PacketsSent: parsed[4], 85 Errout: parsed[5], 86 BytesSent: parsed[6], 87 Dropout: parsed[7], 88 } 89 ret = append(ret, n) 90 } 91 92 if pernic == false { 93 return getIOCountersAll(ret) 94 } 95 96 return ret, nil 97 } 98 99 // NetIOCountersByFile is an method which is added just a compatibility for linux. 100 func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { 101 return IOCountersByFileWithContext(context.Background(), pernic, filename) 102 } 103 104 func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { 105 return IOCounters(pernic) 106 } 107 108 func FilterCounters() ([]FilterStat, error) { 109 return FilterCountersWithContext(context.Background()) 110 } 111 112 func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { 113 return nil, errors.New("NetFilterCounters not implemented for freebsd") 114 } 115 116 func ConntrackStats(percpu bool) ([]ConntrackStat, error) { 117 return ConntrackStatsWithContext(context.Background(), percpu) 118 } 119 120 func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { 121 return nil, errors.New("ConntrackStats not implemented for freebsd") 122 } 123 124 // NetProtoCounters returns network statistics for the entire system 125 // If protocols is empty then all protocols are returned, otherwise 126 // just the protocols in the list are returned. 127 // Not Implemented for FreeBSD 128 func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { 129 return ProtoCountersWithContext(context.Background(), protocols) 130 } 131 132 func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { 133 return nil, errors.New("NetProtoCounters not implemented for freebsd") 134 }