Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/data/data/com.termux/files/usr/bin/bash
- # V0.2
- ### Simple color output
- NoCol='\033[0m'
- Black='\033[0;30m'
- Red='\033[0;31m'
- Green='\033[0;32m'
- Yellow='\033[0;33m'
- Blue='\033[0;34m'
- Purple='\033[0;35m'
- Cyan='\033[0;36m'
- White='\033[0;37m'
- BBlack='\033[1;30m'
- BRed='\033[1;31m'
- BGreen='\033[1;32m'
- BYellow='\033[1;33m'
- BBlue='\033[1;34m'
- BPurple='\033[1;35m'
- BCyan='\033[1;36m'
- BWhite='\033[1;37m'
- UBlack='\033[4;30m'
- URed='\033[4;31m'
- UGreen='\033[4;32m'
- UYellow='\033[4;33m'
- UBlue='\033[4;34m'
- UPurple='\033[4;35m'
- UCyan='\033[4;36m'
- UWhite='\033[4;37m'
- #### Trap bad exits and errors
- errored () {
- echo -e "${BYellow} Oops! Sonething wasn't right with line ${BWhite}$LINENO${NoCol}"
- }
- trap "errored" ERR
- #### Trap all exits to clear text formatting
- exited () {
- if [ $? -gt 0 ]; then
- echo -e "${BYellow} Sorry, sonething wasn't right with line ${BWhite}$LINENO${NoCol}"
- else
- echo -e "${Bgreen}Script completed normally!${NoCol}"
- fi
- }
- trap "exited" EXIT
- #### Do when killwd
- killed () {
- kill -s HUP $pid
- echo -en "\b\b${BRed}Quit!${NoCol} "
- echo -en "${BWhite}Plot what we have? (y/n)${NoCol} "; read answer
- if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
- fixplot "$OUT"
- exit 0
- else
- echo -e "${BWhite} OK!\n ${UWhite}Plot file is at ${NoCol}${BGreen}$PWD/$1${NoCol}"
- exit 0
- fi
- echo -e "${NoCol}"
- }
- ### Setu0 Plotping (install packages etc)
- setup-plotping () {
- echo -e "${BRed} GNUplot not installed!!"
- echo -en "${BWhite} Install it (y/n)?${NoCol} "; read ans
- if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
- echo -e "${Green} Installing w/o touching pkg...${NoCol}"
- if apt-get install gnuplot; then
- echo -e "${BGreen} All seems good, continuing...${NoCol}"
- else
- echo -e "${BRed} Sonething didn't work, sorry!${NoCol}"
- exit 1
- fi
- else
- echo -e "${BRed}Unfortunately, this script cant graph without it!${NoCol}"
- exit 1
- fi
- }
- #### Teat for GNUPlot and ask to install if not found!
- if dpkg-query -W gnuplot > /dev/null; then
- true
- else
- setup-plotping
- fi
- ############# Adds average RRT per ping and total transit time
- fixplot () {
- TMP=$(mktemp)
- newfile="$1_avg.plot"
- tavg=1
- ttot=0
- ctot=0
- cavg=1
- tail -n+2 $1 > $TMP
- xrange=$(cat $TMP |wc -l)
- lpcount=$(tail -n 1 $TMP |awk '{print $2}')
- rtts=$(cat $TMP |awk '{printf "%.0f + ", $5}')
- yrange=$(cat $TMP |awk '{print $5}' |sort -rn |head -n+1)
- iylow=$(cat $TMP |awk '{print $5}' |sort -n |head -n+1)
- ylow=$(printf "%.0f" $iylow)
- ttot=$(( $rtts 0 ))
- tavg=$(awk -va=$ttot -vb=$lpcount, 'BEGIN{print a / b}')
- rm -f $newfile
- stime=$(cat $TMP |head -n 1 |awk '{print $1}')
- orig=$(cat "$1" |wc -l)
- run=1
- xrange=$(cat $TMP |wc -l)
- cat $TMP |while read line
- do
- rtt=$(echo $line|awk '{printf "%.0f", $5}')
- cping=$(echo $line |awk '{print $2}')
- ctot=$(( $ctot + $rtt ))
- ctots=$(echo "$ctot" | awk '{printf "%.2f", $1 / 1000}')
- cavg=$(( $ctot / $cping ))
- dtime=$(echo $line |awk '{print $1}')
- difftime=$(( $dtime - $stime ))
- sed -e "s/$/\t$cavg\t$ctots/g" <<< $line >> $newfile
- left=$(( $xrange - $run ))
- proc=$(awk -vn=$xrange -vrun=$run 'BEGIN{printf "%.0f", run*(100/n)}')
- echo -ne " ${White}Proc:${BGreen} $proc% ${White}|| Rem:${BGreen}$left ${White} || InFlight:${BGreen}$ctots s ${White}|| Length:${BGreen}$difftime s${White}"\\r
- run=$(( $run + 1 ))
- done
- plotfile "$newfile"
- }
- ######################## End of plotfix
- ### Plot the file
- plotfile () {
- hostpinged="$(echo $1 |sed 's/\.plot_avg\.plot//')"
- newfile="$1"
- echo $newfile
- echo $hostpinged
- startepoc=$(cat $newfile |awk '{print $1}' |head -n 1)
- endepoc=$(cat $newfile |awk '{print $1}' |tail -n 1)
- tlength=$(awk -va=$startepoc -vb=$endepoc 'BEGIN{printf "%.0f", b - a}')
- lavg=$(cat $newfile |awk '{print $5}' |tail -n 1)
- avgrtt=$(printf "%.0f" $lavg)
- topings=$(tail -n1 $newfile |awk '{print $2}')
- yrange=$(cat $newfile |awk '{print $5}' |sort -rn |head -n 1)
- xrange=$(cat $newfile |wc -l)
- if [ $topings -gt $xrange ]; then
- hi=$topings
- lo=$xrange
- elif [ $topings -lt $xrange ]; then
- hi=$xrange
- lo=$topings
- elif [ $topings -eq $xrange ]; then
- hi=$topings
- lo=$topings
- fi
- percent=$(awk -va=$hi -vb=$lo 'BEGIN{printf "%.0f", 100-((100 / a) * b)}')
- PLOT="$newfile"
- pngfile="/sdcard/Download/$PLOT.png"
- error="$(mktemp)"
- echo -e "${Green} Plot file: ${White}$pngfile"
- echo -e "${Green} Values:${White}X:$xrange/Y:$yrange/Z:$avgrtt/P:$tlength/Loss=$percent%/Pings seen:$topings${NoCol}"
- ###### Plot data
- gnuplot 2> $error << EOF
- set style data lines
- #set style histogram clustered gap 0
- set boxwidth 0.5
- unset style line
- set style line 1 linetype 1 linewidth 0.5 pointsize 0.5 linecolor palette
- set style line 2 linetype 2 linewidth 2 linecolor rgb "#2222ff"
- set style line 3 linetype 3 linewidth 1 pointsize 1 pointtype 7 linecolor palette
- set style line 4 linetype 4 linewidth 0.5 pointsize 0.5 pointtype 7 linecolor palette
- set style line 5 linetype 5 linewidth 2 pointsize 1 linecolor rgb "#dddddd"
- set style increment userstyles
- set style fill transparent border lt -1
- set palette model RGB maxcolors 256
- set palette defined ( 0 '#77ff77', 25 '#00dd000', 50 '#ffff00', 75 '#dabd00', 100 '#ff0000', 125 '#990000' )
- set format x '%.0f'
- unset grid
- set title "$hostpinged" textcolor rgb "white"
- set grid linecolor rgb 'white'
- set xtics nomirror out
- set ytics nomirror out
- set border 3 back
- unset key
- #set title auto
- set xlabel "Sent:$hi | Recv:$lo | Loss:$percent\%" textcolor rgb 'white'
- set ylabel "RTT (ms)" textcolor rgb 'white'
- set xrange [1:$xrange]
- set yrange [0:500]
- set xtics rotate 90
- set mxtics 100
- set tics textcolor rgb 'white'
- set cbrange [0:100]
- #set autoscale y
- set terminal png size 1280,720 font 'Source Sans Pro' 16 butt background '#000000'
- set output "$pngfile"
- 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
- #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
- EOF
- end=$(date +'%s')
- printend () {
- echo -ne "${Green} Finished:${BWhite} in" \
- $(awk -va=$start -vb=$end 'BEGIN{print (a - b) / -1}')
- echo " seconds, $1"
- }
- echo -e "${Green} Updated plot file:${BWhite} $newfile"
- echo -e "${Green} PNG location:${BWhite} $pngfile"
- if [ $(cat $error |wc -m) -gt 0 ]; then
- exstat="$(echo -e ${URed} with errors!${NoCol})"
- printend "$exstat" "$error"
- echo -ne "${URed} Error was:${Red}"
- cat $error; echo -e "${NoCol}"
- else
- exstat="$(echo -e ${BWhite}successfully!${NoCol})"
- printend "$exstat"
- fi
- }
- ###### End of plot functi9n
- ####### Ping plot function
- plotpings () {
- OUT="$1.plot"
- echo -e "${Green} Plot file: ${BWhite} $OUT"
- ping -c $2 -W 1s -i 1 "$1" \
- |sed -u -e 's/(\|):\|ttl=\|time=//g' \
- |grep "bytes from" \
- |awk 'BEGIN {print "Time\t\tPing\t\tHost\t\tTTL\t RTT";}
- {srand(); print srand(),"\t", NR,"\t",$5,"\t",$7,"\t",$8;}' \
- > $OUT&
- pid=$!
- trap "killed $OUT $pid" SIGINT
- timer=$2
- prun=1
- while kill -0 $pid 2> /dev/null
- do
- echo -en " ${BBlue}Progress: ${White}$prun/$2 ${NoCol}"\\r
- sleep 1
- prun=$(( $prun + 1 ))
- done
- echo -e "${BGreen}Done! ${White}Adding additional data, may take a while...${NoCol}"
- fixplot "$OUT"
- }
- ###### Start of script
- if [ "$1" = "" ]; then
- echo -e "${URed}Malfunction! Need input!${NoCol}"
- exit 1
- else
- if [ "$2" = "" ] && [ -f "$1" ]; then
- cols="$(tail -n 1 "$1" |wc -w)"
- if [ "$cols" = "5" ]; then
- clear
- echo -e "${UGreen} Processing plot file ${NoCol}"
- start=$(date +'%s')
- fixplot "$1"
- elif [ "$cols" = "7" ]; then
- clear
- start=$(date +'%s')
- plotfile "$1"
- elif [ "$cols" = "" ]; then
- echo -e "${Red} File not valid! ${NoCol}"
- exit 1
- fi
- elif [ "$2" = "" ]; then
- echo -e "${URed}An error happened!${White} Likely invalid time set${NoCol}"
- exit 1
- elif [ $2 -gt 14 ]; then
- clear
- echo -e "${White}Plotting pings on $1 for $2 seconds${NoCol}"
- start=$(date +'%s')
- timer=$2
- plotpings "$1" "$2"
- else
- echo -e "${BRed}Error:${BWhite} Ping longer to build better grqphs!${NoCol}"
- exit 1
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement