Andy's Blog » » 使用 Twitter 监控你的服务器程序下载

使用 Twitter 监控你的服务器程序下载

把里面一些参数改成你的,然后放到 cron 里,1或5分钟运行一次

twitter_alert_server.php
<?php
 
// Specify the target URL in your server
$targetUrl  = 'http://YOUR_SERVER_URL';
 
// Specify what the response is from the server
$targetText = 'Hello from 21andy.com';
 
// 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 = '21andy.com 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.';
}

Tags: 监控, Twitter

本文地址: http://www.21andy.com/new/20100208/1650.html