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

标签 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 执行此脚本而不会出现权限错误?

最佳答案

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

关于python - 尝试使用 input.exec 插件时权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55424516/

相关文章:

docker - Telegraf 无法连接到 Docker sock

Telegram Bot API : How to get MIME type of received photos

linux - 如何使用 telegraf procstat 插件获取进程名称和进程状态(运行或停止)

python - Pandas 根据另一列中的值应用基础值

python - Plotly 3D 绘图注释

python - 在 Python 中解析大型 XML 文档的最快方法是什么?

python - 如何在 Python 中实现类似于 Javascript 回调的功能

python - 如何解决 Telegraf 中的此错误?

ruby-on-rails - Ruby on Rails : getting Prometheus metrics with Telegraf

json - 如何在 Telegraf 中过滤 JSON 响应