Script to ping test for host and alert/notification with display while running from cronjob also

#!/bin/bash
export DISPLAY=:0.0
export XAUTHORITY=/home/user/.Xauthority
HOSTS="xx.xx.xx.xx xx.xx.xx.xx"
COUNT=4
for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed 
    notify-send "Host : $myHost is down (ping failed) at $(date)"
    zenity --warning --text="Host : $myHost is down (ping failed) at $(date)" --title="Ping Failed!"
  fi
done

NOTE : make sure you change the XAUTHORITY path with proper user name

exports are more important when you call the script from cron, other wise you wont alert/notification box in GUI

Advertisements