-
Notifications
You must be signed in to change notification settings - Fork 0
/
intrate.go
64 lines (58 loc) · 1.38 KB
/
intrate.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
package nettable
import (
"fmt"
"io"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
)
// Irates contains a list of interface data rates
type Irates struct {
list []*irate
}
type irate struct {
hostname string
name string
bw uint32
inDataRate uint64
outDataRate uint64
inPktRate uint64
outPktRate uint64
}
func (d *Irates) Read(r io.Reader) error {
data := new(IntRate)
err := decodeTelemetry(data, r)
if err != nil {
return errors.Wrap(err, "error decoding JSON file")
}
for _, b := range data.Rows {
i := new(irate)
// Hostname
i.hostname = data.Telemetry.NodeIDStr
// Interface Name
i.name = b.Keys.InterfaceName
// Data Rate
intf := b.Content
i.bw = intf.Bandwidth
i.inDataRate = intf.InputDataRate
i.outDataRate = intf.OutputDataRate
i.inPktRate = intf.InputPacketRate
i.outPktRate = intf.OutputPacketRate
d.list = append(d.list, i)
}
return nil
}
// DisplayTable pretty prints the info populated.
func (d *Irates) DisplayTable(w io.Writer) {
var data [][]string
for _, s := range d.list {
data = append(data, []string{s.hostname, s.name, fmt.Sprint(s.inDataRate),
fmt.Sprint(s.outDataRate), fmt.Sprint(s.bw)})
}
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"hostname", "interface", "in kbps",
"out kbps", "bw"})
for _, v := range data {
table.Append(v)
}
table.Render() // Send output
}