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.
19 if [ ${PRINT_INFO_MSGS} -gt 0 ]; then
25 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]; then
31 printf $"Usage: iokit-gather-stats [-help] config_file [start|stop|cleanup] <log_name>\n"
32 if [ x$1 = x-h ]; then
34 The distribution script will run on a single node. It is parameterised
35 with a set of target node names. It may assume ssh/scp to these node
36 names works without requiring a password. It will run in 2 modes...
38 iokit-gather-stats config_file start
40 ...will copy the script to /tmp everywhere described in
41 config_file running on all the target hosts. And...
43 iokit-gather-stats config_file stop log_name
45 ...will stop script running on all the hosts it started on and collect
46 all the individual stats files into a single compressed tarball if the log_name is
49 The config file is just a list of shell variable assignments that can be
52 Serveral variables must be set in the config file
54 Targets: the nodes where run the script.
62 options=`getopt -o h --long help:: -- "$@"`
68 eval set -- "$options"
83 if [ $# != 2 -a $# != 3 ] ; then
94 if [ ! -r $CONFIG ]; then
95 error "Config_file: $CONFIG does not exist "
101 if [ -z "$SCRIPT" ]; then
102 error "SCRIPT in ${CONFIG} is empty"
106 if [ -z "$TARGETS" ]; then
107 error "TARGETS in ${CONFIG} is empty"
111 #check nodes accessiable
112 Check_nodes_available() {
113 local NODES_NOT_AVAILABLE=""
115 debug "Entering Check_nodes_available()"
117 for TARGET in $TARGETS; do
118 if ! ping -c 1 -w 3 $TARGET > /dev/null; then
119 NODES_NOT_AVAILABLE=$NODES_NOT_AVAILABLE$TARGET
123 if [ -z "$NODES_NOT_AVAILABLE" ]; then
124 debug "Check_nodes_available() returning 0 "
125 "(success - all nodes available)"
129 error "Check_nodes_available: these nodes are not available "
130 "(did not respond to pings): ${NODES_NOT_AVAILABLE}"
131 debug "Check_nodes_available() returning with errors"
136 if ! Check_nodes_available; then
137 error "not all the nodes are available"
142 # returns 1 if copies of lstats are found running on any of the $TARGETS nodes
144 Nodes_are_not_clean() {
147 debug "Entering Nodes_are_not_clean()"
149 # check whether there are running threads on the targets
150 for TARGET in $TARGETS; do
151 ps_str=`$DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"`
152 if [ -n "$ps_str" ]; then
153 DIRTY_NODES="${DIRTY_NODES} ${TARGET}"
157 if [ -n "$DIRTY_NODES" ]; then
158 debug "Nodes_are_not_clean() returning 1"
162 debug "Nodes_are_not_clean() returning 0"
168 debug "Entering Clean_nodes()"
171 # if debugging is enabled, show lists of lstats processes
172 # still running on the target nodes before the clean operation
174 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]; then
175 for TARGET in $TARGETS; do
176 debug "List of processes which need to be cleaned up on ${TARGET}:"
177 $DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"
178 debug "List of pids which need to be cleaned up on ${TARGET}:"
179 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} | grep -v grep | ${AWK} '{ print \$2 }'"
184 # do the actual cleanup
185 # kill any old lstats processes still running on the target nodes
187 for TARGET in $TARGETS; do
188 ps_str=$($DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}")
189 if [ -n "$ps_str" ]; then
190 debug "cleaning node ${TARGET}"
191 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} |
192 grep -v grep | ${AWK} '{ print \$2 }' |
197 debug "Leaving Clean_nodes()"
201 copy_target_script() {
204 debug "Entering copy_target_script()"
206 #copy alex's run scripts to the target
207 copy_cmd="$DCP $SCRIPT ${USER}${target}:$TMP/${SCRIPT}-${target}"
208 ${copy_cmd} 1>/dev/null 2>&1
209 if [ ${PIPESTATUS[0]} != 0 ]; then
210 echo "copy command failed: ${copy_cmd}" 2>&1
211 debug "Leaving copy_target_script() (error return)"
215 echo "$SCRIPT copied to ${USER}${target} (into $TMP)"
216 debug "Leaving copy_target_script() (normal return)"
220 start_target_script() {
223 debug "Entering start_target_script()"
225 if ! copy_target_script $target; then
226 echo "copy_target_script $target failed." 2>&1
227 debug "Leaving start_target_script() (error return)"
231 #run the script on the target
232 $DSH ${USER}${target} "VMSTAT_INTERVAL=${VMSTAT_INTERVAL} \
233 SDIO_INTERVAL=${SDIO_INTERVAL} \
234 SERVICE_INTERVAL=${SERVICE_INTERVAL} \
235 BRW_INTERVAL=${BRW_INTERVAL} \
236 JBD_INTERVAL=${JBD_INTERVAL} \
237 IO_INTERVAL=${IO_INTERVAL} \
238 MBALLOC_INTERVAL=${MBALLOC_INTERVAL} \
239 sh ${TMP}/${SCRIPT}-${target} start \
240 1> /dev/null 2>/dev/null </dev/null"
242 if [ ${PIPESTATUS[0]} != 0 ]; then
243 echo "Start the ${SCRIPT} on ${target} failed"
244 debug "Leaving start_target_script() (error return)"
248 echo "Start the ${SCRIPT} on ${target} success"
249 debug "Leaving start_target_script() (normal return)"
253 stop_target_script() {
256 debug "Entering stop_target_script()"
258 #stop the target script first
259 $DSH ${USER}${target} "sh ${TMP}/${SCRIPT}-${target} stop" 1>/dev/null 2>&1
260 if [ ${PIPESTATUS[0]} != 0 ]; then
261 echo "stop the collecting stats script on ${target} failed"
262 debug "Leaving stop_target_script() (error return)"
265 echo "stop the collecting stats script on ${target} success"
268 #remove those tmp file
269 $DSH ${USER}${target} "rm -rf $TMP/${SCRIPT}-${target}" 1>/dev/null 2>&1
270 echo "cleanup ${target} tmp file after stop "
272 debug "Leaving stop_target_script() (normal return)"
277 # create a unique timestamp-based name which we can use for
278 # naming files on all the $TARGET nodes.
280 # By creating one timestamp here on the master node, we avoid
281 # the problem of clock skew on the $TARGET nodes causing them
282 # to use different filenames than we expect (if their clocks are
283 # different from the clock on this node)
285 generate_timestamp() {
286 if [ "X${GLOBAL_TIMESTAMP}" = "X" ]; then
287 export GLOBAL_TIMESTAMP=`date +%F-%H.%M.%S`
288 debug "Global Timestamp Created: ${GLOBAL_TIMESTAMP}"
295 local date=${GLOBAL_TIMESTAMP}
296 local target_log_name="stats-${target}-${date}"
298 echo "Getting log: ${target_log_name}.tar.gz from ${target}"
299 $DSH ${USER}${target} "sh ${TMP}/${SCRIPT}-${target} fetch " \
300 > $TMP/${target_log_name}.tar.gz
301 echo "Got log: ${target_log_name}.tar.gz from ${target}"
303 echo "Moving $TMP/${target_log_name}.tar.gz to $TMP/$log_name"
304 mv $TMP/${target_log_name}.tar.gz $TMP/$log_name
309 local log_name=${GLOBAL_TIMESTAMP}
310 local stat_tar_name=$1
312 local -a clients_array
314 debug "Entering fetch_log()"
316 if ! mkdir -p $TMP/$log_name ; then
317 error "can not mkdir $log_name"
321 #retrive the log_tarball from remote nodes background
323 for TARGET in $TARGETS; do
324 (fetch_target_log ${TARGET}) &
326 clients_array[$n]=$TARGET
328 debug "fetch_log: spawned fetch_target_log process for ${TARGET} pid ${pids_array[$n]}"
334 #Waiting log fetch finished
335 for ((n=0; $n < $num_pids; n++)); do
336 debug "fetch_log(): waiting for pid ${pids_array[$n]}"
337 wait ${pids_array[$n]}
340 # TODO: add check of exit status from wait()
344 #compress the log tarball
345 cmd="$TAR ${stat_tar_name} $TMP/${log_name}"
346 echo "Creating compressed tar file ${stat_tar_name} from log files in $TMP/${log_name}"
347 ${cmd} 1>/dev/null 2>&1
348 if [ ${PIPESTATUS[0]} == 0 ]; then
349 echo "removing temporary directory $TMP/${log_name}"
350 rm -rf $TMP/${log_name}
352 echo "Compressed logfiles are in $TMP/${stat_tar_name}"
355 debug "Leaving fetch_log()"
358 stop_targets_script() {
360 local -a clients_array
363 debug "Entering stop_targets_script()"
365 for TARGET in $TARGETS; do
366 (stop_target_script ${TARGET}) &
368 clients_array[$n]=$TARGET
373 #Waiting log fetch finished
374 for ((n=0; $n < $num_pids; n++)); do
375 if ! wait ${pids_array[$n]}; then
376 echo "${clients_array[$n]}: can not stop stats collect"
380 debug "Leaving stop_targets_script()"
385 local -a clients_array
388 debug "Entering gather_start()"
390 #check whether the collect scripts already start in some targets
395 if [ $ret -gt 0 ]; then
396 warning "$SCRIPT already running on some targets, try cleanup"
403 if [ $ret -gt 0 ]; then
404 error "$SCRIPT automatic cleanup attempt failed."
405 error "$SCRIPT Please make sure lstats is not running "\
406 "on target nodes and try again."
407 debug "Error return from gather_start()"
412 for TARGET in $TARGETS; do
413 (start_target_script ${TARGET}) &
415 clients_array[$n]=$TARGET
422 #Waiting log fetch finished
423 for ((n=0; $n < $num_pids; n++)); do
424 if ! wait ${pids_array[$n]}; then
425 echo "${clients_array[$n]}: can not start stats collect"
430 if [ $RC != 0 ]; then
434 debug "Leaving gather_start()"
440 debug "Entering gather_stop()"
442 if [ -n "$log" ]; then
448 debug "Leaving gather_stop()"
455 local ln=$(grep -n snapshot_time ${log_name} |
456 awk -F":" '{ln=$1;} END{print ln;}')
457 local total_ln=$(wc ${log_name} | awk '{print $1}')
459 local endlen=$((total_ln - $ln))
468 local statf_name=`basename ${statf}`
469 type_name=`echo ${statf_name} | awk -F "." '{print $3}'`
470 stat_name=`head -n 1 ${statf} | awk '{print $4}'`
471 stat_type=`head -n 1 ${statf} | awk '{print $1}'`
473 #currently, it can only analyse client application log
474 if [ "$stat_type" != "client" ]; then
475 error "can not analyse ${statf} ......."
480 echo "${node_name}_${type_name}, ${stat_name}" \
481 >> $logdir/analyse_${type_name}.csv
483 #get total stats collection
484 end_len=`get_end_line_num ${statf}`
485 if [ $end_len != 1 -a $end_len != 0 ]; then
486 if [ "$type_name" != "osc-rpc_stats" ]; then
487 tail -n $end_len ${statf} | awk '{print $1 "," $2}' \
488 >> $logdir/analyse_${type_name}.csv
490 tail -n $end_len ${statf} | \
491 awk '/^[[:digit:]]/{print $1","$2","$6} \
492 /^page/{print "page per rpc,read,write"} \
493 /^rpcs/{print "rpcs,read,write"} \
494 /^offset/{print "offset, read,write"}' \
495 >> $logdir/analyse_${type_name}.csv
505 debug "Entering gather_analyze()"
508 if [ -z "$log_tarball" -o -r "$option" ]; then
512 if [ ! -r $log_tarball ]; then
513 error " not exist $log_tarball "
519 local date=`date +%F-%H-%M`
520 local logdir="analyse-${date}"
522 mkdir -p ${TMP}/${logdir}
523 mkdir -p ${TMP}/${logdir}/tmp
525 $UNTAR $log_tarball -C ${TMP}/${logdir}/tmp 1>/dev/null 2>&1
526 for log_file in `find $TMP/$logdir/tmp`; do
527 if test -f $log_file; then
529 local file_name=`basename ${log_file}`
530 node_name=`echo ${file_name} | awk -F "-" '{print $2}'`
531 echo "analysing the sublog ...$log_file"
532 mkdir -p ${TMP}/${logdir}/${node_name}
533 mkdir -p ${TMP}/${logdir}/${node_name}/tmp
535 $UNTAR $log_file -C ${TMP}/${logdir}/${node_name}/tmp 1>/dev/null 2>&1
536 for statf in `find ${TMP}/${logdir}/${node_name}/tmp`; do
537 if test -f $statf ; then
538 if [ "$option" == "csv" ]; then
539 get_csv "$TMP/$logdir/${node_name}" "$statf"
543 rm -rf ${TMP}/${logdir}/${node_name}/tmp
547 rm -rf ${TMP}/${logdir}/tmp
548 $TAR ${TMP}/${logdir}.tar.gz ${TMP}/${logdir} 1>/dev/null 2>&1
550 echo "create analysed tarball ${TMP}/${logdir}.tar.gz"
552 debug "Leaving gather_analyze()"
556 start) gather_start ;;
557 stop) gather_stop $@;;
558 analyse) gather_analyse $@;;
559 *) error "Unknown option ${OPTION}" ; exit 1