Network monitoring and GPIO control with SNMP
Custom GPIO/SNMP Object
A Rasp Pi GPIO pin (pin 18 in this example) is set for read/write access with:
gpio mode 18 output
You can test setting and reading this pin with the gpio read
and gpio write
commands:
gpio read 18 0 grio write 18 1 1
The Net-SNMP agent snmpd
supports the creation of custom read/write objects (OIDs). The pass-through MIB extension command in snmpd.conf
allows you to call script files. Pass-through script files need to follow a few rules:
- An
snmpget
request passes a-g
parameter. - The
snmpget
response needs to be three lines: OID, data type, and value. - An
snmpset
request passes a-s
parameter and value as the fourth item.
Listing 2 is a Bash script file that reads pin 18 on an snmpget
request (-g
), and it will write to pin 18 on an snmpset
(-s
). This script is made executable by entering
Listing 2
powerswitch Script File
chmod +x powerswitch
To enable SNMP calls from the powerswitch
script file, it needs to be referenced in snmpd.conf
. Look for the "Pass-through" section and add a line with the OID and shell you want to use and a path to the script file, such as:
# "Pass-through" MIB extension command # pass .1.3.6.1.2.1.25.1.8 /bin/bash /home/pi/powerswitch
After the snmpd.conf
file is changed, the snmpd
service needs to be restarted. If everything is done correctly, you can use the SNMP command-line routines to test reading and writing to the newly created OID (Listing 3). As a future step you can also give your new custom OID a meaningful MIB name.
Listing 3
Testing SNMP Commands
Node-RED SNMP Set Example
An exec
node can be used to run command-line utilities. Figure 11 shows the logic that sets an SNMP value with an On and Off button. The button
nodes are configured to pass a 1
or 0
payload. The exec
node contains the snmpset
command (Figure 12), which will append the 0
or 1
from the button
node. My final web dashboard (Figure 13) includes a gauge to monitor the output status along with the buttons to control pin 18.
Summary
Network monitoring with the Raspberry Pi can be accomplished in a number of ways. With some basic SNMP configuration on the Rasp Pi, you can make data available to higher level packages or create simple standalone solutions with Node-RED.
A number of other useful GPIO projects are possible with SNMP, as well. For example, I use a PowerSwitch Tail II ($26) [4] power cord that can be enabled and disabled through I/O pins to monitor and control powered devices like lights, PCs, and dehumidifiers from SNMP.
Infos
- NagiosPi: http://everyday-tech.com/nagiospi-server-monitoring-with-the-power-of-pi/
- MRTG: https://oss.oetiker.ch/mrtg/
- Node-RED: https://nodered.org/
- PowerSwitch Tail II: http://www.powerswitchtail.com/
« Previous 1 2
Buy this article as PDF
(incl. VAT)