Get Notification On server load & restart Apache2

A simple Shell Script That will send you email notification on a specific load average of your server.. & also will restart any services you want.

Get Notification On server load & restart Apache2

In my case i am restating the apache2 if the system load reaches 30.

So Lets create the script..

*** create a File like apache2_restart.sh ***

& copy the below code to the file.

#!/bin/sh
tsleep=2 # time to wait before 2 checks
llimit=30 # load limit before action
[email protected] #put your monitor alert email here
host=`hostname -f`

load=`cat /proc/loadavg |awk {'print $1'}|cut -d "." -f1` # The load average now
sleep $tsleep
load2=`cat /proc/loadavg |awk {'print $1'}|cut -d "." -f1` # The load average after tsleep
apache_init='/etc/init.d/apache2';
if test "$load" -ge $llimit

then

if test "$load2" -ge $load
then
$apache_init restart 

date=`date`
echo "The Load Average has reached $load1 and $load2 on $host" | mail -s "$host : High Load Average Alert" $alert
echo "$date : The Load Average has reached $load1 and $load2 on $host" >> /var/log/loadavg.log
else
echo "ok" 1>&2

fi

else

sleep 1

fi

Now Make the shell script Executable

sudo chmod +x myShell

One last step add a cronjob to run the shell script

crontab -e

& paste the following line at the end of the file

*/5 * * * * /path/to/your/scrip.sh >/dev/null 2>&1