IssyPutchy

Plotping v0.3

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