I’ll be discussing server load in an upcoming post, so I wanted to provide everyone with this handy little bash script for automatically checking your server load and sending you an email and a text message if it goes over the maximum load you’ll accept.
This really is a simply one. All you have to do is copy and paste the below script into a new .sh file on your server (e.g., nano checkload.sh) , edit it where indicated and then save it, give it execute permissions (e.g., chmod 755 checkload.sh), and then set up a line in your crontab to run it as often as you like – I run mine every 2 minutes (e.g., */2 * * * * ~/checkload.sh > /dev/null 2>&1).
Click the link below for the file. You’ll need to change the top section, which contains six variables, to suit your needs. There are instructions in the file, so it should be super-simple.
Like this script? Made improvements or have suggestions? Let us know in the comments below!
Click here to view the script!
hello, I would like to know if this script can I use to determine a change in my server, that is when creating new directories, placement of new files, delete files. This script can send an email to notify these changes? What you called “server load” is only traffic?
thanks for your response and effort
The script in this post actually monitors server CPU load, not traffic per se (although high traffic to a CPU-intensive script on your server can cause CPU load to spike). Server CPU Load is (to vastly oversimplify) a basic measure of how much strain your CPU (or CPUs) is under – the higher the number, the more load or “strain” your server is under. It is only one of a number of measurements that inform you as to the general health of your server.
But that wasn’t your question, was it? You wanted to know if you can use this script to monitor changes to your server’s disk, right?
Well, unfortunately, this script cannot be used as-is to monitor files or directories as you wish; however, I aim to please so let me point you over to THIS SCRIPT over at the Premiere Website Solutions website that purports to do exactly what you want. I’ve never used this script and therefore cannot vouch for it, so please use this link and script with caution and do not use it on a production server until you’ve had a chance to test it elsewhere and verify its fitness for your purpose.
Hope that helps!
Small change needed. On line 119 you forgot the $ before ‘load15′ so you actually get a text that says “Server load is at x.xx x.xx load15″
You are absolutely right; thank you. I went ahead and updated the code. Sorry about that, folks.
Hello John,
The script you provided is very useful and I’m using it in my server but there need to be some change in it.
Your script won’t work if the uptime is in hours that means script will spit out errors if the server is restarted recently and it will work only if the uptime in days.
So I have modified it as below.
change below lines :
load15=`uptime | awk -F, ‘{print $6}’`
load5=`uptime | awk -F, ‘{print $5}’`
load1=`uptime | awk -F, ‘{print $4}’ | awk ‘{print $3}’
to below one:
load15=`cat /proc/loadavg | cut -d” ” -f3`
load5=`cat /proc/loadavg | cut -d” ” -f2`
load1=`cat /proc/loadavg | cut -d” ” -f1`
This will work on all cases and first 3 columns will be the load average always. Hopefully this will be helpful to some body else too.