Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Parsing logs

Posted on 2003-11-18
4
276 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Could anyone can give me a suggestion about good client friendly theme. 7 29
PHP error function not working on AWS 10 70
selector:validator cookies 4 30
Ahax pagination 9 32
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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