一个刚刚编写的监控主机是否可以ping通的脚

王朝other·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

初学shell,一个刚刚编写的监控主机是否可以ping通的脚本,如果某个主机ping不通,发出邮件,并记录,如果再次能ping通,再次发出邮件,通知,并可设置声音报警。

代码:

#!/bin/sh

# set -x

#######################################################

PingConf='/opt/monitor/Iptest'

PingCantrech='/opt/monitor/IpcannotReach'

#File where the log will be stored

LogDir='/opt/monitor/Log'

#Define the interval time

INTERVAL='10'

#Define the alarm process

User1='xiaoli'

User2='xiaozhang'

YouNeed='/opt/monitor/YouNeed'

########################################################

proc_alarm()

{

while read IpAddress

do

#ingore any hash signs

case $IpAddress in

\#*)

;;

*)

ping $IpAddress -c 5 >errtmp

if [ $? = 0 ] ; then

rm errtmp

else

rm errtmp

ErrTime=`date +%Y/%m/%d/%H:%M:%S`

echo "$ErrTime"": ""$IpAddress"" cann't be connected">>$LogDir

echo "$IpAddress">>tmp11

sleep 1

mail -s "$IpAddress"_Can_not_reach xiaoli@abced.com </dev/null

echo "from $User1"": ""from $User2" >$YouNeed

echo "$ErrTime"": ""$IpAddress"" off " >>$YouNeed

audioplay -v 50 -p speaker /opt/monitor/space.au

fi

;;

esac

done < $PingConf

}

##########################################################

proc_unalarm()

{

while read IpAddress

do

#ingore any hash signs

case $IpAddress in

\#*)

;;

*)

ping $IpAddress -c 5 >errtmp

if [ $? = 0 ] ; then

rm errtmp

ErrTime=`date +%Y/%m/%d/%H:%M:%S`

echo "$ErrTime"": ""$IpAddress"" can be connected">>$LogDir

sleep 1

mail -s "$IpAddress"_OK_le xiaoli@abced.com </dev/null

echo "from $User1"": ""from $User2" >$YouNeed

echo "$ErrTime"": ""$IpAddress"" on " >>$YouNeed

echo $IpAddress >>tmp12

# audioplay -v 50 -p speaker /opt/monitor/space1.au

else

rm errtmp

fi

;;

esac

done < $PingCantrech

}

##########################################################

# execute the proc_main function every the specified time INTERVAL

uniq $PingCantrech > tmp17

cat tmp17 >$PingCantrech

rm tmp17

if [ -e $PingCantrech ]

then

proc_unalarm

else

echo "$PingCantrech" is null

fi

while [ "1" -eq "1" ]

do

# execute the proc_main function

##########################

proc_alarm

##########################

if [ -e tmp11 ]

then

echo "$MYFILE" is nomal

if [ -e "$PingCantrech" ]

then

echo "$PingCantrech" is nomal

cat tmp11 >>$PingCantrech

uniq $PingCantrech > tmp18

cat tmp18 >$PingCantrech

rm tmp18

proc_unalarm

cat tmp11 >$PingCantrech

else

cat tmp11 >$PingCantrech

proc_unalarm

fi

rm tmp11

MYFILE='tmp12'

if [ -e "$MYFILE" ]

then

comm -23 $PingCantrech tmp12 >tmp13

rm tmp12

cat tmp13 >$PingCantrech

rm tmp13

else

echo "$MYFILE" is null

cat tmp11 >$PingCantrech

fi

elif

echo "$MYFILE" is null

then

echo "$MYFILE" is null

if [ -e "$PingCantrech" ]

then

proc_unalarm

else

echo "$MYFILE" is null

fi

fi

######################################

# suspend execution for INTERVAL seconds

sleep $INTERVAL

done

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航