NodeJS -> IP address and port monitoring

Let's consider an example script for monitoring the availability of an IP address, port, or website on the internet. The script will check the availability of the hosts we need at a 15-second interval and send the time taken between sending the request and receiving the "PING" response to the VizIoT server. If the specified node is unavailable, the PING value will be -500.

What You'll Need:

  • Computer: Any computer with Linux or Windows operating system.
  • Software:
    • cURL: A command-line program for working with HTTP.
    • cron: A daemon used for periodic task execution.
    • NodeJS: A server platform for working with JavaScript through the V8 engine.
    • tcp-ping: A NodeJS package for checking the availability of an IP address.
    • viziot-mqtt-client-nodejs: A NodeJS package for interacting with the VizIoT server using the MQTT protocol.

Adding and Configuring a Device:

  1. Add a new device to VizIoT named "VizIoTPinger".
  2. In the "Basic Settings" of the device, copy the Access Key and Password.

Installing NodeJS:

Refer to the NodeJS documentation. For Windows, download the package from Nodejs.org and install it. For Ubuntu or Debian:

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt-get install -y nodejs
Creating a NodeJS Program on Debian OS:

Create a directory:

mkdir /var/viziot/
mkdir /var/viziot/VizIoTPinger

Navigate to the directory:

cd /var/viziot/VizIoTPinger

Create a NodeJS project:

npm init

Install packages:

  • tcp-ping - NodeJS package for checking the availability of an IP address:
    npm install tcp-ping --save
  • viziot-mqtt-client-nodejs - NodeJS package for sending data to the VizIoT server using the MQTT protocol:
    npm install viziot-mqtt-client-nodejs --save
Create the script file:
nano ./index.js
Script content:
'use strict';

//# Access Key and Password for VizIoT

let keyDevice = '__________________';
let passDevice = '______________________';
// Array of WEB resources to ping
// Consists of:
//   key - parameter name to be sent to the server
//   address - address of the WEB resource, IP address of the device
//   port - port through which the device is accessible
let pingList = [
	{
		key: "microsoft_ping",
		address: 'microsoft.com',
		port: 80
	}, {
		key: "google_ping",
		address: 'google.com'
	}, {
		key: "ipCamera_ping",
		address: '192.168.0.158',
		port: 554
	}
];

let tcpp = require('tcp-ping');

function pingIpAndPortAddress(item, callback) {
	let pingItem = {
		address: item.address,
		port: item.port || 80,
		attempts: 3,
		timeout: 5000
	};
	tcpp.ping(pingItem, function (err, data) {
		if (data.avg >= 0) {
			callback(item, +data.avg.toFixed(2));
		} else {
			callback(item, -500);
		}
	});
}

function getPacketAndSendToServer() {
	let countResults = 0;
	let packet = {
		'date': parseInt(new Date().getTime()/1000)
	};
	for (let ii = 0; ii < pingList.length; ii++) {
		pingIpAndPortAddress(pingList[ii], function (item, timeout) {
			packet[item.key] = timeout;
			console.log(item, timeout);
			countResults++;
			if (countResults == pingList.length) {
				viziotMQTTClient.sendDataToVizIoT(packet, function (err) {
					if (err) {
						console.log("publish", err);
					}
				});
			}
		});
	}
}

let viziotMQTT = require('viziot-mqtt-client-nodejs');
let viziotMQTTClient = new viziotMQTT(keyDevice, passDevice);
viziotMQTTClient.connect(function () {
	getPacketAndSendToServer();
});

let intervalTime = 60000; // interval for checking the host availability 
setInterval(function () {
	getPacketAndSendToServer();
}, intervalTime);

This script will be launched at system startup. To do this, we need to add record to the cron. Open the cron editor:

crontab -e

And at the very end, add a line that will run the script "/var/viziot/VizIoTPinger/index.js":

@reboot node /var/viziot/VizIoTPinger/index.js > /var/viziot/VizIoTPinger/output.txt &

Adding a widget:

  1. Create a new panel named "Internet Services Availability"
  2. Add a widget "Ping Time"
    1. Type: "Stacked Area Chart";
    2. Device: "VizIoTPinger";
    3. Parameters: microsoft_ping, google_ping, ipCamera_ping;
  3. Now you have configured everything and can monitor the availability of WEB services.
  4. You can also set up notifications in case one or more WEB services become unavailable:
    1. Go to "Notifications";
    2. Click "Add Notification";
    3. Select the device "VizIoTPinger" from the list;
    4. Choose the notification method (Telegram or E-Mail);
    5. Select the parameter key, for example, "google";
    6. Choose the trigger type "generate conditionally";
    7. Select the parameter value "less than (<)" and set the value to "0";
    8. Check the box next to "Notify when returning to the value range?";
    9. Click the "Create Notification" button.
    10. You have configured notifications. Now, if the parameter "google" is assigned a value of -500, you will receive a notification. And when the WEB service is restored, you will receive a notification about the parameter returning to its normal state.