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.
tpgoogle — Customtpip — Customtpport — CustomYou will need these in the script.
sudo yum install hping3 curl
sudo apt-get install hping3 curl
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
res_ping_google=$(hping3 -c 1 -S -1 google.com 2>&1)
isOk=$(echo "$res_ping_google" | grep -Eo '1 packets received')
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
Example:
curl --silent "http://VizIoT.com:48656/update?key=______________&pass=______________&tpgoogle=$time_ping_google" > /dev/null
Create a file:
nano /var/viziot_ping.sh
#!/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
chmod +x /var/viziot_ping.sh
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
tpgoogle, tpip, tpportNow 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.