您的位置:首页 > 文档 > Shell >
 文章分类 

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


创建:2005-10-26 00:49:44
作者:Unlinux
来自: http://www.Unlinux.com

初学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

转载自:http://www.unlinux.com/doc/shell/20051026/172.html

评论】 【加入收藏夹】 【 】 【打印】 【关闭
 *  请尊重我们的劳动,转载请注明出自UnLinux.Com及作者名 * 

※ 相关链接
 ·一个杀死终端所有进程的 Shell  (2005-10-26 00:49:44)
 ·一个判断文件日期的问题  (2005-10-26 00:49:44)
 ·一个排序脚本  (2005-10-26 00:49:44)
 ·一个把IP地址转化为长整数的脚本  (2005-10-26 00:49:44)
 ·怎么把一个文本的一列,换成一行  (2005-10-26 00:49:43)
 ·找出一个文件中出现某str的次数  (2005-10-26 00:49:43)
 ·在每个文件夹下建一个.qmail文件  (2005-10-26 00:49:43)
 ·在shell程序中判断一个变量是不是由4个数字组成  (2005-10-26 00:49:43)
 ·如何计算一个日期是星期几  (2005-10-26 00:49:42)
 ·删除一个月以前的文件  (2005-10-26 00:49:42)

Copyright © 2005 UnLinux.Com All Rights Reserved