#!/bin/sh ############################################ # Version: V5.0 2022-06-27 # 1. adjust frame of xbug # 2. record testcase name to testcase.txt # 3. compatible with bash and dash ############################################ #Global parms #define platform name LINUX_PLATFORM="linux" ANDROID_PLATFORM="android" OH_PLATFORM="oh" plat_name="" if [ ! -n "`which awk`" ]; then plat_name=$OH_PLATFORM elif [ -n "`which getprop`" ]; then plat_name=$ANDROID_PLATFORM else plat_name=$LINUX_PLATFORM fi # root pack is eng dir if [ $plat_name = ${ANDROID_PLATFORM} ]; then if [ ! -d /vendor/bin/xbugs ];then if [ ! -d /eng/vendor/bin/xbugs ];then SCRIPT_ROOT_PATH="$(dirname `which xbug+`)/xbugs" else SCRIPT_ROOT_PATH="/eng/vendor/bin/xbugs" fi else SCRIPT_ROOT_PATH="/vendor/bin/xbugs" fi elif [ $plat_name = ${LINUX_PLATFORM} ]; then if [ ! -d /vendor/bin/xbug ];then SCRIPT_ROOT_PATH="$(dirname `which xbug+`)/xbugs" else SCRIPT_ROOT_PATH="/vendor/bin/xbug" fi elif [ $plat_name = ${OH_PLATFORM} ]; then SCRIPT_ROOT_PATH="/vendor/bin/xbugs" fi DFX_LOG_PATH="/data/xbug" SYS_LOG_PATH="${DFX_LOG_PATH}/log" SYS_SPEC_LOG_PATH="${SYS_LOG_PATH}/special" SCRIPT_CLEAN_PATH="${SCRIPT_ROOT_PATH}/common/clean" SCRIPT_BACKGROUD_PATH="${SCRIPT_ROOT_PATH}/common/backgroud" SCRIPT_COLLECT_PATH="${SCRIPT_ROOT_PATH}/common/collector" SCRIPT_SPECIAL_PATH="${SCRIPT_ROOT_PATH}/special" # prepare start_time=$(date +%s) xbug_version="XBUG+ V5.0 2022-06-27 log catcher tool." # 0: no operatiron 1: have prepare operatiron xbug_prepare_flag=0 # 0: default collect if xbug_prepare_flag=0 1: collect system logs 2: collect specical logs xbug_collect_flag=0 # test case name test_name="" log_dir=${DFX_LOG_PATH} run_cmd="" if [ ! -d ${SYS_SPEC_LOG_PATH} ]; then mkdir -p ${SYS_SPEC_LOG_PATH} fi #Help usage() { printf "\n ${xbug_version}\n\n" echo " usage: xbug+ [-m] [COMMAND] [-n] [NAME]" echo "" echo " -h Help" echo " -s special script name + input paramters" echo " -o save log path" echo " -n test case name" echo " -m clean: clear historical logs" echo " tasks: clear historical logs and start some backgroud tasks." echo " collect: collect logs, backgroud tasks informations " echo " example: xbug+ -m clean -s uos_clean" echo " example: xbug+ -m collect -s uos -n 001" printf " example: xbug+ -m collect -s uos -s "pidinfo 2 3 4" -n 001\n\n" exit 1 } check_script_path() { local script_path=$1 if [ $plat_name = ${OH_PLATFORM} ]; then if [ ! -d ${script_path} -o `ls ${script_path} | toybox wc -l` -eq 0 ]; then exit 1 fi elif [ $plat_name = ${ANDROID_PLATFORM} ]; then if [ ! -d ${script_path} -o `ls ${script_path} | busybox wc -l` -eq 0 ]; then exit 1 fi else if [ ! -d ${script_path} -o `ls ${script_path} | wc -l` -eq 0 ]; then exit 1 fi fi } #Execute scripts in SCRIPT_PATH # $1 platform type android or linux # $2 save path of log # $3 script path # $4 script name execute_script() { #Check check_script_path $3 #Add to list local result="" for I in $3/*; do if [ $4 = ${I##*/} ]; then result=$I break fi done if [ -z ${result} ]; then echo "Error, no script found, exit." exit 1 fi #Execute echo "${result} $@" >> ${DFX_LOG_PATH}/${runing_log_name}.txt ${result} $@ 2>> ${DFX_LOG_PATH}/${runing_log_name}.txt } # check platform is Android or Linux check_platform() { if [ ! -n "`which awk`" ]; then echo "OH Start:`date +%Y%m%d%H%M%S`" elif [ -n "`which getprop`" ]; then echo "Android Platform Start:`date +%Y%m%d%H%M%S`" else echo "Linux Platform Start:`date +%Y%m%d%H%M%S`" fi } # dftevent interface record_testcase() { testcase_info="[`date +%Y%m%d%H%M%S`]:$@" log_path=${DFX_LOG_PATH}/testcase.txt if [ ! -f $log_path ]; then touch $log_path fi if [ `cat $log_path | wc -l` -gt 100 ]; then tail -n 50 $log_path > tmp.txt mv tmp.txt $log_path fi echo "$testcase_info" >> $log_path } append_cmd() { run_cmd=${run_cmd}"execute_script ${plat_name} ${SYS_LOG_PATH} $@;" } append_spec_cmd() { run_cmd=${run_cmd}"execute_script ${plat_name} ${SYS_SPEC_LOG_PATH} $@;" } execute_dir_script() { path=$1 fst_prepare="system_clean" if [ $path = ${SCRIPT_CLEAN_PATH} ];then append_cmd $path $fst_prepare fi files=$(ls $path) for filename in $files do if [ $filename != $fst_prepare ];then #echo "file: $filename" append_cmd $path $filename fi done } show_specical_dir_script() { for file in `ls $1`; do if [ -d $1"/"$file ]; then show_specical_dir_script $1"/"$file else echo $1"/"$file fi done } do_specical_dir_script() { local path=$1 local script_name=$2 shift 2 for file in `ls $path`; do if [ -d $path"/"$file ]; then do_specical_dir_script $path"/"$file $script_name $@ #$2 dst script name else # find the script if [ $script_name = $file ]; then # linux logpath paramters append_spec_cmd $path $file $@ special_exit_flag=1 # must collect logs if echo $path |egrep -q "collector";then echo "special collect logs: $file $@" if [ $xbug_collect_flag -eq 0 ];then xbug_collect_flag=2 fi else echo "special prepare or clean operation: $file $@" xbug_prepare_flag=1 fi return fi fi done } execute_special_dir_script() { local special_name=$1 special_exit_flag=0 if [ $plat_name = ${ANDROID_PLATFORM} ];then if [ -d ${SYS_SPEC_LOG_PATH} ]; then rm -rf ${SYS_SPEC_LOG_PATH} mkdir -p ${SYS_SPEC_LOG_PATH} fi fi if [ -n "${special_name}" ];then do_specical_dir_script ${SCRIPT_SPECIAL_PATH} $@ fi if [ $special_exit_flag -eq 0 ];then echo "invalid input, special script as follows:" echo "exp: xbug+ -s xxx" show_specical_dir_script ${SCRIPT_SPECIAL_PATH} exit 1 fi } kill_backgroud_tasks() { # tvos-h (also linux, but not ubuntu, whose progress 'ps' has only arguments with -w, -l and -T) if [ ${plat_name} = ${LINUX_PLATFORM} ] && [ -z "`which dpkg`" ]; then for J in `ps -l | grep xbug | grep -v grep | grep -v xbug+ | sed -e 's/ \+/ /g' | cut -d" " -f 3` do kill $J done # others else for J in `ps -ef | grep xbug | grep -v grep | grep -v xbug+ | sed -e 's/ \+/ /g' | cut -d" " -f 2` do kill $J done fi } system_status_logs() { echo "--- before kill backgroud tasks" > ${SYS_LOG_PATH}/system_status.log 2>&1 if [ ${plat_name} = ${LINUX_PLATFORM} ] && [ -z "`which dpkg`" ]; then ps -l >> ${SYS_LOG_PATH}/system_status.log 2>&1 else ps -eZfww >> ${SYS_LOG_PATH}/system_status.log 2>&1 fi } show_log_dir_size() { echo "----------- log dirs size -----------" du -sh /data/* echo "-------------------------------------" } clean_log_dir() { if [ -d ${log_dir}/log ]; then rm -rf ${log_dir}/log fi } ################main################ printf "${xbug_version}\n" check_platform if [ $plat_name = ${LINUX_PLATFORM} ];then kill_backgroud_tasks fi show_log_dir_size while getopts "m:n:o:s:h" opt; do case $opt in m) case $OPTARG in clean) xbug_prepare_flag=1 if [ $plat_name = ${ANDROID_PLATFORM} ];then kill_backgroud_tasks fi execute_dir_script ${SCRIPT_CLEAN_PATH} ;; tasks) xbug_prepare_flag=1 if [ $plat_name = ${ANDROID_PLATFORM} ];then kill_backgroud_tasks fi execute_dir_script ${SCRIPT_CLEAN_PATH} execute_dir_script ${SCRIPT_BACKGROUD_PATH} ;; collect) xbug_collect_flag=1 ;; *) echo "Error: invalid args ${line}, exit!" usage exit 0 ;; esac ;; n) test_name=${OPTARG} ;; o) if [ $(echo ${OPTARG}|cut -c 1) != "/" ];then log_dir=$(pwd)/${OPTARG} else log_dir=${OPTARG} fi ;; s) execute_special_dir_script ${OPTARG} ;; h|H) usage ;; ?) usage ;; esac done #add testcase to file record_testcase ${test_name} # Rename xbug run log test_time=`date +%Y%m%d%H%M%S` if [ -n "${test_name}" ]; then log_name="xbug_logs_"${test_name}"_"${test_time} runing_log_name="xbug_logs_"${test_name}"_run_details_"${test_time} else log_name="xbug_logs_"${test_time} runing_log_name="xbug_logs_run_details_"${test_time} fi echo > ${DFX_LOG_PATH}/${runing_log_name}.txt # default add collect logs operation if [ ${xbug_collect_flag} -eq 1 ]; then execute_dir_script ${SCRIPT_COLLECT_PATH} fi # exec cmd eval ${run_cmd} #if only prepre operation; exit if [ ${xbug_collect_flag} -eq 0 ]; then if [ ${xbug_prepare_flag} -eq 1 ];then echo "clean or start backgourd task finished." exit 0 fi fi #kill all backgroud tasks, record the ps and dir info system_status_logs kill_backgroud_tasks #pack logs if [ "`ls -A ${SYS_LOG_PATH}`" = "" ]; then echo "NOTICE (${SYS_LOG_PATH}): No such file or directory!" else cd ${DFX_LOG_PATH} if [ ! -d ${log_dir} ]; then echo "[${log_dir}]: No such directory! store log to ${DFX_LOG_PATH}" log_dir=${DFX_LOG_PATH} fi echo "--- taring ${log_dir}/${log_name}.tar.gz" echo "--- log dir file" >> ${SYS_LOG_PATH}/system_status.log 2>&1 ls -l log >> ${SYS_LOG_PATH}/system_status.log tar -zcf ${log_dir}/${log_name}.tar.gz log/ if [ $? -ne 0 ]; then echo "tar error! (${DFX_LOG_PATH}/${log_name}.tar.gz)" exit 1 fi clean_log_dir md5sum ${log_dir}/${log_name}.tar.gz cd - >/dev/null fi # print time end_time=$(date +%s) time_spend=$((${end_time} - ${start_time})) echo "Finished. (time: ${time_spend}s)"