Advertisement
IssyPutchy

Plotping v0.2

May 20th, 2022
745
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 $hostpinged
  138. startepoc=$(cat $newfile |awk '{print $1}' |head -n 1)
  139. endepoc=$(cat $newfile |awk '{print $1}' |tail -n 1)
  140. tlength=$(awk -va=$startepoc -vb=$endepoc 'BEGIN{printf "%.0f", b - a}')
  141. lavg=$(cat $newfile |awk '{print $5}' |tail -n 1)
  142. avgrtt=$(printf "%.0f" $lavg)
  143. topings=$(tail -n1 $newfile |awk '{print $2}')
  144. yrange=$(cat $newfile |awk '{print $5}' |sort -rn |head -n 1)
  145. xrange=$(cat $newfile |wc -l)
  146.   if [ $topings -gt $xrange ]; then
  147.    hi=$topings
  148.    lo=$xrange
  149.   elif [ $topings -lt $xrange ]; then
  150.    hi=$xrange
  151.    lo=$topings
  152.   elif [ $topings -eq $xrange ]; then
  153.    hi=$topings
  154.    lo=$topings
  155.   fi
  156. percent=$(awk -va=$hi -vb=$lo 'BEGIN{printf "%.0f", 100-((100 / a) * b)}')
  157. xlen=$(echo $xrange |wc -m)
  158. xlen=$(( $xlen - 1 ))
  159. xticlen=$(printf -- '0%.0s' {1..$xlen})
  160. xtics="1$xticlen"
  161. echo -e "${White}Xtics at $xtics${NoCol}"
  162. PLOT="$newfile"
  163. pngfile="/sdcard/Download/$PLOT.png"
  164.  
  165. error="$(mktemp)"
  166. echo -e "${Green} Plot file: ${White}$pngfile"
  167. echo -e "${Green} Values:${White}X:$xrange/Y:$yrange/Z:$avgrtt/P:$tlength/Loss=$percent%/Pings seen:$topings${NoCol}"
  168.  
  169. ###### Plot data
  170. gnuplot 2> $error << EOF
  171. set style data lines
  172. #set style histogram clustered gap 0
  173. set boxwidth 0.5
  174. unset style line
  175. set style line 1  linetype 1 linewidth 0.5 pointsize 0.5 linecolor palette
  176. set style line 2  linetype 2 linewidth 2 linecolor rgb "#2222ff"
  177. set style line 3  linetype 3 linewidth 1 pointsize 1 pointtype 7 linecolor palette
  178. set style line 4  linetype 4 linewidth 0.5 pointsize 0.5 pointtype 7 linecolor palette
  179. set style line 5  linetype 5 linewidth 2 pointsize 1 linecolor rgb "#dddddd"
  180. set style increment userstyles
  181. set style fill   transparent border lt -1
  182. set palette model RGB maxcolors 256
  183. set palette defined ( 0 '#77ff77', 25 '#00dd000', 50 '#ffff00', 75 '#dabd00', 100 '#ff0000', 125 '#990000' )
  184. set format x '%.0f'
  185. unset grid
  186. set title "$hostpinged" textcolor rgb "white"
  187. set grid linecolor rgb 'white'
  188. set xtics nomirror out
  189. set ytics nomirror out
  190. set border 3 back
  191. unset key
  192. #set title auto
  193. set xlabel "Sent:$hi | Recv:$lo | Loss:$percent\%" textcolor rgb 'white'
  194. set ylabel "RTT (ms)" textcolor rgb 'white'
  195. set xrange [1:$xrange]
  196. set yrange [0:500]
  197. set xtics rotate 90
  198. set xtics $xtics
  199. set tics textcolor rgb 'white'
  200. set cbrange [0:150]
  201. #set autoscale y
  202. set terminal png size 1280,720 font 'Source Sans Pro' 16 butt background '#000000'
  203. set output "$pngfile"
  204. plot "$PLOT" u 2:5:5 with boxes lw 0.5 palette z,  "" u 2:6:5 with lines palette cb $avgrtt lw 5
  205. #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
  206. EOF
  207.  
  208. end=$(date +'%s')
  209.  
  210. printend () {
  211.   echo -ne "${Green} Finished:${BWhite} in" \
  212.     $(awk -va=$start -vb=$end 'BEGIN{print (a - b) / -1}')
  213.     echo " seconds, $1"
  214.     }
  215.  
  216.  echo -e "${Green} Updated plot file:${BWhite} $newfile"
  217.  echo -e "${Green} PNG location:${BWhite} $pngfile"
  218.   if [ $(cat $error |wc -m) -gt 0 ]; then
  219.      exstat="$(echo -e ${URed} with errors!${NoCol})"
  220.      printend "$exstat" "$error"
  221.      echo -ne "${URed} Error was:${Red}"
  222.      cat $error; echo -e "${NoCol}"
  223.    else
  224.      exstat="$(echo -e ${BWhite}successfully!${NoCol})"
  225.      printend "$exstat"
  226.   fi
  227.  
  228. }
  229.  
  230. ###### End of plot functi9n
  231.  
  232. ####### Ping plot function
  233. plotpings () {
  234.   if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  235.    awk='"\t",$4,"\t",$6,"\t",$7'
  236.    else
  237.    awk='"\t",$5,"\t",$7,"\t",$8'
  238.   fi
  239. OUT="$1.plot"
  240. echo -e "${Green} Plot file: ${BWhite} $OUT"
  241.   if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  242.      ping -c $2 -W 1s -i 1 "$1" \
  243.       |sed -u -e 's/:\|ttl=\|time=//g' \
  244.       |grep "bytes from" \
  245.       |awk 'BEGIN {print "Time\t\tPing\t\tHost\t\tTTL\t  RTT";}
  246.      {srand(); print srand(),"\t", NR,"\t",$4,"\t",$6,"\t",$7;}' \
  247.        > $OUT&
  248.    else
  249.      ping -c $2 -W 1s -i 1 "$1" \
  250.       |sed -u -e 's/(\|):\|ttl=\|time=//g' \
  251.       |grep "bytes from" \
  252.       |awk 'BEGIN {print "Time\t\tPing\t\tHost\t\tTTL\t  RTT";}
  253.      {srand(); print srand(),"\t", NR,"\t",$5,"\t",$7,"\t",$8;}' \
  254.        > $OUT&
  255. fi
  256. pid=$!
  257. trap "killed $OUT $pid" SIGINT
  258. timer=$2
  259. prun=1
  260. while kill -0 $pid 2> /dev/null
  261.         do
  262.    echo -en " ${BBlue}Progress: ${White}$prun/$2 ${NoCol}"\\r
  263.    sleep 1
  264.    prun=$(( $prun + 1 ))
  265. done
  266.    echo -e "${BGreen}Done! ${White}Adding additional data, may take a while...${NoCol}"
  267.    fixplot "$OUT"
  268. }
  269.  
  270. ###### Start of script
  271. if [ "$1" = "" ]; then
  272.   echo -e "${URed}Malfunction! Need input!${NoCol}"
  273.   exit 1
  274.  else
  275.    if [ "$2" = "" ] && [ -f "$1" ]; then
  276.      cols="$(tail -n 1 "$1" |wc -w)"
  277.       if [ "$cols" = "5" ]; then
  278.      clear
  279.      echo -e "${UGreen} Processing plot file ${NoCol}"
  280.      start=$(date +'%s')
  281.      fixplot "$1"
  282.        elif [ "$cols" = "7" ]; then
  283.      clear
  284.      start=$(date +'%s')
  285.      plotfile "$1"
  286.        elif [ "$cols" = "" ]; then
  287.          echo -e "${Red} File not valid! ${NoCol}"
  288.          exit 1
  289.        fi
  290.      elif [ "$2" = "" ]; then
  291.     echo -e "${URed}An error happened!${White} Likely invalid time set${NoCol}"
  292.     exit 1
  293.      elif [ $2 -gt 14 ]; then
  294.        clear
  295.        echo -e "${White}Plotting pings on $1 for $2 seconds${NoCol}"
  296.        start=$(date +'%s')
  297.        timer=$2
  298.        plotpings "$1" "$2"
  299.      else
  300.     echo -e "${BRed}Error:${BWhite} Ping longer to build better grqphs!${NoCol}"
  301.     exit 1
  302.     fi
  303. fi
  304.  
Advertisement
RAW Paste Data Copied
Advertisement