telegraf 和脚本命令发送到 grafana

telegraf and script command to send to grafana

我的 telegraf.conf 中有以下输入到 Grafana。我可以让更简单的前 3 个示例工作,但无法让第 4 个示例工作。

[[inputs.exec]]
commands = ["sh -c 'grep -i DatasetVersion /etc/aaa/systemnameX/configfile | cut -d'=' -f2'"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "dataset_version"

[[inputs.exec]]
commands = ["sh -c 'cat /root/config/OS_Version.txt | tr -d C'"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "os_version"

[[inputs.exec]]
commands = ["sh -c 'grep -i VERSION= /usr/bin/sw_install | head -1 | cut -d'=' -f2 '"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "sw_version"

[[inputs.exec]]
commands = ["/sbin/ifconfig eth0 | grep 'inet' | cut -d: -f2 | awk '{print }'"]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "ip_address"

这是我运行电报测试

时的错误
2021-09-04T06:36:51Z E! Error in plugin [inputs.exec]: exec: exit status 1 for command '/sbin/ifconfig eth0 | grep 'inet' | cut -d: -f2 | awk '{print }''

当我将确切的命令输入命令行时,出现以下错误:

awk: cmd. line:1: {print
awk: cmd. line:1:       ^ unexpected newline or end of string

任何有关如何修复 awk 部分的帮助将不胜感激。

干杯, 塔拉

我已经尝试 运行 您的 /sbin/ifconfig 命令的不同排列和组合,但它也给我同样的错误。但是我已经阅读了 telegraf 手册并提出了以下 approach/steps.

我已经在 Linux 中使用 Telegraf 的 1.19.3 版本对此进行了测试,对我来说效果很好。

步骤:

  • 首先,不要浪费你的时间(直到有人在这里发布答案),届时我们可以根据你在 telegraf inputs.exec 模块中使用的 commands 创建脚本。
  • 所以尽管在 telegraf conf 文件中有 运行 直接命令创建一个脚本(例如:script.bash 在我的测试案例中)并将你的命令放在那里,如下所示:

cat /etc/telegraf/script.bash
#!/bin/bash
/sbin/ifconfig eth0 | grep 'inet' | cut -d: -f2 | awk '{print }'

OR 您还可以将 /sbin/ifconfig 命令更改为以下单个 awk 命令:

/sbin/ifconfig eth0 | awk '/inet/{print }'
  • 现在在您的 conf 文件中输入以下内容:

[[inputs.exec]]
commands = ["/etc/telegraf/script.bash" ]
name_override = "systemnameX"
timeout = "5s"
data_format = "value"
data_type = "string"
[inputs.exec.tags]
type = "ip_address"

  • 重新启动您的 telegraf 服务一次,并通过以下命令测试您的 telegraf 是否运行正常:

sudo -u telegraf telegraf -test -config /etc/telegraf/telegraf.conf

注意: 我已经在 /etc/telegraf/script.bash 中创建了测试脚本,你可以在任何你想创建的地方创建它,但要确保您在 telegraf 的 conf 文件中提供了绝对完整和正确的路径。