首页 > 代码库 > Shell脚本监控CPU、内存和硬盘利用率
Shell脚本监控CPU、内存和硬盘利用率
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
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" baojingtongzhi@163.com
fi
2、监控内存利用率
#!/bin/bash
#====================================================
# Author: lizhenliang - EMail:zhenliang369@163.com
# 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" baojingtongzhi@163.com
fi
3、监控磁盘利用率
#!/bin/bash
#====================================================
# Author: lizhenliang - EMail:zhenliang369@163.com
# 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" baojingtongzhi@163.com
fi
done
本文出自 ““企鹅”那点事儿” 博客,请务必保留此出处http://lizhenliang.blog.51cto.com/7876557/1610415
Shell脚本监控CPU、内存和硬盘利用率
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。