#!/usr/bin/bash
echo "##########关于cpu##########"
vmstat &>/dev/null
if [ $? -ne 0 ];then
echo "vmstat command is not found,please install it!"
yum install -y procps
echo "命令已安装!请重新执行该命令"
exit 0
fi
#us 用户使用cpu花费时间; sy 系统使用cpu花费时间;id cpu空闲时间id=us+sy;wa cpu耗费等待io上时间
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))
echo "cpu空闲时间(id):$cpu_id"
echo "cpu等待时间:"$cpu_wa#!/usr/bin/bash
echo "##########关于cpu##########"
vmstat &>/dev/null
if [ $? -ne 0 ];then
echo "vmstat command is not found,please install it!"
yum install -y procps
echo "命令已安装!请重新执行该命令"
exit 0
fi
#us 用户使用cpu花费时间; sy 系统使用cpu花费时间;id cpu空闲时间id=us+sy;wa cpu耗费等待io上时间
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))
echo "cpu空闲时间(id):$cpu_id"
echo "cpu等待时间:"$cpu_wa
echo "cpu 总共用时(us+sy): $cpu_sum"
if [ $cpu_sum -ge 70 ];then
echo "your cpu utilization is $cpu_sum."|mail -s "cpu utilization" [email protected]
fi
echo "###########关于内存#############"
free &>/dev/null
if [ $? -ne 0 ];then
echo "free command is not found,please install it!"
exit 0
fi
memory=free -m|grep Mem|awk ‘{print "使用的内存:"$2"M,还剩余内存"$3"M,所以内存使用率为:"$3/$2*100"%"}‘
echo $memory
memory_1=free -m|grep Mem|awk ‘{print ""$3/$2*100""}‘
if [ $? -ge 80 ];then
echo "your memory utilization is $memory_1"|mail -s "memory error" [email protected]
fi
echo "#############关于磁盘############"
df &>/dev/null
if [ $? -ne 0 ];then
echo "free command is not found,please install it!"
exit 0
fi
disk_0=df -h|sed -n ‘3,5p‘|awk ‘{print $(NF-1)}‘
disk=df -h|sed -n ‘3,5p‘|awk ‘{print $(NF-1)}‘|cut -b 1
echo $disk_0
for i in $disk
do
if [ $i -ge 70 ];then
echo "your disk utilization is %i"|mail -s "disk error" [email protected]
fi
done
echo "cpu 总共用时(us+sy): $cpu_sum"
if [ $cpu_sum -ge 70 ];then
echo "your cpu utilization is $cpu_sum."|mail -s "cpu utilization" [email protected]
fi
echo "###########关于内存#############"
free &>/dev/null
if [ $? -ne 0 ];then
echo "free command is not found,please install it!"
exit 0
fi
memory=free -m|grep Mem|awk ‘{print "使用的内存:"$2"M,还剩余内存"$3"M,所以内存使用率为:"$3/$2*100"%"}‘
echo $memory
memory_1=free -m|grep Mem|awk ‘{print ""$3/$2*100""}‘
if [ $? -ge 80 ];then
echo "your memory utilization is $memory_1"|mail -s "memory error" [email protected]
fi
echo "#############关于磁盘############"
df &>/dev/null
if [ $? -ne 0 ];then
echo "free command is not found,please install it!"
exit 0
fi
disk_0=df -h|sed -n ‘3,5p‘|awk ‘{print $(NF-1)}‘
disk=df -h|sed -n ‘3,5p‘|awk ‘{print $(NF-1)}‘|cut -b 1
echo $disk_0
for i in $disk
do
if [ $i -ge 70 ];then
echo "your disk utilization is %i"|mail -s "disk error" [email protected]
fi
done
监控系统资源加邮件提醒
原文地址:http://blog.51cto.com/13475803/2059357