Looking for a program that will ping a collection of IPs for 5-10
minutes. If no success, the program does a "shutdown -r now"
I don't have the time to program it myself.
Doing this is C is way overkill. Its like cracking a walnut open with a
steamroller. This is much better accomplished with a shell script or at
most a scripting language such as perl or python.
I use the following script to monitor my servers from my personal desktop
and provide and audio announcement if any of the required ports on my
server are not open. It uses diff to check the trimmed output of nmap
against a known template. My version also logs the success of the test and
if it fails it writes a traceroute to the logs that I can use for
troubleshooting.
You should be able to modify this script to shutdown your system. You will
need to find an open port that you can monitor (port 80 of google should
work fine). Then you will need to provide the template of how the trimmed
output of nmap is supposed to look. Then you can replace the commands
which play sound with a reboot command. If you like, you can also remove
the commands that handle logging.
#! /bin/bash
# checkports.sh - script to check the ports of a given host
# logs whether successful and plays SOUND_FILE if it finds
# unavailable ports
export MON_HOST=example.com
export LOGFILE=$HOME/log/checkports.log
export MON_PORTS="22,25,80"
export MON_NORM_TMPL=$HOME/tmpl/normal_mon_tmpl.txt
export SOUND_FILE=$HOME/scripts/data/down.wav
export LOGFILE_MAX_SIZE="104857600"
if (/usr/bin/nmap -P 0 -p $MON_PORTS $MON_HOST | /bin/head -7 |
/usr/bin/tail -3\
| /usr/bin/diff /dev/stdin $MON_NORM_TMPL)
then
/usr/bin/printf "%s - system normal\n" "`date +\"%m-%d-%y:
%H:%M\"`"\else
/usr/bin/printf "%s - host down:\n" "`date +\"%m-%d-%y: %H:%M\"`" \ /usr/bin/play $SOUND_FILE
/usr/bin/traceroute $MON_HOST | sed -e "s/^/\t/" >> $LOGFILE
fi
# manage the log file size
export LOGFILE_SIZE=$(ls -l $LOGFILE | awk '{print $5}')
if [ $LOGFILE_SIZE -gt $LOGFILE_MAX_SIZE ] ; then
tail -10000 $LOGFILE > $LOGFILE.tmp
mv $LOGFILE.tmp $LOGFILE
fi