![]() |
Understudy.net/orgOk you have made it this far.You obviously must be bored and have nothing better to do with your life. |
![]() |
[Previous entry: "Howto: Change backgrounds in WindowMaker with a script"] [Main Index] [Next entry: "Hurricane Frances"]
08/31/2004 Archived Entry: "A server monitor script"
I am at it again. I am thinking of getting a dedicated server. One of the things I wanted to be able to do was to monitor it and have the computer that monitors the server notify me via e-mail that it was down.
This script requires the fping. port. You can install the port by being root and doing:
cd /usr/ports/net/fping && make install distclean && rehash
I place all my scripts that I run in /usr/local/sbin/scripts/ . I had to mkdir /usr/local/sbin/scripts/ and then I create / edit a file, I called mine "serverm" . Place the following lines in it.
#!/bin/sh # place your shell script choice here
# this script is so that you can monitor the server
# and get an email when it doesn't respond.
# written by Brendhan Horne
# set the varible
# and run the command between the ` `
# replace name.foo.com with the name of your server
myfping=`fping name.foo.com |grep alive | wc -l`
# deal with the results
if [ $myfping -eq "1" ] ;
then
# if it responds to ping exit script go home happy
echo "Success"
exit
# if not happy do this
else
# send a email message
# the next command is all one line
# replace email@email.com with the desired email address
echo "OMG the server went down" | mail -s "Remote server connection problems" email@email.com
fi
# end of file
Exit and save the script. Then do a chmod +x. this will make it executable. You can try it from a command line. What you can then do is set it as a cron job to run every minute or however often you want it to run, by saving it to a cron.txt file and then running crontab cron.txt. You can check you cron settings with crontab -l (lowercase L).
Special thanks to fixx (comp.unix.bsd.freebsd.misc) that helped me discover that a script could do this. To Ian and Colin at #34sp (irc.34sp.com) and ziro at #linuxhelp (irc.oftc.net) for helping me get the script together.