IP, Port, and Website Availability Monitoring with NodeJS and VizIoT

Is your home server offline again? Did your IP camera stop streaming video? Has the internet gone down completely, or is just one website not loading? To get instant answers to these questions, you can create a simple yet effective monitoring system.

In this article, we'll set up a script that will regularly check the availability of any internet resources (IP addresses, ports, websites) and send response times to the VizIoT server. If a resource becomes unavailable, we'll see it immediately on the graph and receive a notification in Telegram.

End Result: Interactive Dashboard and Instant Notifications

Before we begin, let's see what we'll achieve.

1. Live Monitoring Widget

You'll be able to create a visual dashboard that displays the status of your key services in real time.

What you can monitor in this configuration:

  • 🌐 Google.com — a reference resource to check if you have internet access at all.
  • 🏠 Local IP Address — monitoring the availability of devices within your network (e.g., NAS or router).
  • 📹 IP Camera RTSP Port — checking if video streaming is working (standard port 554).
  • 🌍 External IP Addresses — monitoring the availability of branch offices and services.

How to interpret the graph:

  • Positive values — response time in milliseconds (lower is better).
  • Value -500 — resource is unavailable.
  • 📊 Sharp spikes — possible network issues or server overload.

This approach allows you to instantly identify the source of the problem: your local network, internet provider, or a specific remote service.

2. Failure Notifications in Telegram

Seeing a problem on the graph is good, but receiving an instant notification about it is even better. VizIoT allows you to set up alerts that will come to you via Telegram or email as soon as one of your resources becomes unavailable, as well as when it comes back online.

pinger_test_notifi_en

Interested? Let's set up such a system step by step.


What You'll Need

Hardware

  • Any computer running Linux or Windows (that will remain on continuously).

Required Software and Packages

  • NodeJS — JavaScript runtime environment.
  • PM2 — process manager for reliable script autostart.
  • tcp-ping — NodeJS library for TCP ping.
  • viziot-mqtt-client-nodejs — module for transmitting data to VizIoT via MQTT.

Step 1: Setting Up a Device in VizIoT

  1. Create a new device named VizIoTPinger.
  2. In the Basic Settings section, copy the Access Key and Access Password. The script will need these for authorization.

Step 2: Installing NodeJS

It's recommended to install NodeJS via NVM (Node Version Manager).

Linux (Ubuntu / Debian)

# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

# Connect without restarting the shell
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Install the latest LTS version of Node.js
nvm install --lts

# Check version
node -v
npm -v

Windows

Download the installer from the official website: https://nodejs.org


Step 3: Creating the Monitoring Program

Create a project directory and navigate to it

mkdir ./VizIoTPinger
cd ./VizIoTPinger

Initialize a NodeJS project

npm init -y

Install dependencies

npm install tcp-ping --save
npm install viziot-mqtt-client-nodejs --save

Step 4: Creating the Script

Create the index.js file:

nano ./index.js

Complete Script Code

Insert the following code into the file. Don't forget to specify your access keys and configure the list of resources to monitor.

'use strict';

// Insert your VizIoT device key and password here
const keyDevice = '__________________';
const passDevice = '______________________';

// List of resources to monitor
// key     — parameter name that will appear in VizIoT
// address — domain or IP address
// port    — port (optional, default is 80)
const 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 // Standard port for RTSP stream
  }
];

const tcpp = require('tcp-ping');
const viziotMQTT = require('viziot-mqtt-client-nodejs');

const viziotMQTTClient = new viziotMQTT(keyDevice, passDevice);

// Function: ping by IP/port
function pingIpAndPortAddress(item, callback) {
  const 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); // Send -500 if resource is unavailable
    }
  });
}

// Form data and send to VizIoT
function getPacketAndSendToServer() {
  let countResults = 0;
  const packet = { date: Math.floor(Date.now() / 1000) };

  for (let i = 0; i < pingList.length; i++) {
    pingIpAndPortAddress(pingList[i], function (item, timeout) {
      packet[item.key] = timeout;
      console.log(`Pinging ${item.address}:${item.port || 80} -> ${timeout} ms`);

      countResults++;

      if (countResults === pingList.length) {
        viziotMQTTClient.sendDataToVizIoT(packet, function (err) {
          if (err) {
            console.log("Publish error:", err);
          } else {
            console.log("Packet sent successfully!");
          }
        });
      }
    });
  }
}

// Check interval (60 seconds)
const intervalTime = 60000;

// Initial connection and startup
viziotMQTTClient.connect(function () {
  console.log("Connected to VizIoT MQTT broker.");
  getPacketAndSendToServer(); // First run immediately after connection
  setInterval(getPacketAndSendToServer, intervalTime);
});

Step 5: Auto-starting the Script with PM2

PM2 is an advanced process manager for Node.js that will ensure automatic script restart on failures and convenient management.

Installing PM2

npm install pm2 -g

Starting the Script

pm2 start index.js --name "VizIoTPinger"

Setting Up Autostart on System Boot

  1. Save the current process list:
    pm2 save
  2. Generate the autostart script. PM2 will detect your system automatically:
    pm2 startup

    This command will output another command (usually with sudo) that you need to copy and execute.

Useful PM2 Commands

pm2 list          # Show list of running processes
pm2 logs VizIoTPinger # Show logs in real time
pm2 restart VizIoTPinger# Restart process
pm2 stop VizIoTPinger   # Stop process

Step 6: Setting Up Widgets and Notifications in VizIoT

Now that data is flowing in, all that's left is to set up the interface.

Adding Widgets

  1. Create a dashboard, for example "Internet Services Availability".
  2. Add a "Ping Time" widget.
    • Type: Stacked Area Chart
    • Device: VizIoTPinger
    • Parameters: microsoft_ping, google_ping, ipCamera_ping

Setting Up Notifications

  1. Go to the Notifications section and click Add Notification.
  2. Device: VizIoTPinger.
  3. Parameter: Select the one you want to monitor (e.g., ipCamera_ping).
  4. Condition: Type less than (<), value 0.
  5. Enable the "Notify when returning to range" option.
  6. Choose the delivery method (Telegram, Email) and save.

Detailed instructions for setting up notifications can be found in this article.


Done!

You've created a complete monitoring system for the availability of your key services with visual dashboards and instant alerts.