SNMP is a powerful tool that can help you better monitor and get alerts for essential networked devices from printers to storage arrays.
In this Node-RED SNMP tutorial, we’ll cover how to monitor the ink levels of printers and be alerted when the ink levels fall below a defined threshold. Thanks to a Node-RED flow, we will be able to setups the SNMP triggers for new printers without user intervention.
As the example in this tutorial, I chose an example of monitoring printer ink levels, however, the very same approach can be easily applied and used in many different scenarios including:
- Disks utilization
- And many others
Introduction to automating SNMP monitoring with Node-RED
In order to monitor the ink levels on a printer you’ll need to set up different SNMP sensors, one for each of the colors of our printer and add an alert when one of these values falls below a specific threshold. For instance, we want to receive an email when the black ink falls below a 10% capacity.
The Printer-MIB Management Information Base (MIB) is essential for this process.
What is a MIB?
A MIB is a hierarchical collection where each entry is addressed through an object identifier called OID. Each OID is a managed object containing a specific value. For instance, there is an OID which lets us retrieve various information; the temperature of the CPU, the disk utilization, the disk capacity, and also others to get information about the ink levels. Learn more about finding SNMP OIDs.
Many MIBs have been defined, some are standard and some are vendor defined. Here, we’re concentrating on the Printer-MIB described in the very same MIB as “The MIB module for management of printers”.
This standard MIB is supported by the majority of printers. In this MIB the following OIDs are defined:
- prtMarkerColorantValue: The name of the color of this colorant
- prtMarkerSuppliesLevel: The current level of this supply
Manually add SNMP Sensors on new devices
The common approach to monitor these OIDs is to manually add the required sensors every time a new printer is added to one of your monitored networks. This process requires several manual actions from you. We need to know when a new printer is attached to a network, which can be difficult to find out if you’re monitoring many networks. In addition, search for the new device and manually add the required sensors. Also, the list of sensors to be added may be large.
Domotz helps in this process because it autonomously discovers all the MIBs supported by each of your devices. For instance, if you navigate to the “Add SNMP Sensor” functionality it opens the description of the Printer-MIB, as well as all the other MIBs supported by the device. From this view, it is easy to manually identify the OIDs where we need to add the sensors like the prtMarkerSuppliesLevel OID of the prtMarkerSuppliesTable.
Automatically add SNMP Sensors on new devices
In this SNMP node-RED tutorial we will set up a mechanism to automatically add all the sensors needed without the intervention of an operator. This approach has many advantages because it does not require: (i) to know where and when a new printer is added to the network, (ii) to repeat adding sensors manually every time a new printer is added to a network.
The ingredients needed for automatic SNMP sensor alerting are as follows:
- A configured printer which the template can be exported from
- Domotz Public API, templates, and webhook notifications
The idea is to choose one of our already configured printers to be the “template” from which we copy the configuration for the new printers. This printer must already be configured with all the SNMP sensors we want to add to all the printers. Next, we can export the Domotz Template from the already configured printer. We leverage the Domotz Public API to add to the new printers the very same sensors and triggers saved in the template every time we receive a webhook notification regarding the discovery of a new printer supporting the Printer-MIB. In this tutorial we also use Node-RED to perform the Domotz API calls automatically.
Set up SNMP sensors on a printer
Before starting to automate the addition of sensors and triggers we must generate a template containing the sensors we want to add.
In order to export the template of a printer, it is enough to navigate to the device details of the printer that’s already been configured and export the configuration from the “Export Device Setting and Properties” button. You’ll get a JSON file containing all the configurations of the device including the SNMP Sensors and Triggers.
Now, we are ready to start setting up the mechanism to automatically add all the SNMP sensors and triggers to our new printers. We will cover all the necessary steps while building a Node-RED flow.
Set up the Node-RED flow
For the installation instructions of Node-RED please refer to the official guide. You can install the Domotz node from the ‘manage palette’ menu of Node-RED. Just search for node-red-contrib-domotz and install it.
In this flow we have a composition of several Node-RED concepts already introduced in previous tutorials:
The general idea of this flow is the following:
- Load the template for the Printer-MIB and store the sensors and triggers
- Receive the webhook relative to the discovery of a new MIB in a device
- Check if the device supports the Printer-MIB (if the device supports the Printer-MIB we continue the flow otherwise we stop the processing of the message)
- Create all the sensors and triggers contained in the template for the new device
In the following, we describe all the steps required for the flow.
First of all, we can load the previously saved Domotz template that contains the sensors and triggers that we must add, moreover we convert the content to JSON. For these two operations we exploit the ‘file in’ node and the ‘json’ node. The former loads the content of a file and requires the path to the template we want to load, the latter converts the loaded content in the JSON format.
Once the template has been loaded, we can save its content with the use of a function node. The content is saved inside the “flow” variable. Node-RED provides a way to store information that can be shared between different nodes without using the messages that pass through a flow. This is called a Node-RED context. Thanks to this, in the subsequent nodes we can refer to the template information stored in the flow context. In addition, thanks to the context we can load the template just once instead of loading it every time we receive a notification.
Now, we can concentrate on receiving notifications. For this aspect, we need the “http in” node in order to create the web-server that will receive the webhooks. This is the very same set-up described in details in a previous blog post on Webhook management with Node-RED.
The http-in node is a receiver node, any HTTP client expects a response so we need to create also an http response node. Domotz Webhook service expects a response with code 201 and an empty body, hence we can directly attach the http response node to the output of the http in node.
We can now subscribe to the Feature Discovery events setting the configured http-in node as the receiver of the webhooks. This can be accomplished from the Domotz Shared Alerts section where we can add the http-in receiver address to the webhooks able to receive all these events.
When we receive a notification, the payload contains useful information that will be exploited in our flow:
- The agent_id and device_id of the notification. These values are required to add the sensors and triggers to the notified device
- A list containing all the new MIBs that Domotz discovered to be supported by the device having device_id value
Now we need to understand if the device received in the webhook supports the Printer-MIB. If the device does not support the Printer-MIB we stop the processing of the webhook. If it does support the Printer-MIBwe can continue with the subsequent nodes. In order to check if the device supports the Printer-MIB we can analyze the content of the webhook with a Switch Node. With this node we can select multiple properties that need to be present in the content in order to permit the continuation of the analysis. In our case, it is enough to check if the content of the webhook contains the Printer-MIB name inside the mib collection of the payload.
Now, we must exploit the Domotz API to add the sensors and triggers contained in the template. We need:
- A function node to extract all the Domotz sensors from the loaded template
- A sequence split node to generate a different Node-RED message for each sensor contained in the template (in this way the message processed in the flow will become multiple messages, one for each sensor of the template)
- A function node to prepare the payload to add the sensor according to the Domotz API call createEyeSNMP
- A Domotz node to perform the call to the Domotz API
To extract the sensors contained in the template we can access the oid_sensors variable contained in the previously saved context and set all the sensors as the new msg.payload.
In addition, we set the message data with the agent_id and device_id that must be propagated to the subsequent nodes. All these operations can be performed thanks to a function node.
The next step is to split the above message in many messages, one for each sensor contained in the template. For this operation, we make use of the “split node”. The default split node is enough for our use case because it is splitting one message in multiple messages dividing the array contained in the msg.payload value.
The output of the split node will be many messages, one for each sensor. Next, we can use a function node to prepare the payload similar to the payload required in the create EyeSNMP Domotz API call documentation.
Finally, we can perform the call to the Domotz API to add each sensor using the Domotz node provided by the node-red-contrib-domotz package for Node-RED previously installed. It is enough to select the createEyeSNMP function from the API drop down list and set the “Use Node Input Params” in the parameters property. This configuration uses the input to the node to construct the payload to be sent to Domotz. The API’s URL is enriched with the params property (i.e. agent_id and device_id), instead the body payload is constructed from the body previously set in the function node.
The steps to add the triggers are analogous to the ones to create a sensor. We first extract the triggers from the template, and for each trigger we prepare the payload and call the createEyeSNMPTrigger API call.
Your Node-RED SNMP Automation waits for MIBs
In conclusion, the flow we built is now waiting for a webhook notification regarding the discovery of new MIBs in our devices. Every time we find that one of our devices supports the Printer-MIB we let the message process on all the nodes of the flow.
What we obtain in the end is the device enriched with all the sensors and triggers we would like for a device supporting the Printer-MIB. This will permit you to receive all the required alerts when the ink levels fall below the defined threshold.