Search code examples
bashalertzenity

alarm when my host is down


I have this bash script which can help me know when some of my servers are down.

 #!/bin/bash
HOSTS="192.168.11.154 192.168.11.155"
COUNT=5 

pingtest(){
  for myHost in "$@"
  do
    ping -c "$COUNT" "$myHost" && return 1
  done
  return 0
}

if pingtest $HOSTS
then
  # 100% failed
zenity --warning --text="<span size=\"xx-large\">WARNING</span>\n\n\n<b>*IP*</b> is down" --title="SERVER DOWN" --ok-label="OKAY"
fi

so, how can i know what specific server is down? I want exchange IP to 192.168.11.154 or 192.168.11.155 (depending on what is not available)


Solution

  • #!/bin/bash
    
    HOSTS="192.168.11.154 192.168.11.155"
    COUNT=5
    
    pingtest () {
      ping -c "$COUNT" "$1" || return 1
    }
    
    for H in $HOSTS; do
      if ! pingtest $H; then
        zenity --warning --text="<span size=\"xx-large\">WARNING</span>\n\n\n<b>${H}</b> is down" --title="SERVER DOWN" --ok-label="OKAY"
      fi
    done