Solved

Parsing logs

Posted on 2003-11-18
4
278 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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