轉:http://blog.51cto.com/lizhenliang/1610415
1、監控CPU利用率(通過vmstat工具)
#!/bin/bash #==================================================== # Author: lizhenliang - EMail:zhenliang369@163.com # Create Date: 2015-02-01 # Description: cpu utilization monitor # blog:lizhenliang.blog.51cto.com #==================================================== if [ `uname` != "Linux" ];then echo "check os not linux." exit 1 fi which vmstat &>/dev/null #這樣的寫法.這條命令的意思是將標准輸出(返回0)和錯誤輸出(返回非0)全部重定向到/dev/null中,也就是將產生的所有信息丟棄. if [ $? -ne 0 ];then echo "vmstat command no found, please install procps package." exit 1 fi ################################################## cpu_us=`vmstat | awk '{print $13}' | sed -n '$p'` #awk 命令 輸出第13列的數據 sed -n '$p' 最簡輸出內容。 cpu_sy=`vmstat | awk '{print $14}' | sed -n '$p'` cpu_id=`vmstat | awk '{print $15}' | sed -n '$p'` cpu_wa=`vmstat | awk '{print $16}' | sed -n '$p'` #等待I/0完成 cpu_sum=$(($cpu_us+$cpu_sy)) cpu_info() { echo "CPU_Sum : $cpu_sum% ( CPU_Use:${cpu_us}% , CPU_System:${cpu_sy}% )" echo "CPU_Idle : ${cpu_id}%" echo "CPU_Wait : ${cpu_wa}" } #cpu_info; if [ $cpu_sum -ge 90 ];then echo "CPU utilization $cpu_sum." | mail -s "CPU Monitor" baojingtongzhi@163.com fi
2、監控內存利用率
