Search code examples
python-2.7snmpdminimalmodbus

snmpd pass to run python


Im trying to query a modbus device trough snmp using snmpd to pass a python script to retrieve data.

#! /bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.4.1.52612.10.3.1
echo string
python /usr/local/bin/readvolt.py
fi
exit 0

And this is the readvolt.py looks like :

#!/usr/bin/python
import minimalmodbus
eqp = minimalmodbus.Instrument('/dev/ttyUSB0',1) # port name, slave address (in decimal)
# skip some other lines for serial port initialization
volt = eqp.read_float(0,4,2) # getting data from serial modbus
print volt

and this line from my snmpd.conf :

pass .1.3.6.1.4.1.52612.10.3.1 /bin/sh /usr/local/bin/volt.sh

my question : I got traceback from python, couldnot find minimalmodbus module, but when i tried to run the readvolt.py from directly from host, it is working as expected (it can print out the result (volt) )

pi@raspberrypi:/usr/local/bin $ readvolt.py
220.25

I also tried using simple python script (test.py) just to make sure if snmpd pass can run python script on respond of snmpget from snmp manager

#!/usr/bin/python
import sys
print "test"

It run OK :

suryo@r50e:~$ snmpwalk -v2c -c public 192.168.1.5 .1.3.6.1.4.1.52612.10.3.1
iso.3.6.1.4.1.52612.10.3.1 = STRING: "test"
suryo@r50e:~$ 

what is the problem here ? seems that python could not import external module when it is run by snmpd pass. I'm thinking if this is an access control issue, Debian-snmp doesnt have right to access serial port..


Solution

  • Problem is solved, by finding out the username of the snmpd daemon. I put whoami in the script and got 'Debian-snmp', then becoming straight forward, checking group membership by running :

    pi@rraspberrypi:~$ groups Debian-snmp
    Debian-snmp : Debian-snmp
    

    Put Debian-snmp in the dialout membership to grant full access to serial ports:

    pi@raspberrypi:~ $ sudo usermod -a  -G dialout Debian-snmp
    

    Restart the snmpd to logon with new membership, and voilla..It can read the slave modbus device from snmp command /snmpget