站点监控php代码(多站点的话稍加修改就行),可手机短信报警_PHP教程
—————————————–代码内容————————————
function check($host, $find)
{
$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp)
{
echo “$errstr ($errno)\n”;
} else
{
$header = “GET / HTTP/1.1\r\n”;
$header .= “Host: $host\r\n”;
$header .= “Connection: close\r\n\r\n”;
fputs($fp, $header);
while (!feof($fp))
{
$str .= fgets($fp, 1024);
}
fclose($fp);
return (strpos($str, $find) !== false);
}
}
function ok($host)
{
/* 下面的youremail@139.com改成你的邮箱地址 */
/* mail(’200513538@qq.com’, ‘Monitoring’, $host.’ down’); */
echo “$host – OK
”;
}
function posttohost($host)
{
/* 下面是以西部数码短信接口为例 */
$maildomain=’域名’;
$mailpwd=’密码’;
$sendmobile=’接收报警的手机号,联通/电信189/移动都可以’;
$sendcontent=”$host 出现故障!”;
$sendport=’3′;
if($maildomain!=”"){
$key=md5($maildomain.$mailpwd);
$url=”http://usericp.west263.cn/default.aspx“;
$data=array(‘mobile’=>$sendmobile,’sendcontent’=>mb_convert_encoding($sendcontent, “UTF-8″, “gb2312″),’port’=>$sendport,’maildomain’=>$maildomain,’key’=>$key);
$data=http_build_query($data);
$opts=array(‘http’=>array(‘method’=>’POST’,
‘header’=>”Content-type: application/x-www-form-urlencoded\r\n”.
“Content-Length:”.strlen($data).”\r\n”,
‘content’=>$data),);
$context=stream_context_create($opts);
$html=file_get_contents($url,false,$context);
$result=iconv(“UTF-8″,”gb2312″,$html);
if($result==”200 ok”){
echo “$host – sendOK
”;
}else{
echo “$result
”;
}
}
}
for ($i=1; $i
{
if ($i==1)
{
/* 下面的网址改成你要监控的网址 */
$host = ‘www.xxx.com’;
$Content = “www.xxx.com-无法连接”;
}elseif ($i==2)
{
$host = ‘www.yyy.com’;
$Content = “www.yyy.com-无法连接”;
}elseif ($i==3)
{
$host = ‘www.zzz.com’;
$Content = “www.zzz.com-无法连接”;
}
/* 下面的”备案号:”改成你的网站首页源代码中的一段特殊字符串 */
$find = ‘备案号:’;
if (!check($host, $find))
{
posttohost($host);
}
else
{ok($host);}
}
?>
——————————————代码结束———————————————
from:lonely blog