
A visual representation in terminal window can be used to keep a log of commands and their output in a single file and to copy and paste code snips from terminal to emails and chat. It also helps if you don't enjoy switching from terminals to visualisation tools and prefer working with a condensed view
Key to plotting the data is gnuplot, a command line utility for Linux first released in 1986.
Ping response is displayed graphically as impulse and results are refreshed in realtime. X axis represents time and Y axis round trip time for ping. In case of packet loss, an impulse of maximum size is displayed.
We'll user to use output provided by monitoring plugin check_ping since output from standard Linux implementation of ping gives no results when host is unreachable or there is packet loss, instead presents a summarized output.
What's needed
- Nagios plugin check_ping https://www.monitoring-plugins.org
- gnuplot package http://www.gnuplot.info/
Installation
install gnuplot (on CentOS)
yum install -y gnuplot
Install Nagios plugins
wget https://www.monitoring-plugins.org/download/monitoring-plugins-2.2.tar.gz
sudo tar xzf monitoring-plugins-2.2.tar.gz
sudo ./configure
sudo make
sudo make install
Open a terminal and launch the loop to populate input file for the graph. We'll break it down in small parts for clarity, explaining each pipe separated command
while true; do /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1| awk -F"|" '{print $2}'| xargs -I '{}' date '+%Y-%m-%d-%H:%M:%S;{}'|awk -F"[;=]" '{print $1" "$3}' |sed -e s/ms$// >> plot.dat ; sleep 1; done
Step by step
What does first part do ?
/usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1
In our case we are using following options of check_ping plugin. For other options its manual is available at https://www.monitoring-plugins.org/doc/man/check_ping.html
-H IP of host being tested. In out case pinging 216.58.206.14, one of the IPs of google.com
-w warn if rta or round trip average is higher than 100ms. Second parameter is the precentage of packets lost to trigger a warning
-c critical alarm if rta or round trip average is higher than 200ms. Second parameter is the precentage of packets lost to trigger a critical alarm
-p number of packets sent
-t timeout
A sample output of the first command, doesn't quite look like the input necessary for gnuplot
[ec2-user@ip-172-31-0-100 tmp]$ /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1
PING OK - Packet loss = 0%, RTA = 1.01 ms|rta=1.008000ms;100.000000;200.000000;0.000000 pl=0%;2;5;0
We're only interested in the part following pipe symbol, the so called Nagios performance data so we use awk to extract everythnig after the pipe symbol. This is how our output looks now:
[ec2-user@ip-172-31-0-100 tmp]$ /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1| awk -F"|" '{print $2}'
rta=1.021000ms;100.000000;200.000000;0.000000 pl=0%;2;5;0
We would like our graph to represent time on the X axis but there's no time/date in the output of ping. We need to add it manually before output of check_ping
xargs -I '{}' date '+%Y-%m-%d-%H:%M:%S;{}'
Here is how our output looks now:
[ec2-user@ip-172-31-0-100 tmp]$ /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1| awk -F"|" '{print $2}'| xargs -I '{}' date '+%Y-%m-%d-%H:%M:%S;{}'
2017-06-05-18:20:33;rta=1.240000ms;100.000000;200.000000;0.000000 pl=0%;2;5;0
There is still a lot of info we are not using let's filter only info we need. Using separators ";" and "=" first and third fields are extracted, namely time and rta
awk -F"[;=]" '{print $1" "$3}'
Output now looks like this:
[ec2-user@ip-172-31-0-100 tmp]$ /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1| awk -F"|" '{print $2}'| xargs -I '{}' date '+%Y-%m-%d-%H:%M:%S;{}'|awk -F"[;=]" '{print $1" "$3}'
2017-06-05-18:30:01 1.004000ms
Last command removes unit ms from the output:
sed -e s/ms$//
Output is now in the correct form for input to gnugraph, two colums separated by space
[ec2-user@ip-172-31-0-100 tmp]$ /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1| awk -F"|" '{print $2}'| xargs -L 1 -I '{}' date '+%Y-%m-%d-%H:%M:%S;{}'|awk -F"[;=]" '{print $1" "$3}' |sed -e s/ms$//
2017-06-05-18:32:16 0.945000
We want to have second samples written to file plot.dat which serve as input for gnuplot
[ec2-user@ip-172-31-0-100 tmp]$ while true; do /usr/local/libexec/check_ping -H 216.58.206.14 -w 100,2% -c 200,5% -p 1 -t 1| awk -F"|" '{print $2}'| xargs -I '{}' date '+%Y-%m-%d-%H:%M:%S;{}'|awk -F"[;=]" '{print $1" "$3}' |sed -e s/ms$// >> plot.dat ; sleep 1; done
GNU script
In a new terminal window paste following gnuplot script
[ec2-user@ip-172-31-0-100 tmp]$ cat liveplot.gnu
set terminal dumb 121 28;
set xdata time
set timefmt "%Y-%m-%d-%H:%M:%S"
set format x "%M:%S"
set xlabel "time"
set yrange [0:150]
set ylabel "rta"
plot "plot.dat" using 1:2 with impulses title 'Ping (ms)'
pause 1
reread
Result
Run the script and view results on screen
[ec2-user@ip-172-31-0-100 tmp]$ gnuplot liveplot.gnu