-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (119 loc) · 2.58 KB
/
main.go
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
135
136
137
138
139
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"time"
)
const (
rPath string = "/sys/class/net/"
recievePath string = "/statistics/rx_bytes"
transmitPath string = "/statistics/tx_bytes"
)
//up/downs[x][0]: prev transmit/recieve bytes
//up/downs[x][1]: current transmit/recieve bytes
var ups, downs [][]int
func main() {
netNames := getNetNames()
//init values
for i, netName := range netNames {
arr := make([]int, 2)
ups = append(ups, arr)
downs = append(downs, arr)
var err error
ups[i][0], err = getUp(netName)
if err != nil {
log.Fatal(err)
}
downs[i][0], err = getDown(netName)
if err != nil {
log.Fatal(err)
}
}
//get data per second
ticker := time.NewTicker(time.Second)
for {
<-ticker.C
//clean screen
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
for i, netName := range netNames {
detectNetSpeed(i, netName)
}
}
}
func getNetNames() (names []string) {
nets, err := ioutil.ReadDir(rPath)
if err != nil {
log.Fatal(err)
}
for _, netName := range nets {
names = append(names, netName.Name())
}
return names
}
func getUp(netName string) (int, error) {
b, err := ioutil.ReadFile(rPath + netName + transmitPath)
// b, err := ioutil.ReadFile("/sys/class/net/wlp2s0//statistics/tx_bytes")
if err != nil {
log.Fatal(err)
}
b = b[:len(b)-1]
return strconv.Atoi(string(b))
}
func getDown(netName string) (int, error) {
b, err := ioutil.ReadFile(rPath + netName + recievePath)
// b, err := ioutil.ReadFile("/sys/class/net/wlp2s0//statistics/rx_bytes")
if err != nil {
log.Fatal(err)
}
b = b[:len(b)-1]
return strconv.Atoi(string(b))
}
func detectNetSpeed(i int, netName string) {
var err error
ups[i][1], err = getUp(netName)
fmt.Println(ups[i][1])
if err != nil {
log.Fatal(err)
}
downs[i][1], err = getDown(netName)
if err != nil {
log.Fatal(err)
}
fmt.Println(downs[i][1])
up := withUnit(ups[i][1] - ups[i][0])
down := withUnit(downs[i][1] - downs[i][0])
// \r is a method to refreash but sometimes it can leave some result unerased.
// fmt.Printf("%8s\tupload:%5sB/s\tdownload:%5sB/s\r", netName, up, down)
fmt.Printf("%8s upload:%8sB/s download:%8sB/s\n", netName, up, down)
ups[i][0] = ups[i][1]
downs[i][0] = downs[i][1]
}
func withUnit(b int) (res string) {
v := float64(b)
unit := ""
count := 0
for v >= 1024 {
count++
v = v / 1024
}
switch count {
case 0:
case 1:
unit = "K"
case 2:
unit = "M"
case 3:
unit = "G"
case 4:
unit = "T"
default:
log.Fatal("can't handle so much data")
}
return strconv.FormatFloat(v, 'g', 5, 32) + unit
}