Home >Backend Development >PHP Tutorial >PHP method to implement server status monitoring, PHP to implement server monitoring_PHP tutorial

PHP method to implement server status monitoring, PHP to implement server monitoring_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:081087browse

PHP implements server status monitoring method, PHP implements server monitoring

The example in this article describes how PHP implements server status monitoring. Share it with everyone for your reference. The specific analysis is as follows:

Many friends have never done PHP server status monitoring. They only know when the website is down. If the website is shut down in the middle of the night, you don’t know the situation. It is also very bad for the website. So I have taken the time these two days to do this. I wrote a web server status monitoring tool. I saw some friends said they needed it, so I released it. Very simple stuff.

How to use:

Open the status.php file in the compressed package. Edit the content here to your own email information. The code is as follows:

Copy code The code is as follows:
$mail->Host = 'smtp.exmail.qq.com'; // SMTP server
$mail->Port = 25; // SMTP server port number
$mail->Username = 'admin@xxx.com'; // SMTP server username
$mail->Password = 'password'; // SMTP server password
$mail->SetFrom('admin@xxx.com','Status');
$mail->AddReplyTo('admin@xxx.com','Status');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
$mail->MsgHTML($body);
$address = 'admin@admin.com'; //Receive email
//Change the content here to the IP you want to monitor:
$server_ip_list = array(
'61.135.169.121',
'221.204.173.200',
'173.194.127.83'
);

Then visit your http://yourdomain.com/status.php file to see the current server status and automatically send emails to the email address you set. If you need automatic monitoring, please add a Cron task or use some kind of monitoring tool!
The complete code is as follows:
Copy code The code is as follows:
/*
* Server status monitoring
*/
header('Content-type:text/html;charset=utf-8');
include './smtp/class.smtp.php';
include './smtp/class.phpmailer.php';
function sendmail($subject = '',$body = '') {
Date_default_timezone_set('Asia/Shanghai');//Set the time zone Dongba District
$mail = new PHPMailer(); //A new PHPMailer object comes out
                                                                                                                                                                                                                                      $mail->CharSet ="UTF-8";//Set the mail encoding, the default is ISO-8859-1, if you send Chinese, this must be set, otherwise the code will be garbled
$mail->IsSMTP(); // Set to use SMTP service
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Host = 'smtp.exmail.qq.com'; // SMTP server
$mail->Port = 25; // The port number of the SMTP server
$mail->Username = 'admin@xxx.com'; // SMTP server username
$mail->Password = 'password'; // SMTP server password
$mail->SetFrom('admin@xxx.com','Status');
$mail->AddReplyTo('admin@xxx.com','Status');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
$mail->MsgHTML($body);
$address = 'admin@admin.com'; //Receive email
$mail->AddAddress($address, '');
//$mail->AddAttachment("images/phpmailer.gif"); // attachment Attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
If(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
// echo "Message sent! Congratulations, the email was sent successfully!";
}
}
//check server status
function checkServerSatatus($ip) {
$str = null;
$fp = @fsockopen($ip,80,$errno,$errstr,10);
if (!$fp) {
return false;
} else {
fclose($fp);
return true;
}
}
$server_ip_list = array(
'61.135.169.121',
'221.204.173.200',
'173.194.127.83'
);
?>




Server status monitoring



 

 

服务器在线状态监控


 

 

 
  
   
         $i = 0;
     foreach ($server_ip_list as $key => $val) {
      $api = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$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);
      }
     }
     ?>
  
 
IDLocationAddressStatus
{$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程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/924537.htmlTechArticlePHP实现服务器状态监控的方法,php实现服务器监控 本文实例讲述了PHP实现服务器状态监控的方法。分享给大家供大家参考。具体分析如下:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn