Home  >  Article  >  Backend Development  >  PHP monitors log 500, 503 errors and sends email prompt code

PHP monitors log 500, 503 errors and sends email prompt code

WBOY
WBOYOriginal
2016-07-25 08:55:541519browse
  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 邮箱用户名
  16. $mail->Password = "xxxxxxxxx"; // SMTP account password 密码
  17. $mail->SetFrom('xxxxxxx@126.com', '报错');//

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

  19. try{

  20. $log = file("F:/iis-log/W3SVC2/".$now);//日志路径自己改
  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).*(s500s|s503s)/";//这里自己修改
  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. }

复制代码


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