1

我编写了一个打印出指标的 python 脚本,以便我可以将它们添加到 influxdb。该脚本作为我的本地用户工作。它也可以作为telegraf用户工作。

但是,该脚本在作为电报的一部分运行时会失败:

$ /usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d --test
2019-03-29T19:21:48Z I! Starting Telegraf 1.10.1
2019-03-29T19:21:48Z E! [inputs.exec]: Error in plugin: exec: fork/exec /usr/lib/telegraf/custom-plugins/polltest.py: permission denied for command '/usr/lib/telegraf/custom-plugins/polltest.py'
...

但是,如果我以 telegraf 用户身份运行它,它会按预期工作:

$ sudo -u telegraf /usr/lib/telegraf/custom-plugins/polltest.py
ups,hostname=ups1 battery.charge=100
ups,hostname=ups1 battery.runtime=2622
ups,hostname=ups1 battery.type="PbAC"
...

该脚本由 telegraf 拥有和执行:

$ ls -la /usr/lib/telegraf/custom-plugins/
total 12
drwxr-xr-x 2 root     root     4096 Mar 29 14:15 ./
drwxr-xr-x 4 root     root     4096 Mar 29 14:06 ../
-rwxr--r-- 1 telegraf telegraf  906 Mar 29 14:15 polltest.py*

我的 telegraf.conf 文件指向适当的位置:

[[inputs.exec]]
  commands = [
     "/usr/lib/telegraf/custom-plugins/polltest.py"
  ]
  timeout = "5s"
  data_format = "influx"

最后,脚本本身非常基本:

#!/usr/bin/python3

import subprocess

p1 = subprocess.Popen(["/bin/upsc", "tripplite"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, encoding="utf8")

for result in p1.stdout:
      key, value = result.split(":")
      measurement = "ups"
      tags = "hostname=ups1"
      value = value.strip()
      # Check if our value is a number. If it's not, surround it in quotes. 
      # Don't actually use the float() value, as some numbers are returns as 
      # valid integers
      try:
              _ = float(value)
              field = f'{key}={value}'
      except ValueError:
              field = f'{key}="{value}"'
      influx_line_protocol = f"{measurement},{tags} {field}"
      print(influx_line_protocol)

我需要更改什么以便 Telegraf 执行此脚本而不会出现权限错误?

4

1 回答 1

0

在第一次尝试中,您以自己的无权限用户身份运行 telegraf。尝试使用systemctl start telegraf.

于 2019-03-31T13:00:58.600 回答