Simple Bash Script To Monitor Your Webserver Remotely On Different Ports

Channel: Linux
Abstract: send notification below### After 2 failed check it will send a mail notification ###
Simple Bash Script To Monitor Your Webserver Remotely On Different Ports

Simple bash script to monitor a webserver on different ports (here smtp, dns, http & https but it can be customized); I'm sure there are over 100 available programs doing this but I wanted something with small memory usage. Also, I only wanted to be notified once, notifications are received 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.

Source : http://blogama.org

 

Software installation

You need mail, dig and telnet installed.

 

The script

NOTE : The script will not continue until you do :

./whatever_you_called_this_script fix

This is done on purpose to receive only ONE notification...

#!/bin/bash
# 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
# ---------------------------------------------------------------------
 
### This script does a verification on port 25, 53, 80 and 443 ###
### After 2 failed check it will send a mail notification ###
 
######To be modified######
WORKDIR="/root"
###HTTP###
HTTPSERVERIP="192.168.1.106"
HTTPSERVERPORT="80"
##########
###HTTPS###
HTTPSSERVERIP="192.168.1.106"
HTTPSSERVERPORT="443"
##########
###MAIL###
SMTPSERVERIP="192.168.1.106"
SMTPSERVERPORT="25"
##########
###DNS###
DNSSERVERIP="192.168.1.106"
DOMAINTOCHECKDNS="example.com"
ANSWERIP="192.168.1.106"
#########
###NOTIFICATIONS###
EMAIL="[email protected]"
##########
######End to be modified######
 
 
######Do not make modifications below######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Change dir###
cd $WORKDIR
###Restore when problem fix###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
	exit 1;
  fi
fi
###Check if already notified###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Test SMTP###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_smtp.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_smtp.txt ]; then #Second time, send notification below
    echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_smtp.txt
  else #First notification
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Test HTTP###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_http.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_http.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_http.txt ]; then #Second time, send notification below
    echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_http.txt
  else #First notification
    > server_problem_first_time_http.txt
  fi
fi
 
###Test HTTPS###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_https.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_https.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_https.txt ]; then #Second time, send notification below
    echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_https.txt
  else #First notification
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Test DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_dns.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_dns.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_dns.txt ]; then #Second time, send notification below
    echo "DNS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_dns.txt
  else #First notification
    > server_problem_first_time_dns.txt
  fi
fi
 
###Send mail notification after 2 failed check###
if [ -f server_problem.txt ]; then
  $MAIL -s "Server problem" $EMAIL < /root/server_problem.txt
fi

Make it executable:

chmod +x whatever_you_called_this_script

Add it to your crontab:

crontab -e
* * * * * /path/to/whatever_you_called_this_script >/dev/null 2>&1

Ref From: howtoforge

Related articles