Forum Discussion

Jeff_Woeber's avatar
8 years ago

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".  

 

  • Anonymous's avatar
    Anonymous

    Hi Jeff

    I was wondering if you can assist me in configuring/ extending SNMPv3 on my raspberry pi to get temperature readings from a DHT22 sensor.

    - I have written a python program / script that returns temperature and humidity as follows:

    import sys

    import Adafruit_DHT

    sensor = 22

    pin = 4

    humidity, temprature =Adafruit_DHT.read_retry(sensor, pin)

    if humidity is not None and temprature is not None:

          print ('{0:0f}*  {1:0.1f}%'.format(temperature, humidity ))

    else

          print ('Failed to get reading')

           sys.exit()

     

    - in my snmpd.conf file I have put the line below

    extend-sh test1 /usr/bin/python3  /home/pi/Adafruit_Python_DHT/examples/Final_AdafruitDHT.py

    I tried running snmpwalk but I don't get an OID for the temp and humidity. 

  • Anonymous's avatar
    Anonymous
    On 11/19/2018 at 1:04 AM, Guest Bobby said:

    Hi Jeff

    I was wondering if you can assist me in configuring/ extending SNMPv3 on my raspberry pi to get temperature readings from a DHT22 sensor.

    - I have written a python program / script that returns temperature and humidity as follows:

    import sys

    import Adafruit_DHT

    sensor = 22

    pin = 4

    humidity, temprature =Adafruit_DHT.read_retry(sensor, pin)

    if humidity is not None and temprature is not None:

          print ('{0:0f}*  {1:0.1f}%'.format(temperature, humidity ))

    else

          print ('Failed to get reading')

           sys.exit()

     

    - in my snmpd.conf file I have put the line below

    extend-sh test1 /usr/bin/python3  /home/pi/Adafruit_Python_DHT/examples/Final_AdafruitDHT.py

    I tried running snmpwalk but I don't get an OID for the temp and humidity. 

     

    Your print command has "temperature" whereas your variable is named "temprature"