0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
var BitRateUnit = CONFIG.Unit | 1
type BitRate int64
func (b BitRate) String() string {
n := float(b)
for i := 0; i <= CONFIG.Unit; i++ {
n /= 1024
}
n /= float(CONFIG.Time.Seconds())
return fmt.Sprintf("%.2f %cB/s", n, SI[BitRateUnit])
}
type Net struct {
Tx BitRate
Rx BitRate
}
func readDev() *Net {
f, err := os.OpenFile("/proc/net/dev", os.O_RDONLY, 0)
if Is(err) {
return &Net{}
}
b := make([]byte, 1024)
ln, err := f.Read(b)
f.Close()
if Is(err) {
return &Net{}
}
var ns numSeeker
var arr []uint64
var rx, tx BitRate
for _, v := range strings.Split(string(b[0:ln]), "\n") {
arr = []uint64{}
ns.Init([]byte(v))
arr = ns.GetNums()
if len(arr) < 16 {
continue
}
tx += BitRate(int64(arr[8]))
rx += BitRate(int64(arr[0]))
}
return &Net{tx, rx}
}
func (n *Net) update() {
n2 := readDev()
time.Sleep(CONFIG.Time)
n = readDev()
n.Tx = n.Tx - n2.Tx
n.Rx = n.Rx - n2.Rx
}
func (n *Net) String() string {
if CONFIG.Json {
b, _ := json.Marshal(n)
return string(b)
}
return fmt.Sprintf("TX: %s | RX: %s", n.Tx, n.Rx)
}