1 #! /bin/bash 2 3 if [ "$#" -lt 2 ] # $# 傳遞給腳本或函數的參數個數 4 then 5 echo "Usage: $0 program" # $0 腳本名稱 6 exit -1; 7 fi 8 9 SLEEP_TIME=$1 10 LOG=$2 11 12 while true 13 do 14 #線程數 15 thread_num=`sar -q 1 1 | awk '/^Average:/{print $3}'` 16 #句柄數 17 open_files=`sar -v 1 1 | awk '/^Average:/{print $3}'` 18 #cpu佔用率 19 cpu_info=`sar -u 1 1 | awk '/^Average:/{print 100-$8}'` 20 #內存使用量 21 mem=`sar -r 1 1 | awk '/^Average:/{print ($3-$5-$6)/($2+$3)*100}'` 22 #IO傳輸總量 23 io=`sar -b 1 1 | awk '/^Average:/{print $2}'` 24 #輸出到日誌文件 25 echo -e "`date +"%Y-%m-%d %H:%M:%S"`,${cpu_info},${mem},${io},${thread_num},${open_files}" >> ${LOG} 26 sleep $SLEEP_TIME 27 done