码迷,mamicode.com
首页 > 其他好文 > 详细

监控系统资源加邮件提醒

时间:2018-01-10 13:51:16      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:it!   util   时间   提醒   bash   空闲   one   磁盘   监控系统   

#!/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" 838021892@qq.com
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" 838021892@qq.com
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" 838021892@qq.com
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" 838021892@qq.com
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" 838021892@qq.com
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" 838021892@qq.com
fi
done


监控系统资源加邮件提醒

标签:it!   util   时间   提醒   bash   空闲   one   磁盘   监控系统   

原文地址:http://blog.51cto.com/13475803/2059357

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!