2022-05-24 21:49:47 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2018-07-12 07:43:49 +08:00
|
|
|
package net
|
2015-05-19 07:01:42 +08:00
|
|
|
|
|
|
|
|
import (
|
2022-05-24 21:49:47 +08:00
|
|
|
_ "embed"
|
2015-05-19 07:01:42 +08:00
|
|
|
"fmt"
|
|
|
|
|
"net"
|
2024-01-30 04:11:18 +08:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
2015-11-20 05:58:21 +08:00
|
|
|
"strings"
|
2015-05-19 07:01:42 +08:00
|
|
|
|
2016-01-28 05:21:36 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2024-05-11 05:43:43 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2017-11-30 04:16:34 +08:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
2024-11-13 01:26:23 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-21 02:57:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2018-07-12 07:43:49 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/system"
|
2015-05-19 07:01:42 +08:00
|
|
|
)
|
|
|
|
|
|
2022-05-24 21:49:47 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2024-11-25 18:23:17 +08:00
|
|
|
type Net struct {
|
|
|
|
|
Interfaces []string `toml:"interfaces"`
|
|
|
|
|
IgnoreProtocolStats bool `toml:"ignore_protocol_stats"`
|
2015-05-19 07:01:42 +08:00
|
|
|
|
2024-11-25 18:23:17 +08:00
|
|
|
filter filter.Filter
|
|
|
|
|
ps system.PS
|
|
|
|
|
skipChecks bool
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-25 18:23:17 +08:00
|
|
|
func (*Net) SampleConfig() string {
|
2022-05-24 21:49:47 +08:00
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 18:23:17 +08:00
|
|
|
func (n *Net) Init() error {
|
2023-07-12 22:04:00 +08:00
|
|
|
if !n.IgnoreProtocolStats {
|
2024-05-11 05:43:43 +08:00
|
|
|
config.PrintOptionValueDeprecationNotice("inputs.net", "ignore_protocol_stats", "false",
|
2023-07-12 22:04:00 +08:00
|
|
|
telegraf.DeprecationInfo{
|
|
|
|
|
Since: "1.27.3",
|
|
|
|
|
RemovalIn: "1.36.0",
|
2024-05-16 16:54:50 +08:00
|
|
|
Notice: "use the 'inputs.nstat' plugin instead for protocol stats",
|
2023-07-12 22:04:00 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 23:22:35 +08:00
|
|
|
// So not use the interface list of the system if the HOST_PROC variable is
|
|
|
|
|
// set as the interfaces are determined by a syscall and therefore might
|
|
|
|
|
// differ especially in container environments.
|
|
|
|
|
n.skipChecks = os.Getenv("HOST_PROC") != ""
|
|
|
|
|
|
2023-07-12 22:04:00 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 18:23:17 +08:00
|
|
|
func (n *Net) Gather(acc telegraf.Accumulator) error {
|
2021-02-17 07:19:50 +08:00
|
|
|
netio, err := n.ps.NetIO()
|
2015-05-19 07:01:42 +08:00
|
|
|
if err != nil {
|
2023-03-02 05:19:38 +08:00
|
|
|
return fmt.Errorf("error getting net io info: %w", err)
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
if n.filter == nil {
|
|
|
|
|
if n.filter, err = filter.Compile(n.Interfaces); err != nil {
|
2023-03-02 05:19:38 +08:00
|
|
|
return fmt.Errorf("error compiling filter: %w", err)
|
2017-11-30 04:16:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 02:13:14 +08:00
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
|
if err != nil {
|
2023-03-02 05:19:38 +08:00
|
|
|
return fmt.Errorf("error getting list of interfaces: %w", err)
|
2019-04-24 02:13:14 +08:00
|
|
|
}
|
2024-10-25 18:54:05 +08:00
|
|
|
interfacesByName := make(map[string]net.Interface, len(interfaces))
|
2019-04-24 02:13:14 +08:00
|
|
|
for _, iface := range interfaces {
|
|
|
|
|
interfacesByName[iface.Name] = iface
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 07:01:42 +08:00
|
|
|
for _, io := range netio {
|
2021-02-17 07:19:50 +08:00
|
|
|
if len(n.Interfaces) != 0 {
|
2015-05-19 07:01:42 +08:00
|
|
|
var found bool
|
|
|
|
|
|
2021-02-17 07:19:50 +08:00
|
|
|
if n.filter.Match(io.Name) {
|
2017-11-30 04:16:34 +08:00
|
|
|
found = true
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-02-17 07:19:50 +08:00
|
|
|
} else if !n.skipChecks {
|
2019-04-24 02:13:14 +08:00
|
|
|
iface, ok := interfacesByName[io.Name]
|
|
|
|
|
if !ok {
|
2015-05-19 07:01:42 +08:00
|
|
|
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-12 04:07:32 +08: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,
|
2024-01-30 04:11:18 +08:00
|
|
|
"speed": getInterfaceSpeed(io.Name),
|
2015-12-12 04:07:32 +08:00
|
|
|
}
|
2016-09-01 00:27:37 +08:00
|
|
|
acc.AddCounter("net", fields, tags)
|
2015-05-19 07:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 05:58:21 +08:00
|
|
|
// Get system wide stats for different network protocols
|
2015-12-05 06:09:07 +08:00
|
|
|
// (ignore these stats if the call fails)
|
2021-02-17 07:19:50 +08:00
|
|
|
if !n.IgnoreProtocolStats {
|
2024-07-10 18:51:25 +08:00
|
|
|
//nolint:errcheck // stats ignored on fail
|
2021-02-17 07:19:50 +08:00
|
|
|
netprotos, _ := n.ps.NetProto()
|
2018-03-14 12:08:21 +08:00
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
for _, proto := range netprotos {
|
|
|
|
|
for stat, value := range proto.Stats {
|
|
|
|
|
name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
|
|
|
|
|
strings.ToLower(stat))
|
|
|
|
|
fields[name] = value
|
|
|
|
|
}
|
2015-11-20 05:58:21 +08:00
|
|
|
}
|
2018-03-14 12:08:21 +08:00
|
|
|
tags := map[string]string{
|
|
|
|
|
"interface": "all",
|
|
|
|
|
}
|
|
|
|
|
acc.AddFields("net", fields, tags)
|
2015-11-20 05:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 07:01:42 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 04:11:18 +08:00
|
|
|
// Get the interface speed from /sys/class/net/*/speed file. returns -1 if unsupported
|
|
|
|
|
func getInterfaceSpeed(ioName string) int64 {
|
2024-11-13 01:26:23 +08:00
|
|
|
sysPath := internal.GetSysPath()
|
2024-01-30 04:11:18 +08:00
|
|
|
|
|
|
|
|
raw, err := os.ReadFile(filepath.Join(sysPath, "class", "net", ioName, "speed"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
speed, err := strconv.ParseInt(strings.TrimSuffix(string(raw), "\n"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
return speed
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 07:01:42 +08:00
|
|
|
func init() {
|
2016-01-28 05:21:36 +08:00
|
|
|
inputs.Add("net", func() telegraf.Input {
|
2024-11-25 18:23:17 +08:00
|
|
|
return &Net{ps: system.NewSystemPS()}
|
2015-05-19 07:01:42 +08:00
|
|
|
})
|
|
|
|
|
}
|