Node-Red E3: Network monitoring (Your first flow)

In the first 2 episodes in the Node-Red series we’ve seen how to install node red in a correct way. In this episode we’ll upgrade our little demo flow we created to a simple network monitoring tool. So let’s continue where we left of in the first episode.

Requirements

Nodes:

  • node-red-configurable-ping
  • node-red-dashboard
  • node-red-contrib-ui-led
  • node-red-contrib-ui-ping

Install the above using the node-red palette or in a terminal using the npm install command

Pinging your Router

First ting you need to know when monitoring your network is if your router is active cause no router equals no internet. In most cases your router will have an IP address like 192.168.0.1 or 192.168.1.1. In most cases we know the IP address of our router, if not you can find it out using ipconfig (windows) or ip a (Linux) in a terminal window.

Lets get started, first you know you need something to contact your router, most likely this will be a ping node. We’ll want to be able to easily see that the router is running or not so we’ll want to use a web user interface (hence the dashboard). This means the conf-ping . Find it, drag it to the empty flow and dubble click it to acces the node’s properties.

UI ping node's properties
UI ping node’s properties

In the properties speak for themself.

  • Host: the IP or URL for the server/device you wish to ping
  • Timeout: the time from start to end of the ping before the device is considered offline
  • # of request: the amount of pings done
  • Name: the name of the node as seen in the flow

In most cases you can leave the Timeout and # of requests the defaults parameters. Both the Host and the name you’ll want to change, the Host is the IP of our Router (eg 192.168.0.1) and the name of the node (for clarity when you review your flows after a while).

A simple ping flow
Simple ping flow

As in the first episode i dragged in an inject node and a debug node. This for demonstrating the ping node without further settings.
Deploy your flow (upper right corner), open the debug windows and press the inject node’s blue button.

The output you’ll see (when your router is online atleast) will be something like:

{
   "_msgid":"24e5731f777f879a",
   "payload":0.684,
   "topic":"192.168.0.1",
   "_payload":1629723255034,
   "_topic":""
}

In this you find a payload (the time taken to ping request to reach the device and the ping responce to come back). and the topic (or the ip).
This is good when you want to sift thru the debug window all the time but we’ll use Dashboard as a Web UI.

Drag in the UI-led node and dubble click it.

Web UI LED

In the properties window you’ll see the group setting, just pres the edit icon next to it and do the same in the following screen. Then pres the Update button twice again, we’ll look at this later.

In the Label field you can add a name for the LED (eg Router), don’t forget the Name!
As you can see the node expects a boolean, not a value so we’ll have to add a function node to convert the number to a boolean:

Double click the function node and replace the code. As you can see this sets the payload to true whenever the payload is not false (hence when its a number as the ping node returns false when ping failed)

if(msg.payload != false)
    msg.payload = true;
return msg;

Now deplay your code and go to the ui (in a new window) using your pi’s ip, the node-red port (eg the standard 1880) and /ui.
For example: 192.168.1.50:1880/ui

In this new window you’ll see a tab (in my case i renamed it to Home tab), a group called ‘Home’ and out grayed out led.
The led is still gray because we didn’t press the inject node. Once the inject node is pressed and the ping has finished (or failed) your see the color change to either green or red depending on the state of the router.
In my case (as I took an unused IP address) the ping failed.

Failed ping

Now i created a forked node to replace the configurable ping node. The reason for this is that the original node used above is missing a true boolean output on the payload. We added a function node to mimic a boolean but it’s more convenient to use a new custom node and ditch the function node.

The new node replaces both the ping node and the function node clearing space in the flow.
This makes it easier to understand and maintain the flow, the function is unchanged.

How to Dashboard

Now that we had a peak at the dashboard, it’s time to dig deeper into this UI option.
Lets continue with the LED. previously we skipped most of the settings but lets take a closer look at this now.

When adding different dashboard nodes we can chose in which tab or group.
A tab will contain at least 1 group, but is not limited to 1. The dashboard therefore contain at least 1 tab to house the groups. Now we can chose where we want it, but how is it ordered?

When the right hand sidebar is enabled you will be able to change this to the bashboard settings view (the down arrow button –> dashboard).

Dashboard layout

Once there there are 2 options to change the layout. The first one is right there, just move the nodes’ order around to change it in the graphical view. But this doesn’t quite represent the actual dashboard view. As you can see in the image above there is a layout option for each tab, when pressed this will open a new window where there is a more graphical way to not only change the order, but also the actual location and size.

Dashboard Layout option

Monitor you internet connection

Let’s continue with the monitoring of our network. Before adding more monitoring nodes we’ll clean up our current node. Add a text node and rename our current group so it has a more logical name, don’t forget to connect the ping node to the text node, we’ll use the output of the ping node to fill the value of the text. I chose to set the label as Router and use the topic from the ping node to give us the IP of the router so we only have to change it in 1 location when it should change (thought this is very unlikely).

Now that we can check if our router respond we know if its running or not. But what about our internet connection, is that still available?
To be able to check this we’ll ping a few DNS servers from google. The plus side of this is that google has multiple DNS servers so that if one should fail it should still have a response from the other one. Of course you can add other servers that you can ping to have a better view of things. This will look like following:

Now you notice that we still have to push the inject nodes to test the connection, but this can be automated. Open the settings from the inject nodes, set the repeat on an interval of for example 1 minute and inject the node after (eg) 0.1 seconds of flowstart.

Automate inject node

Now we have an automated check if we have still internet connection.

Flow Cleaning

Noticed we have 3 inject nodes that actually do the same now? Lets remove 2 of them.

Cleaning inject nodes

This is much cleaner, and only requires 1 inject node for all the pings.

The link node

There is 1 more way to optimize our code before we continue with our monitoring.
When you add more servers or devices you wish to check, you’ll have more and more wires coming from 1 or more inject nodes, probably not all in the same flow either. When you add more pings you probably don’t need them all at the same frequency, so you add an inject node. But on another flow (a flow can be seen as a new page in the node red editor) you might use the same settings for an inject node.

This is where the 2 linking nodes come in. We’ll create a new flow called 'Timing'. In this flow we’ll add all the automated inject nodes we want and for each of them add a link out node. Note that you’ll want to give the link out nodes a proper name so e can find them more easily later on.

Grouping all Inject nodes together

Did you notice that i added a button? This is for the occasion I like to manualy inject all. So now lets do the same with all out ping nodes but this time a link in node.

Link in nodes

To link a link in node to a link out node (or visaversa) you double click the node. This will open the settings window from this node and you’ll be able to select the link you ant to create. an example:

link node settings

When opening a link in node you’ll only see the link out nodes (from all flows).

Graphical view of data

Until now we’ve only use the boolean out of the ping node, but the node also spits a time out. This can be visually shown in a chart or a gauge. Unfortunately this means that we have to add the configurable node to a new flow as the dashboard nodes use the payload as input value.

Stats

ARP scan

Now we don’t always know the IP of the device we want to scan, in this case we can use ARP request to check if a specific MAC address is available on the local network.

an ARP scan

As seen above I added 2 more nodes to be able to ping my laptop. The reason for this is because my laptop does not have a static IP address like the router or the DNS servers. BUT i do know his MAC address. Using an ARP scan i can request if the mac is available in the network and get it’s IP address. I then use that IP address to ping the device. Thought that this is not really required i still included the ping node. I could have just stopped after the function node as the only thing I’m actually interested in is if the device is active.

Conclusion

We’ve seen a few interesting nodes on how to monitor your network. I might update these flows in the future and if its worth while i’ll create a second tutorial about the changes on a later date.

I hope you found this interesting and learned a little more about how node red functions.
Next on the menu is creating custom nodes. So join us in the next Episode

Leave a reply:

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.