监测服务器的shell脚本

#! /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

发表评论?

1 条评论。

  1. 在crontab中配置该脚本自动执行的周期:
    #每小时执行
    00 */1 * * * /home/jiance.sh

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.