Skip to content

Instantly share code, notes, and snippets.

@artizirk
Created December 8, 2013 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artizirk/7864605 to your computer and use it in GitHub Desktop.
Save artizirk/7864605 to your computer and use it in GitHub Desktop.
monitors if host is up or down
#!/bin/bash
echo "[`date`][Start] Host alive tester"
host_ip_addr="192.168.10.100"
alive_history=()
alive_history_size=5
while true; do
#ping -nqc1 $host_ip_addr &> /dev/null #ping not good because cant set timeout time
fping -t500 -c1 $host_ip_addr &> /dev/null
alive_or_not=$?
alive_history=($alive_or_not ${alive_history[@]:0:$alive_history_size})
numb_of_errors=0
for alive_status in "${alive_history[@]}"; do
if [ $alive_status -eq 1 ]; then
#expr $numb_of_errors = $numb_of_errors + 1
numb_of_errors=$((numb_of_errors + 1))
fi
done
if [ $numb_of_errors -ge $alive_history_size ]; then
echo "[`date`][dead] Host $host_ip_addr is down"
beep
espeak "Host $host_ip_addr is down. Do something, fast!"
#else
# echo "[`date`][alive]"
fi
#echo "error count:" $numb_of_errors " array content: " ${alive_history[@]}
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment