Advertisement
IssyPutchy

Plotping v0.2

May 18th, 2022
855
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.2
  3.  
  4. ### Simple color output
  5. NoCol='\033[0m'
  6. Black='\033[0;30m'
  7. Red='\033[0;31m'
  8. Green='\033[0;32m'
  9. Yellow='\033[0;33m'
  10. Blue='\033[0;34m'
  11. Purple='\033[0;35m'
  12. Cyan='\033[0;36m'
  13. White='\033[0;37m'
  14. BBlack='\033[1;30m'
  15. BRed='\033[1;31m'
  16. BGreen='\033[1;32m'
  17. BYellow='\033[1;33m'
  18. BBlue='\033[1;34m'
  19. BPurple='\033[1;35m'
  20. BCyan='\033[1;36m'
  21. BWhite='\033[1;37m'
  22. UBlack='\033[4;30m'
  23. URed='\033[4;31m'
  24. UGreen='\033[4;32m'
  25. UYellow='\033[4;33m'
  26. UBlue='\033[4;34m'
  27. UPurple='\033[4;35m'
  28. UCyan='\033[4;36m'
  29. UWhite='\033[4;37m'
  30.  
  31. #### Trap bad exits and errors
  32. errored () {
  33.   echo -e "${BYellow} Oops! Sonething wasn't right with line ${BWhite}$LINENO${NoCol}"
  34. }
  35. trap "errored" ERR
  36.  
  37. #### Trap all exits to clear text formatting
  38. exited () {
  39.    if [ $? -gt 0 ]; then
  40.       echo -e "${BYellow} Sorry, sonething wasn't right with line ${BWhite}$LINENO${NoCol}"
  41.     else
  42.       echo -e "${Bgreen}Script completed normally!${NoCol}"
  43.    fi
  44. }
  45. trap "exited" EXIT
  46.  
  47. #### Do when killwd
  48. killed () {
  49.   kill -s HUP $pid
  50.   echo -en "\b\b${BRed}Quit!${NoCol} "
  51.   echo -en "${BWhite}Plot what we have? (y/n)${NoCol} "; read answer
  52.    if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
  53.       fixplot "$OUT"
  54.       exit 0
  55.     else
  56.       echo -e "${BWhite} OK!\n ${UWhite}Plot file is at ${NoCol}${BGreen}$PWD/$1${NoCol}"
  57.       exit 0
  58.    fi
  59.  echo -e "${NoCol}"
  60. }
  61.  
  62. ### Setu0 Plotping (install packages etc)
  63. setup-plotping () {
  64.    echo -e "${BRed} GNUplot not installed!!"
  65.    echo -en "${BWhite} Install it (y/n)?${NoCol} "; read ans
  66.     if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
  67.       echo -e "${Green} Installing w/o touching pkg...${NoCol}"
  68.         if apt-get install gnuplot; then
  69.        echo -e "${BGreen} All seems good, continuing...${NoCol}"
  70.          else
  71.        echo -e "${BRed} Sonething didn't work, sorry!${NoCol}"
  72.        exit 1
  73.     fi
  74.       else
  75.        echo -e "${BRed}Unfortunately, this script cant graph without it!${NoCol}"
  76.        exit 1
  77.      fi
  78. }
  79.  
  80. #### Teat for GNUPlot and ask to install if not found!
  81. if dpkg-query -W gnuplot > /dev/null; then
  82.     true
  83.    else
  84.     setup-plotping
  85. fi
  86.  
  87. ############# Adds average RRT per ping and total transit time
  88. fixplot () {
  89.  
  90. TMP=$(mktemp)
  91. newfile="$1_avg.plot"
  92. tavg=1
  93. ttot=0
  94. ctot=0
  95. cavg=1
  96. tail -n+2 $1 > $TMP
  97. xrange=$(cat $TMP |wc -l)
  98. lpcount=$(tail -n 1 $TMP |awk '{print $2}')
  99. rtts=$(cat $TMP |awk '{printf "%.0f + ", $5}')
  100. yrange=$(cat $TMP |awk '{print $5}' |sort -rn |head -n+1)
  101. iylow=$(cat $TMP |awk '{print $5}' |sort -n |head -n+1)
  102. ylow=$(printf "%.0f" $iylow)
  103. ttot=$(( $rtts  0 ))
  104. tavg=$(awk -va=$ttot -vb=$lpcount, 'BEGIN{print a / b}')
  105. rm -f $newfile
  106.  
  107. stime=$(cat $TMP |head -n 1 |awk '{print $1}')
  108. orig=$(cat "$1" |wc -l)
  109. run=1
  110. xrange=$(cat $TMP |wc -l)
  111.  
  112. cat $TMP |while read line
  113.     do
  114.    rtt=$(echo $line|awk '{printf "%.0f", $5}')
  115.    cping=$(echo $line |awk '{print $2}')
  116.    ctot=$(( $ctot + $rtt ))
  117.    ctots=$(echo "$ctot" | awk '{printf "%.2f", $1 / 1000}')
  118.    cavg=$(( $ctot / $cping ))
  119.    dtime=$(echo $line |awk '{print $1}')
  120.    difftime=$(( $dtime - $stime ))
  121.    sed -e "s/$/\t$cavg\t$ctots/g"  <<< $line >> $newfile
  122.    left=$(( $xrange - $run ))
  123.    proc=$(awk -vn=$xrange -vrun=$run 'BEGIN{printf "%.0f", run*(100/n)}')
  124.    echo -ne " ${White}Proc:${BGreen} $proc% ${White}|| Rem:${BGreen}$left ${White} || InFlight:${BGreen}$ctots s ${White}|| Length:${BGreen}$difftime s${White}"\\r
  125.    run=$(( $run + 1 ))
  126.  
  127. done
  128.  
  129. plotfile "$newfile"
  130. }
  131. ######################## End of plotfix
  132.  
  133. ### Plot the file
  134. plotfile () {
  135. hostpinged="$(echo $1 |sed 's/\.plot_avg\.plot//')"
  136. newfile="$1"
  137. echo $newfile
  138. echo $hostpinged
  139. startepoc=$(cat $newfile |awk '{print $1}' |head -n 1)
  140. endepoc=$(cat $newfile |awk '{print $1}' |tail -n 1)
  141. tlength=$(awk -va=$startepoc -vb=$endepoc 'BEGIN{printf "%.0f", b - a}')
  142. lavg=$(cat $newfile |awk '{print $5}' |tail -n 1)
  143. avgrtt=$(printf "%.0f" $lavg)
  144. topings=$(tail -n1 $newfile |awk '{print $2}')
  145. yrange=$(cat $newfile |awk '{print $5}' |sort -rn |head -n 1)
  146. xrange=$(cat $newfile |wc -l)
  147.   if [ $topings -gt $xrange ]; then
  148.    hi=$topings
  149.    lo=$xrange
  150.   elif [ $topings -lt $xrange ]; then
  151.    hi=$xrange
  152.    lo=$topings
  153.   elif [ $topings -eq $xrange ]; then
  154.    hi=$topings
  155.    lo=$topings
  156.   fi
  157. percent=$(awk -va=$hi -vb=$lo 'BEGIN{printf "%.0f", 100-((100 / a) * b)}')
  158. PLOT="$newfile"
  159. pngfile="/sdcard/Download/$PLOT.png"
  160.  
  161. error="$(mktemp)"
  162. echo -e "${Green} Plot file: ${White}$pngfile"
  163. echo -e "${Green} Values:${White}X:$xrange/Y:$yrange/Z:$avgrtt/P:$tlength/Loss=$percent%/Pings seen:$topings${NoCol}"
  164.  
  165. ###### Plot data
  166. gnuplot 2> $error << EOF
  167. set style data lines
  168. #set style histogram clustered gap 0
  169. set boxwidth 0.5
  170. unset style line
  171. set style line 1  linetype 1 linewidth 0.5 pointsize 0.5 linecolor palette
  172. set style line 2  linetype 2 linewidth 2 linecolor rgb "#2222ff"
  173. set style line 3  linetype 3 linewidth 1 pointsize 1 pointtype 7 linecolor palette
  174. set style line 4  linetype 4 linewidth 0.5 pointsize 0.5 pointtype 7 linecolor palette
  175. set style line 5  linetype 5 linewidth 2 pointsize 1 linecolor rgb "#dddddd"
  176. set style increment userstyles
  177. set style fill   transparent border lt -1
  178. set palette model RGB maxcolors 256
  179. set palette defined ( 0 '#77ff77', 25 '#00dd000', 50 '#ffff00', 75 '#dabd00', 100 '#ff0000', 125 '#990000' )
  180. set format x '%.0f'
  181. unset grid
  182. set title "$hostpinged" textcolor rgb "white"
  183. set grid linecolor rgb 'white'
  184. set xtics nomirror out
  185. set ytics nomirror out
  186. set border 3 back
  187. unset key
  188. #set title auto
  189. set xlabel "Sent:$hi | Recv:$lo | Loss:$percent\%" textcolor rgb 'white'
  190. set ylabel "RTT (ms)" textcolor rgb 'white'
  191. set xrange [1:$xrange]
  192. set yrange [0:500]
  193. set xtics rotate 90
  194. set mxtics 100
  195. set tics textcolor rgb 'white'
  196. set cbrange [0:100]
  197. #set autoscale y
  198. set terminal png size 1280,720 font 'Source Sans Pro' 16 butt background '#000000'
  199. set output "$pngfile"
  200. plot "$PLOT" u 2:5:5 with linespoints lw 0.5 ps 2 palette z,  "" u 2:6:5 with lines palette cb $avgrtt lw 5
  201. #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, "" u 2:6:6 with lines lw 5 palette z
  202. EOF
  203.  
  204. end=$(date +'%s')
  205.  
  206. printend () {
  207.   echo -ne "${Green} Finished:${BWhite} in" \
  208.     $(awk -va=$start -vb=$end 'BEGIN{print (a - b) / -1}')
  209.     echo " seconds, $1"
  210.     }
  211.  
  212.  echo -e "${Green} Updated plot file:${BWhite} $newfile"
  213.  echo -e "${Green} PNG location:${BWhite} $pngfile"
  214.   if [ $(cat $error |wc -m) -gt 0 ]; then
  215.      exstat="$(echo -e ${URed} with errors!${NoCol})"
  216.      printend "$exstat" "$error"
  217.      echo -ne "${URed} Error was:${Red}"
  218.      cat $error; echo -e "${NoCol}"
  219.    else
  220.      exstat="$(echo -e ${BWhite}successfully!${NoCol})"
  221.      printend "$exstat"
  222.   fi
  223.  
  224. }
  225.  
  226. ###### End of plot functi9n
  227.  
  228. ####### Ping plot function
  229. plotpings () {
  230. OUT="$1.plot"
  231. echo -e "${Green} Plot file: ${BWhite} $OUT"
  232. ping -c $2 -W 1s -i 1 "$1" \
  233.  |sed -u -e 's/(\|):\|ttl=\|time=//g' \
  234.  |grep "bytes from" \
  235.  |awk 'BEGIN {print "Time\t\tPing\t\tHost\t\tTTL\t  RTT";}
  236.  {srand(); print srand(),"\t", NR,"\t",$5,"\t",$7,"\t",$8;}' \
  237.   > $OUT&
  238. pid=$!
  239. trap "killed $OUT $pid" SIGINT
  240. timer=$2
  241. prun=1
  242. while kill -0 $pid 2> /dev/null
  243.         do
  244.    echo -en " ${BBlue}Progress: ${White}$prun/$2 ${NoCol}"\\r
  245.    sleep 1
  246.    prun=$(( $prun + 1 ))
  247. done
  248.    echo -e "${BGreen}Done! ${White}Adding additional data, may take a while...${NoCol}"
  249.    fixplot "$OUT"
  250. }
  251.  
  252. ###### Start of script
  253. if [ "$1" = "" ]; then
  254.   echo -e "${URed}Malfunction! Need input!${NoCol}"
  255.   exit 1
  256.  else
  257.    if [ "$2" = "" ] && [ -f "$1" ]; then
  258.      cols="$(tail -n 1 "$1" |wc -w)"
  259.       if [ "$cols" = "5" ]; then
  260.      clear
  261.      echo -e "${UGreen} Processing plot file ${NoCol}"
  262.      start=$(date +'%s')
  263.      fixplot "$1"
  264.        elif [ "$cols" = "7" ]; then
  265.      clear
  266.      start=$(date +'%s')
  267.      plotfile "$1"
  268.        elif [ "$cols" = "" ]; then
  269.          echo -e "${Red} File not valid! ${NoCol}"
  270.          exit 1
  271.        fi
  272.      elif [ "$2" = "" ]; then
  273.     echo -e "${URed}An error happened!${White} Likely invalid time set${NoCol}"
  274.     exit 1
  275.      elif [ $2 -gt 14 ]; then
  276.        clear
  277.        echo -e "${White}Plotting pings on $1 for $2 seconds${NoCol}"
  278.        start=$(date +'%s')
  279.        timer=$2
  280.        plotpings "$1" "$2"
  281.      else
  282.     echo -e "${BRed}Error:${BWhite} Ping longer to build better grqphs!${NoCol}"
  283.     exit 1
  284.     fi
  285. fi
  286.  
Advertisement
RAW Paste Data Copied
Advertisement