-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathinterfaces_infos.go
137 lines (117 loc) · 2.63 KB
/
interfaces_infos.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
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import (
"fmt"
"unsafe"
)
// InterfaceInfos network interface related infos
type InterfaceInfos struct {
Name string
// Speed in factor/sec
Speed int
Factor int
Duplex InterfaceDuplexType
State InterfaceState
}
// InterfaceDuplexType network interface duplex type
type InterfaceDuplexType int
const (
InterfaceFullDuplex InterfaceDuplexType = iota
InterfaceHalfDuplex
InterfaceUnknownDuplex
)
var (
interfaceDuplexTypes = []string{
"Full Duplex", "Half Duplex", "Unknown Duplex",
}
)
func (s InterfaceDuplexType) String() string {
return interfaceDuplexTypes[s]
}
func (s InterfaceDuplexType) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *InterfaceDuplexType) UnmarshalText(text []byte) error {
enum := string(text)
for i := range interfaceDuplexTypes {
if enum == interfaceDuplexTypes[i] {
*s = InterfaceDuplexType(i)
return nil
}
}
return fmt.Errorf("unknown status")
}
// InterfaceState up or down
type InterfaceState int
const (
InterfaceUpState InterfaceState = iota
InterfaceDownState
)
var (
interfaceStates = []string{
"UP", "DOWN",
}
)
func (s InterfaceState) String() string {
return interfaceStates[s]
}
func (s InterfaceState) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *InterfaceState) UnmarshalText(text []byte) error {
enum := string(text)
for i := range interfaceStates {
if enum == interfaceStates[i] {
*s = InterfaceState(i)
return nil
}
}
return fmt.Errorf("unknown status")
}
func (s *Stat) InteraceInfos() []*InterfaceInfos {
s.Lock()
defer s.Unlock()
var ifaceSize C.size_t
var cArray *C.sg_network_iface_stats = C.sg_get_network_iface_stats(&ifaceSize)
length := int(ifaceSize)
slice := (*[1 << 16]C.sg_network_iface_stats)(unsafe.Pointer(cArray))[:length:length]
var res []*InterfaceInfos
for _, v := range slice {
i := &InterfaceInfos{
Name: C.GoString(v.interface_name),
Speed: int(v.speed),
Factor: int(v.factor),
}
switch v.duplex {
case C.SG_IFACE_DUPLEX_FULL:
i.Duplex = InterfaceFullDuplex
case C.SG_IFACE_DUPLEX_HALF:
i.Duplex = InterfaceHalfDuplex
case C.SG_IFACE_DUPLEX_UNKNOWN:
i.Duplex = InterfaceUnknownDuplex
}
switch v.up {
case C.SG_IFACE_DOWN:
i.State = InterfaceDownState
case C.SG_IFACE_UP:
i.State = InterfaceUpState
}
res = append(res, i)
}
return res
}
func (i *InterfaceInfos) String() string {
return fmt.Sprintf(
"Name:\t%s\n"+
"Speed:\t\t%d\n"+
"Factor:\t\t%d\n"+
"Duplex:\t\t%s\n"+
"State:\t\t%s\n",
i.Name,
i.Speed,
i.Factor,
i.Duplex,
i.State)
}