1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,81 @@
# Network Connection Statistics Input Plugin
This plugin collects statistics about TCP connection states and UDP socket
counts.
⭐ Telegraf v0.2.0
🏷️ 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
# Read TCP metrics such as established, time wait and sockets counts.
[[inputs.netstat]]
# no configuration
```
## Metrics
Supported TCP Connection states are follows.
- established
- syn_sent
- syn_recv
- fin_wait1
- fin_wait2
- time_wait
- close
- close_wait
- last_ack
- listen
- closing
- none
### TCP Connection State measurements
Meta:
- units: counts
Measurement names:
- tcp_established
- tcp_syn_sent
- tcp_syn_recv
- tcp_fin_wait1
- tcp_fin_wait2
- tcp_time_wait
- tcp_close
- tcp_close_wait
- tcp_last_ack
- tcp_listen
- tcp_closing
- tcp_none
If there are no connection on the state, the metric is not counted.
### UDP socket counts measurements
Meta:
- units: counts
Measurement names:
- udp_socket
## Example Output
```text
netstat tcp_close=0i,tcp_close_wait=0i,tcp_closing=0i,tcp_established=14i,tcp_fin_wait1=0i,tcp_fin_wait2=0i,tcp_last_ack=0i,tcp_listen=1i,tcp_none=46i,tcp_syn_recv=0i,tcp_syn_sent=0i,tcp_time_wait=0i,udp_socket=10i 1668520568000000000
```

View file

@ -0,0 +1,71 @@
//go:generate ../../../tools/readme_config_includer/generator
package netstat
import (
_ "embed"
"fmt"
"syscall"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/psutil"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type NetStat struct {
ps psutil.PS
}
func (*NetStat) SampleConfig() string {
return sampleConfig
}
func (ns *NetStat) Gather(acc telegraf.Accumulator) error {
netconns, err := ns.ps.NetConnections()
if err != nil {
return fmt.Errorf("error getting net connections info: %w", err)
}
counts := make(map[string]int)
counts["UDP"] = 0
// TODO: add family to tags or else
tags := make(map[string]string)
for _, netcon := range netconns {
if netcon.Type == syscall.SOCK_DGRAM {
counts["UDP"]++
continue // UDP has no status
}
c, ok := counts[netcon.Status]
if !ok {
counts[netcon.Status] = 0
}
counts[netcon.Status] = c + 1
}
fields := map[string]interface{}{
"tcp_established": counts["ESTABLISHED"],
"tcp_syn_sent": counts["SYN_SENT"],
"tcp_syn_recv": counts["SYN_RECV"],
"tcp_fin_wait1": counts["FIN_WAIT1"],
"tcp_fin_wait2": counts["FIN_WAIT2"],
"tcp_time_wait": counts["TIME_WAIT"],
"tcp_close": counts["CLOSE"],
"tcp_close_wait": counts["CLOSE_WAIT"],
"tcp_last_ack": counts["LAST_ACK"],
"tcp_listen": counts["LISTEN"],
"tcp_closing": counts["CLOSING"],
"tcp_none": counts["NONE"],
"udp_socket": counts["UDP"],
}
acc.AddFields("netstat", fields, tags)
return nil
}
func init() {
inputs.Add("netstat", func() telegraf.Input {
return &NetStat{ps: psutil.NewSystemPS()}
})
}

View file

@ -0,0 +1,66 @@
package netstat
import (
"syscall"
"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 TestNetStats(t *testing.T) {
var mps psutil.MockPS
defer mps.AssertExpectations(t)
mps.On("NetConnections").Return([]net.ConnectionStat{
{
Type: syscall.SOCK_DGRAM,
},
{
Status: "ESTABLISHED",
},
{
Status: "ESTABLISHED",
},
{
Status: "CLOSE",
},
}, nil)
var acc testutil.Accumulator
require.NoError(t, (&NetStat{ps: &mps}).Gather(&acc))
expected := []telegraf.Metric{
metric.New(
"netstat",
map[string]string{},
map[string]interface{}{
"tcp_established": 2,
"tcp_syn_sent": 0,
"tcp_syn_recv": 0,
"tcp_fin_wait1": 0,
"tcp_fin_wait2": 0,
"tcp_time_wait": 0,
"tcp_close": 1,
"tcp_close_wait": 0,
"tcp_last_ack": 0,
"tcp_listen": 0,
"tcp_closing": 0,
"tcp_none": 0,
"udp_socket": 1,
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t,
expected,
acc.GetTelegrafMetrics(),
testutil.IgnoreTime(),
)
}

View file

@ -0,0 +1,3 @@
# Read TCP metrics such as established, time wait and sockets counts.
[[inputs.netstat]]
# no configuration