Quick Tips

Free server monitoring script with push notifications

Google+ Pinterest LinkedIn Tumblr

Why monitoring is important

Monitoring your website and server is an important task for every website owner. By using a monitoring service you can make sure your website is available and get notified once your server goes down. You can then perform the necessary steps to make sure your website goes back online. If your website is offline you will loose customers.

Monitoring tools

You could simply use a service like Pingdom or Monitis to make sure you get notified once your server goes down. This makes sense if you are running a large business website and would like to have a detailed overview of how long and when your server is running slow or crashing. However all monitoring services I know about will cost you between 7 to 15 bucks a month or even more. If you just want to receive a notification when your server goes down this seems a bit too much money.

A solution – building your own monitoring tool

Building your own website monitoring tool is fairly simple. We will create a PHP function which will try creating a socket connection to our server. If the function fails to connect to the server within a given time period we will send a request to a free notification service which will then send us a notification to our smartphone. All we need to do is create a cronjob which runs the function every single minute.

The function ping checks whether a socket connection to $host using the $port can be established.

function ping($host,$port=80,$timeout=5)
{
if(fsockopen($host, $port, $errno, $errstr, $timeout))
{
return true;}
else return false;
}

All we need to do now is run the function ping passing our website or our server IP as $host. If the socket connection can not be established we send a POST request to the Pushover API. Pushover is a great notification service which sends a notification to whatever device we install their app to. You can register for free and send up to 7500 notifications a month (however you shouldn’t get that many notifications or something is wrong with your server 😉 ). After a trial of seven days you need to buy their app for 4,95$ which is a one-time fee and will give you the ability to install the Pushover app on any device on the same platform.

if(!ping('www.example.com'))
{curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => "YOUR_API_TOKEN",
"user" => "YOUR_USER_KEY",
"message" => "Oh no, the server is down!",
"priority" => 1,
"sound" => "alien"
),
CURLOPT_SAFE_UPLOAD => true,
));
curl_exec($ch);
curl_close($ch);
}

Now all we need to do is add the script to some different server (note that you have to use a different server otherwise our script won’t even run and you won’t get a notification). Thankfully there are free server hosting providers out there, for example Serversfree. Then we simply create a cronjob which will run the script every one minute automatically. If your server ever goes down the ping function will return false and a cURL request will be send to the Pushover API. You will then get a push notification on your device a few seconds later telling you “Oh no, the server is down!”.

I am a frontend and backend web developer who loves building up new projects from scratch. In my blog "Lingulo" me and some guest authors regularly post new tutorials, web design trends or useful resources.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments