/
plt_darwin.go
143 lines (129 loc) · 3.77 KB
/
plt_darwin.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
//-----------------------------------------------------------------------------
// 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 (
"bytes"
"encoding/binary"
"net"
tm "github.com/nsf/termbox-go"
"golang.org/x/sys/unix"
)
/*#include <sys/socket.h>
#include <sys/socketvar.h>
#include <netinet/in.h>
#include<netinet/tcp_var.h>*/
import "C"
func getNetDevStats(stats *ethrNetStat) {
ifs, err := net.Interfaces()
if err != nil {
ui.printErr("%v", err)
return
}
for _, iface := range ifs {
if iface.Flags&net.FlagUp == 0 {
continue
}
ifaceData, err := getIfaceData(iface.Index)
if err != nil {
ui.printErr("failed to load data for interface %q: %v", iface.Name, err)
continue
}
stats.netDevStats = append(stats.netDevStats, ethrNetDevStat{
interfaceName: iface.Name,
rxBytes: ifaceData.Data.Ibytes,
rxPkts: ifaceData.Data.Ipackets,
txBytes: ifaceData.Data.Obytes,
txPkts: ifaceData.Data.Opackets,
})
}
}
func getTCPStats(stats *ethrNetStat) {
var data C.struct_tcpstat
rawData, err := unix.SysctlRaw("net.inet.tcp.stats")
if err != nil {
return
}
buf := bytes.NewReader(rawData)
// This is ugly. Cannot read the full structure since the
// C struct has unexported fields, which reflect does not like
binary.Read(buf, binary.LittleEndian, &data.tcps_connattempt)
binary.Read(buf, binary.LittleEndian, &data.tcps_accepts)
binary.Read(buf, binary.LittleEndian, &data.tcps_connects)
binary.Read(buf, binary.LittleEndian, &data.tcps_drops)
binary.Read(buf, binary.LittleEndian, &data.tcps_conndrops)
binary.Read(buf, binary.LittleEndian, &data.tcps_closed)
binary.Read(buf, binary.LittleEndian, &data.tcps_segstimed)
binary.Read(buf, binary.LittleEndian, &data.tcps_rttupdated)
binary.Read(buf, binary.LittleEndian, &data.tcps_delack)
binary.Read(buf, binary.LittleEndian, &data.tcps_timeoutdrop)
binary.Read(buf, binary.LittleEndian, &data.tcps_rexmttimeo)
binary.Read(buf, binary.LittleEndian, &data.tcps_persisttimeo)
binary.Read(buf, binary.LittleEndian, &data.tcps_keeptimeo)
binary.Read(buf, binary.LittleEndian, &data.tcps_keepprobe)
binary.Read(buf, binary.LittleEndian, &data.tcps_keepdrops)
binary.Read(buf, binary.LittleEndian, &data.tcps_sndtotal)
binary.Read(buf, binary.LittleEndian, &data.tcps_sndpack)
binary.Read(buf, binary.LittleEndian, &data.tcps_sndbyte)
binary.Read(buf, binary.LittleEndian, &data.tcps_sndrexmitpack)
// return the TCP Retransmits
stats.tcpStats.segRetrans = uint64(data.tcps_sndrexmitpack)
return
}
func hideCursor() {
tm.SetCursor(0, 0)
}
func blockWindowResize() {
}
func getIfaceData(index int) (*ifMsghdr2, error) {
var data ifMsghdr2
rawData, err := unix.SysctlRaw("net", unix.AF_ROUTE, 0, 0, unix.NET_RT_IFLIST2, index)
if err != nil {
return nil, err
}
err = binary.Read(bytes.NewReader(rawData), binary.LittleEndian, &data)
return &data, err
}
type ifMsghdr2 struct {
Msglen uint16
Version uint8
Type uint8
Addrs int32
Flags int32
Index uint16
_ [2]byte
SndLen int32
SndMaxlen int32
SndDrops int32
Timer int32
Data ifData64
}
type ifData64 struct {
Type uint8
Typelen uint8
Physical uint8
Addrlen uint8
Hdrlen uint8
Recvquota uint8
Xmitquota uint8
Unused1 uint8
Mtu uint32
Metric uint32
Baudrate uint64
Ipackets uint64
Ierrors uint64
Opackets uint64
Oerrors uint64
Collisions uint64
Ibytes uint64
Obytes uint64
Imcasts uint64
Omcasts uint64
Iqdrops uint64
Noproto uint64
Recvtiming uint32
Xmittiming uint32
Lastchange unix.Timeval32
}