首頁  >  文章  >  後端開發  >  php監控日誌500、503錯誤並傳送郵件提示的程式碼

php監控日誌500、503錯誤並傳送郵件提示的程式碼

WBOY
WBOY原創
2016-07-25 08:55:541500瀏覽
  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).*(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. }

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn