Visualise Ping response time in Grafana (to determine latency in your network)

Hi Guys
I wanted to do some network troubleshooting and noticed that I cant use a ping function with Telgraf data collector service.
So I decided to make my own, I thought I share with everyone, maybe it helps someone in the new future.
So here is goes:
I am using a Linux debian with my Grafana server and I use Influxdb with my Grafana.
However I did not know how to write in influx as the language is a tiny bit different to mysql.
SO I ended up installing mysql
apt-get install mysql-server
setup your users however you want and create the following database

CREATE USER ‘user’@‘localhost’ IDENTIFIED BY ‘password’;
#change the user with your username and password with your desired password

GRANT ALL PRIVILEGES ON PingPlot.* TO ‘user’@‘localhost’ IDENTIFIED BY ‘password’;

Create table ip_table(

id int not null auto_increment,

ip varchar(30) not null,

primary key (ID));

Create table ping_log (

log_time timestamp default current_timestamp,

ip varchar(30) not null,

ping_time decimal(6,2));

#Also don’t forget to add indexes for the ping_log table to make it load faster

ALTER TABLE ping_log ADD PRIMARY KEY (ip,log_time,ping_time);

At this point I have created 2 scripts to ping IPs in different intervals. Since I have IP addresses that I want to monitor every minute and IP addresses that I want to monitor every hour. You might not need both.
Note that you can change the time in the script.

setup a script called
save it
make sure its executable
chmod 755 local
and paste the following in this script.

#create the below file and put your root password in it
dbpw="$(cat /root/dbpw)"
ping_timeout=10 # in seconds

while read -a row
[[ $ip = “ip” ]] && continue

# note: do 2 pings and only log the seconds one, since the first may fail
duration="$(ping -c2 -w$ping_timeout $ip | head -3 | tail -1 | cut -d'=' -f4 | cut -d' ' -f1)"

[[ $duration == "" ]] && duration="0"
echo "ip: $ip       duration: $duration"
echo "INSERT INTO ping_log (ip, ping_time) VALUES ('$ip', $duration)" | /usr/bin/mysql PingPlot -u user -p"$dbpw"

done < <(echo “SELECT ip FROM ip_table WHERE location = ‘internal’” | /usr/bin/mysql PingPlot -u user -p"$dbpw")


last step is to make this run in the intervals that you want
edit crontab -e and make the following entry (paste it at the end)

***** /usr/share/scripts/

This will run your script every minute.
Note that I have placed my in /usr/share/scripts
you can put it anywhere but make sure you reference it currently in here.

I hope the info I provided is helpful to the Grafana community

1 Like