3 #########################################################################
4 # gather_stats_everywhere:
5 # script on a selection of nodes and collect all the results into a single
8 # Copyright (c) 2007 - Cluster File Systems, Inc.
9 #########################################################################
20 if [ ${PRINT_INFO_MSGS} -gt 0 ]
27 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]
34 printf $"Usage: gather_stats_everywhere [-help] config_file [start|stop|cleanup] <log_name>\n"
38 The distribution script will run on a single node. It is parameterised
39 with a set of target node names. It may assume ssh/scp to these node
40 names works without requiring a password. It will run in 2 modes...
42 gather_stats_everywhere config_file start
44 ...will copy the script to /tmp everywhere described in
45 config_file running on all the target hosts. And...
47 gather_stats_everywhere config_file stop log_name
49 ...will stop script running on all the hosts it started on and collect
50 all the individual stats files into a single compressed tarball if the log_name is
53 The config file is just a list of shell variable assignments that can be
56 Serveral variables must be set in the config file
58 Targets: the nodes where run the script.
66 options=`getopt -o h --long help:: -- "$@"`
73 eval set -- "$options"
88 if [ $# != 2 -a $# != 3 ] ; then
99 if [ ! -r $CONFIG ]; then
100 error "Config_file: $CONFIG does not exist "
106 if [ -z "$SCRIPT" ]; then
107 error "SCRIPT in ${CONFIG} is empty"
111 if [ -z "$TARGETS" ]; then
112 error "TARGETS in ${CONFIG} is empty"
116 #check nodes accessiable
117 Check_nodes_available() {
118 local NODES_NOT_AVAILABLE=""
120 debug "Entering Check_nodes_available()"
122 for TARGET in $TARGETS; do
123 if ! ping -c 1 -w 3 $TARGET > /dev/null; then
124 NODES_NOT_AVAILABLE=$NODES_NOT_AVAILABLE$TARGET
128 if [ -z "$NODES_NOT_AVAILABLE" ]; then
129 debug "Check_nodes_available() returning 0 (success - all nodes available)"
133 error "Check_nodes_available: these nodes are not available (did not respond to pings): ${NODES_NOT_AVAILABLE}"
134 debug "Check_nodes_available() returning with errors"
139 if ! Check_nodes_available; then
140 error "not all the nodes are available"
145 # returns 1 if copies of lstats are found running on any of the $TARGETS nodes
147 Nodes_are_not_clean() {
150 debug "Entering Nodes_are_not_clean()"
152 # check whether there are running threads on the targets
153 for TARGET in $TARGETS; do
154 ps_str=`$DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"`
155 if [ -n "$ps_str" ]; then
156 DIRTY_NODES="${DIRTY_NODES} ${TARGET}"
160 if [ -n "$DIRTY_NODES" ]; then
161 debug "Nodes_are_not_clean() returning 1"
165 debug "Nodes_are_not_clean() returning 0"
171 debug "Entering Clean_nodes()"
174 # if debugging is enabled, show lists of lstats processes
175 # still running on the target nodes before the clean operation
177 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]
179 for TARGET in $TARGETS; do
180 debug "List of processes which need to be cleaned up on ${TARGET}:"
181 $DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"
182 debug "List of pids which need to be cleaned up on ${TARGET}:"
183 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} | grep -v grep | ${AWK} '{ print \$2 }'"
188 # do the actual cleanup
189 # kill any old lstats processes still running on the target nodes
191 for TARGET in $TARGETS; do
193 ps_str=`$DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"`
194 if [ -n "$ps_str" ]; then
195 debug "cleaning node ${TARGET}"
196 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} | grep -v grep | ${AWK} '{ print \$2 }' | ${XARGS} kill"
200 debug "Leaving Clean_nodes()"
204 copy_target_script() {
207 debug "Entering copy_target_script()"
209 #copy alex's run scripts to the target
210 copy_cmd="$DCP $SCRIPT ${USER}${target}:$TMP/${SCRIPT}-${target}"
211 ${copy_cmd} 1>/dev/null 2>&1
212 if [ ${PIPESTATUS[0]} != 0 ]; then
213 echo "copy command failed: ${copy_cmd}" 2>&1
214 debug "Leaving copy_target_script() (error return)"
218 echo "$SCRIPT copied to ${USER}${target} (into $TMP)"
219 debug "Leaving copy_target_script() (normal return)"
223 start_target_script() {
226 debug "Entering start_target_script()"
228 if ! copy_target_script $target; then
229 echo "copy_target_script $target failed." 2>&1
230 debug "Leaving start_target_script() (error return)"
234 #run the script on the target
235 $DSH ${USER}${target} "VMSTAT_INTERVAL=${VMSTAT_INTERVAL} \
236 SDIO_INTERVAL=${SDIO_INTERVAL} \
237 SERVICE_INTERVAL=${SERVICE_INTERVAL} \
238 BRW_INTERVAL=${BRW_INTERVAL} \
239 JBD_INTERVAL=${JBD_INTERVAL} \
240 IO_INTERVAL=${IO_INTERVAL} \
241 MBALLOC_INTERVAL=${MBALLOC_INTERVAL} \
242 sh ${TMP}/${SCRIPT}-${target} start \
243 1> /dev/null 2>/dev/null </dev/null"
245 if [ ${PIPESTATUS[0]} != 0 ]; then
246 echo "Start the ${SCRIPT} on ${target} failed"
247 debug "Leaving start_target_script() (error return)"
251 echo "Start the ${SCRIPT} on ${target} success"
252 debug "Leaving start_target_script() (normal return)"
256 stop_target_script() {
259 debug "Entering stop_target_script()"
261 #stop the target script first
262 $DSH ${USER}${target} "sh ${TMP}/${SCRIPT}-${target} stop" 1>/dev/null 2>&1
263 if [ ${PIPESTATUS[0]} != 0 ]; then
264 echo "stop the collecting stats script on ${target} failed"
265 debug "Leaving stop_target_script() (error return)"
268 echo "stop the collecting stats script on ${target} success"
271 #remove those tmp file
272 $DSH ${USER}${target} "rm -rf $TMP/${SCRIPT}-${target}" 1>/dev/null 2>&1
273 echo "cleanup ${target} tmp file after stop "
275 debug "Leaving stop_target_script() (normal return)"
280 # create a unique timestamp-based name which we can use for
281 # naming files on all the $TARGET nodes.
283 # By creating one timestamp here on the master node, we avoid
284 # the problem of clock skew on the $TARGET nodes causing them
285 # to use different filenames than we expect (if their clocks are
286 # different from the clock on this node)
288 generate_timestamp() {
289 if [ "X${GLOBAL_TIMESTAMP}" = "X" ]
291 export GLOBAL_TIMESTAMP=`date +%F-%H.%M.%S`
292 debug "Global Timestamp Created: ${GLOBAL_TIMESTAMP}"
299 local date=${GLOBAL_TIMESTAMP}
300 local target_log_name="stats-${target}-${date}"
302 echo "Getting log: ${target_log_name}.tar.gz from ${target}"
303 $DSH ${USER}${target} "sh ${TMP}/${SCRIPT}-${target} fetch " \
304 > $TMP/${target_log_name}.tar.gz
305 echo "Got log: ${target_log_name}.tar.gz from ${target}"
307 echo "Moving $TMP/${target_log_name}.tar.gz to $TMP/$log_name"
308 mv $TMP/${target_log_name}.tar.gz $TMP/$log_name
313 local log_name=${GLOBAL_TIMESTAMP}
314 local stat_tar_name=$1
316 local -a clients_array
318 debug "Entering fetch_log()"
320 if ! mkdir -p $TMP/$log_name ; then
321 error "can not mkdir $log_name"
325 #retrive the log_tarball from remote nodes background
327 for TARGET in $TARGETS; do
328 (fetch_target_log ${TARGET}) &
330 clients_array[$n]=$TARGET
332 debug "fetch_log: spawned fetch_target_log process for ${TARGET} pid ${pids_array[$n]}"
338 #Waiting log fetch finished
339 for ((n=0; $n < $num_pids; n++)); do
340 debug "fetch_log(): waiting for pid ${pids_array[$n]}"
341 wait ${pids_array[$n]}
344 # TODO: add check of exit status from wait()
348 #compress the log tarball
349 cmd="$TAR ${stat_tar_name} $TMP/${log_name}"
350 echo "Creating compressed tar file ${stat_tar_name} from log files in $TMP/${log_name}"
351 ${cmd} 1>/dev/null 2>&1
352 if [ ${PIPESTATUS[0]} == 0 ]; then
353 echo "removing temporary directory $TMP/${log_name}"
354 rm -rf $TMP/${log_name}
356 echo "Compressed logfiles are in $TMP/${stat_tar_name}"
359 debug "Leaving fetch_log()"
362 stop_targets_script() {
364 local -a clients_array
367 debug "Entering stop_targets_script()"
369 for TARGET in $TARGETS; do
370 (stop_target_script ${TARGET}) &
372 clients_array[$n]=$TARGET
377 #Waiting log fetch finished
378 for ((n=0; $n < $num_pids; n++)); do
379 if ! wait ${pids_array[$n]}; then
380 echo "${clients_array[$n]}: can not stop stats collect"
384 debug "Leaving stop_targets_script()"
390 local -a clients_array
393 debug "Entering gather_start()"
395 #check whether the collect scripts already start in some targets
402 warning "$SCRIPT already running in some targets, attempting cleanup..."
411 error "$SCRIPT automatic cleanup attempt failed."
412 error "$SCRIPT Please make sure lstats is no longer running on target nodes and try again."
413 debug "Error return from gather_start()"
418 for TARGET in $TARGETS; do
419 (start_target_script ${TARGET}) &
421 clients_array[$n]=$TARGET
428 #Waiting log fetch finished
429 for ((n=0; $n < $num_pids; n++)); do
430 if ! wait ${pids_array[$n]}; then
431 echo "${clients_array[$n]}: can not start stats collect"
436 if [ $RC != 0 ]; then
440 debug "Leaving gather_start()"
446 debug "Entering gather_stop()"
448 if [ -n "$log" ]; then
454 debug "Leaving gather_stop()"
461 ln=`grep -n snapshot_time ${log_name} | awk -F":" '{ln=$1;} END{print ln;}'`
462 total_ln=`wc ${log_name} | awk '{print $1}'`
464 local endlen=$((${total_ln} - ${ln}))
473 local statf_name=`basename ${statf}`
474 type_name=`echo ${statf_name} | awk -F "." '{print $3}'`
475 stat_name=`head -n 1 ${statf} | awk '{print $4}'`
476 stat_type=`head -n 1 ${statf} | awk '{print $1}'`
478 #currently, it can only analyse client application log
479 if [ "$stat_type" != "client" ]; then
480 error "can not analyse ${statf} ......."
485 echo "${node_name}_${type_name}, ${stat_name}" \
486 >> $logdir/analyse_${type_name}.csv
488 #get total stats collection
489 end_len=`get_end_line_num ${statf}`
490 if [ $end_len != 1 -a $end_len != 0 ]; then
491 if [ "$type_name" != "osc-rpc_stats" ]; then
492 tail -n $end_len ${statf} | awk '{print $1 "," $2}' \
493 >> $logdir/analyse_${type_name}.csv
495 tail -n $end_len ${statf} | \
496 awk '/^[[:digit:]]/{print $1","$2","$6} \
497 /^page/{print "page per rpc,read,write"} \
498 /^rpcs/{print "rpcs,read,write"} \
499 /^offset/{print "offset, read,write"}' \
500 >> $logdir/analyse_${type_name}.csv
510 debug "Entering gather_analyze()"
513 if [ -z "$log_tarball" -o -r "$option" ]; then
517 if [ ! -r $log_tarball ]; then
518 error " not exist $log_tarball "
524 local date=`date +%F-%H-%M`
525 local logdir="analyse-${date}"
527 mkdir -p ${TMP}/${logdir}
528 mkdir -p ${TMP}/${logdir}/tmp
530 $UNTAR $log_tarball -C ${TMP}/${logdir}/tmp 1>/dev/null 2>&1
531 for log_file in `find $TMP/$logdir/tmp`; do
532 if test -f $log_file; then
534 local file_name=`basename ${log_file}`
535 node_name=`echo ${file_name} | awk -F "-" '{print $2}'`
536 echo "analysing the sublog ...$log_file"
537 mkdir -p ${TMP}/${logdir}/${node_name}
538 mkdir -p ${TMP}/${logdir}/${node_name}/tmp
540 $UNTAR $log_file -C ${TMP}/${logdir}/${node_name}/tmp 1>/dev/null 2>&1
541 for statf in `find ${TMP}/${logdir}/${node_name}/tmp`; do
542 if test -f $statf ; then
543 if [ "$option" == "csv" ]; then
544 get_csv "$TMP/$logdir/${node_name}" "$statf"
548 rm -rf ${TMP}/${logdir}/${node_name}/tmp
552 rm -rf ${TMP}/${logdir}/tmp
553 $TAR ${TMP}/${logdir}.tar.gz ${TMP}/${logdir} 1>/dev/null 2>&1
555 echo "create analysed tarball ${TMP}/${logdir}.tar.gz"
557 debug "Leaving gather_analyze()"
561 start) gather_start ;;
562 stop) gather_stop $@;;
563 analyse) gather_analyse $@;;
564 *) error "Unknown option ${OPTION}" ; exit 1