php監控日誌500、503錯誤並發送郵件提示的代碼

來源:互聯網
上載者:User
  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. }

複製代碼
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.