This repository was archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed_monitor.go
More file actions
76 lines (67 loc) · 1.43 KB
/
speed_monitor.go
File metadata and controls
76 lines (67 loc) · 1.43 KB
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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
type speedStats struct {
MaxRxR uint64
MaxTxR uint64
}
func speedMonitor(speedMonitorChan chan<- speedStats, requestSpeedMonitorChan <-chan bool) {
var maxRxR, maxTxR uint64 = 0, 0
if len(wanInterface) == 0 {
log.Println("Speed Monitor DISABLED")
}
log.Printf("Speed Monitor watching %s\n", wanInterface)
prevRx, prevTx := getInterfaceByteValues(wanInterface)
for range time.Tick(time.Second) {
rx, tx := getInterfaceByteValues(wanInterface)
rxr := rx - prevRx
txr := tx - prevTx
if rxr > maxRxR {
maxRxR = rxr
}
if txr > maxTxR {
maxTxR = txr
}
prevRx, prevTx = rx, tx
select {
case _, ok := <-requestSpeedMonitorChan:
if ok {
report := speedStats{
MaxRxR: maxRxR,
MaxTxR: maxTxR,
}
speedMonitorChan <- report
maxRxR, maxTxR = 0, 0
} else {
fmt.Println("requestSpeedMonitorChan closed!")
}
default:
}
}
}
func getInterfaceByteValues(in string) (uint64, uint64) {
file, err := os.Open("/proc/net/dev")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
items := strings.Fields(text)
if strings.HasPrefix(items[0], in) {
rx, _ := strconv.ParseUint(items[1], 10, 64)
tx, _ := strconv.ParseUint(items[9], 10, 64)
return rx, tx
}
}
log.Println("Missing Vlan2 Interface")
return 0, 0
}