#!/bin/sh ## ---------------------------------------------------------------------- ## check_server_daemons.sh -- a script to see if various demon are UP. ## ## Here is a script that will check to see if various deamons are running. ## If they are not, a message is printed out and -- if configured -- the ## demon is restarted. mail has a flag to not mail anything unless the ## body of the message has text. So, read your mail man page, and pipe ## the output of this script into the mail program. The flag is -e in ## Linux and -E in FreeBSD. ## ## set this up in a cronjob like this: ## @hourly /path/to/this/script/check_server_daemons.sh | mail -e -s '[ERROR] checking server' gregory@example.org ## ---------------------------------------------------------------------- # Thu Mar 15 15:58:38 PDT 2007 (c) Rudy Rucker ## ---------------------------------------------------------------------- ## Configure ports to check at the end of the script... ## ---------------------------------------------------------------------- uname='FreeBSD'; [ -x /bin/uname ] && uname=`uname`; [ -x /usr/bin/uname ] && uname=`uname`; # set this to 0 chen cron'd DEBUG=1; check_netstat () { IP=$1; PORT=$2 NAME=$3; START_CMD=$4 if [ uname = 'Linux' ]; then [ PORT = '*' ] && PORT='0.0.0.0' NUM=`/bin/netstat -ln | /bin/grep -c "$IP:$PORT .*LISTEN"` else NUM=`/usr/bin/netstat -an | /usr/bin/grep -c "$IP.$PORT .*LISTEN"` fi if [ $NUM -lt 1 ]; then echo "ERROR! $0 detected $NAME was not running." echo "This ip: $IP and this port: $PORT were checked." date if [ -n "$START_CMD" ] ; then echo "Attempting to restart with: $START_CMD" $START_CMD & if [ -z "$DEBUG" ] && [ $DEBUG = 0 ]; then sleep 1; echo "Here is the current ps output" echo " ============ ============ ============ ============" /bin/ps -ax echo " ============ ============ ============ ============" fi fi echo elif [ -n "$DEBUG" ] && [ $DEBUG = 1 ]; then echo "$NAME OK"; fi } ## # Linux examples ## check_netstat "*" 21 pure-ftp "/etc/init.d/pure-ftpd-mysql start" ## check_netstat "*" 25 postfix "/etc/init.d/postfix start" ## check_netstat 69.22.154.64 80 "web server" "/etc/init.d/apache2 start" ## check_netstat 69.22.154.65 80 "other web server" "/etc/init.d/apache2 start" ## check_netstat "*" 110 pop3 "/etc/init.d/courier-pop start" ## check_netstat 127.0.0.1 3306 mysqld "/etc/init.d/mysql start" ## ## # FreeBSD examples ## check_netstat "*" 21 pure-ftp "/usr/local/etc/rc.d/pure-ftpd start" ## check_netstat 69.22.154.64 80 "web server" "/etc/init.d/apache2 start" check_netstat 69.22.154.65 80 "other web server" "/etc/init.d/apache2 start" ## check_netstat "*" 110 pop3 "/etc/init.d/courier-pop start" ## check_netstat * 3306 mysqld "/usr/local/etc/rc.d/mysql-server.sh start" ## check_netstat 127.0.0.1 53 "Local DNS cacher " "/etc/rc.d/named start" # this is a neat test... runs nc listening on localhost. check_netstat 127.0.0.1 9999 "Test server on port 9999" "nc -l -p 9999 -s 127.0.0.1" ## ---------------------------------------------------------------------- ## People should put this crap at the end, not the top of their software. ## ---------------------------------------------------------------------- ## Copyright (c) 2007 Rudy Rucker ## ## Permission is hereby granted, free of charge, to any person ## obtaining a copy of this software and associated documentation ## files (the "Software"), to deal in the Software without ## restriction, including without limitation the rights to use, ## copy, modify, merge, publish, distribute, sublicense, and/or sell ## copies of the Software, and to permit persons to whom the ## Software is furnished to do so, subject to the following ## conditions: ## ## The above copyright notice and this permission notice shall be ## included in all copies or substantial portions of the Software. ## ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES ## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING ## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR ## OTHER DEALINGS IN THE SOFTWARE. ## ----------------------------------------------------------------------