forked from wifinigel/wiperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·346 lines (296 loc) · 12 KB
/
setup.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/bin/bash
# Installer for wiperf on WLAN Pi & RPi
VERSION='2.0.0-beta6'
POLLER_VERSION='0.1.25'
# Installation script log file
LOG_FILE="/var/log/wiperf_install.log"
# define global vars
CLONE_DIR="/usr/share"
INSTALL_DIR="$CLONE_DIR/wiperf"
CFG_DIR="/etc/wiperf"
BACKUP_DIR="/etc/.wiperf"
GITHUB_REPO="https://github.com/wifinigel/wiperf.git"
GITHUB_BRANCH='main'
OPERATION=$1
PLATFORM=$2
# install function
install () {
echo "(ok) Starting wiperf install process for $PLATFORM (see $LOG_FILE for details)" | tee -a $LOG_FILE
# Check which platform we're installing for
if ! [[ $PLATFORM =~ ^(wlanpi|rpi)$ ]]; then
echo "Unknown (or no) platform supplied (exiting)"
exit 1
fi
### check we can get to pypi before staring
curl -s --head -m 2 --connect-timeout 2 --request GET https://pypi.org | head -n 1 | grep '200' >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "Unable to reach Internet - check connection (exiting)" | tee -a $LOG_FILE
exit 1
fi
### Check python is 3.7 or better
echo "(ok) Checking we have a compatible python version..."
python3 -V | egrep '3\.7|3\.8|3\.9|3\.10|3\.11|3\.12'
if [ "$?" != '0' ]; then
echo "(fail) Compatible version of python3 not installed (3.7+). Please upgrade to a newer version, though this may require a new platform image." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Python3 looks OK" | tee -a $LOG_FILE
fi
### check git is present
echo "(ok) Checking we have git available..."
git --version >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Unable to proceed as git not installed...please install with command 'apt-get install git' " | tee -a $LOG_FILE
exit 1
else
echo "(ok) Git looks OK" | tee -a $LOG_FILE
fi
### check iperf3 is present
echo "(ok) Checking we have iperf3 available..."
iperf3 -v >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Unable to proceed as iperf3 not installed...please install with command 'apt-get install iperf3' " | tee -a $LOG_FILE
exit 1
else
echo "(ok) iperf3 looks OK" | tee -a $LOG_FILE
fi
# check that pip3 is available
echo "(ok) Checking we have pip3 available..."
pip3 -V >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Unable to proceed as pip3 not installed...please install with command 'apt-get install python3-pip' " | tee -a $LOG_FILE
exit 1
else
echo "(ok) pip3 looks OK" | tee -a $LOG_FILE
fi
# check that netcat is available
echo "(ok) Checking we have netcat available..."
nc -h >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Unable to proceed as netwcat not installed...please install with command 'apt-get install netcat' " | tee -a $LOG_FILE
exit 1
else
echo "(ok) netcat looks OK" | tee -a $LOG_FILE
fi
### install the wiperf poller from PyPi - exit if errors
echo "(ok) Installing wiperf python module (please wait)..." | tee -a $LOG_FILE
pip3 install wiperf_poller==$POLLER_VERSION >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) pip installation of wiperf_poller failed. Exiting." | tee -a $LOG_FILE
exit 1
else
echo "(ok) wiperf_poller module python installed" | tee -a $LOG_FILE
fi
### pull & install the Splunk Event collector class
echo "(ok) Cloning the Splunk Event collector class..." | tee -a $LOG_FILE
# take out existing dir (if there)
rm -rf /tmp/Splunk-Class-httpevent
repo_src="https://github.com/wifinigel/Splunk-Class-httpevent.git"
#repo_src="https://github.com/georgestarcher/Splunk-Class-httpevent.git"
git -C /tmp clone $repo_src >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Clone of Splunk Python module failed." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Python Splunk module cloned OK." | tee -a $LOG_FILE
fi
### Install the Splunk collector module
echo "(ok) Installing the Splunk Event collector class (please wait)..." | tee -a $LOG_FILE
pip3 install /tmp/Splunk-Class-httpevent >> $LOG_FILE 2>&1
if [ -z "$?" ]; then
echo "(fail) Install of Splunk Python module failed." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Splunk Python module installed OK." | tee -a $LOG_FILE
fi
### Pull in the wiperf github code
echo "(ok) Cloning GitHub wiperf repo (please wait)..." | tee -a $LOG_FILE
# get rid of the local copy if already exists (in case installing over the top)
rm -rf $INSTALL_DIR >> $LOG_FILE 2>&1
git -C $CLONE_DIR clone $GITHUB_REPO -b $GITHUB_BRANCH >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Clone of GitHub repo failed." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Cloned OK." | tee -a $LOG_FILE
fi
### copy config.ini.default to $CFG_DIR
echo "(ok) Moving config.default.ini to $CFG_DIR..." | tee -a $LOG_FILE
mkdir -p $CFG_DIR >> $LOG_FILE 2>&1
mv "$INSTALL_DIR/config.default.ini" $CFG_DIR >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Copy of config.ini.default failed." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Copied OK." | tee -a $LOG_FILE
fi
### move files in ./conf to $CFG_DIR for wlanpi, remove 'conf' dir for rpi
echo "(ok) Moving conf directory to $CFG_DIR..." | tee -a $LOG_FILE
mv "$INSTALL_DIR/conf" $CFG_DIR >> $LOG_FILE 2>&1
if [ -z "$?" ]; then
echo "(fail) Copy of conf directory failed." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Copied OK." | tee -a $LOG_FILE
fi
if [ "$PLATFORM" = 'rpi' ]; then
# remove the conf dir if rpi, as don't need it
echo "(ok) Removing conf directory $CFG_DIR/conf...(not needed on RPi)" | tee -a $LOG_FILE
rm -rf $CFG_DIR/conf >> $LOG_FILE 2>&1
fi
# if we have old config files, copy them back im
if [ -e "${BACKUP_DIR}/config.ini" ] ; then
echo "(ok) Restoring old config file..." | tee -a $LOG_FILE
# copy files back in to retain old config
cp "${BACKUP_DIR}/config.ini" ${CFG_DIR} >> $LOG_FILE 2>&1
fi
### copy across the wiperf switcher & restore connectivity files if this is a WLAN Pi
### remove switcher if rpi
if [ "$PLATFORM" = 'wlanpi' ]; then
INTERFACES_FILE=${BACKUP_DIR}/conf/network/interfaces
if [ -e "${INTERFACES_FILE}" ] ; then
echo "(ok) Restoring interfaces file..." | tee -a $LOG_FILE
# copy files back in to retain old connectivity
cp "${INTERFACES_FILE}" ${CFG_DIR}/conf/network/interfaces >> $LOG_FILE 2>&1
fi
WPA_FILE=${BACKUP_DIR}/conf/etc/wpa_supplicant/wpa_supplicant.conf
if [ -e "${WPA_FILE}" ] ; then
echo "(ok) Restoring supplicant file..." | tee -a $LOG_FILE
# copy files back in to retain old connectivity
cp "${WPA_FILE}" ${CFG_DIR}/conf/etc/wpa_supplicant/wpa_supplicant.conf >> $LOG_FILE 2>&1
fi
# copy wiperf_switcher to /usr/bin/wiperf_switcher
echo "(ok) Moving wiperf_switcher to /usr/bin/wiperf_switcher..." | tee -a $LOG_FILE
mv "$INSTALL_DIR/wiperf_switcher" /usr/bin/wiperf_switcher >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Copy of wiperf_switcher failed." | tee -a $LOG_FILE
exit 1
else
echo "(ok) Copied OK." | tee -a $LOG_FILE
# make sure it can be executed
chmod 755 /usr/bin/wiperf_switcher
fi
else
# remove the conf dir if rpi, as don't need it
echo "(ok) Removing wiperf_switcher file...(not needed on RPi)" | tee -a $LOG_FILE
rm -f $INSTALL_DIR/wiperf_switcher >> $LOG_FILE 2>&1
fi
#TODO: Add series of tests to check out the final env
echo "(ok) Install complete." | tee -a $LOG_FILE
if [ "$PLATFORM" = 'rpi' ]; then
if [ "$OPERATION" = 'install' ]; then
echo ""
echo "================================================="
echo "Don't forget to modify the following files before"
echo "trying to use wiperf (unless this is an upgrade):"
echo ""
echo " 1. Edit wireless auth settings: sudo nano /etc/wpa_supplicant/wpa_supplicant.conf"
echo " 2. Edit wlan0 settings: sudo nano /etc/network/interfaces"
echo " 3. Copy default cfg file to live cfg: sudo cp $CFG_DIR/config.default.ini $CFG_DIR/config.ini"
echo " 4. Edit the cfg file for your env: nano $CFG_DIR/config.ini"
echo " 5. Add a cron job to run wiperf regularly, e.g. sudo crontab -e (add line below)"
echo " 0-59/5 * * * * /usr/bin/python3 /usr/share/wiperf/wiperf_run.py > /var/log/wiperf_cron.log 2>&1"
echo " 6. If you are running several probes on a network, change their hostnames to be unique:"
echo " sudo nano /etc/hostname (change raspberrypi to your req hostname)"
echo " sudo nano /etc/hosts (change raspberrypi to your req hostname)"
echo " sudo reboot"
echo "================================================="
echo ""
fi
#else
# echo ""
# echo "================================================="
# echo "Don't forget to modify the following files before"
# echo "switching in to wiperf mode:"
# echo ""
# echo " 1. Copy default cfg file to live cfg: sudo cp $CFG_DIR/config.default.ini $CFG_DIR/config.ini"
# echo " 2. Edit the cfg file for your env: sudo nano $CFG_DIR/config.ini"
# echo " 3. Edit the WLAN config file for your env: sudo nano $CFG_DIR/conf/etc/wpa_supplicant/wpa_supplicant.conf"
# echo " (add WLAN info)"
# echo " 4. Reboot the WLAN Pi before first-use from fpms: sudo sync; sudo reboot"
# echo "================================================="
# echo ""
fi
}
uninstall () {
echo "(ok) Starting wiperf uninstall process (see $LOG_FILE for details)" | tee -a $LOG_FILE
cd /tmp
# remove python modules
echo "(ok) Removing Python modules" | tee -a $LOG_FILE
echo "(ok) ...splunk_http_event_collector" | tee -a $LOG_FILE
pip3 uninstall -y Splunk-HEC >> $LOG_FILE 2>&1
echo "(ok) ...wiperf_poller" | tee -a $LOG_FILE
pip3 uninstall -y wiperf_poller >> $LOG_FILE 2>&1
# remove directories
echo "(ok) Removing install dir" | tee -a $LOG_FILE
rm -rf $INSTALL_DIR >> $LOG_FILE 2>&1
echo "(ok) Removing config dir" | tee -a $LOG_FILE
mv $CFG_DIR "${BACKUP_DIR}" >> $LOG_FILE 2>&1
echo "(ok) Removing switcher script" | tee -a $LOG_FILE
rm -f /usr/bin/wiperf_switcher >> $LOG_FILE 2>&1
# remove log files
echo "(ok) Removing log files" | tee -a $LOG_FILE
rm -f /var/log/wiperf_agent.log >> $LOG_FILE 2>&1
rm -f /var/log/wiperf_cron.log >> $LOG_FILE 2>&1
echo "(ok) Done"
}
upgrade () {
# Check which platform we're installing for
if ! [[ $PLATFORM =~ ^(wlanpi|rpi)$ ]]; then
echo "Unknown (or no) platform supplied (exiting)"
exit 1
fi
echo "(ok) wiperf will now be unistalled, then re-installed with a new version"
### check we can get to pypi before staring
echo "(ok) checking we can get to the Internet before we start..." | tee $LOG_FILE
curl -s --head -m 2 --connect-timeout 2 --request GET https://pypi.org | head -n 1 | grep '200' >> $LOG_FILE 2>&1
if [ "$?" != '0' ]; then
echo "(fail) Unable to reach Internet - check connection (exiting)" | tee -a $LOG_FILE
exit 1
fi
# kick off the uninstall
uninstall
# install new code
echo "(ok) Installing latest version of wiperf..."
curl -s https://raw.githubusercontent.com/wifinigel/wiperf/main/setup.sh | sudo bash -s install $PLATFORM
exit 0
}
check_ver () {
# get version file from github main branch
current_ver=$VERSION
available_ver="`wget -qO- --timeout=1 --tries=2 https://raw.githubusercontent.com/wifinigel/wiperf/main/version.txt`"
echo ""
echo "Installed version: $current_ver"
echo "Available version: $available_ver"
echo ""
}
case "$1" in
-v)
echo ""
echo "wiperf version: $VERSION"
echo ""
;;
install)
install
;;
upgrade)
upgrade
;;
remove)
uninstall
;;
check_ver)
check_ver
;;
*)
echo "Usage: install.sh {-i | -u | -r} {wlanpi | rpi}"
echo ""
echo " setup.sh install [wlanpi|rpi] : run installer"
echo " setup.sh upgrade [wlanpi|rpi]: upgrade"
echo " setup.sh remove [wlanpi|rpi] : remove wiperf completely"
echo " setup.sh check_ver : check version of code installed/available"
echo ""
exit 0
;;
esac
exit 0