This repository has been archived by the owner on Jan 30, 2018. It is now read-only.
/
stat.go
385 lines (330 loc) · 8.37 KB
/
stat.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package edgemax
import (
"encoding/json"
"fmt"
"net"
"reflect"
"sort"
"strconv"
"strings"
"time"
)
// A Stat is a statistic provided by an EdgeMAX device. Type assertions
// can be used to determine the specific type of a Stat, and to access
// a Stat's fields.
type Stat interface {
StatType() StatType
}
// A StatType is a type of Stat. StatType values can be used to retrieve
// certain types of Stats from Client.Stats
type StatType string
const (
// StatTypeDPIStats retrieves EdgeMAX deep packet inspection statistics.
StatTypeDPIStats StatType = "export"
// StatTypeSystemStats retrieves EdgeMAX system statistics, including
// system uptime, CPU utilization, and memory utilization.
StatTypeSystemStats StatType = "system-stats"
// StatTypeInterfaces retrieves EdgeMAX network interface statistics.
StatTypeInterfaces StatType = "interfaces"
)
// SystemStats is a Stat which contains system statistics for an EdgeMAX
// device.
type SystemStats struct {
CPU int
Uptime time.Duration
Memory int
}
var _ Stat = &SystemStats{}
// StatType implements the Stats interface.
func (ss *SystemStats) StatType() StatType {
return StatTypeSystemStats
}
// UnmarshalJSON unmarshals JSON into a SystemStats.
func (ss *SystemStats) UnmarshalJSON(b []byte) error {
var v struct {
CPU string `json:"cpu"`
Uptime string `json:"uptime"`
Mem string `json:"mem"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
cpu, err := strconv.Atoi(v.CPU)
if err != nil {
return err
}
uptime, err := strconv.Atoi(v.Uptime)
if err != nil {
return err
}
memory, err := strconv.Atoi(v.Mem)
if err != nil {
return err
}
*ss = SystemStats{
CPU: cpu,
Uptime: time.Duration(uptime) * time.Second,
Memory: memory,
}
return nil
}
// Interfaces is a slice of Interface values, which contains information about
// network interfaces for EdgeMAX devices.
type Interfaces []*Interface
var _ Stat = &Interfaces{}
// StatType implements the Stats interface.
func (i Interfaces) StatType() StatType {
return StatTypeInterfaces
}
// An Interface is an EdgeMAX network interface.
type Interface struct {
Name string
Up bool
Autonegotiation bool
Duplex string
Speed int
MAC net.HardwareAddr
MTU int
Addresses []net.IP
Stats InterfaceStats
}
// InterfaceStats contains network interface data transmission statistics.
type InterfaceStats struct {
ReceivePackets int
TransmitPackets int
ReceiveBytes int
TransmitBytes int
ReceiveErrors int
TransmitErrors int
ReceiveDropped int
TransmitDropped int
Multicast int
ReceiveBPS int
TransmitBPS int
}
// UnmarshalJSON unmarshals JSON into an Interfaces.
func (i *Interfaces) UnmarshalJSON(b []byte) error {
var v map[string]struct {
Up string `json:"up"`
Autoneg string `json:"autoneg"`
Duplex string `json:"duplex"`
Speed string `json:"speed"`
MAC string `json:"mac"`
MTU string `json:"mtu"`
Addresses interface{} `json:"addresses"`
Stats struct {
RXPackets string `json:"rx_packets"`
TXPackets string `json:"tx_packets"`
RXBytes string `json:"rx_bytes"`
TXBytes string `json:"tx_bytes"`
RXErrors string `json:"rx_errors"`
TXErrors string `json:"tx_errors"`
RXDropped string `json:"rx_dropped"`
TXDropped string `json:"tx_dropped"`
Multicast string `json:"multicast"`
RXBPS string `json:"rx_bps"`
TXBPS string `json:"tx_bps"`
} `json:"stats"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
is := make(Interfaces, 0, len(v))
for k, vv := range v {
ss := []string{
vv.Speed,
vv.MTU,
vv.Stats.RXPackets,
vv.Stats.TXPackets,
vv.Stats.RXBytes,
vv.Stats.TXBytes,
vv.Stats.RXErrors,
vv.Stats.TXErrors,
vv.Stats.RXDropped,
vv.Stats.TXDropped,
vv.Stats.Multicast,
vv.Stats.RXBPS,
vv.Stats.TXBPS,
}
ints := make([]int, 0, len(ss))
for _, str := range ss {
if str == "" {
ints = append(ints, 0)
continue
}
v, err := strconv.Atoi(str)
if err != nil {
return err
}
ints = append(ints, v)
}
var mac net.HardwareAddr
if vv.MAC != "" {
var err error
mac, err = net.ParseMAC(vv.MAC)
if err != nil {
return err
}
}
ips := make([]net.IP, 0)
switch reflect.ValueOf(vv.Addresses).Kind() {
case reflect.Slice:
for _, ip := range vv.Addresses.([]interface{}) {
ip, _, err := net.ParseCIDR(ip.(string))
if err != nil {
return err
}
ips = append(ips, ip)
}
case reflect.String:
v := vv.Addresses.(string)
if v != "" {
ip, _, err := net.ParseCIDR(v)
if err != nil {
return err
}
ips = append(ips, ip)
}
}
is = append(is, &Interface{
Name: k,
Up: vv.Up == "true",
Autonegotiation: vv.Autoneg == "true",
Duplex: vv.Duplex,
Speed: ints[0],
MAC: mac,
MTU: ints[1],
Addresses: ips,
Stats: InterfaceStats{
ReceivePackets: ints[2],
TransmitPackets: ints[3],
ReceiveBytes: ints[4],
TransmitBytes: ints[5],
ReceiveErrors: ints[6],
TransmitErrors: ints[7],
ReceiveDropped: ints[8],
TransmitDropped: ints[9],
Multicast: ints[10],
ReceiveBPS: ints[11],
TransmitBPS: ints[12],
},
})
}
sort.Sort(byInterfaceName(is))
*i = is
return nil
}
// byInterfaceName is used to sort Interfaces by network interface name.
type byInterfaceName []*Interface
func (b byInterfaceName) Len() int { return len(b) }
func (b byInterfaceName) Less(i int, j int) bool { return b[i].Name < b[j].Name }
func (b byInterfaceName) Swap(i int, j int) { b[i], b[j] = b[j], b[i] }
// DPIStats is a slice of DPIStat values, and contains Deep Packet Inspection
// stats from an EdgeMAX device.
type DPIStats []*DPIStat
// A DPIStat contains Deep Packet Inspection stats from an EdgeMAX device, for
// an individual client and traffic type.
type DPIStat struct {
IP net.IP
Type string
Category string
ReceiveBytes int
ReceiveRate int
TransmitBytes int
TransmitRate int
}
// StatType implements the Stats interface.
func (d DPIStats) StatType() StatType {
return StatTypeDPIStats
}
// UnmarshalJSON unmarshals JSON into a DPIStats.
func (d *DPIStats) UnmarshalJSON(b []byte) error {
var v map[string]map[string]struct {
RXBytes string `json:"rx_bytes"`
RXRate string `json:"rx_rate"`
TXBytes string `json:"tx_bytes"`
TXRate string `json:"tx_rate"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var out DPIStats
for statIP := range v {
ip := net.ParseIP(statIP)
if ip == nil {
continue
}
for statType, stats := range v[statIP] {
nameCat := strings.SplitN(statType, "|", 2)
if len(nameCat) != 2 {
return fmt.Errorf("invalid stat type: %q", statType)
}
rxBytes, err := strconv.Atoi(stats.RXBytes)
if err != nil {
return err
}
rxRate, err := strconv.Atoi(stats.RXRate)
if err != nil {
return err
}
txBytes, err := strconv.Atoi(stats.TXBytes)
if err != nil {
return err
}
txRate, err := strconv.Atoi(stats.TXRate)
if err != nil {
return err
}
out = append(out, &DPIStat{
IP: ip,
Type: nameCat[0],
Category: nameCat[1],
ReceiveBytes: rxBytes,
ReceiveRate: rxRate,
TransmitBytes: txBytes,
TransmitRate: txRate,
})
}
}
sort.Sort(byIPAndType(out))
*d = out
return nil
}
// byIPAndType is used to sort Interfaces by network interface name.
type byIPAndType []*DPIStat
func (b byIPAndType) Len() int { return len(b) }
func (b byIPAndType) Less(i int, j int) bool {
less := ipLess(b[i].IP, b[j].IP)
if less {
return true
}
if b[i].Type < b[j].Type {
return true
}
return false
}
func (b byIPAndType) Swap(i int, j int) { b[i], b[j] = b[j], b[i] }
func ipLess(a net.IP, b net.IP) bool {
// Need 4-byte IPv4 representation where possible
if ip4 := a.To4(); ip4 != nil {
a = ip4
}
if ip4 := b.To4(); ip4 != nil {
b = ip4
}
switch {
// IPv4 addresses should appear before IPv6 addresses
case len(a) == net.IPv4len && len(b) == net.IPv6len:
return true
case len(a) == net.IPv6len && len(b) == net.IPv4len:
return false
case a == nil && b == nil:
return false
}
for i := range a {
if a[i] < b[i] {
return true
}
}
return false
}