forked from datastax/diagnostic-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_diag.sh
executable file
·296 lines (269 loc) · 9.09 KB
/
collect_diag.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
#!/usr/bin/env bash
#
# File: collect_diag.sh
#
# Created: Friday, May 31 2019
# Modified: $Format:%cD$
# Hash: $Format:%h$
#
# This script collects diagnostic from multiple nodes of cluster
##
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
echo "You need to use Bash 4 or higher, but you have ${BASH_VERSION}"
exit 1
fi
function usage() {
echo "Usage: $0 -t <type> [options] [path]"
echo " ----- Required --------"
echo " -t type - valid choices are \"coss\", \"ddac\", \"dse\" "
echo " ----- Options --------"
echo " -c cqlsh_options - e.g \"-u user -p password\" etc. Ensure you enclose with \""
echo " -d dsetool_options - options to pass to dsetool. Syntax the same as \"-c\""
echo " -e encryption key file - Key file for encryption of the generated tarball"
echo " -f file_name - file with list of hosts where to execute command (default - try to get list from 'nodetool status')"
echo " -k keystore_ssl_info - collect keystore and truststore information"
echo " -i insights - collect only data for DSE Insights"
echo " -I insights_dir - directory that contains insights .gz files"
echo " -n nodetool_options - options to pass to nodetool. Syntax the same as \"-c\""
echo " -o output_dir - where to put resulting file (default: $OUT_DIR)"
echo " -p pid - PID of DSE or DDAC process"
echo " -r - remove collected files after generation of resulting tarball"
echo " -s ssh/scp options - options to pass to SSH/SCP"
echo " -B S3 bucket - AWS S3 bucket to upload the artifacts to"
echo " -S S3 secret - AWS secret for the S3 upload"
echo " -K S3 key - AWS key for the S3 upload"
echo " -T ticket number - Ticket for the S3 upload and encrypted tarball naming"
echo " -u timeout - timeout for SSH in seconds (default: $TIMEOUT)"
echo " -m collection_mode - light, normal, extended. Default: normal"
echo " -v - verbose output"
echo " -z - don't execute commands that require sudo"
echo " -P top directory of COSS, DDAC or DSE installation (for tarball installs)"
echo " -C path - explicitly set Cassandra configuration location"
echo " -D path - explicitly set DSE configuration location"
}
function check_type {
if [ "$TYPE" != "ddac" ] && [ "$TYPE" != "coss" ] && [ "$TYPE" != "dse" ]; then
usage
exit 1
fi
}
function debug {
if [ -n "$VERBOSE" ]; then
DT="$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
echo "[${DT}]: $1"
fi
}
function s3_push() {
srcFilePath="$1"
dstFileName="$2"
ticket="$3"
s3_bucket="$4"
s3_key="$5"
s3_secret="$6"
timestamp="$7"
contentType="application/octet-stream"
s3Date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
resource="/${s3_bucket}/${ticket}-${timestamp}/${dstFileName}"
stringToSign="PUT\n\n${contentType}\n${s3Date}\n${resource}"
signature=$(echo -en "${stringToSign}" | openssl sha1 -hmac "${s3_secret}" -binary | base64)
echo "Uploading ${srcFilePath} to s3://${s3_bucket}/${ticket}-${timestamp}/"
curl -X PUT -T "${srcFilePath}" \
-H "Host: ${s3_bucket}.s3.amazonaws.com" \
-H "Date: ${s3Date}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3_key}:${signature}" \
https://"${s3_bucket}.s3.amazonaws.com/${ticket}-${timestamp}/${dstFileName}"
statusState=$?
print_status_state
return $statusState
}
# ----------
# Setup vars
# ----------
CQLSH_OPTS=""
DT_OPTS=""
OLDWD="$(pwd)"
OUT_DIR=$(mktemp -d)
TIMEOUT=600
HOST_FILE=""
SSH_OPTS=""
NT_OPTS=""
COLLECT_OPTS=""
REMOVE_OPTS=""
INSIGHT_COLLECT_OPTS=""
VERBOSE=""
TYPE=""
ENCRYPTION_KEY=""
TICKET=""
S3_BUCKET=""
DSE_DDAC_ROOT=""
CONF_DIR=""
DSE_CONF_DIR=""
# ---------------
# Parse arguments
# ---------------
while getopts ":hzivrk:c:n:d:f:o:p:s:t:u:I:m:e:S:K:T:B:P:C:D:" opt; do
case $opt in
c) CQLSH_OPTS="$OPTARG"
;;
d) DT_OPTS="$OPTARG"
;;
e) ENCRYPTION_KEY="$OPTARG"
;;
f) HOST_FILE="$OPTARG"
;;
k) COLLECT_OPTS="$COLLECT_OPTS -k"
;;
i) COLLECT_OPTS="$COLLECT_OPTS -i"
;;
I) INSIGHT_COLLECT_OPTS="-I '${OPTARG}'"
;;
n) NT_OPTS=$OPTARG
;;
o) OUT_DIR=$OPTARG
;;
p) COLLECT_OPTS="$COLLECT_OPTS -p $OPTARG"
;;
r) REMOVE_OPTS="-r"
;;
s) SSH_OPTS=$OPTARG
;;
t) TYPE=$OPTARG
;;
u) TIMEOUT=$OPTARG
;;
v) COLLECT_OPTS="$COLLECT_OPTS -v"
VERBOSE="true"
;;
z) COLLECT_OPTS="$COLLECT_OPTS -z"
;;
m) MODE="$OPTARG"
if [ "$MODE" != "normal" ] && [ "$MODE" != "extended" ] && [ "$MODE" != "light" ]; then
echo "Incorrect collection mode: '$MODE'"
usage
exit 1
fi
COLLECT_OPTS="$COLLECT_OPTS -m $MODE"
;;
S) export DS_AWS_SECRET="$OPTARG"
;;
K) export DS_AWS_KEY="$OPTARG"
;;
B) S3_BUCKET="$OPTARG"
;;
T) TICKET="$OPTARG"
;;
P) DSE_DDAC_ROOT="$OPTARG"
;;
C) CONF_DIR="$OPTARG"
;;
D) DSE_CONF_DIR="$OPTARG"
;;
h) usage
exit 0
;;
*) echo "Unknown flag passed: '$opt'"
usage
exit 1
;;
esac
done
shift "$((OPTIND -1))"
echo "Using output directory: ${OUT_DIR}"
# ------------------------
# Check valid install type
# ------------------------
check_type
if [ "$TYPE" = "ddac" ] && [ -z "$DSE_DDAC_ROOT" ]; then
echo "You must specify root location of DDAC installation"
usage
exit 1
fi
TMP_HOST_FILE=""
if [ -z "$HOST_FILE" ] || [ ! -f "$HOST_FILE" ]; then
echo "File with hosts isn't specified, or doesn't exist, using 'nodetool status'"
TMP_HOST_FILE=${OUT_DIR}/diag-hosts.$$
nodetool $NT_OPTS status|grep -e '^UN'|sed -e 's|^UN [ ]*\([^ ]*\) .*$|\1|' > "$TMP_HOST_FILE"
HOST_FILE=$TMP_HOST_FILE
fi
# TODO: calculate ServerAliveCountMax based on the timeout & ServerAliveInterval...
SSH_OPTS="$SSH_OPTS -o StrictHostKeyChecking=no -o ConnectTimeout=$TIMEOUT -o BatchMode=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=40"
[[ $0 == */* ]] && LAUNCH_PATH=${0%/*}/ || LAUNCH_PATH=./
declare -A servers
for host in $(cat "$HOST_FILE"); do
debug "Copying collect_node_diag.sh to $host..."
scp $SSH_OPTS "${LAUNCH_PATH}collect_node_diag.sh" "${host}:~/"
if [ "$TYPE" = "coss" ]; then
debug "Copying sjk jar to $host..."
scp $SSH_OPTS "${LAUNCH_PATH}libs/sjk-plus.jar" "${host}:~/"
fi
RES=$?
if [ $RES -ne 0 ]; then
echo "Error during execution SCP, copying script to host $host, exiting..."
exit 1
fi
NODE_OUT_DIR="$(ssh $SSH_OPTS "$host" 'mktemp -d'| tr -d '\r')"
if [ $RES -ne 0 ]; then
echo "Error creating a temp directory on host $host, exiting..."
exit 1
fi
servers[$host]=$NODE_OUT_DIR
debug "host: $host out_dir=$NODE_OUT_DIR"
done
declare -A pids
for host in "${!servers[@]}"; do
NODE_OUT_DIR="${servers[$host]}"
ssh $SSH_OPTS $host "bash --login ./collect_node_diag.sh -t $TYPE -o $NODE_OUT_DIR $COLLECT_OPTS $INSIGHT_COLLECT_OPTS -c '$CQLSH_OPTS' -n '$NT_OPTS' -d '$DT_OPTS' -P '$DSE_DDAC_ROOT' -C '$CONF_DIR' -D '$DSE_CONF_DIR'" &
pids[$host]="${!}"
done
declare -a hosts_failed
declare -a hosts_success
for host in "${!pids[@]}"; do
debug "Going to wait for PID ${pids[$host]} for host $host"
if wait ${pids[$host]}; then
RES=$?
if [ "$RES" -eq 0 ]; then
hosts_success+=("$host")
else
hosts_failed+=("$host")
fi
else
hosts_failed+=("$host")
fi
done
failed_len=${#hosts_failed[@]}
if [ "$failed_len" -gt 0 ]; then
if [ "$failed_len" -eq ${#servers[@]} ]; then
echo "Collection failed on all hosts!"
exit 1
else
echo "Collection failed on $failed_len hosts: ${hosts_failed[*]}"
echo "We will generate diagnostic tarball only for hosts where collection was successful"
fi
fi
# we continue to
for host in "${hosts_success[@]}"; do
NODE_OUT_DIR="${servers[$host]}"
debug "host: $host out_dir=$NODE_OUT_DIR"
scp $SSH_OPTS "${host}:${NODE_OUT_DIR}/*.tar.gz" "$OUT_DIR"
RES=$?
if [ $RES -ne 0 ]; then
echo "Error during execution SCP, copying data from host $host, exiting..."
exit 1
fi
if [ -n "$REMOVE_OPTS" ] && [ "$NODE_OUT_DIR" != "/" ] ; then
ssh $SSH_OPTS "$host" "rm -rf '$NODE_OUT_DIR'"
fi
done
${LAUNCH_PATH}generate_diag.sh -o "$OUT_DIR" -t "$TYPE" $REMOVE_OPTS $COLLECT_OPTS "$OUT_DIR"
if [ -f "$ENCRYPTION_KEY" ] && [ -n "$TICKET" ] && [ -n "$S3_BUCKET" ]; then # encrypt and upload the generated tarball
tarball_path=$(ls $OUT_DIR/*.tar.gz)
${LAUNCH_PATH}encrypt_and_upload.sh -e $ENCRYPTION_KEY -f $tarball_path -B $S3_BUCKET -T $TICKET
else
echo "No valid encryption file provided. Tarball will not get encrypted nor uploaded."
fi
# do cleanup
if [ -n "$TMP_HOST_FILE" ]; then
rm -f "$TMP_HOST_FILE"
fi
cd "$OLDWD" || exit 1