Shell指令碼監控CPU、記憶體和硬碟利用率

來源:互聯網
上載者:User

標籤:vmstat   shell監控cpu利用率 shell監控記憶體利用率 shell監控磁碟利用率

1、監控CPU利用率(通過vmstat工具)

#!/bin/bash
#====================================================
# Author: lizhenliang - EMail:[email protected]
# 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
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‘`
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‘`
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" [email protected]
fi

2、監控記憶體利用率

#!/bin/bash
#====================================================
# Author: lizhenliang - EMail:[email protected]
# Create Date: 2015-02-01
# Description: memory utilization monitor
# blog:lizhenliang.blog.51cto.com
#====================================================
which bc &>/dev/null
if [ $? -ne 0 ];then
        echo "bc command no found, Please install bc package." 
        exit 1
fi
Date=`date +%F" "%H:%M`
IP=`ifconfig eth0 | awk ‘/inet addr/ {print $2}‘ | cut -d: -f2`
Total=`free -m | grep Mem | awk ‘{print $2}‘`
Use=`free -m | awk ‘/buffers\// {print $NF}‘`
Free=$(($Total-$Use))
Total_conv=`echo "scale=2;$Total/1024" | bc | awk ‘{print $1"G"}‘`  #通過bc計算,保留小數點後兩位(scale)
if [ $Free -lt 200 ];then
        Content=`echo -e "Date : $Date \nHost : $IP \nTotal : ${Total_conv} \nUse : ${Use}M \nFree : ${Free}M"`
        echo "$Content" | mail -s "Memory Monitor" [email protected]
fi

3、監控磁碟利用率

#!/bin/bash
#====================================================
# Author: lizhenliang - EMail:[email protected]
# Create Date: 2015-02-01
# Description: disk utilization monitor
# blog:lizhenliang.blog.51cto.com
#====================================================
Date=`date +%F" "%H:%M`
IP=`ifconfig eth0 | awk ‘/inet addr/ {print $2}‘ | cut -d: -f2`
Total=`fdisk -l | grep "Disk /dev/sd[a-z]" |awk ‘{print $2$3"GB"}‘ |sed ‘s/:/=/‘`
Disk_Use=`df -h |awk ‘{print $1"="$5}‘ | sed ‘1d‘ | sed ‘s/%//g‘`
for i in $Disk_Use
do
        A=`echo $i |awk -F‘=‘ ‘{print $2}‘`
        if [ $A -gt 8 ];then
                echo -e "Date : $Date \nHost : $IP \nTotal : $Total \nProblem : Part Use ${i}%" | mail -s "Disk Mo
nitor" [email protected]
        fi
done


本文出自 ““企鵝”那點事兒” 部落格,請務必保留此出處http://lizhenliang.blog.51cto.com/7876557/1610415

Shell指令碼監控CPU、記憶體和硬碟利用率

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.