Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
89
plugins/inputs/net/README.md
Normal file
89
plugins/inputs/net/README.md
Normal file
|
@ -0,0 +1,89 @@
|
|||
# Network Input Plugin
|
||||
|
||||
This plugin gathers metrics about network interface and protocol usage.
|
||||
|
||||
⭐ Telegraf v0.1.1
|
||||
🏷️ network
|
||||
💻 all
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Gather metrics about network interfaces
|
||||
[[inputs.net]]
|
||||
## By default, telegraf gathers stats from any up interface (excluding loopback)
|
||||
## Setting interfaces will tell it to gather these explicit interfaces,
|
||||
## regardless of status. When specifying an interface, glob-style
|
||||
## patterns are also supported.
|
||||
# interfaces = ["eth*", "enp0s[0-1]", "lo"]
|
||||
|
||||
## On linux systems telegraf also collects protocol stats.
|
||||
## Setting ignore_protocol_stats to true will skip reporting of protocol metrics.
|
||||
##
|
||||
## DEPRECATION NOTICE: A value of 'false' is deprecated and discouraged!
|
||||
## Please set this to `true` and use the 'inputs.nstat'
|
||||
## plugin instead.
|
||||
# ignore_protocol_stats = false
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
The fields from this plugin are gathered in the _net_ measurement.
|
||||
|
||||
Fields (all platforms):
|
||||
|
||||
* bytes_sent - The total number of bytes sent by the interface
|
||||
* bytes_recv - The total number of bytes received by the interface
|
||||
* packets_sent - The total number of packets sent by the interface
|
||||
* packets_recv - The total number of packets received by the interface
|
||||
* err_in - The total number of receive errors detected by the interface
|
||||
* err_out - The total number of transmit errors detected by the interface
|
||||
* drop_in - The total number of received packets dropped by the interface
|
||||
* drop_out - The total number of transmitted packets dropped by the interface
|
||||
* speed - The interface's latest or current speed value, in Mbits/sec. May be -1 if unsupported by the interface
|
||||
|
||||
Different platforms gather the data above with different mechanisms. Telegraf
|
||||
uses the ([gopsutil](https://github.com/shirou/gopsutil)) package, which under
|
||||
Linux reads the /proc/net/dev file. Under freebsd/openbsd and darwin the plugin
|
||||
uses netstat.
|
||||
|
||||
Additionally, for the time being _only under Linux_, the plugin gathers system
|
||||
wide stats for different network protocols using /proc/net/snmp (tcp, udp, icmp,
|
||||
etc.). Explanation of the different metrics exposed by snmp is out of the scope
|
||||
of this document. The best way to find information would be tracing the
|
||||
constants in the [Linux kernel source][source] and their usage. If
|
||||
/proc/net/snmp cannot be read for some reason, telegraf ignores the error
|
||||
silently.
|
||||
|
||||
[source]: https://elixir.bootlin.com/linux/latest/source/net/ipv4/proc.c
|
||||
|
||||
Tags:
|
||||
|
||||
* Net measurements have the following tags:
|
||||
* interface (the interface from which metrics are gathered)
|
||||
|
||||
Under Linux the system wide protocol metrics have the interface=all tag.
|
||||
|
||||
## Example Output
|
||||
|
||||
All platforms provide metrics like the following:
|
||||
|
||||
```text
|
||||
net,interface=eth0,host=HOST bytes_sent=451838509i,bytes_recv=3284081640i,packets_sent=2663590i,packets_recv=3585442i,err_in=0i,err_out=0i,drop_in=4i,drop_out=0i 1492834180000000000
|
||||
```
|
||||
|
||||
On Linux additional metrics might be provided:
|
||||
|
||||
```text
|
||||
net,interface=eth0,host=HOST bytes_sent=451838509i,bytes_recv=3284081640i,packets_sent=2663590i,packets_recv=3585442i,err_in=0i,err_out=0i,drop_in=4i,drop_out=0i 1492834180000000000
|
||||
net,interface=all,host=HOST ip_reasmfails=0i,icmp_insrcquenchs=0i,icmp_outtimestamps=0i,ip_inhdrerrors=0i,ip_inunknownprotos=0i,icmp_intimeexcds=10i,icmp_outaddrmasks=0i,icmp_indestunreachs=11005i,icmpmsg_outtype0=6i,tcp_retranssegs=14669i,udplite_outdatagrams=0i,ip_reasmtimeout=0i,ip_outnoroutes=2577i,ip_inaddrerrors=186i,icmp_outaddrmaskreps=0i,tcp_incsumerrors=0i,tcp_activeopens=55965i,ip_reasmoks=0i,icmp_inechos=6i,icmp_outdestunreachs=9417i,ip_reasmreqds=0i,icmp_outtimestampreps=0i,tcp_rtoalgorithm=1i,icmpmsg_intype3=11005i,icmpmsg_outtype69=129i,tcp_outsegs=2777459i,udplite_rcvbuferrors=0i,ip_fragoks=0i,icmp_inmsgs=13398i,icmp_outerrors=0i,tcp_outrsts=14951i,udplite_noports=0i,icmp_outmsgs=11517i,icmp_outechoreps=6i,icmpmsg_intype11=10i,icmp_inparmprobs=0i,ip_forwdatagrams=0i,icmp_inechoreps=1909i,icmp_outredirects=0i,icmp_intimestampreps=0i,icmpmsg_intype5=468i,tcp_rtomax=120000i,tcp_maxconn=-1i,ip_fragcreates=0i,ip_fragfails=0i,icmp_inredirects=468i,icmp_outtimeexcds=0i,icmp_outechos=1965i,icmp_inaddrmasks=0i,tcp_inerrs=389i,tcp_rtomin=200i,ip_defaultttl=64i,ip_outrequests=3366408i,ip_forwarding=2i,udp_incsumerrors=0i,udp_indatagrams=522136i,udplite_incsumerrors=0i,ip_outdiscards=871i,icmp_inerrors=958i,icmp_outsrcquenchs=0i,icmpmsg_intype0=1909i,tcp_insegs=3580226i,udp_outdatagrams=577265i,udp_rcvbuferrors=0i,udplite_sndbuferrors=0i,icmp_incsumerrors=0i,icmp_outparmprobs=0i,icmpmsg_outtype3=9417i,tcp_attemptfails=2652i,udplite_inerrors=0i,udplite_indatagrams=0i,ip_inreceives=4172969i,icmpmsg_outtype8=1965i,tcp_currestab=59i,udp_noports=5961i,ip_indelivers=4099279i,ip_indiscards=0i,tcp_estabresets=5818i,udp_sndbuferrors=3i,icmp_intimestamps=0i,icmpmsg_intype8=6i,udp_inerrors=0i,icmp_inaddrmaskreps=0i,tcp_passiveopens=452i 1492831540000000000
|
||||
```
|
163
plugins/inputs/net/net.go
Normal file
163
plugins/inputs/net/net.go
Normal file
|
@ -0,0 +1,163 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package net
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/config"
|
||||
"github.com/influxdata/telegraf/filter"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
"github.com/influxdata/telegraf/plugins/common/psutil"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type Net struct {
|
||||
Interfaces []string `toml:"interfaces"`
|
||||
IgnoreProtocolStats bool `toml:"ignore_protocol_stats"`
|
||||
|
||||
filter filter.Filter
|
||||
ps psutil.PS
|
||||
skipChecks bool
|
||||
}
|
||||
|
||||
func (*Net) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (n *Net) Init() error {
|
||||
if !n.IgnoreProtocolStats {
|
||||
config.PrintOptionValueDeprecationNotice("inputs.net", "ignore_protocol_stats", "false",
|
||||
telegraf.DeprecationInfo{
|
||||
Since: "1.27.3",
|
||||
RemovalIn: "1.36.0",
|
||||
Notice: "use the 'inputs.nstat' plugin instead for protocol stats",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 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") != ""
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Net) Gather(acc telegraf.Accumulator) error {
|
||||
netio, err := n.ps.NetIO()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting net io info: %w", err)
|
||||
}
|
||||
|
||||
if n.filter == nil {
|
||||
if n.filter, err = filter.Compile(n.Interfaces); err != nil {
|
||||
return fmt.Errorf("error compiling filter: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting list of interfaces: %w", err)
|
||||
}
|
||||
interfacesByName := make(map[string]net.Interface, len(interfaces))
|
||||
for _, iface := range interfaces {
|
||||
interfacesByName[iface.Name] = iface
|
||||
}
|
||||
|
||||
for _, io := range netio {
|
||||
if len(n.Interfaces) != 0 {
|
||||
var found bool
|
||||
|
||||
if n.filter.Match(io.Name) {
|
||||
found = true
|
||||
}
|
||||
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
} else if !n.skipChecks {
|
||||
iface, ok := interfacesByName[io.Name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if iface.Flags&net.FlagLoopback == net.FlagLoopback {
|
||||
continue
|
||||
}
|
||||
|
||||
if iface.Flags&net.FlagUp == 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
tags := map[string]string{
|
||||
"interface": io.Name,
|
||||
}
|
||||
|
||||
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,
|
||||
"speed": getInterfaceSpeed(io.Name),
|
||||
}
|
||||
acc.AddCounter("net", fields, tags)
|
||||
}
|
||||
|
||||
// Get system wide stats for different network protocols
|
||||
// (ignore these stats if the call fails)
|
||||
if !n.IgnoreProtocolStats {
|
||||
//nolint:errcheck // stats ignored on fail
|
||||
netprotos, _ := n.ps.NetProto()
|
||||
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
|
||||
}
|
||||
}
|
||||
tags := map[string]string{
|
||||
"interface": "all",
|
||||
}
|
||||
acc.AddFields("net", fields, tags)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the interface speed from /sys/class/net/*/speed file. returns -1 if unsupported
|
||||
func getInterfaceSpeed(ioName string) int64 {
|
||||
sysPath := internal.GetSysPath()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("net", func() telegraf.Input {
|
||||
return &Net{ps: psutil.NewSystemPS()}
|
||||
})
|
||||
}
|
216
plugins/inputs/net/net_test.go
Normal file
216
plugins/inputs/net/net_test.go
Normal file
|
@ -0,0 +1,216 @@
|
|||
package net
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/net"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/metric"
|
||||
"github.com/influxdata/telegraf/plugins/common/psutil"
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
func TestNetIOStats(t *testing.T) {
|
||||
var mps psutil.MockPS
|
||||
defer mps.AssertExpectations(t)
|
||||
|
||||
netio := net.IOCountersStat{
|
||||
Name: "eth0",
|
||||
BytesSent: 1123,
|
||||
BytesRecv: 8734422,
|
||||
PacketsSent: 781,
|
||||
PacketsRecv: 23456,
|
||||
Errin: 832,
|
||||
Errout: 8,
|
||||
Dropin: 7,
|
||||
Dropout: 1,
|
||||
}
|
||||
|
||||
mps.On("NetIO").Return([]net.IOCountersStat{netio}, nil)
|
||||
|
||||
netprotos := []net.ProtoCountersStat{
|
||||
{
|
||||
Protocol: "Udp",
|
||||
Stats: map[string]int64{
|
||||
"InDatagrams": 4655,
|
||||
"NoPorts": 892592,
|
||||
},
|
||||
},
|
||||
}
|
||||
mps.On("NetProto").Return(netprotos, nil)
|
||||
|
||||
t.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))
|
||||
|
||||
plugin := &Net{ps: &mps, skipChecks: true}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"net",
|
||||
map[string]string{"interface": "eth0"},
|
||||
map[string]interface{}{
|
||||
"bytes_sent": uint64(1123),
|
||||
"bytes_recv": uint64(8734422),
|
||||
"packets_sent": uint64(781),
|
||||
"packets_recv": uint64(23456),
|
||||
"err_in": uint64(832),
|
||||
"err_out": uint64(8),
|
||||
"drop_in": uint64(7),
|
||||
"drop_out": uint64(1),
|
||||
"speed": int64(100),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
telegraf.Counter,
|
||||
),
|
||||
metric.New(
|
||||
"net",
|
||||
map[string]string{"interface": "all"},
|
||||
map[string]interface{}{
|
||||
"udp_noports": int64(892592),
|
||||
"udp_indatagrams": int64(4655),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
||||
}
|
||||
|
||||
func TestNetIOStatsSpeedUnsupported(t *testing.T) {
|
||||
var mps psutil.MockPS
|
||||
defer mps.AssertExpectations(t)
|
||||
|
||||
netio := net.IOCountersStat{
|
||||
Name: "eth1",
|
||||
BytesSent: 1123,
|
||||
BytesRecv: 8734422,
|
||||
PacketsSent: 781,
|
||||
PacketsRecv: 23456,
|
||||
Errin: 832,
|
||||
Errout: 8,
|
||||
Dropin: 7,
|
||||
Dropout: 1,
|
||||
}
|
||||
|
||||
mps.On("NetIO").Return([]net.IOCountersStat{netio}, nil)
|
||||
|
||||
netprotos := []net.ProtoCountersStat{
|
||||
{
|
||||
Protocol: "Udp",
|
||||
Stats: map[string]int64{
|
||||
"InDatagrams": 4655,
|
||||
"NoPorts": 892592,
|
||||
},
|
||||
},
|
||||
}
|
||||
mps.On("NetProto").Return(netprotos, nil)
|
||||
|
||||
t.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))
|
||||
|
||||
plugin := &Net{ps: &mps, skipChecks: true}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"net",
|
||||
map[string]string{"interface": "eth1"},
|
||||
map[string]interface{}{
|
||||
"bytes_sent": uint64(1123),
|
||||
"bytes_recv": uint64(8734422),
|
||||
"packets_sent": uint64(781),
|
||||
"packets_recv": uint64(23456),
|
||||
"err_in": uint64(832),
|
||||
"err_out": uint64(8),
|
||||
"drop_in": uint64(7),
|
||||
"drop_out": uint64(1),
|
||||
"speed": int64(-1),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
telegraf.Counter,
|
||||
),
|
||||
metric.New(
|
||||
"net",
|
||||
map[string]string{"interface": "all"},
|
||||
map[string]interface{}{
|
||||
"udp_noports": int64(892592),
|
||||
"udp_indatagrams": int64(4655),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
||||
}
|
||||
|
||||
func TestNetIOStatsNoSpeedFile(t *testing.T) {
|
||||
var mps psutil.MockPS
|
||||
defer mps.AssertExpectations(t)
|
||||
|
||||
netio := net.IOCountersStat{
|
||||
Name: "eth2",
|
||||
BytesSent: 1123,
|
||||
BytesRecv: 8734422,
|
||||
PacketsSent: 781,
|
||||
PacketsRecv: 23456,
|
||||
Errin: 832,
|
||||
Errout: 8,
|
||||
Dropin: 7,
|
||||
Dropout: 1,
|
||||
}
|
||||
|
||||
mps.On("NetIO").Return([]net.IOCountersStat{netio}, nil)
|
||||
|
||||
netprotos := []net.ProtoCountersStat{
|
||||
{
|
||||
Protocol: "Udp",
|
||||
Stats: map[string]int64{
|
||||
"InDatagrams": 4655,
|
||||
"NoPorts": 892592,
|
||||
},
|
||||
},
|
||||
}
|
||||
mps.On("NetProto").Return(netprotos, nil)
|
||||
|
||||
t.Setenv("HOST_SYS", filepath.Join("testdata", "general", "sys"))
|
||||
|
||||
plugin := &Net{ps: &mps, skipChecks: true}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
require.NoError(t, plugin.Gather(&acc))
|
||||
|
||||
expected := []telegraf.Metric{
|
||||
metric.New(
|
||||
"net",
|
||||
map[string]string{"interface": "eth2"},
|
||||
map[string]interface{}{
|
||||
"bytes_sent": uint64(1123),
|
||||
"bytes_recv": uint64(8734422),
|
||||
"packets_sent": uint64(781),
|
||||
"packets_recv": uint64(23456),
|
||||
"err_in": uint64(832),
|
||||
"err_out": uint64(8),
|
||||
"drop_in": uint64(7),
|
||||
"drop_out": uint64(1),
|
||||
"speed": int64(-1),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
telegraf.Counter,
|
||||
),
|
||||
metric.New(
|
||||
"net",
|
||||
map[string]string{"interface": "all"},
|
||||
map[string]interface{}{
|
||||
"udp_noports": int64(892592),
|
||||
"udp_indatagrams": int64(4655),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
}
|
||||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
||||
}
|
15
plugins/inputs/net/sample.conf
Normal file
15
plugins/inputs/net/sample.conf
Normal file
|
@ -0,0 +1,15 @@
|
|||
# Gather metrics about network interfaces
|
||||
[[inputs.net]]
|
||||
## By default, telegraf gathers stats from any up interface (excluding loopback)
|
||||
## Setting interfaces will tell it to gather these explicit interfaces,
|
||||
## regardless of status. When specifying an interface, glob-style
|
||||
## patterns are also supported.
|
||||
# interfaces = ["eth*", "enp0s[0-1]", "lo"]
|
||||
|
||||
## On linux systems telegraf also collects protocol stats.
|
||||
## Setting ignore_protocol_stats to true will skip reporting of protocol metrics.
|
||||
##
|
||||
## DEPRECATION NOTICE: A value of 'false' is deprecated and discouraged!
|
||||
## Please set this to `true` and use the 'inputs.nstat'
|
||||
## plugin instead.
|
||||
# ignore_protocol_stats = false
|
1
plugins/inputs/net/testdata/general/sys/class/net/eth0/speed
vendored
Normal file
1
plugins/inputs/net/testdata/general/sys/class/net/eth0/speed
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
100
|
1
plugins/inputs/net/testdata/general/sys/class/net/eth1/speed
vendored
Normal file
1
plugins/inputs/net/testdata/general/sys/class/net/eth1/speed
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
-1
|
Loading…
Add table
Add a link
Reference in a new issue