function program_exists() { local ret='0' command -v $1 >/dev/null 2>&1 || { local ret='1'; } # 不存在返回0 if [ "$ret" -ne 0 ]; then return 0 fi return 1 }
# iostat 信息收集 function iostat_info() { iostat_info_filename=${SCRIPT_ABS_PATH}/${OUTPUT_PREFIX}_iostat_${CURRENT_TIME}.log
# 如果dstat存在则开启dstat统计 dstat_pid="" program_exists dstat dstat_exist=$? if [ ${dstat_exist} -eq 1 ];then dstat_info & dstat_pid=$! echo "dstat_pid="$dstat_pid fi
blktrace_pid="" if [ ${BLKTRACE_SWITCH} -eq 1 ];then # blktrace 一定要指定磁盘 if [ ${ARG_IOSTAT_DISK} ];then blktrace_info & blktrace_pid=$! echo "blktrace_pid="$blktrace_pid else echo "blktrace need specific device" fi fi
pidstat_pid="" if [ ${PIDSTAT_PROCESS_NAME} ];then pidstat_info & pidstat_pid=$! echo "pidstat_pid="$pidstat_pid fi
echo "iostat_pid:${iostat_pid} vmstat_pid:${vmstat_pid} top_pid:${top_pid} dstat_pid:${dstat_pid} blktrace_pid:${blktrace_pid} pidstat_pid:${pidstat_pid}" > ${STAT_PID_FILE} echo "start stat perf success" elif [ "${SWITCH}" = "stop" ];then echo "stop stat perf" # 判断pid文件是否存在 if [ -e ${STAT_PID_FILE} ];then for pidname_pid in `cat ${STAT_PID_FILE}` do pid=${pidname_pid#*:} pidname=${pidname_pid%:*} echo "stop pid:${pid} pidname:${pidname}" if [ -z ${pid} ];then # 如果这种情况不过滤掉,空的pid值会kill掉整个系统的进程 continue fi pstree -p ${pid} if [ ${pidname} = "blktrace_pid" ];then pstree -p ${pid} | awk -F"[()]" '{for(i=0;i<=NF;i++)if($i~/([0-9])$/)print $i}' | xargs kill -2 2>/dev/null else pstree -p ${pid} | awk -F"[()]" '{for(i=0;i<=NF;i++)if($i~/([0-9])$/)print $i}' | xargs kill -9 2>/dev/null fi done rm -f ${STAT_PID_FILE} fi echo "stop stat perf success" else echo "sh stat_perf.sh [start|stop] [output_file_prefix] &" fi