Using Twitter for sending server downtime alert

Mar 25, 2009

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):

[sourcecode language='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&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.';
}

[/sourcecode]

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!

There are 28 comments in this article:

  1. 25/03/2009Lenin says:

    Excellent :)

  2. 25/03/2009Lenin says:

    Excellent :)

  3. 25/03/2009Lenin says:

    Excellent :)

  4. 25/03/2009Lenin says:

    Excellent :)

  5. 25/04/2009maSnun says:

    Just cool… I need it as well…

    Thanks for sharing :)

  6. 25/04/2009maSnun says:

    Just cool… I need it as well…

    Thanks for sharing :)

  7. 25/04/2009maSnun says:

    Just cool… I need it as well…

    Thanks for sharing :)

  8. 25/04/2009maSnun says:

    Just cool… I need it as well…

    Thanks for sharing :)

  9. 11/05/2009shibs says:

    cool one ….

    thanks a lot

  10. 11/05/2009shibs says:

    cool one ….

    thanks a lot

  11. 11/05/2009shibs says:

    cool one ….

    thanks a lot

  12. 15/05/2009Arafat Rahman says:

    Twe2 as it is now is no more!

  13. 15/05/2009Arafat Rahman says:

    Twe2 as it is now is no more!

  14. 15/05/2009Arafat Rahman says:

    Twe2 as it is now is no more!

  15. 15/05/2009Arafat Rahman says:

    Twe2 as it is now is no more!

  16. 29/05/2009The Technology Post for May 29th - Jason N. Gaylord's Blog says:

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

  17. 1/06/2009Neorom says:

    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. 1/06/2009Neorom says:

    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. 1/06/2009Neorom says:

    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. 1/06/2009Neorom says:

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

    Thanks

    Neorom

  21. 27/07/2009mehedibd says:

    Great post !!!

    Thanks
    Mehedi
    http://www.mehedibd.com' rel=”nofollow”>PHP Developer Bangladesh

  22. 27/07/2009mehedibd says:

    Great post !!!

    Thanks
    Mehedi
    http://www.mehedibd.com' rel=”nofollow”>PHP Developer Bangladesh

  23. 27/07/2009mehedibd says:

    Great post !!!

    Thanks
    Mehedi
    http://www.mehedibd.com' rel=”nofollow”>PHP Developer Bangladesh

  24. 27/07/2009mehedibd says:

    Great post !!!

    Thanks
    Mehedi
    http://www.mehedibd.com' rel=”nofollow”>PHP Developer Bangladesh

  25. 28/07/2009mehedibd says:

    Great post !!!

    Thanks
    Mehedi
    http://www.mehedibd.com' rel=”nofollow”>PHP Developer Bangladesh

  26. 1/09/2009Is Twitter a server exception notification tool? | Servers and Storage | TechRepublic.com says:

    [...] to administrators – and maybe even customers - quickly and easily. (I’m not the first person to think of this, but it seemed interesting) You probably already have a mechanism in place to [...]

  27. 2/03/2010Using Twitter for sending server downtime alert « XLNC Solution INC says:

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

  28. 23/07/2010codex73 says:

    Well done!

Write a comment: