Advertisement
IssyPutchy

Plotping v0.3

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