There are some great full featured networking packages like Nagios and MRTG that can be loaded on Raspberry Pi’s. If, however, you are looking for something smaller scale that you can play with then Node-Red might be your answer. Node-Red is a visual programming environment that allows users to create applications by dragging and dropping blocks (nodes) on the screen. Logic flows are then created by connecting wires between the different blocks (nodes). Node-Red also comes with Web Dashboards, so you can view data or do control from your smart phone.
In this blog we’ll look at:
- running some SNMP commands
- setup NodeRed for SNMP
- making read/write SNMP values on the Pi
Getting Started with SNMP
Simple Network Management Protocol (SNMP) is the standard for communicating and monitoring of network devices. Common device information is grouped into MIBs or Management Information Bases. Data items are called OIDs or Object Identifiers. OIDs are referenced by either their MIB name or by their OID numeric name. So for example the SNMP device name object could be queried by either its MIB name of: SNMPv2-MIB::sysName.0 or the object identifier number of: .22.214.171.124.126.96.36.199.0.
To install both the SNMP monitor and server on your Pi enter:
sudo apt-get update sudo apt-get install snmp snmpd snmp-mibs-downloader
To show meaningful MIB names, you will need to modify the SNMP config file by:
sudo nano /etc/snmp/snmp.conf
The first line should be commented out, it should just read: #mibs .
There are many configuration options in the SNMP server agent that need to be considered. For a real/product system you will need to consider your user security but for a test system we can open up the Pi by:
sudo nano /etc/snmp/snmpd.conf
Then uncomment the agentAddress line so that all interfaces are open, and in the Access Control section comment out all the existing user access and add a new line with public access set to read/write (definitely not recommended in a real system):
# Listen for connections on all interfaces (both IPv4 *and* IPv6) agentAddress udp:161,udp6:[::1]:161 # ACCESS CONTROL # # Set read/write access to public anywhere # rwcommunity public
After saving the changes to snmpd.conf, the service needs to be restarted:
sudo service snmpd restart
There are a number of useful SNMP command line programs, such as:
- snmpget – gets a SNMP message for a specific OID
- snmpset – sets a SNMP OID (OID needs to be writeable)
- snmpwalk – gets multiple OID values in a MIB tree
The basic syntax for these commands is:
command -c community -v version node OID
To test that SNMP is working, enter the following:
pi@raspberrypi:~ $ snmpwalk -c public -v 1 localhost .1.3 SNMPv2-MIB::sysDescr.0 = STRING: Linux raspberrypi 4.4.21-v7+ ... SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10 DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (319234) 0:53:12.34 SNMPv2-MIB::sysContact.0 = STRING: Me <firstname.lastname@example.org> SNMPv2-MIB::sysName.0 = STRING: raspberrypi ...
If SNMP is working correctly a very long list of SNMP objects will be shown.
Get a Specific SNMP Result
There are thousands of SNMP results that can be read. Some of the more common and useful SNMP are:
1 minute CPU Load: .188.8.131.52.4.1.2021.10.1.3.1
5 minute CPU Load: .184.108.40.206.4.1.2021.10.1.3.2
15 minute CPU Load: .220.127.116.11.4.1.2021.10.1.3.3
Idle CPU time (%): .18.104.22.168.4.1.2021.11.11.0
Total RAM in machine: .22.214.171.124.4.1.2021.4.5.0
Total RAM used: .126.96.36.199.4.1.2021.4.6.0
Total RAM Free: .188.8.131.52.4.1.2021.4.11.0
Total disk/partion size(kBytes): .184.108.40.206.4.1.2021.9.1.6.1
Available space on the disk: .220.127.116.11.4.1.2021.9.1.7.1
Used space on the disk: .18.104.22.168.4.1.2021.9.1.8.1
An example to get the Idle CPU time with the SNMP command line tool would be:
$ snmpget -c public -v 1 localhost .22.214.171.124.4.1.2021.11.11.0 UCD-SNMP-MIB::ssCpuIdle.0 = INTEGER: 97
If the syntax is correct, a result is returned with the OID identifier, result type and the result value.
Getting Started with NodeRed
NodeRed is pre-installed with the Raspbian images but it will need to have some SNMP and some support options loaded. At a terminal window enter the commands:
sudo apt-get update sudo apt-get install npm cd $HOME/.node-red npm install node-red-node-snmp npm install node-red-dashboard npm install node-red-contrib-bigtimer node-red-start &
Once Node-RED starts, you use a web browser to build applications. If you are working directly on your Pi, enter 127.0.0.1:1880 in the URL address box of your browser. You drop palettes from the left pane into the large flow window in the middle and wire them together in the correct order.
NodeRed Ping Monitor
A good starting program is to make a Web Dashboard that shows ping (node-to-node) delay times. The dashboards are defined in the right panel of Node-Red. A dashboard items are put into groups, and groups are put into tabs. Each tab will be shown as a separate page on your smart phone.
By double-clicking on the Ping node you can enter the different IP address.
Similarly by double-clicking on the Chart node, you can define the label and look and feel of the chart.
After the configuration is finished, click the Deploy button, (top right on menu bar). The Node-RED dashboard user interface is accessed by entering <IPaddress>:1880/ui (e.g., 192.168.1.102:1880/ui). Chart data values are shown by clicking on the chart line.
NodeRed with SNMP Nodes
The ping node is quite simple and it returns just the ping value. The snmp node is more complex and it returns multiple pieces of information. To use snmp nodes in a Node-Red program you need some support nodes to parse/pass the payload messages. To send SNMP data to a chart dashboard, the following nodes are used:
- Big Timer – to trigger the polling of data
- snmp – gets SNMP/OID information
- split – split the message into addressable variables
- change – put the OID value into the message playload
- chart – show the payload
The SNMP node configuration can get multiple values, a simple example to get the CPU free time is below (Note: the leading “.” is NOT included) :
The split node is used move the SNMP array data in a payload string. The change node is then used to move just the value into the payload.
The final step is to configure the charts to show the correct label information.
An SNMP Readable Pi Value
The Net-SNMP agent (snmpd) supports the creation of custom read/write objects (OIDs). The “Pass-through” MIB extension command in snmpd.conf allows for script files to be called.
Pass-through script files need to follow a few rules:
- snmpget request passes a “-g” parameter (get)
- the snmpget response needs to be 3 lines: OID, data type, and value
- an snmpset request passes a “-s” parameter (set)
A simple example would be to define the Rasperry Pi board temperature as an SNMP object. The board temp is available by:
cat /sys/class/thermal/thermal_zone0/temp 46160
This returns the value in 1/1000s of a degree C. So to get just the Celsius temperature we can use:
echo $(($(cat /sys/class/thermal/thermal_zone0/temp) / 1000)) 46
A SNMP bash script (/home/pi/pitemp), with our Pi CPU temp as OID .126.96.36.199.4.1.8072.2.1 would be:
#!/bin/bash if [ "$1" = "-g" ] then echo .188.8.131.52.4.1.8072.2.1 echo integer echo $(($(cat /sys/class/thermal/thermal_zone0/temp) / 1000)) fi exit 0
After the file is saved remember to make it executable (chmod +x pitemp).
In the “Pass-through” section of /etc/snmp/snmpd.conf a line is added to reference the new OID, shell and command:
# # "Pass-through" MIB extension command # pass .184.108.40.206.2.1.8072.2.1 /bin/bash /home/pi/pitemp
After snmpd.conf is updated the snmpd service needs to restarted (sudo service snmpd restart), then our Pi temp OID can be accessed by:
snmpget -c public -v 1 localhost .220.127.116.11.2.1.8072.2.1 NET-SNMP-EXAMPLES-MIB::netSnmpExampleScalars = INTEGER: 47
Pi Writable SNMP GPIO Value
My goal was to use SNMP to turn on and off powered devices, so for this I used a PowerSwitch Tail II, however simple low cost relays could also be used.
The PowerSwitch Tail II ($26) is a power cord that is enabled/disabled with I/O pins. The PowerSwitch pins connect to the Pi pins 6 and 12.
The gpio tool is used to read and write to GPIO pins. GPIO 1 is made writable by:
gpio mode 1 out
The SNMP script (/home/pi/powerswitch) to read and write to GPIO pin 1 (physical pin 12) is:
#!/bin/bash if [ "$1" = "-g" ] then echo .18.104.22.168.2.1.8072.2.2 echo integer gpio read 1 fi if [ "$1" = "-s" ] then gpio write 1 $4 fi exit 0
This new script file needs to made executable by: chmod +x powerswitch. The powerswitch script file is referenced in the SNMP server configuration file (/etc/snmp/snmpd.conf ):
# # "Pass-through" MIB extension command # pass .22.214.171.124.2.1.8072.2.1 /bin/bash /home/pi/pitemp pass .126.96.36.199.2.1.8072.2.2 /bin/bash /home/pi/powerswitch
Once again the smnpd needs to be restarted. Our read/write actions can be tested by:
$ snmpget -c public -v 1 localhost .188.8.131.52.2.1.8072.2.2 SNMPv2-SMI::mib-2.8072.2.2 = INTEGER: 0 $ snmpset -c public -v 1 localhost .184.108.40.206.2.1.8072.2.2 i 1 SNMPv2-SMI::mib-2.8072.2.2 = INTEGER: 1 $ snmpget -c public -v 1 localhost .220.127.116.11.2.1.8072.2.2 SNMPv2-SMI::mib-2.8072.2.2 = INTEGER: 1
NodeRed Setting SNMP Values
The exec node can be used to call the snmpset command. A example with an ON and OFF button is:
The configuration for the exec node is:
And the web dashboard is:
Network monitoring and SNMP is huge topic, hopefully this will give you a good start.