How to monitor server status in PHP

Source: Internet
Author: User
This article mainly introduces how to monitor the server status in PHP, which can effectively monitor the status of the specified IP server. It is of great practical value. For more information, see

This article mainly introduces how to monitor the server status in PHP, which can effectively monitor the status of the specified IP server. It is of great practical value. For more information, see

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'
);


Then, access your file to view the current server status and automatically send the email to your configured 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.

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.