In this article we'll discuss how you can setup a small bash script on a scheduled cron job to monitor your server's load average, and e-mail you a notice if it's above the limit that you've set.

A lot of times your server will probably be running at a load much lower than it can actually handle. Although at certain times your server's usage might suddenly spike leading to the server possibly going unstable and it could potentially need a reboot to recover from.

Our system administration team has monitors setup on all of our dedicated servers for extreme load issues. However you might be interested in setting up some monitoring of your own so that you can be alerted via e-mail as soon as your server is spiking so that you can login to investigate that usage spike more closely.

This would primarily be used for a dedicated server. While it could also be used on a VPS the load average of a VPS typically shouldn't exceed a 1.00 and it can temporarily fluctuate up and down as it's a virtualized environment. So you might end up getting too many notices and by the time you get in to investigate it could have dropped back down.

Create bash load monitoring script

  1. First you'll want to figure out how many CPU cores you have on your dedicated server as this determines the optimal load your server can operate at.

     

    You can login to your server via SSH and then run the following command to find this out:

    grep pro /proc/cpuinfo -c

    You should get back the number of cores on your server such as  4 .

  2. You won't want your load average spiking much over how many CPU cores your server has, in this case I know this particular server isn't doing anything other than serving up one static website, so if the load gets even up to a 4.00 I'll want to investigate it. So I'm going to set my trigger value for load at a 4.00.

     

    Start editing a new file to create your bash alert script, in this case I'm using the vim text-editor and making a new file called loadMon in my user's home directory with the following command:

    vim /home/userna1/loadMon

    Then you'll want to hit i to enter Insert mode once vim is loaded up and enter in the following code:

    #!/bin/bash
    trigger=4.00
    load=`cat /proc/loadavg | awk '{print $1}'`
    response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`
    if [[ $response = "greater" ]]
    then
    sar -q | mail -s"High load on server - [ $load ]" recipient@YourDomain.com
    fi
    

    Here is the same code again with comments walking through what each line is doing:

    #!/bin/bash

    We set a trigger for how high the load can get before we're
    alerted via e-mail from this script.
    trigger=4.00

    We set a load variable to read the current server load from
    /proc/loadavg and only from the first column which is the live load.
    load=`cat /proc/loadavg | awk '{print $1}'`

    We set a response variable to the word "greater" if the current
    load is greater than our trigger that we set.
    response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`

    If the response is set to "greater" we run the sar -q command
    and pipe | that data to the mail command for recipient@YourDomain.com
    this sends an e-mail with the server's recent load averages there.
    if [[ $response = "greater" ]]
    then
    sar -q | mail -s"High load on server - [ $load ]" recipient@YourDomain.com
    fi

Setup cron job to run monitoring script

  1. Now that you have your load monitoring bash script setup, the next thing you'll want to do is create a cron job to run this task, you can read about how to run a cron job.

     

    Typically setting up a cron job for monitoring of this type would be fine at every 5 minutes, you can select that premade option in cPanel's cron jobs section, it should end up looking like the following once you're done:

    */5 * * * * bash /home/userna1/loadMon

You should now have successfully setup a small bash script to monitor your servers load average, and a cron job to run that monitoring script on a set interval to send you an e-mail if the server's load has exceeded the limit you've set.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-03-24 7:04 am

Awsome.. helpful

n/a Points
2014-07-15 4:52 am

This is working fine. You can alter according to your requirements.

n/a Points
2014-07-15 4:55 am
Nothing. Good article.

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

3 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!