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
-
Gnome OS Adopting systemd-sysupdate
Gnome OS is about to undergo a major under-the-hood change that promises enhanced security.
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
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.