Code for php monitoring log 500 and 503 errors and sending email prompts
/**
- * Monitoring log 500 503 error
- * By bbs.it-home.org
- */
- Include ("PHPMailer/class. phpmailer. php ");
- // Error_reporting (0 );
- $ Mail = new PHPMailer ();
- $ Mail-> IsSMTP (); // telling the class to use SMTP
- $ Mail-> SMTPDebug = 1; // enables SMTP debug information (for testing)
- // 1 = errors and messages
- // 2 = messages only
- $ Mail-> SMTPAuth = true; // enable SMTP authentication
- $ Mail-> Host = "smtp.126.com"; // sets the SMTP server
- $ Mail-> Port = 25; // set the SMTP port for the GMAIL server
- $ Mail-> Username = "xxx@126.com"; // SMTP account username mailbox Username
- $ Mail-> Password = "xxxxxxxxx"; // SMTP account password
- $ Mail-> SetFrom ('xxxxxxx @ 126.com ', 'error ');//
$ Now = "u_ex". date ("ymdH"). ". log ";
Try {
- $ Log = file ("F:/iis-log/W3SVC2/". $ now); // modify the log path
- } Catch (Exception $ e ){
- Echo "no file ";
- Exit;
- }
$ Error = array ();
- $ Start = date ("I", time ()-120 );
- $ End = date ("I ");
- If ($ start> $ end ){
- Exit;
- }
- $ Aa = range ($ start, $ end );
- $ Code = mkstr ($ aa );
- Echo $ code;
- $ Pattern = "/($ code). * (\ s500 \ s | \ s503 \ s)/"; // modify it here
- $ I = 0;
- Foreach ($ log as $ k =>$ v ){
- If (preg_match ($ pattern, $ v )){
- $ Error [] = $ v;
- Echo $ v;
- $ I + = 1;
- }
- }
- If (! Empty ($ error )){
- $ Content = implode ("
", $ Error );
- $ Mail-> Subject = "$ I errors ";
- $ Mail-> AddAddress ("xxxxxxxxxx@163.com ");
- $ Mail-> AddAddress ("xxxxxxxx@126.com ");
- $ Mail-> MsgHTML ($ content );
- $ Mail-> send ();
- }
- Echo "\ nover ";
- Function mkstr ($ arr ){
- $ Code = '';
- Foreach ($ arr as $ k => $ e ){
- $ Code. = "\: $ e \: | ";
- }
- $ Code = rtrim ($ code, "| ");
- Return $ code;
- }
|