#! /bin/bash
#jiance.sh
#硬盘使用率
ds=`df|awk '{if(NR==4){print int($5)}}'`
#内存使用率
used=`free -m|awk '{if(NR==2){print int($3)}}'`
tot=`free -m|awk '{if(NR==2){print int($2)}}'`
per=$(($used*100/$tot))
#apache2是否正常运行
apa=`netstat -tanpl | grep 80 | awk '{print $4}' | sed -n '1p'`
#mysql是否正常运行
mysql=`netstat -tanpl | grep 3306 | awk '{print $4}' | sed -n '1p'`
#统计
res1="硬盘使用率:"$ds"%\n内存使用率:"$per"%\n"
#echo -e $res1
if [ $apa != "0.0.0.0:80" ];then
str1="apache2已经停止运行"
else
str1="apache2正常运行"
fi
if [ $mysql != "127.0.0.1:3306" ];then
str2="myqsl已经停止运行"
else
str2="mysql正常运行"
fi
#统计输出
text=$res1$str1"\n"$str2;
#监控发邮件
if [ $ds -gt 45 ];then
echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi
if [ $per -gt 55 ];then
echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi
监测服务器的shell脚本
发表评论?
在crontab中配置该脚本自动执行的周期:
#每小时执行
00 */1 * * * /home/jiance.sh