momo zone

调核人的blog

一个硬盘压力测试脚本

主要调用iozone 进行压力测试测试,另外要求hdparm新版本

 

#!/bin/sh

# usage : ./hddtest.sh [device] mode [loop count]

# default device is /dev/sda
# "mode" can be "test","benchmark"
# "loop count" is only for benchmark mode

HDPARM=$(which hdparm 2>/dev/null)
DD=$(which dd 2>/dev/null)
SMARTCTL=$(which smartctl 2>/dev/null)
IOZONE="/opt/iozone/bin/iozone"

TOTAL_MEM=$(grep MemTotal /proc/meminfo|awk '{print $2}')

DEV="/dev/sda"
HD_PARM="-T -t"

LOOP=100

if [ -z ${HDPARM}   ]
then	echo "hdparm can't found"
	exit 1
fi

if [ -z ${DD}   ]
then    echo "dd can't found"
	exit 1
fi

if [ -z ${SMARTCTL}   ]
then    echo "smartctl can't found"
	exit 1
fi

if [ -b "$1"  ]
then 
	DEV=$1
	echo "set device to ${DEV}"
#else
# echo "device can't found , set default $DEV"

fi

#get MAX_SPACE(KB) to alloc fsblk for iozone and dd write test
MAX_SPACE_SIZE=$(df |sort -n -k 4 |grep -e "^${DEV}"|tail -1|awk '{printf $4}')
TMP_PATH="$(df |sort -n -k 4 |grep -e "^${DEV}"|tail -1|awk '{printf $6}')/test.tmp"

#IOZ_PARM="-i 0 -i 1 -i 2 -r 16k -s $((TOTAL_MEM*2))"
IOZ_PARM="-a -s 32g -y 4k -q 10240k -i 0 -f ${TMP_PATH} -b report.xls"

usage(){
	echo 'Usage:'
	echo -e ' hddtest.sh device mode [loop_count]\n'
	echo -e '"device" is block device in /dev'
	echo '"mode" can be "test" or "benchmark"'
	echo '"loop count" is only for benchmark mode,default is 100'

	echo -e '\nExample:\n\thddtest.sh /dev/sda test\n'
	echo -e '\thddtest.sh /dev/sda benchmark 1000\n'
}

hddinfo(){	
	$HDPARM -I $DEV
}

hddspeed(){
	echo -e "\n#####################################"
	echo -e "Start cached and buffered read test:"
	echo -e "\n#####################################"
	$HDPARM $HD_PARM $DEV
}

ddspeed(){
	echo -e "\n#####################################"
	echo -e "Start direct read and write test:"
	echo -e "\n#####################################"

	dd if=${DEV} of=/dev/null bs=16384K count=100 iflag=nonblock,noatime,direct oflag=nonblock,noatime,nocache
	dd if=/proc/kcore of=${TMP_PATH} bs=16384K count=100 iflag=nonblock,nocache,noatime oflag=nonblock,direct,noatime	

	rm ${TMP_PATH}
}

fsspeed(){
	echo -e "\n#####################################"
	echo -e "Start file-system read and write test"
	echo -e "\n#####################################"

	$IOZONE $IOZ_PARM
}

benchmark(){

        echo -e "\n#####################################"
        echo -e "Start to benchmark fs I/O"
        echo -e "\n#####################################"

	while [  $LOOP -gt 0  ]
	do
		ctime=$(date '+%Y-%m-%d-%H-%M-%S')
		$SMARTCTL -A $DEV > /tmp/smartlog
		grep -E '(Reallocated_Sector_Ct|Current_Pending_Sector)' /tmp/smartlog > /tmp/smartlog.${ctime}

		$IOZONE $IOZ_PARM

		grep -E '(Reallocated_Sector_Ct|Current_Pending_Sector)' /tmp/smartlog > /tmp/smartlog.after

		diff -u /tmp/smartlog.${ctime} /tmp/smartlog.after > /tmp/smartlog

		if [ $? -ne 0  ]
		then
			printf "33[31m!!Found HDD issue,please cat /tmp/smartlog for detail!!\n33[0m"
			echo "remain loop: ${LOOP}" >> /tmp/smartlog
	# diff -u /tmp/smartlog.${ctime} /tmp/smartlog.after > /tmp/smartlog
			break
		fi

		LOOP=$((LOOP-1))
	done

# rm /tmp/smartlog*
}

# start here

if [ "$2" = "test" ]
then
	hddinfo
	hddspeed
	ddspeed
	fsspeed
elif [ "$2" = "benchmark" ]
then
	if [ -n "$3" ]
	then
		LOOP=$3
	else
		echo "loop count set to $LOOP"
	fi
	benchmark
else
	echo -e "Unknow command\n"
	usage
	exit 1
fi
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: