Server Monitoring via Twitter

One of my favorite things about Twitter is that its a complete snapshot of everything happen, all at once, in one place.

What better tool to use to monitor server loads and uptimes. I created a twitter account for all the servers I look after, its @EckoServers and once every two hours, it spits out information i can use to make sure everything on each server is working perfectly :)

[php]
## QUERY THE SERVER FOR SOME DETAILS
$uptime = system(“uptime”);
$apache = @fsockopen(“localhost”, 80, $errno, $errstr, 30);
$mysql = @fsockopen(“localhost”, 3306, $errno, $errstr, 30);

## GRAB THE STATS FROM $UPTIME
preg_match_all(‘/([\d]+) days.+load average: ([\d].[\d]+), ([\d].[\d]+), ([\d].[\d]+)/’, $uptime, $load);

## PING APACHE
if($apache) {
$apache_status = ‘Online’;
} else {
$apache_status = ‘Offline’;
}

## PING MYSQL
if($mysql) {
$mysql_status = ‘Online’;
} else {
$mysql_status = ‘Offline’;
}

## OUTPUT
$status = ‘Server Name: up ‘.$load[1][0].’ days – Apache: ‘.$apache_status.’ / MySQL: ‘.$mysql_status.’ – Load: ‘.$load[2][0].’, ‘.$load[3][0].’, ‘.$load[4][0].’.’;

## TWEET STATUS
$twitter = ‘http://www.twitter.com/statuses/update.xml’;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $twitter);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, “status=$status”);
curl_setopt($curl, CURLOPT_USERPWD, “twitter-username:twitter-password”);

$result = curl_exec($curl);
$resultArray = curl_getinfo($curl);

curl_close($curl);
[/php]

Sadly, this version doesnt use OAuth or the new Out-Of-Band Authentification method (which is what i think i need to change it to for when they no longer allow basic auth after 30th June 2010).

All you need to do is register a new account on twitter, fill in your account name and password and then setup a cronjob to hit it however often you like. Not too often though, as twitter now filters tweets that are similar, I have mine spaced 10 minutes apart every two hours :)

Trixx: up 75 days – Apache: Online / MySQL: Online – Load: 0.63, 0.68, 0.40. #eckostatusless than a minute ago via API