文档文档

Socket Listener 输入插件

此服务插件侦听套接字(TCP、UDP、Unix 或 Unixgram)上的消息,并以支持的 数据格式之一解析接收到的数据包。

引入于: Telegraf v1.3.0 标签: network 操作系统支持: all

服务输入

此插件是服务输入。普通插件收集由 interval 设置确定的指标。服务插件启动一个服务来监听并等待指标或事件发生。服务插件与普通插件的两个主要区别是:

  1. 全局或插件特定的 interval 设置可能不适用
  2. --test--test-wait--once 的 CLI 选项可能不会为此插件生成输出

全局配置选项

插件支持其他全局和插件配置设置,用于修改指标、标签和字段,创建别名以及配置插件顺序等任务。更多详情请参阅 CONFIGURATION.md

配置

# Generic socket listener capable of handling multiple socket types.
[[inputs.socket_listener]]
  ## URL to listen on
  # service_address = "tcp://:8094"
  # service_address = "tcp://127.0.0.1:http"
  # service_address = "tcp4://:8094"
  # service_address = "tcp6://:8094"
  # service_address = "tcp6://[2001:db8::1]:8094"
  # service_address = "udp://:8094"
  # service_address = "udp4://:8094"
  # service_address = "udp6://:8094"
  # service_address = "unix:///tmp/telegraf.sock"
  # service_address = "unixgram:///tmp/telegraf.sock"
  # service_address = "vsock://cid:port"

  ## Permission for unix sockets (only available on unix sockets)
  ## This setting may not be respected by some platforms. To safely restrict
  ## permissions it is recommended to place the socket into a previously
  ## created directory with the desired permissions.
  ##   ex: socket_mode = "777"
  # socket_mode = ""

  ## Maximum number of concurrent connections (only available on stream sockets like TCP)
  ## Zero means unlimited.
  # max_connections = 0

  ## Read timeout (only available on stream sockets like TCP)
  ## Zero means unlimited.
  # read_timeout = "0s"

  ## Optional TLS configuration (only available on stream sockets like TCP)
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key  = "/etc/telegraf/key.pem"
  ## Enables client authentication if set.
  # tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]

  ## Maximum socket buffer size (in bytes when no unit specified)
  ## For stream sockets, once the buffer fills up, the sender will start
  ## backing up. For datagram sockets, once the buffer fills up, metrics will
  ## start dropping. Defaults to the OS default.
  # read_buffer_size = "64KiB"

  ## Period between keep alive probes (only applies to TCP sockets)
  ## Zero disables keep alive probes. Defaults to the OS configuration.
  # keep_alive_period = "5m"

  ## Content encoding for message payloads
  ## Can be set to "gzip" for compressed payloads or "identity" for no encoding.
  # content_encoding = "identity"

  ## Maximum size of decoded packet (in bytes when no unit specified)
  # max_decompression_size = "500MB"

  ## List of allowed source IP addresses for incoming packets/messages.
  ## If not specified or empty, all sources are allowed.
  # allowed_sources = []

  ## Message splitting strategy and corresponding settings for stream sockets
  ## (tcp, tcp4, tcp6, unix or unixpacket). The setting is ignored for packet
  ## listeners such as udp.
  ## Available strategies are:
  ##   newline         -- split at newlines (default)
  ##   null            -- split at null bytes
  ##   delimiter       -- split at delimiter byte-sequence in hex-format
  ##                      given in `splitting_delimiter`
  ##   fixed length    -- split after number of bytes given in `splitting_length`
  ##   variable length -- split depending on length information received in the
  ##                      data. The length field information is specified in
  ##                      `splitting_length_field`.
  # splitting_strategy = "newline"

  ## Delimiter used to split received data to messages consumed by the parser.
  ## The delimiter is a hex byte-sequence marking the end of a message
  ## e.g. "0x0D0A", "x0d0a" or "0d0a" marks a Windows line-break (CR LF).
  ## The value is case-insensitive and can be specified with "0x" or "x" prefix
  ## or without.
  ## Note: This setting is only used for splitting_strategy = "delimiter".
  # splitting_delimiter = ""

  ## Fixed length of a message in bytes.
  ## Note: This setting is only used for splitting_strategy = "fixed length".
  # splitting_length = 0

  ## Specification of the length field contained in the data to split messages
  ## with variable length. The specification contains the following fields:
  ##  offset        -- start of length field in bytes from begin of data
  ##  bytes         -- length of length field in bytes
  ##  endianness    -- endianness of the value, either "be" for big endian or
  ##                   "le" for little endian
  ##  header_length -- total length of header to be skipped when passing
  ##                   data on to the parser. If zero (default), the header
  ##                   is passed on to the parser together with the message.
  ## Note: This setting is only used for splitting_strategy = "variable length".
  # splitting_length_field = {offset = 0, bytes = 0, endianness = "be", header_length = 0}

  ## Data format to consume.
  ## Each data format has its own unique set of configuration options, read
  ## more about them here:
  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
  # data_format = "influx"

操作系统 UDP 缓冲区大小

read_buffer_size 配置选项可用于调整套接字缓冲区的大小,但此数字受操作系统设置的限制。在 Linux 上,read_buffer_size 将默认为 rmem_default,并受 rmem_max 的限制。在 BSD 系统上,read_buffer_sizemaxsockbuf 的限制,并且没有操作系统默认设置。

以下提供了有关如何调整这些操作系统设置的说明。

某些操作系统(最值得注意的是 Linux)对 UDP 协议的性能设置了非常严格的限制。强烈建议您在尝试向您的实例发送大量 UDP 流量之前,将这些操作系统限制提高到至少 8MB。8MB 仅为建议值,可以调高。

Linux

使用以下命令检查当前的 UDP/IP 接收缓冲区限制和默认值

sysctl net.core.rmem_max
sysctl net.core.rmem_default

如果值小于 8388608 字节,您应该将以下行添加到 /etc/sysctl.conf 文件中

net.core.rmem_max=8388608
net.core.rmem_default=8388608

/etc/sysctl.conf 的更改在重启之前不会生效。要立即更新值,请以 root 用户身份键入以下命令

sysctl -w net.core.rmem_max=8388608
sysctl -w net.core.rmem_default=8388608

BSD/Darwin

在 BSD/Darwin 系统上,您需要为内核限制套接字缓冲区增加约 15% 的填充。这意味着如果您想要一个 8MB 的缓冲区(8388608 字节),您需要将内核限制设置为 8388608*1.15 = 9646900。这在任何地方都没有记录,但可以在 内核源代码中看到。

键入以下命令检查当前的 UDP/IP 缓冲区限制

sysctl kern.ipc.maxsockbuf

如果值小于 9646900 字节,您应该将以下行添加到 /etc/sysctl.conf 文件中(如果需要,请创建它)

kern.ipc.maxsockbuf=9646900

/etc/sysctl.conf 的更改在重启之前不会生效。要立即更新值,请以 root 用户身份键入以下命令

sysctl -w kern.ipc.maxsockbuf=9646900

Metrics

该插件接受任意输入,并根据 data_format 设置进行解析。没有预定义的指标格式。

示例输出

没有预定义的指标格式,因此输出取决于插件输入。


此页面是否有帮助?

感谢您的反馈!


InfluxDB 3.8 新特性

InfluxDB 3.8 和 InfluxDB 3 Explorer 1.6 的主要增强功能。

查看博客文章

InfluxDB 3.8 现已适用于 Core 和 Enterprise 版本,同时发布了 InfluxDB 3 Explorer UI 的 1.6 版本。本次发布着重于操作成熟度,以及如何更轻松地部署、管理和可靠地运行 InfluxDB。

更多信息,请查看

InfluxDB Docker 的 latest 标签将指向 InfluxDB 3 Core

在 **2026 年 2 月 3 日**,InfluxDB Docker 镜像的 latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。

如果使用 Docker 来安装和运行 InfluxDB,latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。例如,如果使用 Docker 运行 InfluxDB v2,请将 latest 版本标签替换为 Docker pull 命令中的特定版本标签 — 例如

docker pull influxdb:2