Skip to content

Instantly share code, notes, and snippets.

@singe
Last active May 28, 2017 15:29
Show Gist options
  • Save singe/9b130fd9b34c2d9c0d60085e57040f00 to your computer and use it in GitHub Desktop.
Save singe/9b130fd9b34c2d9c0d60085e57040f00 to your computer and use it in GitHub Desktop.
A super crude method of checking the latency of your local connection.
#!/bin/sh
# Super crude ping viz for debugging local network jitter
upstream=$1 #The gateway of your Internet provider
router=$2 #Your local gateway. This can be a MAC address, you'll need arping though
avg=2 #How many points to average for the smooted graph
limit=100 #The maximum number of points to show in the graph
graphsize="2100,600" # width x height this works nicely on my MBP
pingdelay=1
# Plot the raw ping time to the two IPs
cat << EOF > pings-both.gpi
set terminal png size $graphsize font "Gill Sans,12" linewidth 2 rounded
set output "pings-both.png"
set ylabel "Time in milliseconds"
set xlabel "Number of Pings"
plot '< tail -n $limit pings-$router.dat' title 'Router' w lp, '< tail -n $limit pings-$upstream.dat' title 'Upstream' w lp
pause 0.5
replot
reread
EOF
echo 0.0 > pings-$router.dat
echo 0.0 > pings-$upstream.dat
# Plot the smoothed graph
cat << EOF > pings-dual.gpi
set terminal png size $graphsize font "Gill Sans,12" linewidth 2 rounded
set output "pings-dual.png"
set ylabel "Difference in Time (ms)"
set xlabel "Number of Pings"
plot '< tail -n $limit pings-dual.dat' w lp title 'Smoothed Avg $avg'
pause 0.5
replot
reread
EOF
echo 0.0 > pings-dual.dat
gnuplot pings-both.gpi 2> /dev/null&
gnuplot pings-dual.gpi 2> /dev/null&
# Display the two graphs in your browser
cat << EOF > pings-dual.html
<html>
<head><title>Ping Diff Plot</title></head>
<body>
<img src="" id="dual" />
<img src="" id="both" />
<script type="text/javascript">
var dualelem = document.getElementById("dual");
var bothelem = document.getElementById("both");
function reload(type){
stamp = (new Date()).getTime()
url = "pings-"+type+".png?"+stamp;
dualurl = "pings-dual.png?"+stamp;
bothurl = "pings-both.png?"+stamp;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.response != "" ) {
if (xmlhttp.responseURL.search("dual") != -1)
dualelem.src = dualurl
if (xmlhttp.responseURL.search("both") != -1)
bothelem.src = bothurl
}
}
};
xmlhttp.open("HEAD", url, true);
xmlhttp.send();
}
window.setInterval("reload('both')",500);
window.setInterval("reload('dual')",500);
</script>
</body>
</html>
EOF
open pings-dual.html&
function cleanup {
rm pings-$router.dat
rm pings-$upstream.dat
rm pings-dual.gpi
rm pings-dual.png
rm pings-dual.html
rm pings-dual.dat
rm pings-both.gpi
rm pings-both.png
exit
}
# Status output
function hup {
echo
echo "# HUP received:"
wc -l pings-*.dat
tail -n$avg pings-*.dat
}
# Catch signals and cleanup
trap cleanup SIGINT SIGTERM
trap hup SIGHUP
function pong {
# Check if it's a MAC, if so arping instead of ping
echo "$1"|grep ':' 2>&1 > /dev/null
if [ $? == 0 ]; then
# The sed converts usec to msecs for consistency
pingout=$(arping -c1 $1|sed "s/\([0-9]*\)\.[0-9]* usec/0.\1 msec/")
else
pingout=$(ping $1 -W 1000 -c1)
fi
if [ $? == 2 ]; then #error with ping
echo 1000.000 >> pings-$1.dat
else
echo "$pingout" | awk -F '[= ]' {'print $(NF-1)'} 2> /dev/null | grep -E '[0-9]' >> pings-$1.dat
fi
}
# Loop
while ((1)); do
#Make the pings, parse out the time, and put them in a data file
pong $router&
pid1=$!
pong $upstream&
pid2=$!
wait $pid1 $pid2
/bin/echo -n "."
sleep $pingdelay
# Calculate the averages
uptime=$(tail -n$avg pings-$upstream.dat|awk "{s+=\$1}END{print s/$avg}")
rttime=$(tail -n$avg pings-$router.dat|awk "{s+=\$1}END{print s/$avg}")
# Subtract the router time from the upstream time to remove local link jitter
diff=$(echo $rttime|awk "{print $uptime-\$0}")
echo $diff >> pings-dual.dat
done
cleanup
@singe
Copy link
Author

singe commented Apr 13, 2017

screen shot 2017-04-13 at 10 16 04 pm
An example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment