Linux Performance Monitoring Shell Script

Linux Performance Monitoring Shell Script

這個 script 會利用linux 內建的一些效能監控工具,執行相關的效能工具並且輸出到記錄檔。

主要用來對於 Linux 主機的效能做一定的基準與監控。

 

可以適當的調整這兩個值,讓效能收集更符合環境所需。

  •  ITER=10  (收集效能資訊幾次,這個例子為十次)
  •  let t=”10*$ITER” (每次收集資訊,間隔多久。這個例子中為 10 sec)

 

(參考來源:http://people.redhat.com)

[pastacode lang=”bash” message=”Linux Performance Monitoring Shell Script” highlight=”” provider=”manual”]

if test x"$1" == x""; then
	echo "Usage: ./perf1.sh <logfile-name>"
	exit 1
fi

FILE=$1
ITER=10

if test -e $FILE; then
	echo "WARNING: $FILE exists -- will NOT overwrite"
	exit 1
fi


function log () {
	d='date'
	echo "$d: $1"
	echo "==================================================" >> $FILE
	echo "$d: $1" >> $FILE
	if test x"$2" != x""; then
	shift
	fi
	$* >> $FILE 2>&1
}
function logi () {
	d='date'
	printf "$d: %-20.20s (PASS $1 of $ITER)\n" $2
	echo "==================================================" >> $FILE
	printf "$d: %-20.20s (PASS $1 of $ITER)\n" $2 >> $FILE
	shift
	if test x"$2" != x""; then
		shift
	fi
	$* >> $FILE 2>&1
}
function ccc () {
	log $1 cat $1
}
function ccci () {
	logi $1 $2 cat $2
}

function note () {
	echo "'date': (NOTE: $*)"
}


function banner () {
	d='date'
	echo "=================================================="
	echo "===== $d: $* ====="
	echo "==================================================" >> $FILE
	echo "===== $d: $* =====" >> $FILE
}


banner "Start of Testing ($FILE)"
banner General System Information
log uname uname -a
log free
log df df -h
log mount
log lsmod
log lspci lspci -v
log dmidecode
log route route -n
log ifconfig
log "ip rule ls" ip rule ls
log "ip route ls" ip route ls
log iptables "iptables -L -n -v"
log sysctl sysctl -a
ccc /proc/cpuinfo
ccc /proc/meminfo
ccc /proc/net/dev
ccc /proc/interrupts
ccc /proc/devices
ccc /proc/cmdline
ccc /proc/scsi/scsi
ccc /etc/modules.conf
ccc /var/log/dmesg

banner Performance Snapshot
log ps ps auxwww
log sar sar -A
let t="10*$ITER"
note "The following takes about $t seconds"

log "vmstat" vmstat $ITER 10
note "The following takes about $t seconds"
log "iostat" iostat -k $ITER 10
note "Each pass takes 10 seconds"
for i in $(seq 1 $ITER); do
	note "**** PASS $i of $ITER"
	logi $i uptime
	logi $i free
	ccci $i /proc/interrupts
	ccci $i /proc/stat
	logi $i ifconfig ifconfig -a
	sleep 10
done
banner "End of Testing ($FILE)"




[/pastacode]

Leave a Reply

Your email address will not be published. Required fields are marked *