• May 22, 2012, 10:39:27 PM
Welcome, Guest. Please login or register. Registration is free.
Did you miss your activation email?

Author Topic: Bash script to check Apache for a pulse  (Read 1006 times)

0 Members and 1 Guest are viewing this topic.

Offline Michael McNamara

  • Administrator
  • Hero Member
  • *****
  • Posts: 2517
    • Michael McNamara
Bash script to check Apache for a pulse
« on: February 24, 2011, 12:31:07 AM »
We've been having some growing pains of late where the site is becoming overwhelmed with traffic (unfortunately the majority of that traffic isn't legitimate users but SPAM bots trying to harvest email addresses and post SPAM comments).

In order to help determine when problems arise I've written a quick Bash shell script that utilizes the Nagios plugin check_http.

Code: [Select]
#!/bin/bash
#
# Filename: /root/monitor.sh
#
# Purpose:  Perform a service check of the HTTP process by connecting to
#           the port utilizing the Nagios Plug-in check_http
#
# Language: Bash Script
#
# Author:   Michael McNamara
#
# Date:     Feb 21, 2011
#
# Changes:  02232011 cleaned up script/updated documentation
#
# Requirements:
#
#           Nagios check_http plugin
#           http://nagiosplugins.org/man/check_http
#
# Notes:
#        Command Line Reference;
#          ./monitor.sh
#
#

# Declare Variables
SENDMAIL="/usr/bin/mutt"
CHECK_HTTP="/root/check_http"
LOG="/var/log/check_http.log"
MAIL_TO="root"
MAIL_SUBJECT="CHECK HTTP REPORT"
DATETIME=`date`

# Site specific information
HOST1="blog.michaelfmcnamara.com"
URL1="/"
STRING1="Disclaimer"
HOST2="forums.networkinfrastructure.info"
URL2="/"
STRING2="SMF"

#
# Loop1 for blog.michaelfmcnamara.com
#

RESULT1="`${CHECK_HTTP} -H ${HOST1} -s ${STRING1}`"

# Debug output
#echo RESULT $HOST1 = ${RESULT1}

if [[ $RESULT1 =~ "OK" ]]
then
   echo $DATETIME OK $HOST1 >> $LOG
else
   $SEDNMAIL -s $MAIL_SUBJECT $MAIL_TO < 'echo $RESULT1'
   echo $DAYTIME FAIL $HOST1 >> $LOG
fi

#
# Loop2 for forums.networkinfrastructure.info
#

RESULT2="`${CHECK_HTTP} -H ${HOST2} -s ${STRING2}`"

# Debug output
#echo RESULT $HOST2 = ${RESULT2}

if [[ $RESULT2 =~ "OK" ]]
then
   echo $DATETIME OK $HOST2 >> $LOG
else
   $SEDNMAIL -s $MAIL_SUBJECT $MAIL_TO < 'echo $RESULT2'
   echo $DAYTIME FAIL $HOST2 >> $LOG
fi

I've placed this script on a remote server and run it every minute from cron to check the availability of the site.

A little dirty but very effective.

Cheers!
We've been helping network engineers, system administrators and technology professionals since June 2009.
If you've found this site useful or helpful, please help me spread the word. Link to us in your blog or homepage - Thanks!


Offline Michael McNamara

  • Administrator
  • Hero Member
  • *****
  • Posts: 2517
    • Michael McNamara
Re: Bash script to check Apache for a pulse
« Reply #1 on: March 07, 2011, 11:16:35 PM »
I've added code to perform an ICMP ping to check for network connectivity using the Nagios plug-in check_icmp. I also fixed the syntax of the email notification and changed to /bin/mail from mutt. It's more likely that a greater number of systems will have /bin/mail than will have mutt installed.

Code: [Select]
#!/bin/bash
#
# Filename: /root/monitor.sh
#
# Purpose:  Perform a service check of the HTTP process by connecting to
#           the port utilizing the Nagios plug-in check_http
#
# Language: Bash Script
#
# Author:   Michael McNamara
#
# Verzion:  0.2
#
# Date:     Feb 21, 2011
#
# Changes:
#           Mar 07, 2011 added check_icmp routine to script
#           Mar 06, 2011 fixed email notification
#           Feb 23, 2011 cleaned up script/updated documentation
#
# Requirements:
#
#           Nagios check_icmp plugin
#           Nagios check_http plugin
#           http://nagiosplugins.org/man/check_http
#
# Notes:
#        Command Line Reference;
#          ./monitor.sh
#
#

# Declare Variables
SENDMAIL="/bin/mail"

CHECK_HTTP="/root/check_http"
CHECK_ICMP="/root/check_icmp"

LOG="/var/log/check_http.log"

MAIL_TO="root"
MAIL_SUBJECT="Server Host check script report"

DATETIME=`date`

# Site specific information
HOST1="blog.michaelfmcnamara.com"
URL1="/"
STRING1="Disclaimer"
HOST2="forums.networkinfrastructure.info"
URL2="/"
STRING2="SMF"

########################################################################
########################################################################

#
# Loop1 for blog.michaelfmcnamara.com
#

PING1="`${CHECK_ICMP} ${HOST1}`"
RESULT1="`${CHECK_HTTP} -H ${HOST1} -s ${STRING1}`"

# Debug output
#echo RESULT $HOST1 = ${RESULT1}

if [[ $RESULT1 =~ "OK" ]]
then
   echo $DATETIME OK $HOST1 $RESULT1 >> $LOG
else
   echo $RESULT1 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
   echo $DATETIME FAIL $HOST1 $RESULT1 >> $LOG
fi


if [[ $PING1 =~ "OK" ]]
then
   echo $DATETIME OK $HOST1 $PING1 >> $LOG
else
   echo $PING1 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
   echo $DATETIME FAIL $HOST1 $PING1 >> $LOG
fi

#
# Loop2 for forums.networkinfrastructure.info
#

PING2="`${CHECK_ICMP} ${HOST2}`"
RESULT2="`${CHECK_HTTP} -H ${HOST2} -s ${STRING2}`"

# Debug output
#echo RESULT $HOST2 = ${RESULT2}

if [[ $RESULT2 =~ "OK" ]]
then
   echo $DATETIME OK $HOST2 $RESULT2>> $LOG
else
   echo $RESULT2 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
   echo $DATETIME FAIL $HOST2 $RESULT2 >> $LOG
fi

if [[ $PING2 =~ "OK" ]]
then
   echo $DATETIME OK $HOST2 $PING2 >> $LOG
else
   echo $PING2 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
   echo $DATETIME FAIL $HOST2 $PING2 >> $LOG
fi

exit 0
« Last Edit: March 07, 2011, 11:31:52 PM by Michael McNamara »
We've been helping network engineers, system administrators and technology professionals since June 2009.
If you've found this site useful or helpful, please help me spread the word. Link to us in your blog or homepage - Thanks!

Offline Michael McNamara

  • Administrator
  • Hero Member
  • *****
  • Posts: 2517
    • Michael McNamara
Re: Bash script to check Apache for a pulse
« Reply #2 on: March 17, 2011, 10:30:23 PM »
Another update... the check_icmp routine was consuming a lot of bandwidth to my host and needlessly cluttering my logs so I move the routine so it only gets executed if the check_http routine detects a problem.

Code: [Select]
#!/bin/bash
#
# Filename: /root/monitor.sh
#
# Purpose:  Perform a service check of the HTTP process by connecting to
#           the port utilizing the Nagios plug-in check_http
#
# Language: Bash Script
#
# Author:   Michael McNamara
#
# Verzion:  0.4
#
# Date:     Feb 21, 2011
#
# Changes:
#           Mar 17, 2011 moved check_icmp routine so it executes only if
#                        the check_http routine detects a failure, this
#                        should cut down on the large ICMP overhead
#           Mar 07, 2011 added check_icmp routine to script
#           Mar 06, 2011 fixed email notification
#           Feb 23, 2011 cleaned up script/updated documentation
#
# Requirements:
#
#           Nagios check_icmp plugin
#           Nagios check_http plugin
#           http://nagiosplugins.org/man/check_http
#
# Notes:
#        Command Line Reference;
#          ./monitor.sh
#
#

# Declare Variables
SENDMAIL="/bin/mail"

CHECK_HTTP="/root/check_http"
CHECK_ICMP="/root/check_icmp"

LOG="/var/log/check_http.log"

MAIL_TO="root"
MAIL_SUBJECT="Server Host check script report"

DATETIME=`date`

# Site specific information
HOST1="blog.michaelfmcnamara.com"
URL1="/"
STRING1="Disclaimer"
HOST2="forums.networkinfrastructure.info"
URL2="/"
STRING2="SMF"

########################################################################
########################################################################

#
# Loop1 for blog.michaelfmcnamara.com
#

PING1="`${CHECK_ICMP} ${HOST1}`"
RESULT1="`${CHECK_HTTP} -H ${HOST1} -s ${STRING1}`"

# Debug output

if [[ $RESULT1 =~ "OK" ]]
then
   echo $DATETIME OK $HOST1 $RESULT1 >> $LOG
else
   echo $RESULT1 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
   echo $DATETIME FAIL $HOST1 $RESULT1 >> $LOG
   PING1="`${CHECK_ICMP} ${HOST1}`"
   if [[ $PING1 =~ "OK" ]]
   then
      echo $DATETIME OK $HOST1 $PING1 >> $LOG
   else
      echo $PING1 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
      echo $DATETIME FAIL $HOST1 $PING1 >> $LOG
   fi
fi

#
# Loop2 for forums.networkinfrastructure.info
#

RESULT2="`${CHECK_HTTP} -H ${HOST2} -s ${STRING2}`"

# Debug output
#echo RESULT $HOST2 = ${RESULT2}

if [[ $RESULT2 =~ "OK" ]]
then
   echo $DATETIME OK $HOST2 $RESULT2>> $LOG
else
   echo $RESULT2 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
   echo $DATETIME FAIL $HOST2 $RESULT2 >> $LOG
   PING2="`${CHECK_ICMP} ${HOST2}`"
   if [[ $PING2 =~ "OK" ]]
   then
      echo $DATETIME OK $HOST2 $PING2 >> $LOG
   else
      echo $PING2 | $SENDMAIL -s "$MAIL_SUBJECT" $MAIL_TO
      echo $DATETIME FAIL $HOST2 $PING2 >> $LOG
   fi
fi

exit 0
We've been helping network engineers, system administrators and technology professionals since June 2009.
If you've found this site useful or helpful, please help me spread the word. Link to us in your blog or homepage - Thanks!