Measuring Packet Latency (PING) and Sending Data to VizIoT

In this example, we will create a tool that regularly measures the latency (RTT) to websites, IP addresses, or specific TCP ports and automatically sends the data to the VizIoT server for graphing.


What You'll Need

Hardware

  • Any computer with a Linux operating system

Required Software

  • nano — a console text editor
  • grep — a utility for text searching
  • cURL — an HTTP client
  • hping3 — a powerful tool for generating TCP/IP packets
  • crontab — a task scheduling tool

Adding and Configuring the Device in VizIoT

  1. Create a new device: "Test PING"
  2. Add parameters:
    • tpgoogleCustom
    • tpipCustom
    • tpportCustom
  3. In the "General Settings" section, copy:
    • Access Key
    • Access Password

You will need these in the script.


Installing Required Software

RedHat / CentOS

sudo yum install hping3 curl

Debian / Ubuntu / Linux Mint

sudo apt-get install hping3 curl

Example of the hping3 command

Request:

hping3 -c 1 -S -1 google.com

Example response:

HPING google.com (...): icmp mode set, 28 headers + 0 data bytes
len=46 ip=74.125.232.40 ttl=128 id=16442 icmp_seq=0 rtt=15.1 ms

--- google.com hping statistic ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 15.1/15.1/15.1 ms

Algorithm for Getting the Latency Time

1. Save the command's result

res_ping_google=$(hping3 -c 1 -S -1 google.com 2>&1)

2. Check if the packet was successfully delivered

isOk=$(echo "$res_ping_google" | grep -Eo '1 packets received')

3. Extract the latency (or 9999 on error)

time_ping_google=9999
if [ "$isOk" == "1 packets received" ]; then
    time_ping_google=$(echo "$res_ping_google" \
      | grep -Eo 'min/avg/max = [^ ]*' \
      | grep -Eo '\/[^\/a-z]*\/' \
      | grep -Eo '[^\/a-z]*')
fi

Sending Data to VizIoT via cURL

Example:

curl --silent "http://VizIoT.com:48656/update?key=______________&pass=______________&tpgoogle=$time_ping_google" > /dev/null

Creating a Bash Script for Automatic Execution

Create a file:

nano /var/viziot_ping.sh

Full Script

#!/bin/bash
key='_____________'
pass='_________________'

# Ping a website
res_ping_google=$(hping3 -c 1 -S -1 google.com 2>&1)
# Ping an IP address
res_ping_ip=$(hping3 -c 1 -S -1 8.8.8.8 2>&1)
# Ping a TCP port
res_ping_port=$(hping3 -p 8080 -c 1 -S -1 google.com 2>&1)

time_ping_google=9999
time_ping_ip=9999
time_ping_port=9999

isOk=$(echo "$res_ping_google" | grep -Eo '1 packets received')
if [ "$isOk" == "1 packets received" ]; then
  time_ping_google=$(echo "$res_ping_google" | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

isOk=$(echo "$res_ping_ip" | grep -Eo '1 packets received')
if [ "$isOk" == "1 packets received" ]; then
  time_ping_ip=$(echo "$res_ping_ip" | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

isOk=$(echo "$res_ping_port" | grep -Eo '1 packets received')
if [ "$isOk" == "1 packets received" ]; then
  time_ping_port=$(echo "$res_ping_port" | grep -Eo 'min/avg/max = [^ ]*' | grep -Eo '\/[^\/a-z]*\/' | grep -Eo '[^\/a-z]*')
fi

# Send data
curl --silent \
"http://VizIoT.com:48656/update?key=$key&pass=$pass&tpgoogle=$time_ping_google&tpip=$time_ping_ip&tpport=$time_ping_port" \
> /dev/null

Granting Execution Permissions

chmod +x /var/viziot_ping.sh

Automatic Execution via cron

Open crontab:

crontab -e

The beginning of the file should contain these lines:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

Add the job to run every minute:

*/1 * * * * /var/viziot_ping.sh

Adding Widgets

  1. Create a dashboard: "Test PING"
  2. Add a widget named "Ping Time":
    • type: Graph
    • parameters: tpgoogle, tpip, tpport

All Set!

Now the system will automatically measure packet latency to the site, IP address, and TCP port and send the data to VizIoT. You will see the first points on the graph within a few minutes.