Blog

#!/bin/bash
################################################################################
# Derek Demuro, this script is given as is, CopyLEFT                           #
################################################################################
################################################################################
# README                              LEAME                                    #
################################################################################
# This script will keep checking load, and log it.
# Once the log hits the high load, its time to shutdown services and recall
# memory, this way we avoid running into a kernel panic.
# If we had DDOS attack, as services are down, the server won't be killed
# Once the attack ends, the load will come down, and the services will load up 
# again
#
# The script will be modified later on to use functions, but not for now.
############################To Set Up###########################################
#
# To set this script up, you'll need to add it to a cronjob to run on boot
# Most linux distros will allow a param <a href="//twitter.com/reboot" class="twitter-atreply ext">@reboot<span class="ext"><span class="element-invisible"> (link is external)</span></span></a> /(path)/servermon.sh
# So... crontab -e
# At the bottom add: <a href="//twitter.com/reboot" class="twitter-atreply ext">@reboot<span class="ext"><span class="element-invisible"> (link is external)</span></span></a> /(path)/servermon.sh
# REMEMBER TO ADD EXECUTABLE BIT TO THE FILE (777 Permissions)
################################################################################
# SCRIPT CONFIGURATION                                                         #
################################################################################
 
#Initialize counter
times=0
  
#How many times to run before clearing log
linestoclear=$1
#Critical load to stop everything and restart
highload=$2
#Wait till load gets to this load
lowload=$3
  
#Wait for highload
high=$4
#Wait for midload
mid=$5
#Wait more for lowload
low=$6
################################################################################
#Log how many lines to log
echo 'Script will run ' $1 ' times then will clear itself, High load: ' $highload 'Will wait to: ' $lowload "HighWait: $high, MidWait: $mid, LowWait: $low"
  
while [ 0 ]
do
    #add 1 to times
    times=`expr $times + 1`
    #check = load at the moment
    check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
    date=`date`
    #Print of the time the cron started.
    echo 'Load at the moment' $check 'at date ' $date 'script ran: ' $times ' times'
    #How many times did it run?
    if [ $times -eq $linestoclear ]
    then
        #Clean the log!
        echo 'Log cleared' > hsysmon.log
        echo 'Script will run ' $1 ' times then will clear itself'
        times=0
    fi
    #If load > 10
    if [ $check -gt $highload ]
    then
        #Apache STOP
        var=`service apache2 stop`
        #Moment of shutdown
        date=`date`
        msg='Highload autorestart did run and load average was: '
        echo 'At: ' $date 'With load: ' $check 'services shutted down successfoully'
        #Sleep while HIGH LOAD
            while [ $check -gt $lowload ]
            do
                if [ $check -gt $high ]
                then
                    sleep $high
                else
                    if [ $check -gt $mid ]
                    then
                        sleep $mid
                    else
                        if [ $check -gt $low ]
                        then
                            sleep $low
                        else
                            sleep 3                     
                        fi                
                    fi
                fi            
                #New check
                check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
            done
        #Continue
        echo 'Restarting on load: ' $check
        #Apache START
        apacheres=`service apache2 start`
        #Prints
        echo '#######################Apache Restart Log###########################'
        echo  $apacheres
        echo '#######################Apache Restart Log###########################'
        echo '###########-----------------------------------------################'
     fi
#Keep checking load
sleep 3
done

You may also like

LUKS Drive Encryption

Leave a Reply

Recent Comments