Sign in to follow this  

Extending SNMP with results from a Python Script

Recommended Posts

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()

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/

After a restart of the SNMP Daemon, you can see the actual output by walking .

$ !snmpwalk .
Walking OID . from host=, version=v2c, port=161, timeout=3 seconds:
	1.0 => 1 => /usr/bin/python2.7 => 4 => 1 => /home/pi/scripts/weather/ => => 5 => 1 => 1 => 47.3 => 47.3
79.0 => 2 => 0 => 47.3 => 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

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


Share this post

Link to post
Share on other sites
Guest Bobby

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 ))


      print ('Failed to get reading')



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

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

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

Share this post

Link to post
Share on other sites
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this