egscripts/egapachewatch/egapachewatch.sh

140 lines
3.1 KiB
Bash
Raw Normal View History

#!/bin/bash
#
# count how many apache processes are running and warn us if it's getting high..
SCRIPT=egapachewatch.sh
VERSION=0.1
#
LOG=/var/log/egapachewatch.log
TMP_DIR=/tmp
DATE=`which date`
MAIL=`which mail`
RM=`which rm`
NUMWARN=40
#
TODAY=`$DATE '+%Y-%m-%d-%a'`
# determine today's date
NOW=`$DATE '+%H-%M-%S'`
# a timestamp for logging purposes
#
# function to direct a message...
message() {
#
# a timestamp for logging purposes
timestamp
if test -w $LOG ; then
echo "$SCRIPT: $TIMESTAMP $@" >> $LOG
fi
if test -w $TMP_EMAIL ; then
echo "$SCRIPT: $TIMESTAMP $@" >> $TMP_EMAIL
fi
verbose "$TIMESTAMP $@"
}
#
# function to direct a message...
verbose() {
if test $VERBOSE ; then
echo "$@"
fi
}
#
# insert a blank line into the log and on the console
insert_blank() {
echo "" >> $TMP_EMAIL
verbose ""
}
#
# update date and time info..
today() {
# determine today's date
TODAY=`$DATE '+%Y-%m-%d-%a'`
}
now() {
# determine today's date
NOW=`$DATE '+%H-%M-%S'`
}
timestamp() {
# a timestamp for logging purposes
TIMESTAMP=`$DATE '+%Y-%m-%d %H:%M.%S'`
}
#
# create the temporary email file
create_tmp_email() {
if test -d $TMP_DIR ; then
if test -w $TMP_DIR ; then
touch $TMP_EMAIL 2>&1
else
error "Email tmp directory $TMP_DIR is not writable"
fi
else
error "Email tmp directory $TMP_DIR does not exist"
fi
if test -w $TMP_EMAIL ; then
message "created temporary email $TMP_EMAIL"
else
error "Failed to create temporary email $TMP_EMAIL"
fi
}
#
# send the contents of the temporary file to the
# designated report recipient
send_email_report() {
if test -f $TMP_EMAIL ; then
message "sending email report to $EMAIL_TO"
for EMAIL_ADD in $EMAIL_TO
do
RES=`$MAIL -s "$EMAIL_SUBJ" $EMAIL_ADD < $TMP_EMAIL`
if test -z $RES ; then
if test $ERROR_STATUS == 1 ; then
message "Error email report successfully sent to $EMAIL_ADD"
else
message "Email report successfully sent to $EMAIL_ADD"
fi
fi
done
if test -w $TMP_EMAIL ; then
$RM $TMP_EMAIL 2>&1
fi
fi
}
# modified dave@egressive.com 20071029
max_clients() {
NUMAVAIL=`grep -A 8 "IfModule prefork.c" /etc/apache2/apache2.conf | grep "^MaxClients" | awk '{print $2;}'`
# An email will be sent if the number of processes running is greater than ($NUMAVAIL - $NUMWARN)
# As of 20071025, MaxClients was set to 150 this may be outrageously high for the amount
# of memory on the server, so NUMWARN is set high too . . .
}
TMP_EMAIL=$TMP_DIR/$SCRIPT"_tmp_email_"$TODAY.$NOW
EMAIL_TO="support@egressive.com"
NUMPROC=`ps auxwww | grep -c "apache2"`
max_clients
NUMWARNLEVEL=$[$NUMAVAIL - $NUMWARN]
MACHINE_NAME="geprod"
ERROR_STATUS=0
#
# set up alert
if ! test -f $TMP_EMAIL ; then
touch $TMP_EMAIL
fi
if ! test -f $LOG ; then
touch $LOG
fi
message " " # blank line
message "running $NOW"
#NUMAVAIL=1
message "current number of processes $NUMPROC out of $NUMAVAIL"
if [ $NUMPROC -ge $NUMWARNLEVEL ] ; then
message "sending email alert!"
EMAIL_SUBJ="ALERT - $MACHINE_NAME - exceeding num apache processes!"
send_email_report
fi
now
message "completed run"
exit 0