call php page using ajax

Last updated on August 19th, 2022 at 09:57 am

Parse nginx | apache logs using PHP and send an email

Send an email to admin or website owner when ever the logs show 404 or any response code of your choice. This script can be used as a cron and is run on everyday basis to get all the details in your email.You can modify the code URI accordingly to get different error message details such as 404, 401, 400, 500 server errors. This will help an admin or a website owner in different ways mainly for tracking any unusual activities too. Example below is Nginx logs but you can also use Apache logs provided you know how the logs are customized.

The little snippet of PHP code below will send an email to you every time a document is requested that is no longer available or throwing an application internal server error etc., This will allow you to track where outdated links are, giving you the possibility of taking action.

I am assuming here that you are using nginx access logs.

FOR EXAMPLE:


FOR 500 ISE[Internal Server Error]
http://YOUR_WEBSITE_URL/read_logs.php?errcode=500
FOR 400 Error
http://YOUR_WEBSITE_URL/read_logs.php?errcode=400
FOR 404 Error
http://YOUR_WEBSITE_URL/read_logs.php?errcode=404

Use this script as a cron in your control panel or you can run this script through any browser. Make sure to pass error code as query string as shown below. It works by reading your access log file.

Please modify it according to your requirement. Here is the complete script. In the script below I am splitting the lines in log file and $each_word[8] is the variable I am getting the error code from the log file.

<?php
$to = "YOUR_EMAIL_ADDRESS";
$subject = "Error Document Mailer Result ";
#$headers = "From: FROM_EMAIL_ADDRESS ";
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$file = file('/var/log/nginx/access.log');
$read_rev = array_reverse($file);
$count=0;
foreach ($read_rev as $dis_line) {
$each_word = explode(" ",$dis_line);
if($each_word[8]==stripslashes($_GET['errcode']))
{
$date_split = explode(":",$each_word[3]);
$today="[".date("d/M/Y");
if($date_split[0] == $today)
$message .= "<b>Date and Time</b>: $each_word[3]]\n<br /><b>Request URL:</b> $each_word[6]\n<br /><b>Referring page:</b> $each_word[10]\n\n<br /><b>Client:</b> $each_word[11]\"\n<br /><b>Remote IP:</b><a target='_blank' href='http://www.iplocationfinder.com/$each_word[0]'>$each_word[0]</a>\n\n<hr />";
}
}
$message .= "";
$message .= "</body>";
echo "===========Message Content============";
echo $message;
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>

Make sure to modify these variables according to your requirement

$to = "Your EMAIL";
$subject = "Error Document Mailer Result ";
$headers = "From: YOUR_FROM_EMAIL";
$headers .= "Reply-To: YOUR_REPLY_TO_EMAIL ";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";

Access log file path (Make sure the script has permission to read the file)

$file = file('/var/log/nginx/access.log');

Note: This script is not smart enough to cross check whether a particular entry has been sent as an email or not (There will be duplicate entries every time you initiate the script.). That is something you may have to implement. This script just check the access.log file or any other file you give as an input and blindly parse the file for the given error code.

Leave a Reply

Your email address will not be published. Required fields are marked *