红联Linux门户
Linux帮助

监控主机是否可以ping通的脚本

发布时间:2005-10-22 17:36:08来源:红联作者:linux2006
:) 初学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 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 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
文章评论

共有 0 条评论