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:
Adding and Configuring a Device:
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 nodejsCreating 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:
npm install tcp-ping --save
npm install viziot-mqtt-client-nodejs --save
nano ./index.jsScript 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: