方法1:
监控磁盘或CPU超过一定值邮件报警脚本:
[[email protected] scripts]# cat check.sh
#!/bin/bash
LANG=en_US.UTF-8
cpuUsed=`top -n 1|awk -F ‘[, %]+‘ ‘NR==3 {print100-$11}‘`
diskUsed=$(df -h|awk -F ‘[ %]+‘ ‘/\/$/{print $5}‘)
logFile=/tmp/jk.log
function Sendmail(){
mail -s"监控报警" 1665***[email protected] <$logFile
}
function check(){
if [ `echo"$cpuUsed>80"|bc` -eq 1 -o $diskUsed -ge 80 ];then
echo"CPU使用率:${cpuUsed}%,磁盘使用率:${diskUsed}%">$logFile
Sendmail
fi
}
function main(){
check
}
main
方法2:
监控磁盘空间大小并用邮件通知脚本:
#!/bin/bash
d=`df -h|grep "/dev/sda6" |awk ‘{print int($5)}‘`
logFile=/tmp/jk.log
function Sendmail(){
mail -s "监控报警" 1665***[email protected] <$logFile
}
function check(){
if
[ $d -gt 80 ];then
echo "当前硬盘为${d}%" >$logFile
Sendmail
else
echo "硬盘够用"
fi
}
function main(){
check
}
main
时间: 2024-12-12 05:57:47