文档文档

Exec 输入插件

此插件在每个间隔执行给定的 commands,并以支持的 数据格式之一解析其输出中的指标。此插件可用于轮询任何来源的自定义指标。

引入版本: Telegraf v0.1.5 标签: system 操作系统支持: all

全局配置选项

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

配置

# Read metrics from one or more commands that can output to stdout
[[inputs.exec]]
  ## Commands array
  commands = []

  ## Environment variables
  ## Array of "key=value" pairs to pass as environment variables
  ## e.g. "KEY=value", "USERNAME=John Doe",
  ## "LD_LIBRARY_PATH=/opt/custom/lib64:/usr/local/libs"
  # environment = []

  ## Timeout for each command to complete.
  # timeout = "5s"

  ## Measurement name suffix
  ## Used for separating different commands
  # name_suffix = ""

  ## Ignore Error Code
  ## If set to true, a non-zero error code in not considered an error and the
  ## plugin will continue to parse the output.
  # ignore_error = false

  ## Data format
  ## By default, exec expects JSON. This was done for historical reasons and is
  ## different than other inputs that use the influx line protocol. 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 = "json"

command 选项中的全局模式会在每次运行时进行匹配,因此添加匹配该模式的新脚本将立即生效。

示例

此脚本生成静态值,由于未指定时间戳,因此值对应于当前时间。请确保整数值后跟 i 以便正确解析。

#!/bin/sh
echo 'example,tag1=a,tag2=b i=42i,j=43i,k=44i'

它可以与以下配置配对,并将在代理的 interval 间隔内运行。

[[inputs.exec]]
  commands = ["sh /tmp/test.sh"]
  timeout = "5s"
  data_format = "influx"

常见问题

我的脚本手动运行时有效,但在 Telegraf 作为服务运行时无效

这可能与 Telegraf 服务以不同的用户身份运行有关。在 Linux 系统上,官方软件包以 telegraf 用户和组运行 Telegraf。

在 Windows PowerShell 上,脚本的输出似乎被截断了

您可能需要在脚本中设置一个变量来增加输出列的可用数量

$host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(1024,50)

Metrics

示例输出


此页面是否有帮助?

感谢您的反馈!


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