Tutorials

Monitor Your Websites For Downtime Using My Free Method

By November 20, 2018 No Comments

Recently i had an issue on my server where the load was crazy high and caused all my sites loading times to crawl. Unfortunately i was out at the time and didn’t notice this until i got home, by then i had lost a substantial amount of income.

While increased load and server issues are sometimes unavoidable, being notified of an issues when it happens is something that can help every webmaster.

After this issues i decided to do something about it so i Googles various Website monitoring services and there are plenty of free ones they often have limited features and send notifications via email while SMS notification require a paid subscription. Sure $15 per month is cheap but its still outgoings that can be saved with a bit of clever coding and some out of the box thinking.

Today i want to share with you my solution to this problem and best of all its FREE.

What you need

  • WHM Root Access
  • Piece of PHP code i will supply
  • Ability to make a Cron Job
  • Dedicated email account
  • Mobile Phone if you want notifications

Firstly you will need to create an access token inside WHM. This token is used to access WHM api without the need of a password.

Typically when you log into WHM you will get a randomly generated CPSESS value in the URL which is a session ID. like,

https://xx.xx.xx.xx/<strong>cpsessxxxxxxxxx</strong>/?login=1

With the token you wont need this which makes it perfect for using PHP to use WHM API.

So to generate this you will need to log into WHM and go to Development -> Manage API Token

Click on “Generate Token”

Give the token a name

Deselect all tick boxes accept :

Server Information Global Privileges
View Server Status status
View Server Information stats

Then click save.

WHM will present you with a popup with your Access Token. Make a note of this for the next steps.

You will now need to create a dedicated email address. You want to use a provider that offers a free IOS or Android app so you can get notification on your phone. I uses Hotmail as i already had a Gmail account and wanted something different from my usual Gmail notifications.

Once you have done this download the relevant app to your phone and set that up also.

Now we get to the PHP code.

<?
$user = "root";
$token = "<strong>REPLACE WITH ACCESS TOKEN</strong>";

$query = "https://WHM SERVER URL/json-api/loadavg?api.version=1";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

$header[0] = "Authorization: whm $user:$token";
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_URL, $query);

$result = curl_exec($curl);

$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status != 200) {
echo "[!] Error: " . $http_status . " returned\n";
} else {
$high = "5";
$json = json_decode($result);
echo $json->one,"\r\n";
echo $json->five,"\r\n";
echo $json->fifteen,"\r\n";
if($json->one > 20 || $json->five > 20 || $json->fifteen > 20) {
$msg = "Your Server Resource is Too High";
$msg = wordwrap($msg,70);
mail("<strong>YOUR DEDICATED EMAIL ADDRESS</strong>","Urgent Server Issue",$msg);
}

}
curl_close($curl);
?>

With the php code above you will want to change the $token value for your own Access Token and also change the email address towards the bottom.

You can alter the first $msg value to what ever you want and also where it says “Urgent Server Issue” this is the heading so this again can be changed.

Take a look at the following line in the code

if($json->one > 20 || $json->five > 20 || $json->fifteen > 20)

The value 20 means if your CPU usuage goes above 20% you will get notification to your email address.

This value cant be anything you want but ideally you want to set a value that doesn’t give of a false positive when a sudden burst of server load happens and you want it low enough to give you time to react to the situation.

Save the file and upload to the root directly of a website you control.

Visit the file URL in your browser and you will be presented a list of 3 values. These values are the server load for the past 1 minutes, 5 minutes and 20 minutes. So with the code above if any of these values are above 20 then the script will send me an email.

To test everything is working i recommend changing the value in the php code 0.1 then re visiting the URL. You should get an email to your dedicated email address.

Now we need to automate the checking of this process and for this we need to run a Cron Job.

To do this simply log into CPanel for the domain where you uploaded the PHP script and click on Cron Job under the Advanced heading.

For testing purposes enter an email address that the server will email each time the Cron runs. We want to know that the Cron is running correctly

Under common settings change this to “Once per 5 minutes”

Under the command enter php -f and then the full path to the URL for example

php -f /home/foteezy/public_html/test.php

You value may be different so you may need to play around with this or ask your hosting provider.

Save the Cron and wait to see if you get an email that it has run, then check your dedicated email to see if you got the email.

All being well both emails should have come through.

To stop emails being sent each time the cron runs simply add >/dev/null 2>&1 to the end of the command for example

php -f /home/foteezy/public_html/test.php >/dev/null 2>&1

For testing purposes you can change the Cron to run every 1 minutes instead of waiting every 5 minutes, alternatively you can log into the server via SSH and run the Cron command.

Remember once you confirm its all working you should change the PHP script values to something higher and change the Cron to run every 5 minutes.

Now you have a free working method to get notified when your Server load is getting high and gives you time to react.Something like this

Let me know in the comments if you like this or if you have any other way to accomplish this.