Using Twitter for sending server downtime alert

25 March 2009 » In General, PHP, Programming »

Today I’ve written this simple PHP script to alert me through Twitter whenever our company’s local server is down. The script is called by a cron every 5 mins in my central hosting. Without much babble, here goes the code (if you’re interested to know why I needed this, that’s at the bottom of the post):

<?php

// Specify the target URL in your server
$targetUrl  = 'http://YOUR_SERVER_URL';

// Specify what the response is from the server
$targetText = 'Hello from Daredevil';

// We will be using cURL for fetching the content
$ch = curl_init();

// Set the params
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Get the response
$response = curl_exec($ch);
curl_close($ch);

// Are things in right place ?
if ($response == $targetText) {
    die('Site is up and running!');
}

// Nope, so here are the sender's twitter info
$username = 'SENDER_TWITTER_USERNAME';
$password = 'SENDER_TWITTER_PASSWORD';

// Receiver's twitter username
$receiver = 'RECEIVER_TWITTER_USERNAME';

// Alert message to send
$message = 'Daredevil is not responding, please fix ASAP!';

// The Twitter API address (new direct message)
$url = 'http://twitter.com/direct_messages/new.json';

// We will be using cURL for this
$ch = curl_init();

// Set the params
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$receiver&amp;text=$message");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

// Send the request
$response = curl_exec($ch);
curl_close($ch);

// Success or failure
if (!empty($response)) {
    echo 'Recipient has been notified.';
} else {
    echo 'No response from twitter.';
}

Why I needed this?

Recently we have setup a server at our office for committing work to a local SVN repository and have the QA test our work whenever they are ready. We also have a staging server where we do SVN update from this repo. Now, for the last few days, I’ve found the local server to be off due to a few reasons – but every time I realized this at night when I am back home and can’t do anything to turn it on. So I thought about this Twitter alert which is sent to my cell phone immediately when the server goes offline.

Btw, if Twitter doesn’t send SMS to your country, don’t worry. Check out the excellent service at Twe2 that I’ve been using for a couple days.

Cheers!

Share and Enjoy:
  • Digg
  • DZone
  • Twitter
  • Posterous
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Simpy
  • Ping.fm
  • Tumblr

Tags: , , , , , , ,

Trackback URL

28 Comments on "Using Twitter for sending server downtime alert"

  1. admin
    Lenin
    25/03/2009 at 11:54 am Permalink

    Excellent :)

  2. admin
    Lenin
    25/03/2009 at 11:54 am Permalink

    Excellent :)

  3. admin
    Lenin
    25/03/2009 at 11:54 am Permalink

    Excellent :)

  4. admin
    Lenin
    25/03/2009 at 11:54 am Permalink

    Excellent :)

  5. admin
    maSnun
    25/04/2009 at 8:28 pm Permalink

    Just cool… I need it as well…

    Thanks for sharing :)

  6. admin
    maSnun
    25/04/2009 at 8:28 pm Permalink

    Just cool… I need it as well…

    Thanks for sharing :)

  7. admin
    maSnun
    25/04/2009 at 8:28 pm Permalink

    Just cool… I need it as well…

    Thanks for sharing :)

  8. admin
    maSnun
    25/04/2009 at 8:28 pm Permalink

    Just cool… I need it as well…

    Thanks for sharing :)

  9. admin
    shibs
    11/05/2009 at 10:40 pm Permalink

    cool one ….

    thanks a lot

  10. admin
    shibs
    11/05/2009 at 10:40 pm Permalink

    cool one ….

    thanks a lot

  11. admin
    shibs
    11/05/2009 at 10:40 pm Permalink

    cool one ….

    thanks a lot

  12. admin
    Arafat Rahman
    15/05/2009 at 11:07 am Permalink

    Twe2 as it is now is no more!

  13. admin
    Arafat Rahman
    15/05/2009 at 11:07 am Permalink

    Twe2 as it is now is no more!

  14. admin
    Arafat Rahman
    15/05/2009 at 11:07 am Permalink

    Twe2 as it is now is no more!

  15. admin
    Arafat Rahman
    15/05/2009 at 11:07 am Permalink

    Twe2 as it is now is no more!

  16. admin
    Neorom
    01/06/2009 at 2:54 am Permalink

    does anyone has test it ? It doesn't work on my server. Do you use the same accouent for sender and receiver ?

    Thanks

    Neorom

  17. admin
    Neorom
    01/06/2009 at 2:54 am Permalink

    does anyone has test it ? It doesn't work on my server. Do you use the same accouent for sender and receiver ?

    Thanks

    Neorom

  18. admin
    Neorom
    01/06/2009 at 2:54 am Permalink

    does anyone has test it ? It doesn't work on my server. Do you use the same accouent for sender and receiver ?

    Thanks

    Neorom

  19. admin
    Neorom
    01/06/2009 at 2:54 am Permalink

    does anyone has test it ? It doesn't work on my server. Do you use the same accouent for sender and receiver ?

    Thanks

    Neorom

  20. admin
    mehedibd
    27/07/2009 at 11:49 pm Permalink

    Great post !!!

    Thanks
    Mehedi
    PHP Developer Bangladesh

  21. admin
    mehedibd
    27/07/2009 at 11:49 pm Permalink

    Great post !!!

    Thanks
    Mehedi
    PHP Developer Bangladesh

  22. admin
    mehedibd
    27/07/2009 at 11:49 pm Permalink

    Great post !!!

    Thanks
    Mehedi
    PHP Developer Bangladesh

  23. admin
    mehedibd
    27/07/2009 at 11:49 pm Permalink

    Great post !!!

    Thanks
    Mehedi
    PHP Developer Bangladesh

  24. admin
    mehedibd
    28/07/2009 at 6:49 am Permalink

    Great post !!!

    Thanks
    Mehedi
    PHP Developer Bangladesh

  25. admin
    codex73
    23/07/2010 at 7:32 am Permalink

    Well done!

Trackbacks

  1. [...] Twitter - Using Twitter for sending server downtime alert - Emran Hasan (Suggested by Umut Muhaddisoglu) [...]

  2. [...] to administrators - and maybe even customers - quickly and easily. (I’m not the first person to think of this, ...

  3. [...] Today I’ve written this simple PHP script to alert me through Twitter whenever our company&#82... [...]

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments