This PHP tutorial is for intermediate users and will show you how to use directories for parsing files located there. It’s useful if you want a log searching for your server. You can make a GUI, add a cronjob for it and you’re done. When a string is found it sends an e-mail to address provided using SwiftMailer. I will make a SwiftMailer tutorial at a later time.
Here goes the PHP code:
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance(“$file”)
->setFrom(array(“$from” => “Search Log Alert “))
->setTo(array(“$to” => ‘Log Searcher’))
->setBody(“$needle found at line $i in file $file”)
;
$result = $mailer->send($message);
}
function CheckNeedle($log, $log_write, $needle) {
$needle_changed = 0;
if (trim($log[0]) !== $needle) $needle_changed = 1;
fwrite($log_write,”$needle\n”);
return $needle_changed;
}
$needle_changed = CheckNeedle($log, $log_write, $needle);
//read the nr. of lines parsed in last search . 0 if new path or files are found.
function GetLines($needle_changed, $log, $file) {
$last_line = 0;
if ($needle_changed == 0) {
for($i = 0;$i < count($log); $i++){
if (strcmp($file,trim($log[$i])) == 0)
{$last_line = (int)trim($log[$i+1]);}
}
}
return $last_line;
}
I will explain the idea in a few words: Open the specified directory, search each file, if the string is found then send the e-mail. Also and the end of each search remeber the last line searched because the log file can be very big (100 gb) so we don’t want to search from the the first line of each file again.
Function CheckNeedle checks if string changed from the previous search. So if needle changed then we need to reset our search to 0.
Function GetLines reads the nr. of last line for each file.
Let’s go on:
Function SearchNeedle goes on each line of the file, starting from the line previously searched and checks if needle is found and sends the e-mail to address provided.
while (false !== ($file = readdir($dir)) ) {
if ($file != “.” && $file != “..” && $file != “search.log”) {
$last_line = GetLines($needle_changed, $log, $file);
SearchNeedle($last_line, $file, $needle, $to, $from, $log_write);
}
}
fclose($log_write);
closedir($dir);
}
}
Start($dir, $log, $needle, $needle_changed, $log_write, $to, $from);
?>
Finally I made a Start procedure to open the directory and start SearchNeedle.
As this is for intermediate users I did not explain line by line, instruction by instruction. But I will provide some links for each PHP function used in this tutorial.
strcmp: http://php.net/manual/en/function.strcmp.php
readdir: http://php.net/manual/en/function.readdir.php
strpos: http://php.net/manual/en/function.strpos.php
trim: http://php.net/manual/en/function.trim.php
The script was tested on 2-10 gb files and it’s working good. Also don’t forget to include swiftmailer in your script.
You can download it from here: http://swiftmailer.org/download.
You can use require_once directive to do that.
Leave a Reply