Solved

Parsing logs

Posted on 2003-11-18
4
273 Views
Last Modified: 2006-11-17
I'm in the middle of making a PHP script that will parse FTP logs and then add the unique connects, etc into my MySQL DB.

Let's say I have a FTP_logs directory, and the files are labeled as such (also look in subfolders to find these files)...

11072003.log
11082003.log
11092003.log

How would I tell PHP to loop through all those logs, and once PHP has opened the file, find a string like the following:

L 11/16/2003 - 12:07:26: "USERNAME<164><USERNAME_VALIDATING><>" connected, address "209.128.68.226:2767"

Get the address...in my case the IP address of the remote connection, and store it into $connectors_ip, in this case, 209.128.68.205, not the 2767. Also get the USERNAME and store it into $connectors_username.

Then, look for the following through the file,

L 11/16/2003 - 12:07:34: <USERID_0:0:347764><>" USERID validated

Where I need to just get the value after USERID_, in this case 0:0:347764, store it into $connectors_USERID.

Thanks in advance.
0
Comment
Question by:drakkarnoir
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Fendrin
ID: 9781554
It seems to me that you need to use regular expressions. Here is a basic algorithm:

1) Open the file (fopen)
2) get the contents as a string (fread)
3) close the file (fclose)
4) get the first string (ereg or preg_match)
5) get the IP address from the first string (ereg or preg_match)
6) get the Username from the first string (ereg or preg_match)
7) get the second string (ereg or preg_match)
8) get the userid from the second string (ereg or preg_match)

repeat this for each file.
0
 

Author Comment

by:drakkarnoir
ID: 9782182
But how would I tell PHP to go through each file? Even in the sub-directories?

Something like the following: ?

while(dir(EOF();)) ?
0
 
LVL 4

Accepted Solution

by:
bljak earned 500 total points
ID: 9785798
No, you would list all files in directory add them to some kind of array, and loop through array, something like this

function file_list($dir,$extension)
{
  if(is_dir($dir))
  {
    $fd = @opendir($dir);
    while(($part = @readdir($fd)))
    {
      clearstatcache();
      if ($part != "." && $part != ".." && !is_dir($dir."/".$part) && eregi("\.".$extension."$",$part))
      {
        $files[] = $part;
      }
    }
  if($fd)
  {
    closedir($fd);
  }
  if(is_array($files))
  {
    asort($files);
    return $files;
  } else {
    return false;
  }
  } else {
    return false;
  }
}

This should return array, so if you do

$logs = file_list("/my/path", "log");

you should have $log array with all your file names in it, and then you can do what you want
as for the subdirectories, you could add check somewhere in function after if check
elseif($part != "." && $part != ".." && is_dir($dir."/".$part))
{
  file_list($dir."/".$part,$extension)
}

so long
bljak
0
 

Author Comment

by:drakkarnoir
ID: 9806899
Erm so how would I tell PHP to loop through all the files in just a particular directory and parsing their information...?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now