Add to Favorites
Hosting Discussion
 

forgot password?


Reply


Old
  Post #1 (permalink)   05-02-2007, 11:27 AM
HD Newbie
 
Join Date: Apr 2007
Posts: 34

Status: RadixHosting is offline
Here's an interesting script I wrote. It basically keep an eye on Apache processes. I've had some problems where Apache suddenly started to consume a lot of memory causing the server load to increase which eventually results in a server crash. Anyway it took me some time to figure out what was causing this, so in meanwhile I wrote a script which just restarts Apache before it can do any damage. Prevents downtimes, etc. Hope you like it.

It's written for Apache 1.3 running on cPanel servers, but you can easilly modify it for your own server.

Code:
#!/bin/bash
# +----------------------------------------------------------------------------
# | chkApache.sh
# | Written for www.radixhosting.com
# | 
# | - Performs a basic scan to check the Apache load
# | - Forcefully restarts Apache if necessary
# | - Sends a report to the system administrator if actions taken
# +----------------------------------------------------------------------------

###############################################################################
# *** START OF CONFIGURATION SETTINGS ***
###############################################################################

# The delay between integrity checks
DELAY=3s

# Minimum server load (5 min. average) for the script to run (integer value)
MIN_LOAD=4

# Maximum % CPU all httpd processes are allowed to use
MAX_CPU=90

# Maximum % memory all httpd processes are allowed to use
MAX_MEM=110

# Kill all httpd processes and restart Apache when a limit has been reached
RESTART_HTTPD=1

# Send a report to the system administrator when a limit has been reached
SEND_REPORT=1

# System administrator to send reports to
ADMIN_MAIL=root

###############################################################################
# *** END OF CONFIGURATION SETTINGS ***
###############################################################################

while [ 1 ]
do

if [ `cat /proc/loadavg | gawk -F "." '{ print $1 }'` -ge $MIN_LOAD ]
then
  KILL=0
  echo "The minimum server load has been reached, performing Apache integrity scan..."
  read REACHED_CPU CUR_CPU < <(ps aux | grep httpd | grep -v grep | gawk -vlimit=$MAX_CPU '{ total+=$3 } END { if (total>limit) { print 1, total } else { print 0, total } }')
  if [ $REACHED_CPU -eq 1 ]
  then
    echo "The CPU limit has been reached. Apache is using $CUR_CPU (limit: $MAX_CPU)."
    KILL=1
  else
    echo "The CPU limit has NOT been reached. Apache is using $CUR_CPU (limit: $MAX_CPU)."
  fi
  read REACHED_MEM CUR_MEM < <(ps aux | grep httpd | grep -v grep | gawk -vlimit=$MAX_MEM '{ total+=$4 } END { if (total>limit) { print 1, total } else { print 0, total } }')
  if [ $REACHED_MEM -eq 1 ]
  then
    echo "The memory limit has been reached. Apache is using $CUR_MEM (limit: $MAX_MEM)."
    KILL=1
  else
    echo "The memory limit has NOT been reached. Apache is using $CUR_MEM (limit: $MAX_MEM)."
  fi
  if [ $KILL -eq 1 ]
  then
    if [ $SEND_REPORT -eq 1 ]
    then
      echo "Sending report to the system administrator..."
      mail -s "chkApache.sh alert on `hostname`" $ADMIN_MAIL <<< cat <<EOF
******************************************************
chkApache.sh report
******************************************************

*** APACHE LOAD

CPU usage: $CUR_CPU (limit: $MAX_CPU)
Mem usage: $CUR_MEM (limit: $MAX_MEM)

*** CURRENT LOAD

`cat /proc/loadavg`

*** MEMORY STATISTICS

`free -m`

*** APACHE STATUS REPORT

`lynx -dump http://127.0.0.1/whm-server-status`

*** TOP PROCESSES LIST

`top -b -n 1`

*** NETSTAT HTTP CONNECTIONS

`netstat -a | grep :http`

*** APACHE PROCESSES

`ps aux | grep httpd`

******************************************************
EOF
    fi
    if [ $RESTART_HTTPD -eq 1 ]
    then
      echo "Trying to kill all httpd processes..."
      COUNTER=0
      while [ $COUNTER -lt 50 ]; do
        killall -15 httpd
        killall -9 httpd
        if [ $? -ne 0 ]
        then
          echo "All httpd processes were killed."
          COUNTER=100
        else
          COUNTER=`expr $COUNTER + 1`
        fi
      done
      if [ $COUNTER -ne 100 ]
      then
        echo "WARNING: Failed to kill all httpd processes!"
      fi
      echo "Restarting httpd..."
      service httpd startssl
      echo "Done."
      sleep 5s
    fi
  fi
else
  echo "The minimum server load has not been reached."
fi

sleep $DELAY

done
__________________
RadixHosting - Professional shared and reseller web hosting solutions
www.radixhosting.com
 
 
 


Old
  Post #2 (permalink)   05-03-2007, 06:00 AM
Data Center Specialist
 
Join Date: Aug 2005
Location: United Kingdom
Posts: 825

Status: Marks is offline
Very impressive thanks for sharing I'm sure many members will find it useful
 
 
 


Old
  Post #3 (permalink)   07-04-2007, 05:35 PM
HD Newbie
 
Join Date: Jul 2007
Posts: 8

Status: expedio is offline
Nice script.

I am little concerned about this:

killall -15 httpd
killall -9 httpd

I'd prefer a ps scan and kill only few processes instead of stopping entire http server.
__________________
Expedio Servers Clustered Dedicated Servers

Failsafe Clusters | Fully Redundant | Supports all leading control panels
Get 99.999% uptime even if hardware fails!
 
 
 


Old
  Post #4 (permalink)   07-04-2007, 05:46 PM
HD Newbie
 
Join Date: Apr 2007
Posts: 34

Status: RadixHosting is offline
Quote:
Originally Posted by expedio
Nice script.

I am little concerned about this:

killall -15 httpd
killall -9 httpd

I'd prefer a ps scan and kill only few processes instead of stopping entire http server.
PRM (Process Resource Monitor) already does that. My script is designed to kill Apache before it gets to a non-functioning state and eventually crash the complete server. I had a problem with an earlier version of Apache, such behavior occurred frequently. However, an upgrade solved this which means the script is no longer needed. I'm not certain that my experiences with the older version of Apache were isolated incidents. The binary could have been corrupt, for example. You could alter the script and only kill the PID's that are causing the high server load, for more general purposes.
__________________
RadixHosting - Professional shared and reseller web hosting solutions
www.radixhosting.com
 
 
 


Old
  Post #5 (permalink)   07-17-2007, 01:08 AM
HD Amateur
 
Join Date: May 2006
Location: The Stars!
Posts: 110

Status: GnomeyNewt is offline
Interesting script. Will need to dive into it a bit further. Thanks for sharing!
__________________
Sarah :c)

cPanelDemos.com - live remotely hosted cPanel & WHM demo hosting service.
BlueJar.com - the webmasters guide to the galaxy, including tips and guides.
 
 
 


Old
  Post #6 (permalink)   07-17-2007, 02:30 AM
HD Newbie
 
Join Date: Jul 2007
Posts: 43

Status: rshosting is offline
Good script.. making few customization as per our needs it can help many.
__________________
RSHosting - Friendly, Reliable & Secure UK & US reseller hosting!
UK & US based reseller hosting and FULLY managed dedicated servers.
100% anonymous reseller hosting + FFMPEG + Ruby on Rails + Free ENOM reseller account + 10 min support
http://www.rshosting.co.uk
 
 
 


Old
  Post #7 (permalink)   07-17-2007, 05:00 PM
HD Amateur
 
Join Date: Mar 2006
Posts: 168

Status: Galaxy-Hosts is offline
That is a neat script. Thanks for sharing it, I am sure many people will find it useful.
__________________
http://Galaxy-Solutions.net Out of this world hosting, at down to earth prices http://Galaxy-Hosts.com
Move up to our QUALITY Servers
Patrick ~ 1-888-751-0100 ext 71 ~ 1-386-984-3717~ patrick@galaxy-hosts.com
 
 
 


Old
  Post #8 (permalink)   07-23-2007, 05:58 AM
HD Newbie
 
Join Date: Jun 2007
Posts: 17

Status: eukvps is offline
Quote:
Originally Posted by RadixHosting
Here's an interesting script I wrote. It basically keep an eye on Apache processes. I've had some problems where Apache suddenly started to consume a lot of memory causing the server load to increase which eventually results in a server crash. Anyway it took me some time to figure out what was causing this, so in meanwhile I wrote a script which just restarts Apache before it can do any damage. Prevents downtimes, etc. Hope you like it.

It's written for Apache 1.3 running on cPanel servers, but you can easilly modify it for your own server.

Code:
#!/bin/bash
# +----------------------------------------------------------------------------
# | chkApache.sh
# | Written for www.radixhosting.com
# | 
# | - Performs a basic scan to check the Apache load
# | - Forcefully restarts Apache if necessary
# | - Sends a report to the system administrator if actions taken
# +----------------------------------------------------------------------------

###############################################################################
# *** START OF CONFIGURATION SETTINGS ***
###############################################################################

# The delay between integrity checks
DELAY=3s

# Minimum server load (5 min. average) for the script to run (integer value)
MIN_LOAD=4

# Maximum % CPU all httpd processes are allowed to use
MAX_CPU=90

# Maximum % memory all httpd processes are allowed to use
MAX_MEM=110

# Kill all httpd processes and restart Apache when a limit has been reached
RESTART_HTTPD=1

# Send a report to the system administrator when a limit has been reached
SEND_REPORT=1

# System administrator to send reports to
ADMIN_MAIL=root

###############################################################################
# *** END OF CONFIGURATION SETTINGS ***
###############################################################################

while [ 1 ]
do

if [ `cat /proc/loadavg | gawk -F "." '{ print $1 }'` -ge $MIN_LOAD ]
then
  KILL=0
  echo "The minimum server load has been reached, performing Apache integrity scan..."
  read REACHED_CPU CUR_CPU < <(ps aux | grep httpd | grep -v grep | gawk -vlimit=$MAX_CPU '{ total+=$3 } END { if (total>limit) { print 1, total } else { print 0, total } }')
  if [ $REACHED_CPU -eq 1 ]
  then
    echo "The CPU limit has been reached. Apache is using $CUR_CPU (limit: $MAX_CPU)."
    KILL=1
  else
    echo "The CPU limit has NOT been reached. Apache is using $CUR_CPU (limit: $MAX_CPU)."
  fi
  read REACHED_MEM CUR_MEM < <(ps aux | grep httpd | grep -v grep | gawk -vlimit=$MAX_MEM '{ total+=$4 } END { if (total>limit) { print 1, total } else { print 0, total } }')
  if [ $REACHED_MEM -eq 1 ]
  then
    echo "The memory limit has been reached. Apache is using $CUR_MEM (limit: $MAX_MEM)."
    KILL=1
  else
    echo "The memory limit has NOT been reached. Apache is using $CUR_MEM (limit: $MAX_MEM)."
  fi
  if [ $KILL -eq 1 ]
  then
    if [ $SEND_REPORT -eq 1 ]
    then
      echo "Sending report to the system administrator..."
      mail -s "chkApache.sh alert on `hostname`" $ADMIN_MAIL <<< cat <<EOF
******************************************************
chkApache.sh report
******************************************************

*** APACHE LOAD

CPU usage: $CUR_CPU (limit: $MAX_CPU)
Mem usage: $CUR_MEM (limit: $MAX_MEM)

*** CURRENT LOAD

`cat /proc/loadavg`

*** MEMORY STATISTICS

`free -m`

*** APACHE STATUS REPORT

`lynx -dump http://127.0.0.1/whm-server-status`

*** TOP PROCESSES LIST

`top -b -n 1`

*** NETSTAT HTTP CONNECTIONS

`netstat -a | grep :http`

*** APACHE PROCESSES

`ps aux | grep httpd`

******************************************************
EOF
    fi
    if [ $RESTART_HTTPD -eq 1 ]
    then
      echo "Trying to kill all httpd processes..."
      COUNTER=0
      while [ $COUNTER -lt 50 ]; do
        killall -15 httpd
        killall -9 httpd
        if [ $? -ne 0 ]
        then
          echo "All httpd processes were killed."
          COUNTER=100
        else
          COUNTER=`expr $COUNTER + 1`
        fi
      done
      if [ $COUNTER -ne 100 ]
      then
        echo "WARNING: Failed to kill all httpd processes!"
      fi
      echo "Restarting httpd..."
      service httpd startssl
      echo "Done."
      sleep 5s
    fi
  fi
else
  echo "The minimum server load has not been reached."
fi

sleep $DELAY

done
Thank a Ton buddy
 
 
 
Reply

Thread Tools

New Post New Post   Old Post Old Post
Posting Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On