Advertisement
Guest User

Plot Ping

a guest
Dec 2nd, 2014
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.18 KB | None | 0 0
  1. #!/bin/bash
  2. # Requires bc, gnuplot
  3. # Usage: plotping -a <address> -i <interval> -d (delete temporary files on exit) -x (Don't plot on exit)
  4. # p / t to plot, q to quit
  5.  
  6. while getopts "a:i:dx" opt; do
  7.     case "$opt" in
  8.     a)  ping_address=$OPTARG
  9.         ;;
  10.     i)  interval=$OPTARG
  11.         ;;
  12.     d)  delete_temp=1
  13.         # Graph manipulation will not be possible after script exits if this option is passed
  14.         ;;
  15.     x)  no_plot=1
  16.         ;;
  17.     esac
  18. done
  19.  
  20. if [ "$ping_address" = "" ]; then
  21.     echo "Invalid Input. Exiting."
  22.     exit
  23. fi
  24.  
  25. if [ "$interval" = "" ]; then
  26.     interval=1
  27. fi
  28.  
  29. rm ping_table pinglog_avg 2> /dev/null
  30.  
  31. ping $ping_address -i $interval > pinglog_avg &
  32. PID=$!
  33. # Suppresses kill output
  34. disown $PID
  35.  
  36. echo "Logging to" $(pwd)
  37. echo "(p) to plot vs packets / (t) to plot vs time / (q) to exit"
  38. echo "# Ping results for" $ping_address >> ping_table
  39. echo -e "# Valid-Packets"'\t'"Average-Ping"'\t'"Current-Ping"'\t'"Dropped-Packets"'\t'"Time" >> ping_table
  40.  
  41. total_ping=0
  42. dropped_packets=0
  43. valid_packets=0
  44. oldseq=0
  45. highest_ping=0
  46. starttime=$(date +%H%M%S)
  47.  
  48. # For non autoadjusting ping axis
  49. # set ytics 50 nomirror tc lt 3
  50. gnuplot_common=$(echo -n "\
  51.     set title \"$ping_address every $interval s\" font \"Arial,16\"
  52.     set term wxt size 1100,550 font "Arial,12"
  53.     set autoscale
  54.     set y2tics 10 nomirror tc lt 1
  55.     set ylabel \"Milliseconds\"
  56.     set y2label \"Dropped Packets\"
  57.     ")
  58.  
  59. function plot_graph_packets {
  60.     echo -n "$gnuplot_common""\
  61.     set xrange [1:]
  62.     set xlabel \"Valid Packets\"
  63.     plot \"ping_table\" using 1:2 title \"Average ping\" with lines lt 3, \"ping_table\" using 1:3 title 'Ping' with lines lt 4, \"ping_table\" using 1:4 title 'Dropped Packets' with lines lt 1 axes x1y2
  64.     " | gnuplot -p 2>/dev/null
  65. }
  66.  
  67. function plot_graph_time {
  68.     currenttime=$(date +%H%M%S)
  69.     echo -n "$gnuplot_common""\
  70.     set xdata time
  71.     set timefmt \"%H%M%S\"
  72.     set xtics format \"%H:%M:%S\"
  73.     set xrange [\"$starttime\":\"$currenttime\"]
  74.     set xtics 10 nomirror
  75.     set xlabel \"Time\"
  76.     plot \"ping_table\" using 5:2 title \"Average ping\" with lines lt 3, \"ping_table\" using 5:3 title 'Ping' with lines lt 4, \"ping_table\" using 5:4 title 'Dropped Packets' with lines lt 1 axes x1y2
  77.     " | gnuplot -p 2>/dev/null
  78. }
  79.  
  80. function finish {
  81.     echo
  82.     echo "Done."
  83.         if [[ ! $no_plot = 1 ]]; then
  84.             plot_graph_packets
  85.             plot_graph_time
  86.         fi
  87.     kill $PID
  88.        
  89.     if [ "$delete_temp" = 1 ]; then
  90.         rm pinglog_avg ping_table
  91.     fi
  92. }
  93.  
  94. trap finish EXIT
  95.  
  96. # Wait for first packet
  97. while true
  98. do
  99.     echo -ne "Waiting for response to first packet..." \\r
  100.     final_line_length=$(tail -n1 pinglog_avg | wc -m)
  101.    
  102.     if [ ! $final_line_length = 0 ]; then
  103.         echo -e '\t\t\t\t\t'"FOUND"
  104.         echo
  105.         break
  106.     fi
  107. done
  108.  
  109. while :
  110. do
  111.     final_line=$(tail -n1 pinglog_avg)
  112.  
  113.     echo $final_line | grep -q "Destination"
  114.     grep_error=$?
  115.        
  116.         if [ $grep_error = 0 ]; then
  117.             dropped_packets=$[ $dropped_packets + 1 ]
  118.             seq_def=1
  119.         else
  120.             icmp_sequence=$(echo $final_line | cut -d ":" -f2 | awk '{print $1}' | cut -d "=" -f2)
  121.             seq_def=$[ $icmp_sequence - $oldseq ]
  122.         fi
  123.        
  124.         if [ $grep_error = 1 ] && [ $seq_def -gt 1 ]; then
  125.             dropped_packets=$[ $dropped_packets + $[ $seq_def - 1 ] ]
  126.         else
  127.         if [ $grep_error = 1 ] && [ $seq_def = 1 ]; then
  128.             valid_packets=$[ $valid_packets + 1 ]
  129.             current_ping=$(echo $final_line | cut -d ":" -f2 | awk '{print $3}' | cut -d "=" -f2)
  130.  
  131.                     # This doesn't work sometimes. I don't know why.
  132.                     if [[ $current_ping > $highest_ping ]]; then
  133.                         highest_ping=$current_ping
  134.                     fi
  135.            
  136.             total_ping=$(echo "$total_ping" + "$current_ping" | bc)
  137.             avg_ping=$(echo $total_ping / $valid_packets | bc -l)
  138.  
  139.             echo -e $valid_packets'\t\t'${avg_ping::-18}'\t\t'$current_ping'\t\t'$dropped_packets'\t\t'$(date +%H%M%S) >> ping_table
  140.             echo -ne "Valid Packets: "$valid_packets'\t'"Average Ping: "${avg_ping::-18}'\t'"Current Ping: "$current_ping'\t'"Highest Ping: "$highest_ping'\t'"Dropped Packets: "$dropped_packets \\r
  141.         fi
  142.         fi
  143.  
  144.     oldseq=$icmp_sequence
  145.    
  146.     read -t 0.1 -n 1 key
  147.         if [[ $key = q ]]; then
  148.             exit
  149.         else
  150.         if [[ $key = p ]]; then
  151.             echo -ne \\r
  152.             plot_graph_packets &
  153.         else
  154.         if [[ $key = t ]]; then
  155.             echo -ne \\r
  156.             plot_graph_time &
  157.         else
  158.             echo -ne \\r
  159.         fi
  160.         fi
  161.         fi
  162.    
  163. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement