/
netstatistics
executable file
·228 lines (184 loc) · 5.78 KB
/
netstatistics
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
#
# netstatistics collects statistics network data in CSV format for latter analysis.
# Copyright (C) 2012 Alexandre Gomes
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Author: Alexandre Gomes (alegomes at Google Public Mail)
#
# Changelog:
# v0.1 (05/15/2012) - First release
ORIGINAL_CMDS=$@
LOG_FOLDER="/var/log/netstatistics"
HEADER="timestamp;ssid;signal strength;noise;server tested;packet loss (%);round-trip min (ms);round-trip avg (ms);round-trip max (ms);round-trip std dev (ms);"
function usage {
echo
echo "netstatistics collects statistics network data in CSV format."
echo "Generated files can be reached at /var/log/netstatistics"
echo
echo "Usage:"
echo
echo "$ netstatistics start|stop [server_address1] [server_address2] ..."
echo
echo " if no server address is specified, 3 addresses will be used to statistics:"
echo
echo " 1. default gateway (e.g. 10.1.1.1)"
echo " 2. primary DNS (/etc/resolv.conf)"
echo " 3. Google DNS Server (8.8.8.8)"
echo
exit
}
function check_for_required_commands {
for reqcmd in wget; do
if [[ -z "$(which $reqcmd)" ]]
then
echo
echo "ERROR: Missing required '$reqcmd' command."
echo
exit 1
fi
done
}
function check_for_update {
# Inspired by https://github.com/pmalves/ctools-installer
rm -rf .tmp
mkdir -p .tmp/dist
wget --no-check-certificate 'https://raw.github.com/alegomes/netstatistics/master/netstatistics' -P .tmp -o /dev/null
if ! diff $0 .tmp/netstatistics >/dev/null ; then
answer="n"
echo
echo -n "There a new netstatistics version available. Do you want to upgrade? (y/N) "
read -e answer
case $answer in
[Yy]* ) cp .tmp/netstatistics $0; echo "Upgrade successful. Rerunning command '$0 $ORIGINAL_CMDS'"; /bin/bash $0 $ORIGINAL_CMDS; exit 0;;
esac
fi
}
function check_log_folder {
USERNAME=$(whoami)
if [ ! -d $LOG_FOLDER ]; then
if [ ! -w ${LOG_FOLDER%/*} ]; then
echo "Sudo password required to create ${LOG_FOLDER}"
sudo mkdir $LOG_FOLDER
sudo chmod a+w $LOG_FOLDER
echo "$LOG_FOLDER created successfully"
fi
else
if [ ! -w $LOG_FOLDER ]; then
echo "Sudo password required to give write permission to $USERNAME in $LOG_FOLDER"
sudo chmod a+w $LOG_FOLDER
echo "Write permission successfully set on $LOG_FOLDER"
fi
fi
}
function run_ping {
IP=$1
PING_LOG_FILE=".ping.${IP}"
rm ${PING_LOG_FILE} 2> /dev/null
ping -t 60 $IP > ${PING_LOG_FILE}
REGEX="^.*packets\ transmitted.*packets\ received,\ (.*)%\ packet\ loss.*round-trip\ min/avg/max/stddev\ =\ (.*)/(.*)/(.*)/(.*)\ ms"
[[ $(cat $PING_LOG_FILE) =~ $REGEX ]]
PACKET_LOSS="${BASH_REMATCH[1]}"
ROUND_TRIP_MIN="${BASH_REMATCH[2]}"
ROUND_TRIP_AVG="${BASH_REMATCH[3]}"
ROUND_TRIP_MAX="${BASH_REMATCH[4]}"
ROUND_TRIP_STDDEV="${BASH_REMATCH[5]}"
PING_LOG="${PACKET_LOSS};${ROUND_TRIP_MIN};${ROUND_TRIP_AVG};${ROUND_TRIP_MAX};${ROUND_TRIP_STDDEV};"
}
function collect_data {
SERVER_ADDRESS=$1
while(true); do
# Outras formas de pegar o SSID:
# /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
# system_profiler SPAirPortDataType | awk -F':' '/Current Network Information:/ {getline; print $1}'
SSID=$(networksetup -getairportnetwork en1 | cut -c 24-)
LOG_FILE="$LOG_FOLDER/netstatistics-$(date +%Y%m%d).csv"
if [ -z $LOG_FILE ]; then
echo $HEADER
fi
TIMESTAMP=$(date +"%d/%m/%Y;%H:%M")
SIGNAL_STRENGHT=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep CtlRSSI | sed -e 's/^.*: //g')
SIGNAL_NOISE=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep -i agrCtlNoise | sed -e 's/^.*: //g')
run_ping $SERVER_ADDRESS
METRICS="${TIMESTAMP};${SSID};${SIGNAL_STRENGHT};${SIGNAL_NOISE};${SERVER_ADDRESS};${PING_LOG}"
echo $METRICS >> $LOG_FILE
done
}
function execute_recursion {
declare -a SERVERS
if [ $# -eq 1 ]; then
DEFAULT_GATEWAY=$(netstat -nr | grep '^default' | awk '{print $2}')
PRIMARY_DNS=$(cat /etc/resolv.conf | grep nameserver | head -1 | awk '{print $2}')
GOOGLE_DNS="8.8.8.8"
SERVERS=($DEFAULT_GATEWAY $PRIMARY_DNS $GOOGLE_DNS)
else
if [ $# -gt 1 ]; then
i=0
shift # Ignore first argument (start)
while test $# -gt 0; do
SERVERS[$i]=$1
(( i = i + 1 ))
shift
done
fi
fi
for ADDRESS in ${SERVERS[*]}; do
CMD="/bin/bash $0 start $ADDRESS"
echo "Calling $CMD"
$CMD
done
}
function execute_script {
check_for_required_commands
check_for_update
check_log_folder
echo "Collected data available at $LOG_FOLDER"
(collect_data $1 &)&
}
function start {
case $# in
2)
execute_script $2
;;
*)
execute_recursion $@
esac
}
function stop {
PID=$(ps aux | grep "$0 start" | grep -v grep | awk '{ print $2 }')
if [ -z $PID ]; then
echo "netstatistics not running..."
exit -1
fi
echo "Killing process ${PID}..."
kill $PID
exit
}
###############
# BEGIN
###############
[ $# -gt 0 ] || usage
trap "stop" HUP
trap "stop" INT
trap "stop" QUIT
trap "stop" PIPE
trap "stop" TERM
trap "stop" KILL
case $1 in
start )
start $@ ;;
stop )
stop ;;
* )
usage ;;
esac