about us

simplewebhosting.co.uk blog simplewebhosting.co.uk blog


Automatically post Pingdom updates to your Twitter feed

If you’re an avid Pingdom supporter (like us) and you want to keep your customers up-to-date with your server uptime, then you may be interested in this little PHP script that will post Pingdom alerts to your Twitter feed.

It’s very simple and works well for us (@simplestatus) and you’re free to use/modify it as you wish.

1) Add an e-mail contact at Pingdom (eg: pingdom2twitter@yourdomain.com) and assign that contact to the appropriate checks.

2) Create an e-mail forwarder to ‘pipe’ e-mail sent to your new address to your script (in cPanel use Mail – Forwarders, choose Pipe to a Program and enter the full path to the script).

3) Enter your Twitter username and password in the PHP script below and upload to your server.

#!/usr/local/bin/php -q
<?php
 $fp = @fopen(“php://stdin”, “r”);
 if ($fp > 0) {
      while(!feof($fp)) {
          $line=fgets($fp, 120);
    $data .= $line;
    if (!isset($body)) {
      $headersmm .= $line;
              if(ereg(“^From:(.+)”,$line)) {
                  $line=ereg_replace(“From:”,””,$line);
                  $line=trim($line);
                  $line=ereg_replace (“\\\n”,””,$line);
                  $line=addslashes($line);
                  finasteride propecia $sender=$line;
              }
              if(ereg(“^Subject:(.+)”,$line)) {
                  $line=ereg_replace(“Subject:”,””,$line);
                  $line=trim($line);
                  $line=ereg_replace (“\\\n”,””,$line);
                  $line=addslashes($line);
                  $subject=$line;
              }
              if(ereg(“^Content-Type:(.+)”,$line)) {
                  $line=ereg_replace(“Content-Type:”,””,$line);
                  $line=trim($line);
                  $line=ereg_replace (“\\\n”,””,$line);
                  $line=addslashes($line);
                  $contenttype=$line;
              }
              if(ereg(“^\\\n”,$line)) {
                  $body=””;
              }
          }
          else {
              $line=addslashes($line);
              $body.=$line;
          }
      }
 fclose($fp);
}

//Get server name from inside brackets (bit naff I know but it does the job!)
preg_match(‘#\(([^)]+)\)#’, $body, $server);

if(strstr($body,”PingdomAlert DOWN”)) {
          $tweet = “Oopsy daisy – ” . $server[1] . ” is having some problems – but don’t worry, we’re looking into it now. [Pingdom]”;
} elseif(strstr($body,”PingdomAlert UP”)) {
          $tweet = “Phew – ” . $server[1] . ” is back online again. Apologies for the brief downtime. [Pingdom]”;
}

// Post tweet (thanks to http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/)

// Set username and password
$username = ‘TWITTERUSERNAME’;
$password = ‘TWITTERPASSWORD’;
// The message you want to send
$message = $tweet;
// The twitter API address
$url = ‘http://twitter.com/statuses/update.xml’;
// Alternative JSON version
// $url = ‘http://twitter.com/statuses/update.json’;
// Set up and execute the curl process
$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=$message”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

?>

Enjoy!

Leave a Reply

about us
top

blog blog

 
top

blog categories blog categories

 
top

blog archives blog archives

 
top

blog categories popular blogs

 

 
10 reasons 10 reasons to choose simplewebhosting.co.uk