pingHost.sh 2.25 KB
#!/bin/sh
AGENT_HOME=$1
HOST_FILE=$2
IS_LOGIN=$3
PINGCMD=""

# init ping command for diff OS
init(){
  case `uname -s` in
  Linux)
    PINGCMD="ping -c 3 #IP_ADDR#"
  ;;

  SUN)
    PINGCMD="ping -s #IP_ADDR# 32 3"
  ;;

  HP)
    PINGCMD="ping #IP_ADDR# 32 3"
  ;;

  *)
    echo "Cannot get this system type!"
  exit
  esac
}

# login host, check host name is changed
login(){
  tmprunstat=0
  if [[ "$5" = *ssh* ]]; then
    echo "Start ssh host:$2"

    tmpDeviceId=`perl sshHost.pl $1 $2 $3 $4 | awk '{print $2}'`

    if [ "$tmpDeviceId" = "$1" ]; then
      echo "Compare right:$tmpDeviceId==$1!"
      tmprunstat=1
    else
      echo "Compare error:$tmpDeviceId==$1!"
      tmprunstat=2
    fi    
    
  elif [[ "$5" = *telnet* ]]; then
    echo "Start telnet host:$2"

    tmpDeviceId=`perl telnetHost.pl $1 $2 $3 $4 | awk '{print $2}'`

    if [ "$tmpDeviceId" = "$1" ]; then
      echo "Compare right:$tmpDeviceId==$1!"
      tmprunstat=1
    else
      echo "Compare error:$tmpDeviceId==$1!"
      tmprunstat=2
    fi
  else
    tmprunstat=2
  fi
  
  return $tmprunstat
}

writeSwap(){
  echo "New KPI: $1 $2 $3 $4"
  sh $AGENT_HOME/bin/WriteSwap.sh "$1" "$2" "$3" "$4"
}

pingHost(){
  pingcmd=$1
  i=0
  tmprunstat=0
  while (( $i < 3 ))
  do
    echo "start the $i circle"
    iRate=`eval $pingcmd | grep "packet loss" | awk -F ',' '{print $(NF-1)}'| awk '{print $1}'`
    echo "iRate==$iRate"
    if [ $iRate = "100%" ]; then
      tmprunstat=0
    else
      if [ $IS_LOGIN = "Y" ]; then
        login $2 $3 $4 $5 $6
        tmprunstat=$?
        break
      else
        tmprunstat=1
        break
      fi
    fi
    let "i++"
  done 

  return $tmprunstat

}

# init PINGCMD
init

cat $HOST_FILE | while read deviceId ipAddr userName password loginType unitId
do
  status=""
  tmpCmd=${PINGCMD/"#IP_ADDR#"/$ipAddr}
  echo "$tmpCmd"

  pingHost "$tmpCmd" $deviceId $ipAddr $userName $password $loginType < /dev/null
  tmpstatus=$?
  if [ $tmpstatus -eq 0 ]; then
    status="DOWN"
  elif [ $tmpstatus -eq 1 ]; then
    status="UP"
  elif [ $tmpstatus -eq 2 ]; then
    status="UnKnown"
  fi
  
  writeSwap "$unitId" "FM-00-01-001-01" "$status" 29950
  
  echo "======================================================================================================"
done

rm $HOST_FILE