Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php监控日志500、503错误并发送邮件提示的代码

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

WBOY
WBOYasal
2016-07-25 08:55:541499semak imbas
  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. }
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn