#!/bin/bash

# 在线快速检测,有问题发邮件报警,早上8:30检测一次,超过3次报警延时10分钟.

# 邮件要分发

# 20130113完成基本功能   间隔30秒 去ping一次, 连接三次有问题就停15分钟,再去ping设备

# nohup /bin/bash /monitor/allping/allping.sh & 

 

export LANG=C

 

  

while :  

do 

 

mail=/usr/local/mutt/bin/mutt   

 

dir=/monitor/allping/

nlog=/monitor/allping/logs/

time=[`date "+%H:%M:%S"`]

logtime=`date "+%Y-%m-%d-%H:%M:%S"`

log=/monitor/allping/checklog/`date +%Y-%m-%d`.log

[ ! -d $log ] && touch $log

[ ! -d $nlog ] && mkdir -p $nlog

 

 

 

Email=""

logmail=""


 

num3=`cat $dir/num.txt | wc -l`

echo "num3 锁的值为 $num3"

[ $num3 -gt 3 ]  && echo "服务器连接三次ping不通,需检查该服务器状态。(15分钟后将再次检查)" |  $mail -s "$time Server 3 times failure"  $Email && sleep 900 && > $dir/num.txt

 

 

cd $dir

num=`cat $dir/iplist.txt | grep -v ^# | wc -l`

 

 

touch $dir/logmail

> $dir/logmail

for i in `seq $num`

do 

 

ip=`cat ./iplist.txt | grep -v ^# | head -$i | tail -1 |  awk '{print $1}'`

use=`cat ./iplist.txt | grep -v ^# | head -$i | tail -1 |  awk '{print $2}'`

os=`cat ./iplist.txt | grep -v ^# | head -$i | tail -1 |  awk '{print $3}'`

manager=`cat ./iplist.txt | grep -v ^# | head -$i | tail -1 |  awk '{print $4}'`

mailother=`cat ./iplist.txt | grep -v ^# | head -$i | tail -1 |  awk '{print $5}'`

 

ping -c 5 $ip  -i 0.05 &> /dev/null

 

if [ $? -eq 0 ];then

echo "$ip OK"

else

        echo "$time $ip ping不通 (功能:$use 系统:$os 负责人:$manager)" | $mail -s "$time $ip ping fail"  $Email

echo "$time $ip ping不通 (功能:$use 系统:$os 负责人:$manager)"  >>  $log

        echo "$time $ip ping不通 (功能:$use 系统:$os 负责人:$manager)"  >>  $dir/logmail

[ -z $mailother ] && continue   # 按机器分别发短信,后期再更新!

[ $mailother -eq 0 ] && echo "mailto 0" 

[ $mailother -eq 1 ] && echo "mailto 1" 

[ $mailother -eq 2 ] && echo "mailto 2" 

[ $mailother -eq 3 ] && echo "mailto 3" 

fi

done 

 

 

# 发送错误报告到邮箱

[ -s $dir/logmail ] && cat $dir/logmail | $mail -s "$time $ip ping fail"  $logmail && echo "报完发送完成---->|"

 

[ -s $dir/logmail ] && echo "1" >> $dir/num.txt

 

# num.txt 这个文件如果10分钟内没有被修改,则清零。

 [ ! `find $dir/ -cmin -10  | grep num.txt` ] && > $dir/num.txt

 

sleep 30

 

done