PHP implements server status monitoring, php implements server monitoring _ PHP Tutorial

Source: Internet
Author: User
PHP implements server status monitoring, and php implements server monitoring. PHP implements server status monitoring. php implements server monitoring. This article describes how PHP implements server status monitoring. Share it with you for your reference. The specific analysis is as follows: PHP implements server status monitoring and php implements server monitoring.

This example describes how to monitor the server status in PHP. Share it with you for your reference. The specific analysis is as follows:

The PHP Server status monitoring has not been done by many friends. only when the website is suspended can we know that such a website is shut down in the middle of the night and is very bad for the website, for this reason, I wrote a web server status monitoring report in the past two days. if you see a friend who says you need it, I will release it. Very simple.

Usage:

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

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 '; // receiving email address
// Modify the content here for the IP address you want to monitor:
$ Server_ip_list = array (
'61. 135.169.121 ',
'1970. 204.173.200 ',
'194.127.83'
);


Visit your http://yourdomain.com/status.php file to view the current server status and automatically send the mail to your mailbox. If you need automatic monitoring, add a Cron task or use a monitoring site!
The complete code is as follows:

The code is as follows:

<? Php
/*
* 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 UTC + 8.
$ Mail = new PHPMailer (); // A new PHPMailer object is displayed.
// $ Body = eregi_replace ("[]", '', $ body); // filter the email content.
$ Mail-> CharSet = "UTF-8"; // Set the mail encoding, the default ISO-8859-1, this must be set if you send Chinese, otherwise garbled
$ Mail-> IsSMTP (); // you can specify the SMTP service.
$ Mail-> SMTPAuth = true; // enable SMTP verification
$ 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 '; // receiving email address
$ Mail-> AddAddress ($ address ,'');
// $ Mail-> AddAttachment ("images/phpmailer.gif"); // attachment
// $ Mail-> AddAttachment ("images/phpmailer_mini.gif"); // attachment
If (! $ Mail-> Send ()){
Echo 'mailer Error: '. $ mail-> ErrorInfo;
} Else {
// Echo "Message sent! Congratulations, the email is 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 ',
'1970. 204.173.200 ',
'194.127.83'
);
?>




Server status monitoring




Server online status monitoring























<? Php$ 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 = "your server {$ server_ip_list [$ key]} cannot be accessed! ";$ Body = "your server {$ server_ip_list [$ key]} cannot be accessed. This email is sent based on the monitoring frequency you set. when the server returns to normal, it will automatically stop sending emails! ";Sendmail ($ subject, $ body );}}?>
ID Location Address Status
{$ I} {$ Result-> country} {$ result-> region} {$ result-> city} {$ Server_ip_list [$ key]}

Online

{$ I} {$ Result-> country} {$ result-> region} {$ result-> city} {$ Server_ip_list [$ key]}

Not online






Note:
Include './smtp/class. smtp. php ';
Include './smtp/class. phpmailer. php ';
You can download the phpmailer package and copy the two files in the package to use it.

Ps: This is just a very simple one that cannot be well monitored to the server. now there are many mature free products that can better meet our requirements, for example, there is a D monitoring in dnspod, and then we can operate it.

I hope this article will help you with PHP programming.

The following example describes how to monitor the server status in PHP. Share it with you for your reference. The specific analysis is as follows :...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.