Extending SNMP with results from a Python Script
I setup a Raspberry Pi with a Sense Hat so I can track the internal Temperature and Humidity of my living room in Logic Monitor. The data is accessable from running a python script on the Raspberry Pi itself, but I didn't want to install a collector on that device. The solution I decided on was using Extend Snmp. This guide can be used as a template for similar logicMonitor task.
I used Steve Francis's "How to teach an old snmpd new tricks" Blog post as a guide.
The first step was creating the python script. This script reads the humidity and temperature readings from the Sense Hat, rounds the results and prints the output to STDOUT.
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
hum = sense.get_humidity()
hum = round(hum,1)
hum= str(hum)
print hum
temp = sense.get_temperature()
temp = ((temp/5)*9)+32
temp = round(temp,1)
temp= str(temp)
print temp
Next I added a line to the to the /etc/snmp/snmpd.conf file to extend SNMP. It basically tells the SNMP daemon to run the python script and include the output in the SNMP extend OIDs.
extend lm-temp /usr/bin/python2.7 /home/pi/scripts/weather/tempature.py
After a restart of the SNMP Daemon, you can see the actual output by walking .1.3.6.1.4.1.8072.1.3.2
$ !snmpwalk 10.73.42.116 .1.3.6.1.4.1.8072.1.3.2
Walking OID .1.3.6.1.4.1.8072.1.3.2 from host=10.73.42.116, version=v2c, port=161, timeout=3 seconds:
1.0 => 1
2.1.2.7.108.109.45.116.101.109.112 => /usr/bin/python2.7
2.1.20.7.108.109.45.116.101.109.112 => 4
2.1.21.7.108.109.45.116.101.109.112 => 1
2.1.3.7.108.109.45.116.101.109.112 => /home/pi/scripts/weather/tempature.py
2.1.4.7.108.109.45.116.101.109.112 =>
2.1.5.7.108.109.45.116.101.109.112 => 5
2.1.6.7.108.109.45.116.101.109.112 => 1
2.1.7.7.108.109.45.116.101.109.112 => 1
3.1.1.7.108.109.45.116.101.109.112 => 47.3
3.1.2.7.108.109.45.116.101.109.112 => 47.3
79.0
3.1.3.7.108.109.45.116.101.109.112 => 2
3.1.4.7.108.109.45.116.101.109.112 => 0
4.1.2.7.108.109.45.116.101.109.112.1 => 47.3
4.1.2.7.108.109.45.116.101.109.112.2 => 79.0
Those last two OIDs is the humidity and Temperature output from my python script.
Putting everything together. The OID for the humidity and temperature reported by my Raspberry Pi are
.1.3.6.1.4.1.8072.1.3.2.4.1.2.7.108.109.45.116.101.109.112.1 and .1.3.6.1.4.1.8072.1.3.2.4.1.2.7.108.109.45.116.101.109.112.2 respectively and can be included into a SNMP datasource as shown below
I did run into some trouble with the SNMP daemon accessing the hardware. Daemons usually run under self-named unprivileged users and I had to add the snmp user to the groups input and gpio so the daemon had access to the hardware. This was easy enough with the command "sudo addgroup snmp gpio" and "sudo addgroup snmp input".