1
2
3 package net
4
5 import (
6 "context"
7 "os/exec"
8 "strconv"
9 "strings"
10
11 "github.com/shirou/gopsutil/internal/common"
12 )
13
14 func IOCounters(pernic bool) ([]IOCountersStat, error) {
15 return IOCountersWithContext(context.Background(), pernic)
16 }
17
18 func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
19 netstat, err := exec.LookPath("netstat")
20 if err != nil {
21 return nil, err
22 }
23 out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW")
24 if err != nil {
25 return nil, err
26 }
27
28 lines := strings.Split(string(out), "\n")
29 ret := make([]IOCountersStat, 0, len(lines)-1)
30 exists := make([]string, 0, len(ret))
31
32 for _, line := range lines {
33 values := strings.Fields(line)
34 if len(values) < 1 || values[0] == "Name" {
35 continue
36 }
37 if common.StringsHas(exists, values[0]) {
38
39 continue
40 }
41 exists = append(exists, values[0])
42
43 if len(values) < 12 {
44 continue
45 }
46 base := 1
47
48 if len(values) < 13 {
49 base = 0
50 }
51
52 parsed := make([]uint64, 0, 8)
53 vv := []string{
54 values[base+3],
55 values[base+4],
56 values[base+5],
57 values[base+6],
58 values[base+7],
59 values[base+8],
60 values[base+9],
61 values[base+11],
62 }
63 for _, target := range vv {
64 if target == "-" {
65 parsed = append(parsed, 0)
66 continue
67 }
68
69 t, err := strconv.ParseUint(target, 10, 64)
70 if err != nil {
71 return nil, err
72 }
73 parsed = append(parsed, t)
74 }
75
76 n := IOCountersStat{
77 Name: values[0],
78 PacketsRecv: parsed[0],
79 Errin: parsed[1],
80 Dropin: parsed[2],
81 BytesRecv: parsed[3],
82 PacketsSent: parsed[4],
83 Errout: parsed[5],
84 BytesSent: parsed[6],
85 Dropout: parsed[7],
86 }
87 ret = append(ret, n)
88 }
89
90 if pernic == false {
91 return getIOCountersAll(ret)
92 }
93
94 return ret, nil
95 }
96
97
98 func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
99 return IOCountersByFileWithContext(context.Background(), pernic, filename)
100 }
101
102 func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
103 return IOCounters(pernic)
104 }
105
106 func FilterCounters() ([]FilterStat, error) {
107 return FilterCountersWithContext(context.Background())
108 }
109
110 func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
111 return nil, common.ErrNotImplementedError
112 }
113
114 func ConntrackStats(percpu bool) ([]ConntrackStat, error) {
115 return ConntrackStatsWithContext(context.Background(), percpu)
116 }
117
118 func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) {
119 return nil, common.ErrNotImplementedError
120 }
121
122
123
124
125
126 func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
127 return ProtoCountersWithContext(context.Background(), protocols)
128 }
129
130 func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
131 return nil, common.ErrNotImplementedError
132 }
133
View as plain text