Advertisement
IssyPutchy

PingPlotter v0.1

May 17th, 2022
816
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/data/data/com.termux/files/usr/bin/bash
  2. # V0.1
  3. ### Simplw color outpur
  4. NoCol='\033[0m'
  5. Black='\033[0;30m'
  6. Red='\033[0;31m'
  7. Green='\033[0;32m'
  8. Yellow='\033[0;33m'
  9. Blue='\033[0;34m'
  10. Purple='\033[0;35m'
  11. Cyan='\033[0;36m'
  12. White='\033[0;37m'
  13. BBlack='\033[1;30m'
  14. BRed='\033[1;31m'
  15. BGreen='\033[1;32m'
  16. BYellow='\033[1;33m'
  17. BBlue='\033[1;34m'
  18. BPurple='\033[1;35m'
  19. BCyan='\033[1;36m'
  20. BWhite='\033[1;37m'
  21. UBlack='\033[4;30m'
  22. URed='\033[4;31m'
  23. UGreen='\033[4;32m'
  24. UYellow='\033[4;33m'
  25. UBlue='\033[4;34m'
  26. UPurple='\033[4;35m'
  27. UCyan='\033[4;36m'
  28. UWhite='\033[4;37m'
  29.  
  30.  
  31. ### Setu0 Plotping (install packages etc)
  32. setup-plotping () {
  33. echo "OK!"
  34. }
  35.  
  36. #setup-plotping
  37.  
  38. ############# Adds average RRT per ping and total transit time
  39. fixplot () {
  40.  
  41. TMP=$(mktemp)
  42. newfile="$1_avg.plot"
  43. tavg=1
  44. ttot=0
  45. ctot=0
  46. cavg=1
  47. tail -n+2 $1 > $TMP
  48. xrange=$(cat $TMP |wc -l)
  49. lpcount=$(tail -n 1 $TMP |awk '{print $2}')
  50. rtts=$(cat $TMP |awk '{printf "%.0f + ", $5}')
  51. yrange=$(cat $TMP |awk '{print $5}' |sort -rn |head -n+1)
  52. iylow=$(cat $TMP |awk '{print $5}' |sort -n |head -n+1)
  53. ylow=$(printf "%.0f" $iylow)
  54. ttot=$(expr $rtts 0)
  55. tavg=$(expr $ttot / $lpcount)
  56.  
  57. rm -f $newfile
  58.  
  59. stime=$(cat $TMP |head -n 1 |awk '{print $1}')
  60. orig=$(cat "$1" |wc -l)
  61. run=1
  62. xrange=$(cat $TMP |wc -l)
  63.  
  64. cat $TMP |while read line
  65.     do
  66.    rtt=$(echo $line|awk '{printf "%.0f", $5}')
  67.    cping=$(echo $line |awk '{print $2}')
  68.    ctot=$(( $ctot + $rtt ))
  69.    ctots=$(echo "$ctot" | awk '{printf "%.2f", $1 / 1000}')
  70.    cavg=$(( $ctot / $cping ))
  71.    dtime=$(echo $line |awk '{print $1}')
  72.    difftime=$(( $dtime - $stime ))
  73.    sed -e "s/$/\t$cavg\t$ctots/g"  <<< $line >> $newfile
  74.    left=$(( $xrange - $run ))
  75.    proc=$(awk -vn=$xrange -vrun=$run 'BEGIN{printf "%.0f", run*(100/n)}')
  76.    echo -ne "${White}Proc:${BGreen} $proc% ${White}|| Rem:${BGreen}$left ${White} || InFlight:${BGreen}$ctots s ${White}|| Length:${BGreen}$difftime s${White}"\\r
  77.    run=$(( $run + 1 ))
  78. done
  79.  
  80. plotfile "$newfile"
  81. }
  82. ######################## End of plotfix
  83.  
  84. ### Plot the file
  85. plotfile () {
  86.  
  87. newfile="$1"
  88. startepoc=$(cat $newfile |awk '{print $1}' |head -n 1)
  89. endepoc=$(cat $newfile |awk '{print $1}' |tail -n 1)
  90. tlength=$(awk -va=$startepoc -vb=$endepoc 'BEGIN{printf "%.0f", b - a}')
  91. lavg=$(cat $newfile |awk '{print $5}' |tail -n 1)
  92. avgrtt=$(printf "%.0f" $lavg)
  93. yrange=$(cat $newfile |awk '{print $5}' |sort -rn |head -n 1)
  94. xrange=$(cat $newfile |wc -l)
  95.  percent=$(awk -va=$tlength -vb=$xrange 'BEGIN{printf "%.0f", 100-((100 / a) * b)}')
  96. PLOT="$newfile"
  97. pngfile="/sdcard/Download/$PLOT.png"
  98. echo -e "${Green} Plotting to: ${BWhite}$pngfile"
  99. echo -e -n "${White}Values:${BGreen}X:$xrange/Y:$yrange/Z:$avgrtt/P:$tlength/Loss=$percent"
  100. start=$(date +'%s')
  101.  
  102. gnuplot << EOF
  103. set style data points
  104. #set style histogram columnstacked
  105. set boxwidth 0.5
  106. unset style line
  107. set style line 1  linetype 1 linewidth 1 pointsize 1 linecolor palette
  108. set style line 2  linetype 2 linewidth 2 linecolor rgb "#222222"
  109. set style line 3  linetype 3 linewidth 2 pointsize 3 linecolor rgb "#999999"
  110. set style line 4  linetype 4 linewidth 5 pointsize 5 linecolor palette
  111. set style line 5  linetype 5 linewidth 4 pointsize -1 linecolor rgb "#dddddd"
  112. set style increment userstyles
  113. set style fill   transparent border lt -1
  114. set palette model RGB maxcolors 256
  115. set palette defined ( 0 '#77ff77', 25 '#00dd000', 50 '#ffff00', 75 '#dabd00', 100 '#ff0000', 125 '#990000' )
  116. set format x '%.0f'
  117. unset grid
  118. set grid linecolor rgb 'white'
  119. set xtics nomirror out
  120. set ytics nomirror out
  121. set border 3 back
  122. unset key
  123. #set title auto
  124. set xlabel "$tlength sent, $xrange replies ($percent\% loss)" textcolor rgb 'white'
  125. set ylabel "RTT (ms)" textcolor rgb 'white'
  126. set xrange [1:$xrange]
  127. set yrange [0:500]
  128. set xtics rotate 90
  129. set mxtics 100
  130. set tics textcolor rgb 'white'
  131. set cbrange [0:90]
  132. #set autoscale y
  133. set terminal png size 1280,720 font 'Source Sans Pro' 16 butt background '#000000'
  134. set output "$pngfile"
  135. plot "$PLOT" u 2:6:6 with lines ls 0.25 palette cb $avgrtt, "" u 2:5:5 with boxes palette z lw 0.25
  136. EOF
  137.  
  138. end=$(date +'%s')
  139. echo -ne "${BWhite} done in " $(awk -va=$start -vb=$end 'BEGIN{print (a - b)}')
  140. echo " seconds."
  141. echo -e "${Green} Updated plot file:${BWhite} $newfile"
  142. echo -e "${Green} PNG location:${BWhite} $pngfile"
  143. }
  144. ###### End of plot functi9n
  145.  
  146. ####### Ping plot function
  147. plotpings () {
  148. OUT="$1.plot"
  149. echo -e "${Green} Plotting to:${BWhite} $OUT"
  150. ping -c $2 -W 1s -i 1 "$1" \
  151.  |sed -u -e 's/(\|):\|ttl=\|time=//g' \
  152.  | grep "bytes from" \
  153.  |awk 'BEGIN {print "Time\t\tPing\t\tHost\t\tTTL\t  RTT";}
  154.  {srand(); print srand(),"\t", NR,"\t",$5,"\t",$7,"\t",$8;}' \
  155.   > $OUT
  156.    echo "Done! Adding additional data, may take a while..."
  157.    fixplot "$OUT"
  158. }
  159.  
  160. if [ "$1" = "" ]; then
  161.  echo "Malfunction! Need input!"
  162.   exit 1
  163.  else
  164.   if [ "$2" == "" ] && [ -f "$1" ]; then
  165.     echo "File found!"
  166.     cols="$(tail -n 1 "$1" |wc -w)"
  167.       if [ "$cols" = "5" ]; then
  168.          echo "Processing plot file"
  169.      fixplot "$1"
  170.        elif [ "$cols" = "7" ]; then
  171.          echo "Plot file already updated!"
  172.      plotfile "$1"
  173.        else
  174.         echo "Yup, I didn't like that!"
  175.         exit 1
  176.       fi
  177.    elif [ $2 > 14 ]; then
  178.      echo "Attempting to plot pings on $1 for $2 seconds"
  179.      plotpings "$1" "$2"
  180.   fi
  181. fi
  182.  
Advertisement
RAW Paste Data Copied
Advertisement