-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate.fish
264 lines (223 loc) · 7 KB
/
activate.fish
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
#!/usr/bin/env fish
echo
echo "[0;5;1;35;47m [0;5;1;37;46mwrk[0;5;1;37;45m-utils[0;5;1;36;47m [0;5;1;35;47m [0;5;1;37;46m [0;5;1;37;45m [0;5;1;36;47m [0;5;1;37;46m [0;5;37;47m [0m"
echo
echo "Set of commands & lua scripts to run wrk in clusters and collect stats"
echo
set -e WRK_PATH
set -e SSH_PORT
set -e SSH_OPTS
set -e SCP_OPTS
set -e SSH_CMD
set -e SCP_CMD
set -q CONFIG_FILE || set CONFIG_FILE config.env
cat $CONFIG_FILE | sed -e 's/\([A-Z_]\+\)=\(.*\)/set \1 "\2"/' | grep -v '^#' | source
# it makes sure that required commands exist on control client (not servers)
set COMMANDS "ssh" "sshpass" "xargs"
for COMMAND in $COMMANDS
if not command -v $COMMAND &> /dev/null
echo $COMMAND \<- command could not be found. Please install it.
exit
end
end
set -q BASE_SERVER_FILE || set BASE_SERVER_FILE servers.txt
# a file that contains options
set REQUIRED_FILES $CONFIG_FILE
for REQUIRED_FILE in $REQUIRED_FILES
if not test -f $REQUIRED_FILE || test (stat -c '%s' $REQUIRED_FILE) -lt 2
echo Can\'t find file or it\'s empty: "$REQUIRED_FILE"
exit
end
end
set -q SSH_USR || echo Please add SSH_USR \(username\) to $CONFIG_FILE file
set -q SSH_PWD || echo Please add SSH_PWD \(password\) to $CONFIG_FILE file.\nIf you wish to use ssh keys, set \$SSH_KEY=true but password is still required for sudo
set SERVERS_FILE $BASE_SERVER_FILE
set -q WRK_PATH || set WRK_PATH ~/.wrk-utils/
set -q SSH_PORT || set SSH_PORT 22
set -q SSH_OPTS || set SSH_OPTS -p$SSH_PORT -oConnectTimeout=5 -oConnectionAttempts=10 -oStrictHostKeyChecking=no
set -q SCP_OPTS || set SCP_OPTS -P$SSH_PORT
set -q SSH_CMD || set SSH_CMD ssh
set -q SCP_CMD || set SCP_CMD scp
set STDIN_TMP_FILE ''
if not test "$SSH_KEY" = "true"
set SSH_CMD sshpass -p "$SSH_PWD[1..-1]" $SSH_CMD
set SCP_CMD sshpass -p "$SSH_PWD" $CSP_CMD
end
function read-from-stdin
if test "$STDIN_TMP_FILE" = ''
if not isatty stdin
set STDIN_TMP_FILE (mktemp)
set SERVERS_FILE $STDIN_TMP_FILE
while read -L line
echo $line >> $STDIN_TMP_FILE
end
else
set SERVERS_FILE $BASE_SERVER_FILE
end
else
return 255
end
end
function clean-stdin-temp
if test -f "$STDIN_TMP_FILE"
rm $STDIN_TMP_FILE
set STDIN_TMP_FILE ''
end
end
echo -e "\033[1m - Commands -\033[0m"
echo
echo ssh-all: executes a command on all servers
echo -e "\t-> ssh-all 'ps aux | grep something'"
function ssh-all
read-from-stdin
set STATUS $status
cat $SERVERS_FILE | xargs -P100 -I{} $SSH_CMD $SSH_OPTS $SSH_USR@{} $argv
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo ssh-one: executes a command on a random server
echo -e "\t-> ssh-one 'ps aux | grep something'"
function ssh-one
read-from-stdin
set STATUS $status
set SERVER (shuf -n1 $SERVERS_FILE)
echo server: $SERVER
$SSH_CMD $SSH_OPTS $SSH_USR@$SERVER $argv
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo ssh-all-sudo: executes a command on all servers as sudo
echo -e "\t-> ssh-all-sudo id | wc"
function ssh-all-sudo
read-from-stdin
set STATUS $status
ssh-all "echo $SSH_PWD | sudo -p \"\" -k -S" $argv
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo ssh-one-sudo: executes a command on a server as sudo
echo -e "\t-> ssh-one-sudo id | wc"
function ssh-one-sudo
read-from-stdin
set STATUS $status
ssh-one "echo $SSH_PWD | sudo -p \"\" -k -S" $argv
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo kill-all: kills all wrk instances on all servers \(friendly\)
echo -e "\t-> kill-all"
function kill-all
read-from-stdin
set STATUS $status
ssh-all 'pkill -INT -f ./wrk'
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo kill-all-force: kills all wrk instances on all servers \(force, can\'t keep logs\)
echo -e "\t-> kill-all-force"
function kill-all-force
read-from-stdin
set STATUS $status
ssh-all 'killall wrk'
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo available-node-count: prints number of available servers
echo -e "\t-> available-node-count"
function available-node-count
read-from-stdin
set STATUS $status
ssh-all "id" | wc -l
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo active-node-count: prints number of active wrk instances in a loop
echo -e "\t-> active-node-count"
function active-node-count
read-from-stdin
set STATUS $status
while true
ssh-all "pidof wrk" | wc -w
sleep 1
end
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo live-stats: live stats for all servers \(per thread\).
echo -e "\t-> live-stats"
function live-stats
if test $WRK_LIVESTATS = 'true'
read-from-stdin
set STATUS $status
cat $SERVERS_FILE | ssh-all "ls $WRK_PATH | tail -n1 | xargs -ILOGFN tail -f $WRK_PATH/LOGFN"
if not test $STATUS -eq 255
clean-stdin-temp
end
else
echo live stats is not enabled
echo add WRK_LIVESTATS=true to $CONFIG_FILE.
end
end
echo
echo init-servers: creates wrk directory on servers copies wrk and lua scripts into that
echo -e "\t-> init-servers"
function init-servers
read-from-stdin
set STATUS $status
echo Uploading wrk and lua scripts to the servers...
echo To upload other files \(e.g., wordlists\) please use sync-file command
ssh-all "mkdir $WRK_PATH 2> /dev/null"
cat $SERVERS_FILE | xargs -P10 -I{} $SCP_CMD $SCP_OPTS wrk *.lua "$SSH_USR@{}:$WRK_PATH"
echo Done!
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo sync-file: copies provided files to wrk directory on all servers
echo -e "\t-> sync-file wordlist.txt *.jpg"
function sync-file
read-from-stdin
set STATUS $status
echo Syncing files: $argv
cat $SERVERS_FILE | xargs -P10 -I{} $SCP_CMD $SCP_OPTS $argv "$SSH_USR@{}:$WRK_PATH"
echo Done!
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo exec-wrk: executes wrk step by step or at once \(first argument is the delay to execute next instance\)
echo -e "\t-> exec-wrk 10 -t10 -c300 -d600s -s stats.lua 'https://example.com/path/?id=1'"
echo -e "\t-> exec-wrk 0 -t10 -c300 -d600s -s custom.lua 'https://example.com/'"
function exec-wrk
read-from-stdin
set STATUS $status
ssh-all-sudo "sysctl net.core.somaxconn=65535"
for SERVER in (cat $SERVERS_FILE)
$SSH_CMD $SSH_OPTS $SSH_USR@$SERVER "ulimit -n 1000000 && cd $WRK_PATH && NODE_IP=$SERVER WRK_DEBUG=$WRK_DEBUG WRK_LIVESTATS=$WRK_LIVESTATS WRK_LOGHEADERS=$WRK_LOGHEADERS SLACK_WEBHOOK=$SLACK_WEBHOOK SLACK_WEBHOOK_DBG=$SLACK_WEBHOOK_DBG ./wrk $argv[2..-1]" &;
echo (date)" - new node ("$SERVER") is started!"
sleep $argv[1]
end
if not test $STATUS -eq 255
clean-stdin-temp
end
end
echo
echo