>  기사  >  백엔드 개발  >  php监控日志500、503错误并发送邮件提示的代码

php监控日志500、503错误并发送邮件提示的代码

WBOY
WBOY원래의
2016-07-25 08:55:541465검색
  1. /**

  2. * 监控日志 500 503错误
  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).*(\s500\s|\s503\s)/";//这里自己修改
  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. }
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.