2015-05-18 23:01:42 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-11-19 21:58:21 +00:00
|
|
|
"strings"
|
2015-05-18 23:01:42 +00:00
|
|
|
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-05-18 23:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NetIOStats struct {
|
|
|
|
ps PS
|
|
|
|
|
2015-05-27 05:14:55 +00:00
|
|
|
skipChecks bool
|
2015-05-18 23:01:42 +00:00
|
|
|
Interfaces []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *NetIOStats) Description() string {
|
|
|
|
return "Read metrics about network interface usage"
|
|
|
|
}
|
|
|
|
|
|
|
|
var netSampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# By default, telegraf gathers stats from any up interface (excluding loopback)
|
|
|
|
# Setting interfaces will tell it to gather these explicit interfaces,
|
|
|
|
# regardless of status.
|
|
|
|
#
|
|
|
|
# interfaces = ["eth0", ... ]
|
2015-05-18 23:01:42 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (_ *NetIOStats) SampleConfig() string {
|
|
|
|
return netSampleConfig
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func (s *NetIOStats) Gather(acc inputs.Accumulator) error {
|
2015-05-18 23:01:42 +00:00
|
|
|
netio, err := s.ps.NetIO()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting net io info: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, io := range netio {
|
|
|
|
if len(s.Interfaces) != 0 {
|
|
|
|
var found bool
|
|
|
|
|
|
|
|
for _, name := range s.Interfaces {
|
|
|
|
if name == io.Name {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-27 05:14:55 +00:00
|
|
|
} else if !s.skipChecks {
|
2015-05-18 23:01:42 +00:00
|
|
|
iface, err := net.InterfaceByName(io.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.Flags&net.FlagLoopback == net.FlagLoopback {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"interface": io.Name,
|
|
|
|
}
|
|
|
|
|
2015-12-11 20:07:32 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"bytes_sent": io.BytesSent,
|
|
|
|
"bytes_recv": io.BytesRecv,
|
|
|
|
"packets_sent": io.PacketsSent,
|
|
|
|
"packets_recv": io.PacketsRecv,
|
|
|
|
"err_in": io.Errin,
|
|
|
|
"err_out": io.Errout,
|
|
|
|
"drop_in": io.Dropin,
|
|
|
|
"drop_out": io.Dropout,
|
|
|
|
}
|
|
|
|
acc.AddFields("net", fields, tags)
|
2015-05-18 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 21:58:21 +00:00
|
|
|
// Get system wide stats for different network protocols
|
2015-12-04 22:09:07 +00:00
|
|
|
// (ignore these stats if the call fails)
|
|
|
|
netprotos, _ := s.ps.NetProto()
|
2016-01-05 23:58:35 +00:00
|
|
|
fields := make(map[string]interface{})
|
2015-11-19 21:58:21 +00:00
|
|
|
for _, proto := range netprotos {
|
|
|
|
for stat, value := range proto.Stats {
|
2015-12-04 22:09:07 +00:00
|
|
|
name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
|
|
|
|
strings.ToLower(stat))
|
2016-01-05 23:58:35 +00:00
|
|
|
fields[name] = value
|
2015-11-19 21:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 18:31:47 +00:00
|
|
|
tags := map[string]string{
|
|
|
|
"interface": "all",
|
|
|
|
}
|
|
|
|
acc.AddFields("net", fields, tags)
|
2015-11-19 21:58:21 +00:00
|
|
|
|
2015-05-18 23:01:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-07 20:39:43 +00:00
|
|
|
inputs.Add("net", func() inputs.Input {
|
2015-05-18 23:01:42 +00:00
|
|
|
return &NetIOStats{ps: &systemPS{}}
|
|
|
|
})
|
|
|
|
}
|