I would like to get the status of some of the GPIO pins and report then via an SNMP agent running on Debian Squeeze. I'm not sure where to start on this, can anyone get me going in the right direction?
Thanks.
Re: Report GPIO pins by SNMP
Here is an example of writing and reading GPIO pins from a shell script:
http://elinux.org/Rpi_Low-leve....._script.29
http://elinux.org/Rpi_Low-leve....._script.29
Re: Report GPIO pins by SNMP
Thank you, jbeale. I'm not an expert on shell scripts (understatement) but I think that script is just reading and writing values to files which sets the GPIO pins. So that part is fine. Now, how can I get the SNMP agent to read the files and report against an IOD when it is queried by an SNMP manager?
Thanks.
Thanks.
Re: Report GPIO pins by SNMP
Write a Python script rather than a shell script. All the "heavy lifting" is done for you with Python modules.
http://pypi.python.org/pypi/pysnmp/4.2.2
http://pypi.python.org/pypi/RP.....GPIO/0.1.0
Disclaimer: I have never used SNMP and don"t even know what it is
http://pypi.python.org/pypi/pysnmp/4.2.2
http://pypi.python.org/pypi/RP.....GPIO/0.1.0
Disclaimer: I have never used SNMP and don"t even know what it is
Re: Report GPIO pins by SNMP
croston said:
Disclaimer: I have never used SNMP and don"t even know what it is
http://en.wikipedia.org/wiki/S.....t_Protocol
Disclaimer: I have never used SNMP and don"t even know what it is
http://en.wikipedia.org/wiki/S.....t_Protocol
Re: Report GPIO pins by SNMP
Writing a script to implement an SNMP agent is kind of a pain in the ass, parsing the OID and providing the correct response in a shell script is pretty awkward. There"s a good template for pysnmp though, so I"d start there http://pysnmp.sourceforge.net/.....mdrsp.html edit actually this example seems to create a network agent, not a shell script.
Then once you"ve got a script that follows the snmp agent "protocol" (see man 5 snmpd.conf section on the pass directive) you can add it to snmpd.conf: (an example I wrote to expose mail stats – it's pretty sloppy and based on someone else's work at log parsing, but I can show you the source if you like)
pass .1.3.6.1.4.1.2021.255 /usr/local/bin/fetch_mail_statistics.pl /var/log/mail.log /var/log/mailstats.db .1.3.6.1.4.1.2021.255
Then once you"ve got a script that follows the snmp agent "protocol" (see man 5 snmpd.conf section on the pass directive) you can add it to snmpd.conf: (an example I wrote to expose mail stats – it's pretty sloppy and based on someone else's work at log parsing, but I can show you the source if you like)
pass .1.3.6.1.4.1.2021.255 /usr/local/bin/fetch_mail_statistics.pl /var/log/mail.log /var/log/mailstats.db .1.3.6.1.4.1.2021.255
Re: Report GPIO pins by SNMP
Thanks for your reply error404. Unfortunately, I'm slightly more confused than I was before I read your post!
I don't want to implement an agent, I want to run snmpd as the agent but make it so I can query or send traps for the status of the GPIO pins. If I understand you, I can write a script or program that follows the snmp agent protcol, then add to the snmpd.conf file a pass directive that enables the data generated by the script to be reported by snmpd. Is that correct?
What is the snmp agent "protocol" exactly? Do I just write out the relevant data to a file in a certain format?
Thanks for you help.
I don't want to implement an agent, I want to run snmpd as the agent but make it so I can query or send traps for the status of the GPIO pins. If I understand you, I can write a script or program that follows the snmp agent protcol, then add to the snmpd.conf file a pass directive that enables the data generated by the script to be reported by snmpd. Is that correct?
What is the snmp agent "protocol" exactly? Do I just write out the relevant data to a file in a certain format?
Thanks for you help.
Re: Report GPIO pins by SNMP
Sorry, my reply was a little scattered, I should have rewritten it after I collected my thoughts.
snmpd allows you to pass certain OIDs to an external script; when they're queried the script is executed and returns the value. The interface is pretty simple, but implementing the 'next OID' functionality can be a pain in the ass depending on how you want to structure your data.
The interface for the script is described in snmpd.conf. I quote:
pass [-p priority] MIBOID PROG
will pass control of the subtree rooted at MIBOID to the specified PROG command. GET and GETNEXT requests for OIDs within this tree will trigger this command, called as:PROG -g OIDPROG -n OIDrespectively, where OID is the requested OID. The PROG command should return the response varbind as three separate lines printed to stdout – the first line should be the OID of the returned value, the second should be its TYPE (one of the text strings integer, gauge, counter, timeticks, ipaddress, objectid, or string ), and the third should be the value itself.If the command cannot return an appropriate varbind – e.g the specified OID did not correspond to a valid instance for a GET request, or there were no following instances for a GETNEXT – then it should exit without producing any output. This will result in an SNMP noSuchName error, or a noSuchInstanceexception.Note:The SMIv2 type counter64 and SNMPv2 noSuchObject exception are not supported.A SET request will result in the command being called as:PROG -s OID TYPE VALUEwhere TYPE is one of the tokens listed above, indicating the type of the value passed as the third parameter.If the assignment is successful, the PROG command should exit without producing any output. Errors should be indicated by writing one of the strings not-writable, or wrong-typeto stdout, and the agent will generate the appropriate error response.Note:The other SNMPv2 errors are not supported.In either case, the command should exit once it has finished processing. Each request (and each varbind within a single request) will trigger a separate invocation of the command.The default registration priority is 127. This can be changed by supplying the optional -p flag, with lower priority registrations being used in preference to higher priority values.
There's also pass_persist which has a similar interface, but leaves the script running and passes the query in on stdin.
I'm not sure about traps.
snmpd allows you to pass certain OIDs to an external script; when they're queried the script is executed and returns the value. The interface is pretty simple, but implementing the 'next OID' functionality can be a pain in the ass depending on how you want to structure your data.
The interface for the script is described in snmpd.conf. I quote:
pass [-p priority] MIBOID PROG
will pass control of the subtree rooted at MIBOID to the specified PROG command. GET and GETNEXT requests for OIDs within this tree will trigger this command, called as:PROG -g OIDPROG -n OIDrespectively, where OID is the requested OID. The PROG command should return the response varbind as three separate lines printed to stdout – the first line should be the OID of the returned value, the second should be its TYPE (one of the text strings integer, gauge, counter, timeticks, ipaddress, objectid, or string ), and the third should be the value itself.If the command cannot return an appropriate varbind – e.g the specified OID did not correspond to a valid instance for a GET request, or there were no following instances for a GETNEXT – then it should exit without producing any output. This will result in an SNMP noSuchName error, or a noSuchInstanceexception.Note:The SMIv2 type counter64 and SNMPv2 noSuchObject exception are not supported.A SET request will result in the command being called as:PROG -s OID TYPE VALUEwhere TYPE is one of the tokens listed above, indicating the type of the value passed as the third parameter.If the assignment is successful, the PROG command should exit without producing any output. Errors should be indicated by writing one of the strings not-writable, or wrong-typeto stdout, and the agent will generate the appropriate error response.Note:The other SNMPv2 errors are not supported.In either case, the command should exit once it has finished processing. Each request (and each varbind within a single request) will trigger a separate invocation of the command.The default registration priority is 127. This can be changed by supplying the optional -p flag, with lower priority registrations being used in preference to higher priority values.
There's also pass_persist which has a similar interface, but leaves the script running and passes the query in on stdin.
I'm not sure about traps.
-
- Posts: 2
- Joined: Mon Sep 17, 2012 11:48 pm
Re: Report GPIO pins by SNMP
hi,
I'm an snmp person (been working on/with snmp for a long time, now).
I plan to get the current version of net-snmp and try it on my own rpi board. I have not tried doing subagent devel for a while, but it wasn't quite so bad as it was 20 yrs ago..
there is a 'mibs for dummies' (mfd) api interface that isn't too bad. I think its good enough to be used to model the gpio pins.
to send traps, you need a local daemon that polls or gets change-event notifications and then generates snmp traps based on whatever mib you want to implement. btw, what mib do you think you would use for gpio pin i/o? if there isn't one, I could throw something together, I think.
/bryan
I'm an snmp person (been working on/with snmp for a long time, now).
I plan to get the current version of net-snmp and try it on my own rpi board. I have not tried doing subagent devel for a while, but it wasn't quite so bad as it was 20 yrs ago..
there is a 'mibs for dummies' (mfd) api interface that isn't too bad. I think its good enough to be used to model the gpio pins.
to send traps, you need a local daemon that polls or gets change-event notifications and then generates snmp traps based on whatever mib you want to implement. btw, what mib do you think you would use for gpio pin i/o? if there isn't one, I could throw something together, I think.
/bryan
Re: Report GPIO pins by SNMP
Has anyone created an SNMP MIB for the RPi? I am starting a SNMP project and would like to use the official one if it available.
Re: Report GPIO pins by SNMP
Bump
Has anyone created an SNMP MIB for the RPi?
Has anyone created an SNMP MIB for the RPi?
Re: Report GPIO pins by SNMP
Might want to take a look at http://www.raspberrypi.org/phpBB3/viewt ... 36&t=34354 

Find more info on Raspberry Pi, Virtualization and all things cloudy on my blog: http://niston.wordpress.com
Re: Report GPIO pins by SNMP
Hi @ all,
I want to read a GPIO-pin of a RaspberryPi via snmp. I wrote a script in python to read the pin. For testing purposes I delivered the value 17 and 99.
/usr/local/bin/snmp-gpio17.py
This script is called by /usr/local/bin/snmp-gpio17.sh
In /etc/snmp/snmpd.conf I added the line
When I run the script on the RasPi it works:
root@raspberrypi:~# /usr/local/bin/snmp-gpio17.sh -g
.1.3.6.1.4.1.18565.1.12.101.1.17
gauge
17
but when I try to read the OID (locally or over the network) I get:
karls@mintbox ~ $ snmpget -v1 -c public 192.168.129.4 .1.3.6.1.4.1.18565.1.12.101.1.17
iso.3.6.1.4.1.18565.1.12.101.1.17 = Gauge32: 0
I also set up measurement of the cpu-temperature via snmp with another pass-line and it works fine. I use nearly the same script:
So the snmp-communication seems to be OK (I compared answers with wireshark), but snmpd on the RasPi gets wrong information from the bash-script.
Anyone have an idea?
Regards, Karl
I want to read a GPIO-pin of a RaspberryPi via snmp. I wrote a script in python to read the pin. For testing purposes I delivered the value 17 and 99.
/usr/local/bin/snmp-gpio17.py
Code: Select all
#!/usr/bin/python3 -u
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if not GPIO.input(17):
var=17
else:
var=99
print ( var )
Code: Select all
#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.4.1.18565.1.12.101.1.17
echo gauge
python /usr/local/bin/snmp-gpio17.py
fi
exit 0
Code: Select all
pass .1.3.6.1.4.1.18565.1.12.101.1.17 /bin/sh /usr/local/bin/snmp-gpio17.sh
root@raspberrypi:~# /usr/local/bin/snmp-gpio17.sh -g
.1.3.6.1.4.1.18565.1.12.101.1.17
gauge
17
but when I try to read the OID (locally or over the network) I get:
karls@mintbox ~ $ snmpget -v1 -c public 192.168.129.4 .1.3.6.1.4.1.18565.1.12.101.1.17
iso.3.6.1.4.1.18565.1.12.101.1.17 = Gauge32: 0
I also set up measurement of the cpu-temperature via snmp with another pass-line and it works fine. I use nearly the same script:
Code: Select all
#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.4.1.18565.1.12.101.1.101
echo gauge
cat /sys/class/thermal/thermal_zone0/temp
fi
exit 0
Anyone have an idea?
Regards, Karl
Re: Report GPIO pins by SNMP
After tinkering around a while, I am pretty sure that the user "snmp", who runs snmpd, has not the right to use the gpio-commands in python.
When I try to fetch a value via snmp from a remote or the local machine, the shell-script runs, the python script is called and runs also, but the gpio-information cannot be retrieved by
Therefore I put the user "snmp" in group "gpio" (along with user "pi") - but no luck 
Regards, Karl
When I try to fetch a value via snmp from a remote or the local machine, the shell-script runs, the python script is called and runs also, but the gpio-information cannot be retrieved by
Code: Select all
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if not GPIO.input(17)

Regards, Karl
Re: Report GPIO pins by SNMP
[solved] It works now, even if I remove user snmp from group gpio.
My problem in testing was, that I did not wait between two tests. I found out, that snmpd on the RasPi caches the result of a get for 30 seconds. Only if you wait that long, you get the new result! Thanks to all
My problem in testing was, that I did not wait between two tests. I found out, that snmpd on the RasPi caches the result of a get for 30 seconds. Only if you wait that long, you get the new result! Thanks to all
Re: Report GPIO pins by SNMP
Hi Karls,
I have been trying to do it but I receive the same response (Gauge=0). The sh py scripts works fine. If I put fix values instead of calling python for querying pi, it also works fine. The temperature examples also works fine.
Do you still be able to run the code and snmpget? Any idea?
Thanks in advanced,
Mariano.
I have been trying to do it but I receive the same response (Gauge=0). The sh py scripts works fine. If I put fix values instead of calling python for querying pi, it also works fine. The temperature examples also works fine.
Do you still be able to run the code and snmpget? Any idea?
Thanks in advanced,
Mariano.
Re: Report GPIO pins by SNMP
Hi Mariano,
have you tried to run your scripts as user "snmp"? Usually this user has no shell, so she cannot execute scripts. If this is the case, you have to add a shell in /etc/shadow
Hope this helps.
Best regards,
Karl
have you tried to run your scripts as user "snmp"? Usually this user has no shell, so she cannot execute scripts. If this is the case, you have to add a shell in /etc/shadow
Hope this helps.
Best regards,
Karl
Re: Report GPIO pins by SNMP
Hi Karsl, I have configured a shell to snmp user and given sudo privileges to this user. I have also added sudo prefix into .sh script to run python with privileges. Now everything works fine.
Thank you so much.
Mariano.
Thank you so much.
Mariano.
-
- Posts: 2
- Joined: Fri May 06, 2016 1:57 pm
Re: Report GPIO pins by SNMP
Hi, well monitoring is interesting and works fine.
Do you have any examples how I can control the GPIO via SNMP?
I tried your scripts and instead of "-g" for a snmpget I add an if-loop with "-s" for snmpset,
but without any result. The system said all is fine, but in real I can't switch them remotely!
Also the GPIO-value doesn't change.
Do you have any other experiences to switch a GPIO via snmp remotely?
Do you have any examples how I can control the GPIO via SNMP?
I tried your scripts and instead of "-g" for a snmpget I add an if-loop with "-s" for snmpset,
but without any result. The system said all is fine, but in real I can't switch them remotely!
Also the GPIO-value doesn't change.
Do you have any other experiences to switch a GPIO via snmp remotely?
Re: Report GPIO pins by SNMP
Sorry for answering late 
No, I don´t have any further experience, but I am interested in controlling GPIO-pins via SNMP too. If you find a way, please let me know. I will work on this topic later, since I have to fix my home-network (esp. the router) first.
Best, Karl

No, I don´t have any further experience, but I am interested in controlling GPIO-pins via SNMP too. If you find a way, please let me know. I will work on this topic later, since I have to fix my home-network (esp. the router) first.
Best, Karl
Re: Report GPIO pins by SNMP
Hello,
I am also interested in controling GPIO and change some custom files of RPi with SNMP. If someone can, could you please share it with us?
I am also interested in controling GPIO and change some custom files of RPi with SNMP. If someone can, could you please share it with us?
-
- Posts: 5
- Joined: Mon Mar 05, 2018 3:36 pm
Re: Report GPIO pins by SNMP
Hi All,
Am also trying to monitor GPIO using SNMP and have followed all the steps above but still get zero as in Mariano's post above.
I think its got something to do with the user access rights.
I have created an "snmp" user and given him sudo rights but still a zero on the snmp query.
The bash code is as below (GetStatus01):
#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.2.1.25.1.9.0
echo gauge
sudo python3 /home/pi/myFlask/GetStatus01.py
fi
exit 0
The output of bash script is ok as below:
pi@raspberrypi:~ $ /home/pi/myFlask/GetStatus_01 -g
.1.3.6.1.2.1.25.1.9.0
gauge
1
"GetStatus01.py" prints a "0" or "1" based on the gpio pin status
The file snmpd.conf has been modified as below:
pass .1.3.6.1.2.1.25.1.8.0 /bin/sh /home/pi/snmp-temp -g
pass .1.3.6.1.2.1.25.1.9.0 /bin/sh /home/pi/myFlask/GetStatus_01 -g
The "snmp-temp -g" points to the pi temp code above and this works ok when snmp data is requested by an external program
But "GetStatus_01 -g" always presents a zero result.
Am I missing something?
Vogue
Am also trying to monitor GPIO using SNMP and have followed all the steps above but still get zero as in Mariano's post above.
I think its got something to do with the user access rights.
I have created an "snmp" user and given him sudo rights but still a zero on the snmp query.
The bash code is as below (GetStatus01):
#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.2.1.25.1.9.0
echo gauge
sudo python3 /home/pi/myFlask/GetStatus01.py
fi
exit 0
The output of bash script is ok as below:
pi@raspberrypi:~ $ /home/pi/myFlask/GetStatus_01 -g
.1.3.6.1.2.1.25.1.9.0
gauge
1
"GetStatus01.py" prints a "0" or "1" based on the gpio pin status
The file snmpd.conf has been modified as below:
pass .1.3.6.1.2.1.25.1.8.0 /bin/sh /home/pi/snmp-temp -g
pass .1.3.6.1.2.1.25.1.9.0 /bin/sh /home/pi/myFlask/GetStatus_01 -g
The "snmp-temp -g" points to the pi temp code above and this works ok when snmp data is requested by an external program
But "GetStatus_01 -g" always presents a zero result.
Am I missing something?
Vogue
-
- Posts: 2
- Joined: Fri May 06, 2016 1:57 pm
Re: Report GPIO pins by SNMP
Hi,
the -g isn't necessary in the snmpd.conf (take away the "-g" from the pass-line).
Because the -g will be transfered from the snmpget-command. Then it will works.
Cheers
the -g isn't necessary in the snmpd.conf (take away the "-g" from the pass-line).
Because the -g will be transfered from the snmpget-command. Then it will works.
Cheers
voguehomes wrote: ↑Mon Mar 05, 2018 6:12 pmHi All,
Am also trying to monitor GPIO using SNMP and have followed all the steps above but still get zero as in Mariano's post above.
I think its got something to do with the user access rights.
I have created an "snmp" user and given him sudo rights but still a zero on the snmp query.
The bash code is as below (GetStatus01):
#!/bin/bash
if [ "$1" = "-g" ]
then
echo .1.3.6.1.2.1.25.1.9.0
echo gauge
sudo python3 /home/pi/myFlask/GetStatus01.py
fi
exit 0
The output of bash script is ok as below:
pi@raspberrypi:~ $ /home/pi/myFlask/GetStatus_01 -g
.1.3.6.1.2.1.25.1.9.0
gauge
1
"GetStatus01.py" prints a "0" or "1" based on the gpio pin status
The file snmpd.conf has been modified as below:
pass .1.3.6.1.2.1.25.1.8.0 /bin/sh /home/pi/snmp-temp -g
pass .1.3.6.1.2.1.25.1.9.0 /bin/sh /home/pi/myFlask/GetStatus_01 -g
The "snmp-temp -g" points to the pi temp code above and this works ok when snmp data is requested by an external program
But "GetStatus_01 -g" always presents a zero result.
Am I missing something?
Vogue
Re: Report GPIO pins by SNMP
Hi,
i do it like karls0 but i get
i add a user snmp with a password
i add him to root gorup, sudo and gpio
i login as snmp an run snmp-gpio17.sh -g (works fine)
i try chmod +x snmp-gpio17.sh so it works on snmp without bash
but nothing change only 0
The CPU Temp work fine. One the raspi and remote mashine.
Can anybody help mel?
i do it like karls0 but i get
Code: Select all
iso.3.6.1.4.1.18565.1.12.101.1.17 = Gauge32: 0
i add him to root gorup, sudo and gpio
i login as snmp an run snmp-gpio17.sh -g (works fine)
i try chmod +x snmp-gpio17.sh so it works on snmp without bash
but nothing change only 0
The CPU Temp work fine. One the raspi and remote mashine.
Can anybody help mel?