Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101874430
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-19 14:33:34

作者:Dalek   
初学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

阅读(409) | 评论(0) | 转发(0) |
0

上一篇:Expect 学习笔记

下一篇:sed工具介绍实例

给主人留下些什么吧!~~