Simple bash script to monitor your webserver remotely on different ports version 2

Simple bash script to monitor one or multiple webserver on different ports. I'm sure there is over 100 available programs doing this but I wanted something with small memory usage. Also, I only wanted to be notified once, notifications are receive by SMS on my cell. With the software I was using before, I was getting notified every minute until I could reach a computer and fix the problem or stop monitoring which was quite annoying.

Software installation

You need mail and nmap installed.

Creating the list of ip to be monitored

Create a file with IP to be monitored, one per line. In this example the file is called iptocheck. You can do it with an editor or by doing simply this :

echo "123.123.123.123" >> iptocheck

The script

NOTE : The script will stop running for the value in minutes of the RESENDAFTER variable or until you do :

./whatever_you_called_this_script fix

This is done on purpose not to receive zillion notifications :)

Here is the script :

#!/bin/bash
# Simple script to check important ports on remote webserver
# Copyright (c) 2009 blogama.org
# This script is licensed under GNU GPL version 2.0 or above
# ---------------------------------------------------------------------
 
###############################
######Modify this section######
###############################
 
###Set the working directory###
WORKDIR="/home/someuser"
##########
 
###SCAN CONFIGURATION###
IPFILE="iptocheck" #FILE WITH IP LIST, ONE PER LINE
PORTS="25,53,80,443" #COMMA SEPARATED LIST OF PORTS TO CHECK
TIMEOUT="10000" #MILLISECONDS, MINIMUM VALUE 1501
##########
 
###NOTIFICATIONS###
EMAIL="you@yourdomain.com"
RESENDAFTER="5" #MINUTES BEFORE THE SCRIPT RESEND AN ALERT
NOTIFICATIONFILE="server_problem.txt"
##########
 
###########################################
######Do not make modifications below######
###########################################
 
### Binaries ###
MAIL=$(which mail)
GREP=$(which grep)
NMAP=$(which nmap)
FIND=$(which find)
TEST=$(which test)
 
###Restore when problem fix###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm $NOTIFICATIONFILE
        exit 1;
  fi
fi
 
###Check if its time to resend an alert
if [ -f $WORKDIR/$NOTIFICATIONFILE ]; then
	if $TEST `$FIND $WORKDIR/$NOTIFICATIONFILE -mmin +$RESENDAFTER`; then
		rm $WORKDIR/$NOTIFICATIONFILE
	fi
fi
 
 
###Check if already notified###
if [ -f $WORKDIR/$NOTIFICATIONFILE ]; then
  exit 1;
fi
 
###Test specified ports###
###3 cases : IP is down (will output string 'HOST_DOWN'), timeout (will output string 'Skipping') or port closed (will output string 'closed')###
 
$NMAP -d9 -n --host-timeout $TIMEOUT -p $PORTS -iL $IPFILE | $GREP -E "HOST_DOWN|Skipping|closed" > /dev/null 2>&1
 
if [ "$?" -ne "1" ]; then #PROBLEM
    $NMAP -d9 -n --host-timeout $TIMEOUT -p $PORTS -iL $IPFILE | $GREP -E "HOST_DOWN|Skipping|closed" > $NOTIFICATIONFILE
fi
 
###Send mail notification after failed check###
if [ -f $WORKDIR/$NOTIFICATIONFILE ]; then
  $MAIL -s "Server problem" $EMAIL < $WORKDIR/$NOTIFICATIONFILE
fi

make it executable :

chmod +x whatever_you_called_this_script

Add the script to your crontab :

* * * * * /home/someuser/whatever_you_called_this_script >/dev/null 2>&1