4 # script on a selection of nodes and collect all the results into a single
7 # Copyright 2008 Sun Microsystems, Inc. All rights reserved
8 # Use is subject to license terms.
10 # Copyright (c) 2014, Intel Corporation.
22 if [ ${PRINT_INFO_MSGS} -gt 0 ]; then
28 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]; then
34 printf $"Usage: iokit-gather-stats [-help] config_file [start|stop|cleanup] <log_name>\n"
35 if [ x$1 = x-h ]; then
37 The distribution script will run on a single node. It is parameterised
38 with a set of target node names. It may assume ssh/scp to these node
39 names works without requiring a password. It will run in 2 modes...
41 iokit-gather-stats config_file start
43 ...will copy the script to /tmp everywhere described in
44 config_file running on all the target hosts. And...
46 iokit-gather-stats config_file stop log_name
48 ...will stop script running on all the hosts it started on and collect
49 all the individual stats files into a single compressed tarball if the log_name is
52 The config file is just a list of shell variable assignments that can be
55 Serveral variables must be set in the config file
57 Targets: the nodes where run the script.
65 options=`getopt -o h --long help:: -- "$@"`
71 eval set -- "$options"
86 if [ $# != 2 -a $# != 3 ] ; then
97 if [ ! -r $CONFIG ]; then
98 error "Config_file: $CONFIG does not exist "
104 if [ -z "$SCRIPT" ]; then
105 error "SCRIPT in ${CONFIG} is empty"
109 if [ -z "$TARGETS" ]; then
110 error "TARGETS in ${CONFIG} is empty"
114 #check nodes accessiable
115 Check_nodes_available() {
116 local NODES_NOT_AVAILABLE=""
118 debug "Entering Check_nodes_available()"
120 for TARGET in $TARGETS; do
121 if ! ping -c 1 -w 3 $TARGET > /dev/null; then
122 NODES_NOT_AVAILABLE=$NODES_NOT_AVAILABLE$TARGET
126 if [ -z "$NODES_NOT_AVAILABLE" ]; then
127 debug "Check_nodes_available() returning 0 "
128 "(success - all nodes available)"
132 error "Check_nodes_available: these nodes are not available "
133 "(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 ]; then
178 for TARGET in $TARGETS; do
179 debug "List of processes which need to be cleaned up on ${TARGET}:"
180 $DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"
181 debug "List of pids which need to be cleaned up on ${TARGET}:"
182 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} | grep -v grep | ${AWK} '{ print \$2 }'"
187 # do the actual cleanup
188 # kill any old lstats processes still running on the target nodes
190 for TARGET in $TARGETS; do
191 ps_str=$($DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}")
192 if [ -n "$ps_str" ]; then
193 debug "cleaning node ${TARGET}"
194 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} |
195 grep -v grep | ${AWK} '{ print \$2 }' |
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" ]; then
290 export GLOBAL_TIMESTAMP=`date +%F-%H.%M.%S`
291 debug "Global Timestamp Created: ${GLOBAL_TIMESTAMP}"
298 local date=${GLOBAL_TIMESTAMP}
299 local target_log_name="stats-${target}-${date}"
301 echo "Getting log: ${target_log_name}.tar.gz from ${target}"
302 $DSH ${USER}${target} "sh ${TMP}/${SCRIPT}-${target} fetch " \
303 > $TMP/${target_log_name}.tar.gz
304 echo "Got log: ${target_log_name}.tar.gz from ${target}"
306 echo "Moving $TMP/${target_log_name}.tar.gz to $TMP/$log_name"
307 mv $TMP/${target_log_name}.tar.gz $TMP/$log_name
312 local log_name=${GLOBAL_TIMESTAMP}
313 local stat_tar_name=$1
315 local -a clients_array
317 debug "Entering fetch_log()"
319 if ! mkdir -p $TMP/$log_name ; then
320 error "can not mkdir $log_name"
324 #retrive the log_tarball from remote nodes background
326 for TARGET in $TARGETS; do
327 (fetch_target_log ${TARGET}) &
329 clients_array[$n]=$TARGET
331 debug "fetch_log: spawned fetch_target_log process for ${TARGET} pid ${pids_array[$n]}"
337 #Waiting log fetch finished
338 for ((n=0; $n < $num_pids; n++)); do
339 debug "fetch_log(): waiting for pid ${pids_array[$n]}"
340 wait ${pids_array[$n]}
343 # TODO: add check of exit status from wait()
347 #compress the log tarball
348 cmd="$TAR ${stat_tar_name} $TMP/${log_name}"
349 echo "Creating compressed tar file ${stat_tar_name} from log files in $TMP/${log_name}"
350 ${cmd} 1>/dev/null 2>&1
351 if [ ${PIPESTATUS[0]} == 0 ]; then
352 echo "removing temporary directory $TMP/${log_name}"
353 rm -rf $TMP/${log_name}
355 echo "Compressed logfiles are in $TMP/${stat_tar_name}"
358 debug "Leaving fetch_log()"
361 stop_targets_script() {
363 local -a clients_array
366 debug "Entering stop_targets_script()"
368 for TARGET in $TARGETS; do
369 (stop_target_script ${TARGET}) &
371 clients_array[$n]=$TARGET
376 #Waiting log fetch finished
377 for ((n=0; $n < $num_pids; n++)); do
378 if ! wait ${pids_array[$n]}; then
379 echo "${clients_array[$n]}: can not stop stats collect"
383 debug "Leaving stop_targets_script()"
388 local -a clients_array
391 debug "Entering gather_start()"
393 #check whether the collect scripts already start in some targets
398 if [ $ret -gt 0 ]; then
399 warning "$SCRIPT already running on some targets, try cleanup"
406 if [ $ret -gt 0 ]; then
407 error "$SCRIPT automatic cleanup attempt failed."
408 error "$SCRIPT Please make sure lstats is not running "\
409 "on target nodes and try again."
410 debug "Error return from gather_start()"
415 for TARGET in $TARGETS; do
416 (start_target_script ${TARGET}) &
418 clients_array[$n]=$TARGET
425 #Waiting log fetch finished
426 for ((n=0; $n < $num_pids; n++)); do
427 if ! wait ${pids_array[$n]}; then
428 echo "${clients_array[$n]}: can not start stats collect"
433 if [ $RC != 0 ]; then
437 debug "Leaving gather_start()"
443 debug "Entering gather_stop()"
445 if [ -n "$log" ]; then
451 debug "Leaving gather_stop()"
458 local ln=$(grep -n snapshot_time ${log_name} |
459 awk -F":" '{ln=$1;} END{print ln;}')
460 local total_ln=$(wc ${log_name} | awk '{print $1}')
462 local endlen=$((total_ln - $ln))
471 local statf_name=`basename ${statf}`
472 type_name=`echo ${statf_name} | awk -F "." '{print $3}'`
473 stat_name=`head -n 1 ${statf} | awk '{print $4}'`
474 stat_type=`head -n 1 ${statf} | awk '{print $1}'`
476 #currently, it can only analyse client application log
477 if [ "$stat_type" != "client" ]; then
478 error "can not analyse ${statf} ......."
483 echo "${node_name}_${type_name}, ${stat_name}" \
484 >> $logdir/analyse_${type_name}.csv
486 #get total stats collection
487 end_len=`get_end_line_num ${statf}`
488 if [ $end_len != 1 -a $end_len != 0 ]; then
489 if [ "$type_name" != "osc-rpc_stats" ]; then
490 tail -n $end_len ${statf} | awk '{print $1 "," $2}' \
491 >> $logdir/analyse_${type_name}.csv
493 tail -n $end_len ${statf} | \
494 awk '/^[[:digit:]]/{print $1","$2","$6} \
495 /^page/{print "page per rpc,read,write"} \
496 /^rpcs/{print "rpcs,read,write"} \
497 /^offset/{print "offset, read,write"}' \
498 >> $logdir/analyse_${type_name}.csv
508 debug "Entering gather_analyze()"
511 if [ -z "$log_tarball" -o -r "$option" ]; then
515 if [ ! -r $log_tarball ]; then
516 error " not exist $log_tarball "
522 local date=`date +%F-%H-%M`
523 local logdir="analyse-${date}"
525 mkdir -p ${TMP}/${logdir}
526 mkdir -p ${TMP}/${logdir}/tmp
528 $UNTAR $log_tarball -C ${TMP}/${logdir}/tmp 1>/dev/null 2>&1
529 for log_file in `find $TMP/$logdir/tmp`; do
530 if test -f $log_file; then
532 local file_name=`basename ${log_file}`
533 node_name=`echo ${file_name} | awk -F "-" '{print $2}'`
534 echo "analysing the sublog ...$log_file"
535 mkdir -p ${TMP}/${logdir}/${node_name}
536 mkdir -p ${TMP}/${logdir}/${node_name}/tmp
538 $UNTAR $log_file -C ${TMP}/${logdir}/${node_name}/tmp 1>/dev/null 2>&1
539 for statf in `find ${TMP}/${logdir}/${node_name}/tmp`; do
540 if test -f $statf ; then
541 if [ "$option" == "csv" ]; then
542 get_csv "$TMP/$logdir/${node_name}" "$statf"
546 rm -rf ${TMP}/${logdir}/${node_name}/tmp
550 rm -rf ${TMP}/${logdir}/tmp
551 $TAR ${TMP}/${logdir}.tar.gz ${TMP}/${logdir} 1>/dev/null 2>&1
553 echo "create analysed tarball ${TMP}/${logdir}.tar.gz"
555 debug "Leaving gather_analyze()"
559 start) gather_start ;;
560 stop) gather_stop $@;;
561 analyse) gather_analyse $@;;
562 *) error "Unknown option ${OPTION}" ; exit 1