forked from diskoverdata/diskover-community
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diskover-bot-launcher.sh
executable file
·209 lines (191 loc) · 6.04 KB
/
diskover-bot-launcher.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
#!/bin/bash
# diskover bot launcher multi-processing script
# starts multiple bots to help with diskover redis queue
# https://github.com/shirosaidev/diskover
#
# Copyright (C) Chris Park 2017-2019
# diskover is released under the Apache 2.0 license. See
# LICENSE for the full license text.
#
############# EDIT BELOW FOR YOUR ENVIRONMENT #############
# paths and default config values below
# path to python command or python if in PATH
PYTHON=python
# path to diskover_worker_bot.py
DISKOVERBOT=./diskover_worker_bot.py
# path to killredisconn.py
KILLREDISCONN=./killredisconn.py
# number of bots to start (cpu cores x 2 might be good start)
WORKERBOTS=8
# run bots in burst mode (quit when all jobs done)
BURST=FALSE
# log bot output to file, if blank redirect to null (no logging)
BOTLOG=
# run bots with verbose output (logging level)
# logging level 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG
LOGLEVEL=2
# file to store bot pids
BOTPIDS=/tmp/diskover_bot_pids
###########################################################
VERSION="1.6.3"
function printhelp {
echo "Usage: $(basename $0) [OPTION]"
echo
echo "Options:"
echo
echo " -w n number of worker bots to start (default $WORKERBOTS)"
echo " -b burst mode (bots quit when no more jobs)"
echo " -s show all bots running"
echo " -k kill all bots"
echo " -r restart all running bots"
echo " -R remove any idle zombie worker bot connections in Redis"
echo " -l n logging level 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG"
echo " -V displays version and exits"
echo " -h displays this help message and exits"
echo
echo "diskover worker bot process launcher v$VERSION"
echo
echo "Adjust default paths and settings at top of script"
exit 1
}
KILLBOTS=FALSE
RESTARTBOTS=FALSE
REMOVEBOTS=FALSE
SHOWBOTS=FALSE
while getopts :h?w:bskrRfl:V opt; do
case "$opt" in
h) printhelp; exit 0;;
w) WORKERBOTS=$OPTARG;;
b) BURST=TRUE;;
s) SHOWBOTS=TRUE;;
k) KILLBOTS=TRUE;;
r) RESTARTBOTS=TRUE;;
R) REMOVEBOTS=TRUE;;
l) LOGLEVEL=$OPTARG;;
V) echo "$0 v$VERSION"; exit 0;;
?) echo "Invalid option ${OPTARG}, use -h for help" >&2; exit 1;;
esac
done
function banner {
echo "$(tput setaf 1)
________ .__ __
\\______ \\ |__| _____| | _________ __ ___________
| | \\| |/ ___/ |/ / _ \\ \\/ // __ \\_ __ \\ /)___(\\
| \` \\ |\\___ \\| < <_> ) /\\ ___/| | \\/ (='.'=)
/_______ /__/____ >__|_ \\____/ \\_/ \\___ >__| (\"\)_(\"\)
\\/ \\/ \\/ \\/
Worker Bot Launcher v$VERSION
https://github.com/shirosaidev/diskover
\"Crawling all your stuff, core melting time\"$(tput sgr 0)
"
}
function startbots {
echo "Starting $WORKERBOTS worker bots in background..."
ARGS=""
if [ $BURST == TRUE ]; then
ARGS+="-b "
fi
if [ $LOGLEVEL == 0 ]; then
ARGS+="-l ERROR"
elif [ $LOGLEVEL == 1 ]; then
ARGS+="-l WARNING"
elif [ $LOGLEVEL == 2 ]; then
ARGS+="-l INFO"
elif [ $LOGLEVEL == 3 ]; then
ARGS+="-l DEBUG"
fi
# check if .py file exists
if [ ! -f $DISKOVERBOT ]; then
echo "Can't find $DISKOVERBOT, check config at top of this file."
exit 1
fi
for (( i = 1; i <= $WORKERBOTS; i++ )); do
if [ ! $BOTLOG ]; then
$PYTHON $DISKOVERBOT $ARGS > /dev/null 2>&1 &
else
$PYTHON $DISKOVERBOT $ARGS >> $BOTLOG.$i 2>&1 &
fi
# check if bot started
if [ $i -eq 1 ]; then
sleep 1
ps | grep -v grep | grep $! > /dev/null 2>&1
if [ $? -gt 0 ]; then
echo "ERROR starting bot, check redis is running and diskover.cfg settings."
echo "There are additional setting at the top of this file."
exit 1
fi
fi
echo "$(hostname -s).$! (pid $!) (botnum $i)"
echo "$!" >> $BOTPIDS
done
echo "DONE!"
echo "All worker bots have started"
if [ $BOTLOG ]; then
echo "Worker bot output is getting logged to $BOTLOG.botnum"
fi
echo "Worker pids have been stored in $BOTPIDS, use -k flag to shutdown workers or -r to restart"
echo "Exiting, sayonara!"
}
function killbots {
if [ -f $BOTPIDS ]; then
echo "Killing worker bot pids:"
for PID in `cat $BOTPIDS`; do
echo "$PID"
if `ps | grep -v grep | grep $PID > /dev/null 2>&1`; then
kill $PID
fi
done
echo "All worker bots have been sent shutdown command"
rm $BOTPIDS
else
echo "Pid file can't be found, killing any bots..."
pkill -f diskover_worker_bot.py
echo "All worker bots have been sent shutdown command"
fi
}
function removebots {
echo "Removing any idle zombie worker bot connections in Redis..."
# check if .py file exists
if [ ! -f $KILLREDISCONN ]; then
echo "Can't find $KILLREDISCONN, check config at top of this file."
exit 1
fi
$PYTHON $KILLREDISCONN
echo "Done"
}
function showbots {
if [ -f $BOTPIDS ]; then
echo "Running worker bots:"
for PID in `cat $BOTPIDS`; do
if `ps | grep -v grep | grep $PID > /dev/null 2>&1`; then
echo "$(hostname -s).$PID (pid $PID)"
fi
done
else
echo "Pid file can't be found, running worker bot pids:"
pgrep -f diskover_worker_bot.py
fi
}
function countbots {
if [ -f $BOTPIDS ]; then
WORKERBOTS=`cat $BOTPIDS | wc -l | tr -d '[:space:]'`
else
WORKERBOTS=`pgrep -f diskover_worker_bot.py | wc -l | tr -d '[:space:]'`
fi
}
banner
if [ $KILLBOTS == TRUE ]; then
killbots
elif [ $REMOVEBOTS == TRUE ]; then
removebots
elif [ $SHOWBOTS == TRUE ]; then
showbots
elif [ $RESTARTBOTS == TRUE ]; then
countbots
killbots
echo sleeping for 2 seconds...
sleep 2
startbots
else
startbots
fi