Test: Have a web server send me critical messages via Twitter.

Not to forget all my UNIX know-how, I run a little NAS based web server that runs Fever°, subversion and a couple of other web applications for me.

I set up automated scripts to update and upgrade the IPKG packages automatically once a week and to refresh the Fever° data daily. I care about the result of these actions, so I logged them locally… only to never look at them.

I have decided to try the following: create a Twitter account and have the server send only the most important status messages to Twitter. Only a select few every day… not to be the one who breaks Twitter…!

Now I can keep up with my server’s health by simply following it on Twitter. Cloud based logging? Let’s see, how this works out!

Image001

I still have to figure out what to do with long messages, for now I just crop them.

I simply call the following PHP script from my batch file using curl and passing the log file to be posted as a parameter.

<?php
    $filename = "/share/logs/" . $_GET["file"];
    echo $filename;
    $fd = fopen ($filename, "r");
    $contents = fread ($fd,filesize ($filename));
    echo $contents;
    $tweet =  substr($contents, 0, 140);
    echo $tweet;
    fclose ($fd); 
    sendTweet($tweet);

    function sendTweet($msg){
        $username = 'x';
        $password = 'y';
        $url = 'http://twitter.com/statuses/update.xml';
        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, "$url")
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_handle, CURLOPT_POST, 1);
        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$msg");
        curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
        $buffer = curl_exec($curl_handle);
        curl_close($curl_handle);
    }
?>

Tags: , ,

About TechPreacher

Technical Evangelist working for Microsoft Switzerland. Focusing on the Internet of Things and Windows development. EV geek and passionate gamer, with a life.
%d bloggers like this: