I am trying to collect temperature and humidity data from my raspberry pi. The telegraf log keeps spitting out the following error:
ERROR in input [exec]: Errors encountered: [exec: exit status 1 for command '/opt/temp.py']
I am able to run the script standalone and get the exit status 0. I tried running telegraf in debug mode but it produces the same error.
The response data from the script returns pretty much immediately and is definitely under the defined timeout value.
[agent]
interval = "10s"
round_interval = false
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "1s"
flush_interval = "10s"
flush_jitter = "0s"
precision = "ms"
[[inputs.exec]]
commands = ["/opt/temp.py"]
timeout = "9s"
data_format = "influx"
Telegraf (version 1.0.0)
Linux rpi 4.4.21-v7+ #911 SMP Thu Sep 15 14:22:38 BST 2016 armv7l GNU/Linux
/var/log/telegraf.log
2016/10/07 04:02:45 Starting Telegraf (version 1.0.0)
2016/10/07 04:02:45 Loaded outputs: influxdb
2016/10/07 04:02:45 Loaded inputs: cpu disk diskio kernel mem processes system ping exec net nstat
2016/10/07 04:02:45 Tags enabled: host=rpi
2016/10/07 04:02:45 Agent Config: Interval:20s, Debug:false, Quiet:false, Hostname:"rpi", Flush Interval:10s
2016/10/07 04:03:00 ERROR in input [exec]: Errors encountered: [exec: exit status 1 for command '/opt/temp.py']
2016/10/07 04:03:10 Output [influxdb] buffer fullness: 13 / 10000 metrics. Total gathered metrics: 13. Total dropped metrics: 0.
2016/10/07 04:03:12 Output [influxdb] wrote batch of 13 metrics in 12.768427ms
2016/10/07 04:03:20 ERROR in input [exec]: Errors encountered: [exec: exit status 1 for command '/opt/temp.py']
2016/10/07 04:03:20 Output [influxdb] buffer fullness: 18 / 10000 metrics. Total gathered metrics: 31. Total dropped metrics: 0.
2016/10/07 04:03:20 Output [influxdb] wrote batch of 18 metrics in 12.039733ms
2016/10/07 04:03:30 Output [influxdb] buffer fullness: 0 / 10000 metrics. Total gathered metrics: 31. Total dropped metrics: 0.
2016/10/07 04:03:40 ERROR in input [exec]: Errors encountered: [exec: exit status 1 for command '/opt/temp.py']
Permissions
-rwxrwxrwx 1 root root 395 Oct 7 04:05 temp.py
temp.py

Any assistance would be greatly appreciated. I am loving Influxdb and Telegraf!
Hi @roycepope, I see that you've set the correct permissions to execute the binary, but I wonder if there are still permissions issues with some of the operations in the script.
What happens when you run the script as the telegraf user? try sudo -u telegraf /opt/temp.py
Thanks @sparrc I think this is definitely a permission issue.
sudo -u telegraf /opt/temp.py
Traceback (most recent call last):
File "/opt/temp.py", line 12, in <module>
result = instance.read()
File "/opt/dht11.py", line 34, in read
RPi.GPIO.setup(self.__pin, RPi.GPIO.OUT)
RuntimeError: No access to /dev/mem. Try running as root!
I also encountered the same problem, how did you solve it?
@wangmin19861205 I ran the script as root
I added telegraf user to sudo and root groups:
sudo usermod -aG sudo telegraf and sudo usermod -aG root telegraf,
and I added telegraf ALL=(ALL) NOPASSWD: ALL to /etc/sudoers,
Then I run my command as sudo.
Adding the user telegraf to the root group is not a good idea.
I came across the same problem when using RaspberryPi with GrovePi, it turns out that I had only to add the user telegraf to the i2c group:
usermod -a -G i2c telegraf
In dealing with the exec plugin it might be useful to grant necessary privileges using file ACLs, for example:
# change owner/group of input script to telegraf, and grant group telegraf read-execute
chown telegraf:telegraf /usr/local/bin/collect_data.py
setfacl -m g:telegraf:rx /usr/local/bin/collect_data.py
In addition consider what privileges your collection script might need. If you are parsing a log file under /var/log you will likely need to do the following:
setfacl -m g:telegraf:r /var/log/my.log
If this log is getting rotated by logrotate process, don't forget to add the ACL grant in the rotate script:
# /etc/logrotate.d/mylog
{
size 1M
rotate 10
create 0640 root root
sharedscripts
postrotate
setfacl -m g:telegraf:r /var/log/my.log
endscript
}
I came here from a Google search and fixed a similar error on my Raspberry Pi 4 with sudo usermod -G video telegraf
vim /usr/lib/systemd/system/telegraf.service锛宼hen change "User=telegraf" to "User=root" can solve the problem
For me the fix was changing /etc/passwd telegraf user shell from /bin/false to /bin/bash
Most helpful comment
Adding the user telegraf to the root group is not a good idea.
I came across the same problem when using RaspberryPi with GrovePi, it turns out that I had only to add the user telegraf to the i2c group:
usermod -a -G i2c telegraf