forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethtool_linux.go
136 lines (104 loc) · 2.62 KB
/
ethtool_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
// +build linux
package ethtool
import (
"net"
"sync"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/pkg/errors"
"github.com/safchain/ethtool"
)
type CommandEthtool struct {
ethtool *ethtool.Ethtool
}
func (e *Ethtool) Gather(acc telegraf.Accumulator) error {
// Get the list of interfaces
interfaces, err := e.command.Interfaces()
if err != nil {
acc.AddError(err)
return nil
}
interfaceFilter, err := filter.NewIncludeExcludeFilter(e.InterfaceInclude, e.InterfaceExclude)
if err != nil {
return err
}
// parallelize the ethtool call in event of many interfaces
var wg sync.WaitGroup
for _, iface := range interfaces {
// Check this isn't a loop back and that its matched by the filter
if (iface.Flags&net.FlagLoopback == 0) && interfaceFilter.Match(iface.Name) {
wg.Add(1)
go func(i net.Interface) {
e.gatherEthtoolStats(i, acc)
wg.Done()
}(iface)
}
}
// Waiting for all the interfaces
wg.Wait()
return nil
}
// Initialise the Command Tool
func (e *Ethtool) Init() error {
return e.command.Init()
}
// Gather the stats for the interface.
func (e *Ethtool) gatherEthtoolStats(iface net.Interface, acc telegraf.Accumulator) {
tags := make(map[string]string)
tags[tagInterface] = iface.Name
driverName, err := e.command.DriverName(iface.Name)
if err != nil {
driverErr := errors.Wrapf(err, "%s driver", iface.Name)
acc.AddError(driverErr)
return
}
tags[tagDriverName] = driverName
fields := make(map[string]interface{})
stats, err := e.command.Stats(iface.Name)
if err != nil {
statsErr := errors.Wrapf(err, "%s stats", iface.Name)
acc.AddError(statsErr)
return
}
for k, v := range stats {
fields[k] = v
}
acc.AddFields(pluginName, fields, tags)
}
func NewCommandEthtool() *CommandEthtool {
return &CommandEthtool{}
}
func (c *CommandEthtool) Init() error {
if c.ethtool != nil {
return nil
}
e, err := ethtool.NewEthtool()
if err == nil {
c.ethtool = e
}
return err
}
func (c *CommandEthtool) DriverName(intf string) (string, error) {
return c.ethtool.DriverName(intf)
}
func (c *CommandEthtool) Stats(intf string) (map[string]uint64, error) {
return c.ethtool.Stats(intf)
}
func (c *CommandEthtool) Interfaces() ([]net.Interface, error) {
// Get the list of interfaces
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
return interfaces, nil
}
func init() {
inputs.Add(pluginName, func() telegraf.Input {
return &Ethtool{
InterfaceInclude: []string{},
InterfaceExclude: []string{},
command: NewCommandEthtool(),
}
})
}