gjs
Posts: 163
Joined: Fri Dec 02, 2011 8:04 pm

Re: Report GPIO pins by SNMP

Mon Apr 30, 2012 10:04 pm

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.

User avatar
jbeale
Posts: 4003
Joined: Tue Nov 22, 2011 11:51 pm

Re: Report GPIO pins by SNMP

Mon Apr 30, 2012 10:51 pm

Here is an example of writing and reading GPIO pins from a shell script:

http://elinux.org/Rpi_Low-leve....._script.29

gjs
Posts: 163
Joined: Fri Dec 02, 2011 8:04 pm

Re: Report GPIO pins by SNMP

Tue May 01, 2012 9:58 am

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.

User avatar
croston
Posts: 723
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool

Re: Report GPIO pins by SNMP

Tue May 01, 2012 10:04 am

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

gjs
Posts: 163
Joined: Fri Dec 02, 2011 8:04 pm

Re: Report GPIO pins by SNMP

Tue May 01, 2012 12:49 pm

croston said:


Disclaimer: I have never used SNMP and don"t even know what it is


http://en.wikipedia.org/wiki/S.....t_Protocol

error404
Posts: 351
Joined: Wed Dec 21, 2011 11:49 pm

Re: Report GPIO pins by SNMP

Tue May 01, 2012 7:40 pm

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

gjs
Posts: 163
Joined: Fri Dec 02, 2011 8:04 pm

Re: Report GPIO pins by SNMP

Thu May 03, 2012 9:39 pm

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.

error404
Posts: 351
Joined: Wed Dec 21, 2011 11:49 pm

Re: Report GPIO pins by SNMP

Fri May 04, 2012 6:47 pm

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.

linux-works
Posts: 2
Joined: Mon Sep 17, 2012 11:48 pm

Re: Report GPIO pins by SNMP

Mon Sep 17, 2012 11:55 pm

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

MrUmunhum
Posts: 34
Joined: Fri Aug 05, 2011 4:37 pm

Re: Report GPIO pins by SNMP

Thu Sep 27, 2012 9:52 pm

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.

UncleDave
Posts: 24
Joined: Tue Feb 21, 2012 6:49 pm

Re: Report GPIO pins by SNMP

Tue Nov 13, 2012 10:45 am

Bump

Has anyone created an SNMP MIB for the RPi?

gridrun
Posts: 46
Joined: Mon Feb 18, 2013 12:26 pm

Re: Report GPIO pins by SNMP

Tue Feb 19, 2013 3:16 pm

Find more info on Raspberry Pi, Virtualization and all things cloudy on my blog: http://niston.wordpress.com

karls0
Posts: 6
Joined: Mon Feb 29, 2016 5:58 pm

Re: Report GPIO pins by SNMP

Mon Feb 29, 2016 6:05 pm

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

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 )
This script is called by /usr/local/bin/snmp-gpio17.sh

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
In /etc/snmp/snmpd.conf I added the line

Code: Select all

pass .1.3.6.1.4.1.18565.1.12.101.1.17 /bin/sh  /usr/local/bin/snmp-gpio17.sh
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:

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

karls0
Posts: 6
Joined: Mon Feb 29, 2016 5:58 pm

Re: Report GPIO pins by SNMP

Tue Mar 01, 2016 5:43 pm

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

Code: Select all

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

if not GPIO.input(17)
Therefore I put the user "snmp" in group "gpio" (along with user "pi") - but no luck :(

Regards, Karl

karls0
Posts: 6
Joined: Mon Feb 29, 2016 5:58 pm

Re: Report GPIO pins by SNMP

Wed Mar 02, 2016 10:12 am

[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

mrabosoma
Posts: 2
Joined: Sun Mar 27, 2016 9:15 am

Re: Report GPIO pins by SNMP

Sun Mar 27, 2016 9:57 am

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.

karls0
Posts: 6
Joined: Mon Feb 29, 2016 5:58 pm

Re: Report GPIO pins by SNMP

Sun Mar 27, 2016 11:17 am

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

mrabosoma
Posts: 2
Joined: Sun Mar 27, 2016 9:15 am

Re: Report GPIO pins by SNMP

Sun Mar 27, 2016 5:08 pm

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.

dykepopeye
Posts: 2
Joined: Fri May 06, 2016 1:57 pm

Re: Report GPIO pins by SNMP

Fri May 06, 2016 2:04 pm

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?

karls0
Posts: 6
Joined: Mon Feb 29, 2016 5:58 pm

Re: Report GPIO pins by SNMP

Sat May 14, 2016 2:40 pm

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

hakao
Posts: 7
Joined: Mon Jan 22, 2018 9:09 am

Re: Report GPIO pins by SNMP

Thu Jan 25, 2018 11:23 am

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?

voguehomes
Posts: 5
Joined: Mon Mar 05, 2018 3:36 pm

Re: Report GPIO pins by SNMP

Mon Mar 05, 2018 6:12 pm

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

dykepopeye
Posts: 2
Joined: Fri May 06, 2016 1:57 pm

Re: Report GPIO pins by SNMP

Tue Jul 17, 2018 1:21 pm

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

voguehomes wrote:
Mon Mar 05, 2018 6:12 pm
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

autowolf
Posts: 1
Joined: Sat Mar 02, 2019 6:32 pm

Re: Report GPIO pins by SNMP

Sat Mar 02, 2019 6:37 pm

Hi,

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

Return to “Other projects”