1

I need help to update this script to if ping fails it would send a wakonlan call to another host (besides the email which is sent now if ping fails). How can this be done from this script?

This is what i like to achive:

Server1 is up > PingComputer pings server1 = everything is fine. Server 1 fails > PingComputer sends mail AND sends wakeonlan call to server 2 = server2 starts up.

Not so important (only if its possible): After a fail on server1 i would like to get a mail when server1 is up again and then this scenario starts over.

    #!/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

Im using ubuntu server on all 3 computers btw.

Viktor Kuznyetso
  • 57
  • 1
  • 1
  • 4
  • Hello. I see you have created a new question as I have advised [here](http://stackoverflow.com/a/8918760/938111) ;-) Thank you. But I am sorry, I do not yet understand what you want... Can you give a scenario please? – oHo Jan 23 '12 at 21:28
  • Hi. Yes i did:-). And i will vote for yor answer directly as i get permission to do so (seems i need some more posts first). A scenario is that i have server1 up and running and at the same also have a "pingcomputer" thats pings server1 (every 5min) as the example script shows (you know). If server1 fails "pingserver" sends a email AND sends a wakeonlan command to server2, and server2 starts up. (...) – Viktor Kuznyetso Jan 23 '12 at 21:56
  • (...) I have tried wakeonlan in terminal in ubuntu and it works fine, but i like it to start up server2 automaticly if server1 fails. With this script it sends a email if server1 (and som more servers fail) but i like to add wakeonlan in some way. Do you understand now? I understand that i have to be in the same lan to make wakeonlan work with mac-adress.. – Viktor Kuznyetso Jan 23 '12 at 21:56

1 Answers1

0

Specification

At least one host must be always pingable (ping success = UP).
When a pingable host becomes unreachable (ping failed) => mail and:
- if another host is already pingable => no problem => continue
- else => wakeonlan another host
When an unreachable host becomes pingable back => mail
When a host is still unreachable after wakeonlan it => mail

Script

The initial script was based on bash. But your Ubuntu Server seems to use a bash version too old without associative array. Fortunately, zsh supports associative array for a while. Therefore the following script has been rewriten for zsh. Moreover support for MAC ADDRESS has been also added.

Script 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

Usage

Give execution permission:

chmod +x one_host_should_ping.sh`

If you use two hosts:

./one_host_should_ping.sh  IP1 MAC1 IP2 MAC2

If you use five hosts:

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

Example

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
oHo
  • 51,447
  • 27
  • 165
  • 200
  • Im trying to understand where i put in the ip-adresses for server1 and the server that should "wake up"=server2. I get a errormessage when i trying to start the script with the two ip-adresses as "./one_host_should_ping.sh HOST1 HOST2". Another question is where i should put the mac-adress for server2, or is it not needed for wol? I think i need som more info where to put my data. I bet this is easy for you ;P – Viktor Kuznyetso Jan 25 '12 at 00:49
  • The error i get is: ./one_host_should_ping.sh: line 9: declare: -A: invalid option declare: usage: declare [-afFirtx] [-p] [name[=value] ...] HOST #2: 192.168.0.192 ./one_host_should_ping.sh: line 13: 192.168.0.192: syntax error: invalid arithme tic operator (error token is ".168.0.192") will check again in 5 minutes – Viktor Kuznyetso Jan 25 '12 at 01:03
  • It seems like it only uses mac-adress from IP1. I tried to use the mac-adress for IP2 that gonna start up with wakeonlan but i seems like it dont start up... – Viktor Kuznyetso Jan 25 '12 at 23:48
  • @oHessing. It looks like the wakeonlan should work if i put it in that order you said. Is i something im doing wrong hmmm – Viktor Kuznyetso Jan 26 '12 at 22:28
  • If IP1 is down IP2 should start with wakeonlan. But if i put IP2 first i feels like the order is wrong, the whole point is if IP1 is down IP2 should start up with wakeonlan...or maybe the order isnt of any importance... – Viktor Kuznyetso Jan 27 '12 at 01:17
  • Dear @ViktorKuznyetso. As you said, the order has no importance. This is because of associative array => it uses it own order => the script will always wakeonlan IP1 :-/ Please try again using `192.168.0.186`as IP1 instead of `192.168.0.197`... – oHo Jan 27 '12 at 08:48
  • @oHessing. Thanx for your help. But i cant get it tom work with wakeonland, it says that it sends magic package to the correct ip and mac but i just wont start up. It says Sending magic packet to 192.168.0.187:9 with f4:6d:04:e5:5c:4c, i dont understand what :9 after ip means.... – Viktor Kuznyetso Jan 28 '12 at 18:15