Skip to content

Commit

Permalink
Merge pull request #1 from marcinrogacki/inprogress
Browse files Browse the repository at this point in the history
plotping v 1.0.0
  • Loading branch information
marcinrogacki committed Apr 15, 2016
2 parents 77ed603 + 0c0b0cd commit b566b7f
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 28 deletions.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
ABOUT
=====

Plotping is a simple script which tracks ping times and plot them at terminal.
In addition it calculates tresholds.

INSTALLATION
============

Grant permissions:

```
chmod +x <full_path_to_plotping_dir>/bin/plotping`
```

Make it user friendly:

```
ln -s <full_path_to_plotping_dir>/bin/plotping /usr/bin/
```

or in `~/.bash_profile`

```
export PATH=$PATH:<full_path_to_plotping_dir>/bin
```

USAGE
=====

plotping help

SCREENSHOT
==========

![plotping screenshot](plotping.png?raw=true "plotping")

NOTE
====

Threshold are calculated incrementally. It can takes time to invoke a COMMAND
'show' after longer lack of usage.
Plotping does not count pings which reached a timeout.
It also does not count pings in equal time intervals.

COPYRIGHT
=========

MIT
Copyright (c) 2016 Marcin Rogacki <rogacki.m@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AUTHOR
======

Marcin Rogacki rogacki.m@gmail.com

https://github.com/marcinrogacki/plotping.git
Binary file added plotping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
199 changes: 171 additions & 28 deletions plotping.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
#!/bin/sh

# DEPENDENCIES: ping, gnuplot, bc, awk, grep

file_tmp_proc_numb=/tmp/plotping.pid
file_tmp_ping_data=/tmp/plotping.ping.dat
file_tmp_plot_data=/tmp/plotping.plot.dat
file_tmp_treshhold=/tmp/plotping.treshhold.dat
file_tmp_treshhold_plot_increment=/tmp/plotping.treshhold_plot_increment.dat

command=$1
shift

domain="www.google.com"
last_shown_ping_number=10
while test $# -gt 0; do
case "$1" in
-d)
shift
domain="$1"
shift
;;
-n)
shift
if [[ $1 =~ ^-?[0-9]+$ ]]; then
last_shown_ping_number=$1
else
echo "Wrong value for parameter '-n'. Value should be an integer"
exit
fi
shift
;;
*)
shift
;;
esac
done


function check_script_dependencies() {
command -v ping >/dev/null 2>&1 || { echo >&2 "A command 'ping' is not installed. Aborting."; exit 1; }
command -v grep >/dev/null 2>&1 || { echo >&2 "A command 'grep' is not installed. Aborting."; exit 1; }
command -v awk >/dev/null 2>&1 || { echo >&2 "A command 'awk' is not installed. Aborting."; exit 1; }
command -v gnuplot >/dev/null 2>&1 || { echo >&2 "A command 'gnuplot' is not installed. Aborting."; exit 1; }
command -v bc >/dev/null 2>&1 || { echo >&2 "A command 'bc' is not installed. Aborting."; exit 1; }
}

function show_remaining_pings() {
echo "List of remaining 'ping' processes which you could consider to stop (command: plotping stop-all)":
ps aux | grep -i ping | grep -v 'grep -i ping' | grep -v 'plotping'
echo "List of remaining 'ping' processes which you could consider to stop (COMMAND: plotping stop-all)":
ps aux | grep -i ping | grep -v 'grep -i ping' | grep -v 'plotping'
}

function rm_tmp_files() {
Expand All @@ -19,21 +60,104 @@ function rm_tmp_files() {
if [ -f "$file_tmp_plot_data" ]; then
rm "$file_tmp_plot_data"
fi
if [ -f "$file_tmp_treshhold" ]; then
rm "$file_tmp_treshhold"
fi
if [ -f "$file_tmp_treshhold_plot_increment" ]; then
rm "$file_tmp_treshhold_plot_increment"
fi
}

command=$1
shift
case $command in
function generate_plot_data() {
cat "$file_tmp_ping_data" 2>/dev/null | awk -F [=\ ] {'print $(NF-1)'} | grep -E "[0-9]" > "$file_tmp_plot_data"
}

function display_treshholds() {
if [ -f "$file_tmp_treshhold" ]; then
t25=`awk '{ print $1 }' "$file_tmp_treshhold"`
t100=`awk '{ print $2 }' "$file_tmp_treshhold"`
t300=`awk '{ print $3 }' "$file_tmp_treshhold"`
t500=`awk '{ print $4 }' "$file_tmp_treshhold"`
tRest=`awk '{ print $5 }' "$file_tmp_treshhold"`
all=`awk '{ print $6 }' "$file_tmp_treshhold"`
else
t25=0
t100=0
t300=0
t500=0
tRest=0
all=0
fi

tail -n "+$((all+1))" "$file_tmp_plot_data" > "$file_tmp_treshhold_plot_increment"
while read t; do
if [ $(bc <<< "$t >= 0") -eq 1 -a $(bc <<< "$t <= 25") -eq 1 ]; then
t25=$((t25+1));
elif [ $(bc <<< "$t > 25") -eq 1 -a $(bc <<< "$t <= 100") -eq 1 ]; then
t100=$((t100+1));
elif [ $(bc <<< "$t > 100") -eq 1 -a $(bc <<< "$t <= 300") -eq 1 ]; then
t300=$((t300+1));
elif [ $(bc <<< "$t > 300") -eq 1 -a $(bc <<< "$t <= 500") -eq 1 ]; then
t500=$((t500+1));
else
tRest=$((tRest+1));
fi
all=$((all+1));
done < "$file_tmp_treshhold_plot_increment"
echo "$t25 $t100 $t300 $t500 $tRest $all" > "$file_tmp_treshhold"

if [ $all -eq 0 ]; then
p25=0
p100=0
p300=0
p500=0
pRest=0
else
p25=`bc -l <<< "scale=2; $t25*100/$all"`
p100=`bc -l <<< "scale=2; $t100*100/$all"`
p300=`bc -l <<< "scale=2; $t300*100/$all"`
p500=`bc -l <<< "scale=2; $t500*100/$all"`
pRest=`bc -l <<< "scale=2; $tRest*100/$all"`
fi

echo "THRESHOLDS"
echo "[0-25: $p25%] [25-100: $p100%] [100-300: $p300%] [300-500: $p500%] [500+: $pRest%]"
}

function plot_ping() {
gnuplot -e "set term dumb; set ylabel 'ms'; set xlabel 'Number of pings'; plot '$file_tmp_plot_data' notitle"
}

function show_latest_pings() {
echo "LATEST $last_shown_ping_number PINGS"
tail -n $last_shown_ping_number "$file_tmp_ping_data"
}

case "$command" in
'start')
check_script_dependencies
if [ -f "$file_tmp_proc_numb" ] && kill -0 `cat "$file_tmp_proc_numb"` > /dev/null 2>&1; then
echo 'Tracking process is already started.'
else
rm_tmp_files
ping www.google.com > "$file_tmp_ping_data" 2>/dev/null &
ping "$domain" > "$file_tmp_ping_data" 2>/dev/null &
echo $! > "$file_tmp_proc_numb"
echo "Ping process id:" `cat "$file_tmp_proc_numb"`
fi
;;
'show')
check_script_dependencies
if [ -f "$file_tmp_ping_data" ]; then
echo
generate_plot_data
display_treshholds
plot_ping
show_latest_pings
exit
else
echo 'Tracking process is not started (COMMAND: plotping start)'
fi
;;
'stop')
if [ -f "$file_tmp_proc_numb" ]; then
kill -9 `cat "$file_tmp_proc_numb"` 2>/dev/null
Expand All @@ -58,29 +182,48 @@ case $command in
fi
;;
'clean')
if [ -f "$file_tmp_ping_data" ]; then
echo -n > "$file_tmp_ping_data"
fi
echo 'Done'
rm_tmp_files
;;
'show')
if [ -f "$file_tmp_ping_data" ]; then
echo
echo "THRESHOLDS"
echo "[0-25: 13%] [25-100: 25%] [100-300: 10%] [300-500: 2%] [500-1000+: 1%]"
cat "$file_tmp_ping_data" 2>/dev/null | awk -F [=\ ] {'print $(NF-1)'} | grep -E "[0-9]" > "$file_tmp_plot_data"
gnuplot -e "set term dumb; set ylabel 'ms'; set xlabel 'Number of pings'; plot '$file_tmp_plot_data' notitle"
echo "LATEST 10 PINGS"
tail -n 10 "$file_tmp_ping_data"
exit
else
echo 'Tracking process is not started (command: plotping start)'
fi
;;
*|-h|--help)
echo 'Help soon...'
*|-h|--help)
cat << USAGE
Usage: plotping COMMAND [OPTIONS]
COMMAND
start Start process of tracking ping
show Plot and displays inforation about ping
stop Stop process of tracking ping
stop-all Stop all 'ping' in system (in case when 'stop' COMMAND fails)
clean Removes all temporarliy files (stored in /tmp). Files
are removed as well during COMMAND 'stop' and 'stop-all'
help|--help|-h This help
OPTIONS
-d Domain name which pings will be tracked (default: www.google.com).
Usable in COMMAND 'start'.
-n Number of last shown pings
Usable in COMMAND 'show'.
Example
plotping start -d www.google.pl
watch -n 5 plotping show -n 5
plotping stop
About
Plotping is a simple script which tracks ping times and plot them at terminal.
In addition it calculates tresholds.
Note
Threshold are calculated incrementally. It can takes time to invoke a COMMAND
'show' after longer lack of usage.
Plotping does not count pings which reached a timeout.
It also does not count pings in equal time intervals.
Author
Marcin Rogacki <rogacki.m@gmail.com>
https://github.com/marcinrogacki/plotping.git
USAGE
exit
;;
esac

echo # move to a new line
echo 'Exit'
echo 'Done. Exit'

0 comments on commit b566b7f

Please sign in to comment.