本文实例讲述了PHP实现服务器状态监控的方法。分享给大家供大家参考。具体分析如下:
PHP服务器状态监控对于很多朋友来讲都没做,只有看到网站挂了才知道,这种半夜网站关了是不知道情况了,对于网站也非常不好,为此这两天抽空写了个网页服务器状态监控,看到有朋友说需要,那我就放出来吧。很简单的东西。
使用方法:
打开压缩包里面的status.php文件。编辑这里的内容为你自己的邮箱信息。代码如下:
$mail->Host = 'smtp.exmail.qq.com'; // SMTP 服务器<br />$mail->Port = 25; // SMTP服务器的端口号<br />$mail->Username = '[email protected]'; // SMTP服务器用户名<br />$mail->Password = 'password'; // SMTP服务器密码<br />$mail->SetFrom('[email protected]','Status');<br />$mail->AddReplyTo('[email protected]','Status');<br />$mail->Subject = $subject;<br />$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test<br />$mail->MsgHTML($body);<br />$address = '[email protected]'; //接收邮箱<br />//更改这里的内容为你要监控的IP:<br />$server_ip_list = array(<br />'61.135.169.121',<br />'221.204.173.200',<br />'173.194.127.83'<br />);
然后访问你http://yourdomain.com/status.php文件,即可看到当前服务器状态并且自动发送邮件到你设置的邮箱。如果需要自动监控,请添加Cron任务或者使用什么监控宝之类的!
完整代码如下:
<?php<br />/*<br /> * 服务器状态监控<br /> */<br />header('Content-type:text/html;charset=utf-8');<br />include './smtp/class.smtp.php';<br />include './smtp/class.phpmailer.php';<br />function sendmail($subject = '',$body = '') {<br /> date_default_timezone_set('Asia/Shanghai');//设定时区东八区<br /> $mail = new PHPMailer(); //new一个PHPMailer对象出来<br /> // $body = eregi_replace("[]",'',$body); //对邮件内容进行必要的过滤<br /> $mail->CharSet ="UTF-8";//设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码<br /> $mail->IsSMTP(); // 设定使用SMTP服务<br /> $mail->SMTPAuth = true; // 启用 SMTP 验证功能<br /> $mail->Host = 'smtp.exmail.qq.com'; // SMTP 服务器<br /> $mail->Port = 25; // SMTP服务器的端口号<br /> $mail->Username = '[email protected]'; // SMTP服务器用户名<br /> $mail->Password = 'password'; // SMTP服务器密码<br /> $mail->SetFrom('[email protected]','Status');<br /> $mail->AddReplyTo('[email protected]','Status');<br /> $mail->Subject = $subject;<br /> $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test<br /> $mail->MsgHTML($body);<br /> $address = '[email protected]'; //接收邮箱<br /> $mail->AddAddress($address, '');<br /> //$mail->AddAttachment("images/phpmailer.gif"); // attachment 附件<br /> //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment<br /> if(!$mail->Send()) {<br /> echo 'Mailer Error: ' . $mail->ErrorInfo;<br /> } else {<br /> // echo "Message sent!恭喜,邮件发送成功!";<br /> }<br />}<br />//check server status<br />function checkServerSatatus($ip) {<br /> $str = null;<br /> $fp = @fsockopen($ip,80,$errno,$errstr,10);<br /> if (!$fp) {<br /> return false;<br /> } else {<br /> fclose($fp);<br /> return true;<br /> }<br />}<br />$server_ip_lis<mark style="color:transparent">本文来源gaodaimacom搞#^代%!码&网*</mark><pre>搞代gaodaima码
t = array(
‘61.135.169.121’,
‘221.204.173.200’,
‘173.194.127.83’
);
?>
<meta charset=”UTF-8″>
<style>
* {
margin: 0px;
padding: 0px;
}
body {
font-family: “Microsoft yahei”,Arial;
font-size:14px;
}
header {
height: 40px;
background-color: #2e2e2e;
width: 100%;
line-height: 35px;
}
header > h3 {
color: #fff;
margin-left: 20px;
}
footer {
text-align: center;
}
a {
color: #424242;
text-decoration: none;
}
.wrap {
height: auto;
zoom:1;
overflow: auto;
max-width: 500px;
width: 100%;
margin: 50px auto;
}
.table {
border-collapse: collapse;
border: 1px solid #eee;
width: 100%;
}
tr,td{
color: #424242;
border-collapse: collapse;
border: 1px solid #F0F0F0;
height: 30px;
text-align: center;
}
tr:nth-child(2n+1) {
background-color: #F7F8FC;
}
tr:hover {
background-color: #F7F8FC;
}
.online,.offline {
height: 20px;
background-color: #2ECC71;
width: 40px;
margin: 0px auto;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
color: #fff;
}
.offline {
width: 50px;
background-color: #E74C3C;
}
</style>
<body>
服务器在线状态监控
ID | Location | Address | Status |
{$i} | {$result->country}{$result->region}{$result->city} | {$server_ip_list[$key]} |
在线 |
{$i} | {$result->country}{$result->region}{$result->city} | {$server_ip_list[$key]} |
不在线
|