forked from microsoft/ethr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plt_linux.go
144 lines (127 loc) · 3.26 KB
/
plt_linux.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
140
141
142
143
144
//-----------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license.
// See LICENSE.txt file in the project root for full license information.
//-----------------------------------------------------------------------------
package main
import (
"bufio"
"net"
"os"
"strconv"
"strings"
tm "github.com/nsf/termbox-go"
)
type ethrNetDevInfo struct {
bytes uint64
packets uint64
drop uint64
errs uint64
fifo uint64
frame uint64
compressed uint64
multicast uint64
}
func getNetDevStats(stats *ethrNetStat) {
ifs, err := net.Interfaces()
if err != nil {
ui.printErr("%v", err)
return
}
netStatsFile, err := os.Open("/proc/net/dev")
if err != nil {
ui.printErr("%v", err)
return
}
defer netStatsFile.Close()
reader := bufio.NewReader(netStatsFile)
// Pass the header
// Inter-| Receive | Transmit
// face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
reader.ReadString('\n')
reader.ReadString('\n')
var line string
for err == nil {
line, err = reader.ReadString('\n')
if line == "" {
continue
}
netDevStat := buildNetDevStat(line)
if isIfUp(netDevStat.interfaceName, ifs) {
stats.netDevStats = append(stats.netDevStats, buildNetDevStat(line))
}
}
}
func buildNetDevStat(line string) ethrNetDevStat {
fields := strings.Fields(line)
interfaceName := strings.TrimSuffix(fields[0], ":")
rxInfo := toNetDevInfo(fields[1:9])
txInfo := toNetDevInfo(fields[9:17])
return ethrNetDevStat{
interfaceName: interfaceName,
rxBytes: rxInfo.bytes,
txBytes: txInfo.bytes,
rxPkts: rxInfo.packets,
txPkts: txInfo.packets,
}
}
func toNetDevInfo(fields []string) ethrNetDevInfo {
return ethrNetDevInfo{
bytes: toInt(fields[0]),
packets: toInt(fields[1]),
errs: toInt(fields[2]),
drop: toInt(fields[3]),
fifo: toInt(fields[4]),
frame: toInt(fields[5]),
compressed: toInt(fields[6]),
multicast: toInt(fields[7]),
}
}
func isIfUp(ifName string, ifs []net.Interface) bool {
for _, ifi := range ifs {
if ifi.Name == ifName {
if (ifi.Flags & net.FlagUp) != 0 {
return true
}
return false
}
}
return false
}
func toInt(str string) uint64 {
res, err := strconv.ParseUint(str, 10, 64)
if err != nil {
panic(err)
}
return res
}
func getTcpStats(stats *ethrNetStat) {
snmpStatsFile, err := os.Open("/proc/net/snmp")
if err != nil {
ui.printErr("%v", err)
return
}
defer snmpStatsFile.Close()
reader := bufio.NewReader(snmpStatsFile)
var line string
for err == nil {
// Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets
// CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts InCsumErrors
line, err = reader.ReadString('\n')
if line == "" || !strings.HasPrefix(line, "Tcp") {
continue
}
// Skip the first line starting with Tcp
line, err = reader.ReadString('\n')
if !strings.HasPrefix(line, "Tcp") {
break
}
fields := strings.Fields(line)
stats.tcpStats.segRetrans = toInt(fields[12])
}
}
func hideCursor() {
tm.SetCursor(0, 0)
}
func blockWindowResize() {
}