Code for php monitoring log 500 and 503 errors and sending email prompts

Source: Internet
Author: User
Code for php monitoring log 500 and 503 errors and sending email prompts

  1. /**

  2. * Monitoring log 500 503 error
  3. * By bbs.it-home.org
  4. */
  5. Include ("PHPMailer/class. phpmailer. php ");
  6. // Error_reporting (0 );
  7. $ Mail = new PHPMailer ();
  8. $ Mail-> IsSMTP (); // telling the class to use SMTP
  9. $ Mail-> SMTPDebug = 1; // enables SMTP debug information (for testing)
  10. // 1 = errors and messages
  11. // 2 = messages only
  12. $ Mail-> SMTPAuth = true; // enable SMTP authentication
  13. $ Mail-> Host = "smtp.126.com"; // sets the SMTP server
  14. $ Mail-> Port = 25; // set the SMTP port for the GMAIL server
  15. $ Mail-> Username = "xxx@126.com"; // SMTP account username mailbox Username
  16. $ Mail-> Password = "xxxxxxxxx"; // SMTP account password
  17. $ Mail-> SetFrom ('xxxxxxx @ 126.com ', 'error ');//

  18. $ Now = "u_ex". date ("ymdH"). ". log ";

  19. Try {

  20. $ Log = file ("F:/iis-log/W3SVC2/". $ now); // modify the log path
  21. } Catch (Exception $ e ){
  22. Echo "no file ";
  23. Exit;
  24. }

  25. $ Error = array ();

  26. $ Start = date ("I", time ()-120 );
  27. $ End = date ("I ");
  28. If ($ start> $ end ){
  29. Exit;
  30. }
  31. $ Aa = range ($ start, $ end );
  32. $ Code = mkstr ($ aa );
  33. Echo $ code;
  34. $ Pattern = "/($ code). * (\ s500 \ s | \ s503 \ s)/"; // modify it here
  35. $ I = 0;
  36. Foreach ($ log as $ k =>$ v ){
  37. If (preg_match ($ pattern, $ v )){
  38. $ Error [] = $ v;
  39. Echo $ v;
  40. $ I + = 1;
  41. }
  42. }
  43. If (! Empty ($ error )){
  44. $ Content = implode ("
    ", $ Error );
  45. $ Mail-> Subject = "$ I errors ";
  46. $ Mail-> AddAddress ("xxxxxxxxxx@163.com ");
  47. $ Mail-> AddAddress ("xxxxxxxx@126.com ");
  48. $ Mail-> MsgHTML ($ content );
  49. $ Mail-> send ();
  50. }
  51. Echo "\ nover ";
  52. Function mkstr ($ arr ){
  53. $ Code = '';
  54. Foreach ($ arr as $ k => $ e ){
  55. $ Code. = "\: $ e \: | ";
  56. }
  57. $ Code = rtrim ($ code, "| ");
  58. Return $ code;
  59. }

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.