-
Notifications
You must be signed in to change notification settings - Fork 2
/
ping+chart.sh
executable file
·122 lines (113 loc) · 2.32 KB
/
ping+chart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
source chart.sh
read_ping() {
# read statistics store
if [ ! -n "$NO_HISTORY" ]; then
while read STAT_FILES; do
cat $STAT_FILES | awk "$AWK_PROG" | xargs echo a
done < <(find statistics -type f | sort -r | tail -n+2 | head -n $((HISTORY_COUNT * BATCH_SIZE)) | tac | xargs -n $BATCH_SIZE)
echo g
fi
# read incremental log
while read LINE; do
if [[ "$LINE" =~ ^.*saved.\((.*)\).*$ ]]; then
cat ${BASH_REMATCH[1]} | awk "$AWK_PROG" | xargs echo a
echo g
fi
done
}
INPUT_COMMAND="bash ping.sh"
CHART_COMMAND="to_chart"
HISTORY_COUNT="$(tput cols)"
BATCH_SIZE=1
while true; do
case "$1" in
-h|--help)
echo >&2 "Usage: $0 [-h] [-f|-q] [-c] [-r] [<column>] [<addr-filter>]"
exit
;;
-f|--follow)
INPUT_COMMAND="tail -n0 -f $2"
shift
;;
-q|--quiet)
INPUT_COMMAND="echo"
;;
-r|--raw)
CHART_COMMAND=cat
;;
-b|--batch)
INPUT_COMMAND="echo"
BATCH_SIZE=$2
shift
;;
-B|--batch-method)
BATCH_METHOD=$2
shift
;;
-h|--history)
HISTORY_COUNT=$2
shift
;;
*)
break
;;
esac
shift
done
if [ -n "$1" ]; then
case "$1" in
x|sent)
COLUMN_ID=3
;;
r|receive)
COLUMN_ID=4
;;
q|loss)
COLUMN_ID=5
;;
a|min)
COLUMN_ID=6
;;
v|avg)
COLUMN_ID=7
;;
b|max)
COLUMN_ID=8
;;
k|stddev|mdev)
COLUMN_ID=9
;;
[3-9])
COLUMN_ID=$1
;;
*)
echo >&2 "Unknown column '$1'."
exit -1
;;
esac
else
COLUMN_ID=5
fi
IP_FILTER=${2:-'.*'}
get_prog() {
K="\$2"
V="\$$COLUMN_ID"
case $1 in
min)
ROW_STMT="min[$K] = (min[$K] != \"\" && min[$K] < $V ? min[$K] : $V)"
END_STMT="for (key in min) { print key \"=\" min[key] }"
;;
max)
ROW_STMT="max[$K] = (max[$K] != \"\" && max[$K] > $V ? max[$K] : $V)"
END_STMT="for (key in max) { print key \"=\" max[key] }"
;;
avg|average|mean)
ROW_STMT="sum[$K] += $V; count[$K]++;"
END_STMT="for (key in sum) { print key \"=\" (sum[key] / count[key]) }"
;;
esac
echo "/^ping $IP_FILTER .*$/ { if ($V!=\"\") { $ROW_STMT } } END { $END_STMT }"
}
AWK_PROG=$(get_prog ${BATCH_METHOD:-max})
$INPUT_COMMAND | read_ping | $CHART_COMMAND