Shell腳本監控CPU、內存和硬盤利用率


: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、監控內存利用率

 1 #!/bin/bash
 2 #====================================================
 3 # Author: lizhenliang - EMail:zhenliang369@163.com
 4 # Create Date: 2015-02-01
 5 # Description: memory utilization monitor
 6 # blog:lizhenliang.blog.51cto.com
 7 #====================================================
 8 which bc &>/dev/null         #驗證bc命令是否存在,若存在返回值是0,若不存在返回值是非零。
 9 if [ $? -ne 0 ];then
10         echo "bc command no found, Please install bc package." 
11         exit 1
12 fi
13 Date=`date +%F" "%H:%M`     #日期輸出    %F 日期(年月日) ; "  "分隔符調整顯示后的格式 ; %H:%M 時間(時:分)。 
14 IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d: -f2` #查詢ip地址
15 Total=`free -m | grep Mem | awk '{print $2}'`           #查詢剩余內存還剩多少以  -m (以M為單位輸出) 
16 Use=`free -m | awk '/buffers\// {print $NF}'`
17 Free=$(($Total-$Use))
18 Total_conv=`echo "scale=2;$Total/1024" | bc | awk '{print $1"G"}'`  #通過bc計算,保留小數點后兩位(scale)
19 if [ $Free -lt 200 ];then
20         Content=`echo -e "Date : $Date \nHost : $IP \nTotal : ${Total_conv} \nUse : ${Use}M \nFree : ${Free}M"`
21         echo "$Content" | mail -s "Memory Monitor" baojingtongzhi@163.com
22 fi

 3、監控磁盤利用率

 1 #!/bin/bash
 2 #====================================================
 3 # Author: lizhenliang - EMail:zhenliang369@163.com
 4 # Create Date: 2015-02-01
 5 # Description: disk utilization monitor
 6 # blog:lizhenliang.blog.51cto.com
 7 #====================================================
 8 Date=`date +%F" "%H:%M`
 9 IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d: -f2`
10 Total=`fdisk -l | grep "Disk /dev/sd[a-z]" |awk '{print $2$3"GB"}' |sed 's/:/=/' |xargs echo -n |sed 's/[ ]/,/g'`    #去掉換行符,並以逗號分隔在郵件顯示總每個分區大小
11 Disk_Use=`df -h |awk '{print $1"="$5}' | sed '1d' | sed 's/%//g'`
12 for i in $Disk_Use
13 do
14         A=`echo $i |awk -F'=' '{print $2}'`
15         if [ $A -gt 8 ];then
16                 echo -e "Date : $Date \nHost : $IP \nTotal : $Total \nProblem : Part Use ${i}%" | mail -s "Disk Mo
17 nitor" baojingtongzhi@163.com
18         fi
19 done


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM