原文:[url]http://tutorial.jcwcn.com/Web-Design/PHP/Networking/2007-08-13/2943.html[/url]
<?php
//需要检查的主机
$host = "localhost"; // ***把这个改成你的域名 ***
$portmsg="";
$headers="";
// 定义检查的端口
$ports = array(21=> "FTP",25=> "Sendmail",80=> "Apache Web Server",3306=> "MySQL Server");
// 开始时假定服务没有问题
$problem = 0;
// 检查下 $ports数组中每一个端口是否可以打开
foreach($ports as $port => $service){
$fp = fsockopen($host,$port,$errno,$errstr,10);
if(!$fp)
{
$portmsg.= "Port ".$port. " - ".$service. "\n";
if($problem!=1){
$problem=1; //有问题
}
} else{
fclose($fp);
}
flush();
}
// 如果有问题通知接收方
if($problem == 1){
// 发送邮件
$recipients = "[email]xcf-007@163.com[/email]"; // *** 把这个改成你的邮箱 ***
$msg = date( "M d, Y h:i:s",time()). "\n\n";
$msg.= "下面的服务不可用需要立即引起注意:\n\n";
$msg.= $portmsg;
$subject = '服务不可得!';
$headers .= "From: 服务器状态<root@localhost>\r\n";
$headers .= "X-Sender: <root@localhost>\r\n";
$headers .= "Content-Type: text; charset=gbk\r\n";
mail($recipients, $subject, $msg, $headers) or die( "发送邮件失败.");
}
?>
//需要检查的主机
$host = "localhost"; // ***把这个改成你的域名 ***
$portmsg="";
$headers="";
// 定义检查的端口
$ports = array(21=> "FTP",25=> "Sendmail",80=> "Apache Web Server",3306=> "MySQL Server");
// 开始时假定服务没有问题
$problem = 0;
// 检查下 $ports数组中每一个端口是否可以打开
foreach($ports as $port => $service){
$fp = fsockopen($host,$port,$errno,$errstr,10);
if(!$fp)
{
$portmsg.= "Port ".$port. " - ".$service. "\n";
if($problem!=1){
$problem=1; //有问题
}
} else{
fclose($fp);
}
flush();
}
// 如果有问题通知接收方
if($problem == 1){
// 发送邮件
$recipients = "[email]xcf-007@163.com[/email]"; // *** 把这个改成你的邮箱 ***
$msg = date( "M d, Y h:i:s",time()). "\n\n";
$msg.= "下面的服务不可用需要立即引起注意:\n\n";
$msg.= $portmsg;
$subject = '服务不可得!';
$headers .= "From: 服务器状态<root@localhost>\r\n";
$headers .= "X-Sender: <root@localhost>\r\n";
$headers .= "Content-Type: text; charset=gbk\r\n";
mail($recipients, $subject, $msg, $headers) or die( "发送邮件失败.");
}
?>
检查服务器上指定的端口是否开放并email通知服务器管理员。
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/109545,如需转载请自行联系原作者