尝试使用input.exec插件时权限被拒绝

时间:2019-03-29 19:41:47

标签: python telegraf telegraf-inputs-plugin telegraf-plugins

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

但是,脚本作为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执行此脚本而不会出现权限错误?

1 个答案:

答案 0 :(得分:0)

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