1

我需要帮助来更新此脚本,如果 ping 失败,它将向另一台主机发送 wakonlan 调用(除了如果 ping 失败现在发送的电子邮件)。如何从这个脚本中做到这一点?

这就是我想要达到的目标:

Server1 已启动 > PingComputer ping server1 = 一切正常。服务器 1 失败 > PingComputer 发送邮件并向服务器 2 发送唤醒呼叫 = 服务器 2 启动。

不那么重要(仅在可能的情况下):在 server1 失败后,我想在 server1 再次启动时收到一封邮件,然后这个场景重新开始。

    #!/bin/bash
HOSTS="IP ADRESS"
COUNT=4
for myHost in $HOSTS
do
    count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | a$
    if [ $count -eq 0 ]; then
        # 100% failed
        echo "Server failed at $(date)" | mail -s "Server Down" myadress@gmail.com
        echo "Host : $myHost is down (ping failed) at $(date)"
    fi
done

顺便说一句,我在所有 3 台计算机上使用 ubuntu 服务器。

4

1 回答 1

0

规格

至少一台主机必须始终可 ping 通(ping 成功 = UP)。
当可 ping 的主机变得无法访问时(ping 失败)=> 邮件并且:
- 如果另一台主机已经可以 ping 通 => 没问题 => 继续
- 否则 => 唤醒另一

主机在 wakeonlan it => mail 之后仍然无法访问

脚本

最初的脚本是基于bash. 但是您的 Ubuntu Server 似乎使用的bash版本太旧,没有关联数组。幸运的是,zsh暂时支持关联数组。因此,以下脚本已针对zsh. 此外,还添加了对 MAC 地址的支持。

脚本one_host_should_ping.sh

#!/bin/zsh

MYADDRESS=myadress@gmail.com
NEXTHOSTWOL=${1?Please provide hosts as arguments of the script}
PINGABLECOUNT=0

# use associative array to store the state of each host
unset      HOSTS
typeset -A HOSTS
unset      MAC
typeset -A MAC
while (($#))
do
  let PINGABLECOUNT++
  HOSTS[$1]=PINGABLE
  MAC[$1]=$2
  echo >&2 -e "Input #$PINGABLECOUNT:\tIP=$1\tMAC=$2"
  shift 2
done

nexthostwol() {
  for host in ${(k)HOSTS[@]}
  do
    if [[ ${HOSTS[$host]} == UNREACHABLE ]] 
    then
      NEXTHOSTWOL=$host
      break
    fi
  done
}

report() {
  for host in ${(k)HOSTS[@]}
  do
    echo "HOST IP=$host MAC=${MAC[$NEXTHOSTWOL]} state=${HOSTS[$host]}"
  done
  echo "Number of hosts Up = $PINGABLECOUNT"
}

pingable() {
  let PINGABLECOUNT++
  HOSTS[$1]=PINGABLE
  SUBJECT="[Server Up] ping $1 success at $(date +%c)"
  echo "$SUBJECT"
  report | mail -s "$MESSAGE" "$MYADDRESS"
  report
  [[ $NEXTHOSTWOL == $1 ]] && nexthostwol
}

unreachable() {
  let PINGABLECOUNT--
  HOSTS[$1]=UNREACHABLE
  SUBJECT="[Server Down] ping $1 failed at $(date +%c)"
  if [[ $PINGABLECOUNT -le 0 ]] 
  then 
    wakeonlan -i $NEXTHOSTWOL ${MAC[$NEXTHOSTWOL]}
    HOSTS[$NEXTHOSTWOL]=WAKEONLAN
    SUBJECT="$SUBJECT => wakeonlan $NEXTHOSTWOL"
  fi
  echo "$SUBJECT"
  report | mail -s "$MESSAGE" "$MYADDRESS"
  report
}

stillwol() {
  SUBJECT="[Server Down] ping $1 failed at $(date +%c)"
  echo "$SUBJECT"
  report | mail -s "$MESSAGE" "$MYADDRESS"
  report
}

# infinite loop => check every 5 minutes 
# use CTRL+C or kill to break it
while true
do
  for host in ${(k)HOSTS[@]}
  do
      STATE=${HOSTS[$host]}
      case $STATE in
         UNREACHABLE) ping -c 1 $host && pingable $host      ;;
         PINGABLE)    ping -c 1 $host || unreachable $host   ;;
         WAKEONLAN)   ping -c 1 $host && pingable $host || stillwol $host ;;
         *)           echo >&2 "ERROR: $host is in an unexpected state=[$STATE]";;
      esac
  done

  echo "will check again in 5 minutes"
  sleep $((5*60))
done

用法

授予执行权限:

chmod +x one_host_should_ping.sh`

如果您使用两个主机:

./one_host_should_ping.sh  IP1 MAC1 IP2 MAC2

如果您使用五台主机:

./one_host_should_ping.sh  IP1 MAC1  IP2 MAC2  IP3 MAC3  IP4 MAC4  IP5 MAC5

例子

root@myc:/usr/local# ./one_host_should_ping.sh 192.168.0.197 00:1c:26:5c:7e:d5 192.168.0.187 f4:6d:04:e5:5c:4c 
Input #1: IP=192.168.0.197 MAC=00:1c:26:5c:7e:d5 
Input #2: IP=192.168.0.187 MAC=f4:6d:04:e5:5c:4c 
PING 192.168.0.187 (192.168.0.186) 56(84) bytes of data. 
From 192.168.0.168 icmp_seq=1 Destination Host Unreachable 
--- 192.168.0.187 ping statistics 
--- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms 
[Server Down] ping 192.168.0.187 failed at tor 26 jan 2012 12.44.29 
HOST IP=192.168.0.186 MAC=00:1c:26:5c:7e:d5 state=UNREACHABLE 
HOST IP=192.168.0.197 MAC=00:1c:26:5c:7e:d5 state=PINGABLE 
Number of hosts Up = 1 
PING 192.168.0.197 (192.168.0.197) 56(84) bytes of data. 
From 192.168.0.168 icmp_seq=1 Destination Host Unreachable
--- 192.168.0.197 ping statistics 
--- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms 
Sending magic packet to 192.168.0.197:9 with 00:1c:26:5c:7e:d5 
[Server Down] ping 192.168.0.197 failed at tor 26 jan 2012 12.44.36 
=> wakeonlan 192.168.0.197 
HOST IP=192.168.0.187 MAC=00:1c:26:5c:7e:d5 state=UNREACHABLE 
HOST IP=192.168.0.197 MAC=00:1c:26:5c:7e:d5 state=WAKEONLAN 
Number of hosts Up = 0 
will check again in 5 minutes
于 2012-01-24T20:46:50.843 回答