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)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.