I have written a python script that prints out metrics so that I can add them to an influxdb. The script works as my local user. It also works as the telegraf
user.
However, the script fails when it runs as part of 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'
...
If I run this as the telegraf user, though, it works as expected:
$ 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"
...
The script is owned and executable by 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*
My telegraf.conf file points to the appropriate location:
[[inputs.exec]]
commands = [
"/usr/lib/telegraf/custom-plugins/polltest.py"
]
timeout = "5s"
data_format = "influx"
Finally, the script itself if pretty basic:
#!/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)
What do I need to change so that this script is executed by telegraf without the permissions error?