3 # gather_stats_everywhere:
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 ]
26 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]
33 printf $"Usage: gather_stats_everywhere [-help] config_file [start|stop|cleanup] <log_name>\n"
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 gather_stats_everywhere config_file start
43 ...will copy the script to /tmp everywhere described in
44 config_file running on all the target hosts. And...
46 gather_stats_everywhere 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:: -- "$@"`
72 eval set -- "$options"
87 if [ $# != 2 -a $# != 3 ] ; then
98 if [ ! -r $CONFIG ]; then
99 error "Config_file: $CONFIG does not exist "
105 if [ -z "$SCRIPT" ]; then
106 error "SCRIPT in ${CONFIG} is empty"
110 if [ -z "$TARGETS" ]; then
111 error "TARGETS in ${CONFIG} is empty"
115 #check nodes accessiable
116 Check_nodes_available() {
117 local NODES_NOT_AVAILABLE=""
119 debug "Entering Check_nodes_available()"
121 for TARGET in $TARGETS; do
122 if ! ping -c 1 -w 3 $TARGET > /dev/null; then
123 NODES_NOT_AVAILABLE=$NODES_NOT_AVAILABLE$TARGET
127 if [ -z "$NODES_NOT_AVAILABLE" ]; then
128 debug "Check_nodes_available() returning 0 (success - all nodes available)"
132 error "Check_nodes_available: these nodes are not available (did not respond to pings): ${NODES_NOT_AVAILABLE}"
133 debug "Check_nodes_available() returning with errors"
138 if ! Check_nodes_available; then
139 error "not all the nodes are available"
144 # returns 1 if copies of lstats are found running on any of the $TARGETS nodes
146 Nodes_are_not_clean() {
149 debug "Entering Nodes_are_not_clean()"
151 # check whether there are running threads on the targets
152 for TARGET in $TARGETS; do
153 ps_str=`$DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"`
154 if [ -n "$ps_str" ]; then
155 DIRTY_NODES="${DIRTY_NODES} ${TARGET}"
159 if [ -n "$DIRTY_NODES" ]; then
160 debug "Nodes_are_not_clean() returning 1"
164 debug "Nodes_are_not_clean() returning 0"
170 debug "Entering Clean_nodes()"
173 # if debugging is enabled, show lists of lstats processes
174 # still running on the target nodes before the clean operation
176 if [ ${PRINT_DEBUG_MSGS} -gt 0 ]
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
192 ps_str=`$DSH $TARGET "ps aux | grep -v grep | grep ${SCRIPT}-${TARGET}"`
193 if [ -n "$ps_str" ]; then
194 debug "cleaning node ${TARGET}"
195 $DSH $TARGET "ps aux | grep ${SCRIPT}-${TARGET} | grep -v grep | ${AWK} '{ print \$2 }' | ${XARGS} kill"
199 debug "Leaving Clean_nodes()"
203 copy_target_script() {
206 debug "Entering copy_target_script()"
208 #copy alex's run scripts to the target
209 copy_cmd="$DCP $SCRIPT ${USER}${target}:$TMP/${SCRIPT}-${target}"
210 ${copy_cmd} 1>/dev/null 2>&1
211 if [ ${PIPESTATUS[0]} != 0 ]; then
212 echo "copy command failed: ${copy_cmd}" 2>&1
213 debug "Leaving copy_target_script() (error return)"
217 echo "$SCRIPT copied to ${USER}${target} (into $TMP)"
218 debug "Leaving copy_target_script() (normal return)"
222 start_target_script() {
225 debug "Entering start_target_script()"
227 if ! copy_target_script $target; then
228 echo "copy_target_script $target failed." 2>&1
229 debug "Leaving start_target_script() (error return)"
233 #run the script on the target
234 $DSH ${USER}${target} "VMSTAT_INTERVAL=${VMSTAT_INTERVAL} \
235 SDIO_INTERVAL=${SDIO_INTERVAL} \
236 SERVICE_INTERVAL=${SERVICE_INTERVAL} \
237 BRW_INTERVAL=${BRW_INTERVAL} \
238 JBD_INTERVAL=${JBD_INTERVAL} \
239 IO_INTERVAL=${IO_INTERVAL} \
240 MBALLOC_INTERVAL=${MBALLOC_INTERVAL} \
241 sh ${TMP}/${SCRIPT}-${target} start \
242 1> /dev/null 2>/dev/null </dev/null"
244 if [ ${PIPESTATUS[0]} != 0 ]; then
245 echo "Start the ${SCRIPT} on ${target} failed"
246 debug "Leaving start_target_script() (error return)"
250 echo "Start the ${SCRIPT} on ${target} success"
251 debug "Leaving start_target_script() (normal return)"
255 stop_target_script() {
258 debug "Entering stop_target_script()"
260 #stop the target script first
261 $DSH ${USER}${target} "sh ${TMP}/${SCRIPT}-${target} stop" 1>/dev/null 2>&1
262 if [ ${PIPESTATUS[0]} != 0 ]; then
263 echo "stop the collecting stats script on ${target} failed"
264 debug "Leaving stop_target_script() (error return)"
267 echo "stop the collecting stats script on ${target} success"
270 #remove those tmp file
271 $DSH ${USER}${target} "rm -rf $TMP/${SCRIPT}-${target}" 1>/dev/null 2>&1
272 echo "cleanup ${target} tmp file after stop "
274 debug "Leaving stop_target_script() (normal return)"
279 # create a unique timestamp-based name which we can use for
280 # naming files on all the $TARGET nodes.
282 # By creating one timestamp here on the master node, we avoid
283 # the problem of clock skew on the $TARGET nodes causing them
284 # to use different filenames than we expect (if their clocks are
285 # different from the clock on this node)
287 generate_timestamp() {
288 if [ "X${GLOBAL_TIMESTAMP}" = "X" ]
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()"
389 local -a clients_array
392 debug "Entering gather_start()"
394 #check whether the collect scripts already start in some targets
401 warning "$SCRIPT already running in some targets, attempting cleanup..."
410 error "$SCRIPT automatic cleanup attempt failed."
411 error "$SCRIPT Please make sure lstats is no longer running on target nodes and try again."
412 debug "Error return from gather_start()"
417 for TARGET in $TARGETS; do
418 (start_target_script ${TARGET}) &
420 clients_array[$n]=$TARGET
427 #Waiting log fetch finished
428 for ((n=0; $n < $num_pids; n++)); do
429 if ! wait ${pids_array[$n]}; then
430 echo "${clients_array[$n]}: can not start stats collect"
435 if [ $RC != 0 ]; then
439 debug "Leaving gather_start()"
445 debug "Entering gather_stop()"
447 if [ -n "$log" ]; then
453 debug "Leaving gather_stop()"
460 ln=`grep -n snapshot_time ${log_name} | awk -F":" '{ln=$1;} END{print ln;}'`
461 total_ln=`wc ${log_name} | awk '{print $1}'`
463 local endlen=$((${total_ln} - ${ln}))
472 local statf_name=`basename ${statf}`
473 type_name=`echo ${statf_name} | awk -F "." '{print $3}'`
474 stat_name=`head -n 1 ${statf} | awk '{print $4}'`
475 stat_type=`head -n 1 ${statf} | awk '{print $1}'`
477 #currently, it can only analyse client application log
478 if [ "$stat_type" != "client" ]; then
479 error "can not analyse ${statf} ......."
484 echo "${node_name}_${type_name}, ${stat_name}" \
485 >> $logdir/analyse_${type_name}.csv
487 #get total stats collection
488 end_len=`get_end_line_num ${statf}`
489 if [ $end_len != 1 -a $end_len != 0 ]; then
490 if [ "$type_name" != "osc-rpc_stats" ]; then
491 tail -n $end_len ${statf} | awk '{print $1 "," $2}' \
492 >> $logdir/analyse_${type_name}.csv
494 tail -n $end_len ${statf} | \
495 awk '/^[[:digit:]]/{print $1","$2","$6} \
496 /^page/{print "page per rpc,read,write"} \
497 /^rpcs/{print "rpcs,read,write"} \
498 /^offset/{print "offset, read,write"}' \
499 >> $logdir/analyse_${type_name}.csv
509 debug "Entering gather_analyze()"
512 if [ -z "$log_tarball" -o -r "$option" ]; then
516 if [ ! -r $log_tarball ]; then
517 error " not exist $log_tarball "
523 local date=`date +%F-%H-%M`
524 local logdir="analyse-${date}"
526 mkdir -p ${TMP}/${logdir}
527 mkdir -p ${TMP}/${logdir}/tmp
529 $UNTAR $log_tarball -C ${TMP}/${logdir}/tmp 1>/dev/null 2>&1
530 for log_file in `find $TMP/$logdir/tmp`; do
531 if test -f $log_file; then
533 local file_name=`basename ${log_file}`
534 node_name=`echo ${file_name} | awk -F "-" '{print $2}'`
535 echo "analysing the sublog ...$log_file"
536 mkdir -p ${TMP}/${logdir}/${node_name}
537 mkdir -p ${TMP}/${logdir}/${node_name}/tmp
539 $UNTAR $log_file -C ${TMP}/${logdir}/${node_name}/tmp 1>/dev/null 2>&1
540 for statf in `find ${TMP}/${logdir}/${node_name}/tmp`; do
541 if test -f $statf ; then
542 if [ "$option" == "csv" ]; then
543 get_csv "$TMP/$logdir/${node_name}" "$statf"
547 rm -rf ${TMP}/${logdir}/${node_name}/tmp
551 rm -rf ${TMP}/${logdir}/tmp
552 $TAR ${TMP}/${logdir}.tar.gz ${TMP}/${logdir} 1>/dev/null 2>&1
554 echo "create analysed tarball ${TMP}/${logdir}.tar.gz"
556 debug "Leaving gather_analyze()"
560 start) gather_start ;;
561 stop) gather_stop $@;;
562 analyse) gather_analyse $@;;
563 *) error "Unknown option ${OPTION}" ; exit 1