• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP实现服务器状态监控的方法_php技巧

php 搞代码 3年前 (2022-01-26) 44次浏览 已收录 0个评论

本文实例讲述了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>

服务器在线状态监控

<?php
$i = 0;
foreach ($server_ip_list as $key => $val) {
$api = file_get_contents(‘http://ip.taobao.com/service/getIpInfo.php?ip=&#8217;.$server_ip_list[$key].”);
$json = json_decode($api);
$result = $json->data;
$i++;
if (checkServerSatatus($server_ip_list[$key])) {
echo “

“;
} else {
echo “

“;
$subject = “您的服务器 {$server_ip_list[$key]} 无法访问!”;
$body = “您的服务器{$server_ip_list[$key]} 无法访问,此邮件根据你设置的监控频率发送,当服务器恢复正常邮件自动停止发送!”;
sendmail($subject,$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]}
不在线

注意:
include ‘./smtp/class.smtp.php’;
include ‘./smtp/class.phpmailer.php’;
文件可以下载phpmailer包然后我们在包里面这两个文件复制出来然后即可使用了。

ps:这个只是一个非常的简单的不能很好的监控到服务器了,现在有很多成熟的免费产品都可以更好的达到我们要求,如dnspod里面有一个D监控了,然后我们就可以操作。

希望本文所述对大家的PHP程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP实现服务器状态监控的方法_php技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址