Get free SMS alert on your server down (or something else)!

Well, first thing first. This blog may be little bit immoral even if it is not illegal by the provider terms of service. So, if you read this or later sentences, the responsibilities are on you.

—————-

I am going to share an idea that I’ve personally used a bit ago (2009) in my previous office to monitor the server and get notified when any server/service is down. I wrote a small PHP application that would do the tasks. Though I planned that time to blog about it, apparently, I forgot about it. The steps of the idea is:

  • A php chunk checks server status
  • If it finds something unexpected (or may be expected; depends on your logic :)) it creates an event in your google calendar which is due just 1-3 minutes from now
  • If your SMS alert is setup in Google Calender, Google will email you about the event.

Here I will not show how to write a PHP script to monitor server. I just told it as I applied this idea for same purpose. I guess you will get lots if you google it. However, the initial version that I developed just checked site’s homepage (using CURL) and if they were returning 200 response within a sensible time limit. If the response was not 200, it would create the event for me.

How to create Event?

It’s very simple. First of all download the following classes from phpclasses.org

Put them in your project path. Create a php file. Lets assume it is createevent.php. Now include the wrapper file in createevent.php.

[note: see the bottom of this post for a crucial amendment]


require ('GoogleCalendarWrapper.php');

Now, lets create our event.

//google account username & password
$gemail = 'xxxx@gmail.com'; //make sure your google calendar is active and configured sms alert
$gpassword = 'xxxxxxx'; //password of your google account

$event = array();
$event["title"] = 'server alert'; //subject of event
$event["content"] = 'may be down?'; //event details
$event["where"] = 'RIP'; //event location

$event["startDay"] = date("Y-m-d");
$event["startTime"] = date('h:i:s', strtotime("+3 minutes"));
$event["endDay"] = date("Y-m-d");
$event["endTime"] = date('h:i:s',strtotime("+30 minutes"));

$result = $gc->add_event($event);
if ($result) {
 echo 'Event added successfully!';
} else {
 var_dump($result);
 echo 'Event can\'t be added!';
}

Now when you run this script, it should be able to add an event on your google calendar. Login there to check whether it was success or not. You may need to fix the timezone issue and I’ve confidence on you that you can do it yourself ;). So, if the event creation is successful, now you can a monitoring script that will check the status of server/site. If its response is not your expected one, you can request/execute this script. Set a cron to run the monitoring script at each 1 or 5 or your desired interval. Finally, to save your ass:

  • make sure this script is not publicly accessible
  • do not run this trick from your main gmail account. try making another for this purpose only. otherwise, if the above things violate google’s terms, your main gmail account may be RIP (rest in peace)

Adding notification directives
The script with me already has those directives (probably I added them but forgot) while the original wrapper class misses them. Thanks Hasin Hayder for reminding me about it.

The wrapper class will create the event for sure but it will not set the alert as it misses those directives. So, you need to add them in GoogleCalendarWrapper.php.
Open GoogleCalendarWrapper.php and find the following codes:

<gd:when startTime='".$settings["startDay"]."T".$settings["startTime"].".000Z'
 endTime='".$settings["endDay"]."T".$settings["endTime"].".000Z'></gd:when>

We need to add notification directives just before </gd:when>. The final codes will be:

<gd:when startTime='".$settings["startDay"]."T".$settings["startTime"].".000Z'
 endTime='".$settings["endDay"]."T".$settings["endTime"].".000Z'>
 <gd:reminder minutes='2' method='email' />
 <gd:reminder minutes='1' method='sms' />
 </gd:when>

As per the above codes, it will email you just 2 minutes before the event and will send you sms just 1 minute before the event. You can customize it as per your desire considering it should be something between the moment you created and the due time.

 

Thanks

  • Sarim Khan

    Good Idea 😀
    Gonna try this tomorrow 😀

  • Great!
    But, we need to take care about the site where we host this script.

  • It works! Great great great 🙂

  • The HungryCoder

    Yeah, you shouldn’t host the monitoring script on same server 🙂

  • The HungryCoder

    Thanks

  • I got error while execute createevent.php
    Fatal error: Call to a member function add_event() on a non-object in /home/myuser/public_html/oscnet.web.id/kalendar/createevent.php on line 17

    how to fix this ? thanks