#!/bin/bash 
#
# loadcheck.sh : A simple load compare checker for alert or action
# Version: 2.0 (stable for use in Production)
# Author: Christophe Casalegno / Brain 0verride
# Website: https://scalarx.com
# Twitter: https://twitter.com/ScalarTech
# Email: christophe.casalegno@scalarx.com
# Note: N/A
#
# Put it in your cron * * * * * 

tsleep=2 # time to wait before 2 checks
llimit=8 # load limit before action
alert=monitor@christophe-casalegno.com #put your monitor alert email here
host=$(hostname -f)

load1=$(awk '{print $1}' < /proc/loadavg |cut -d "." -f1) # The load average now
sleep $tsleep
load2=$(awk '{print $1}' < /proc/loadavg |cut -d "." -f1) # The load average after tsleep

if test "$load1" -ge $llimit

	then

		if test "$load2" -ge "$load1"
	
			then

				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



