[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parsing logs

Posted on 2003-11-18
4
Medium Priority
?
280 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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 count occurrences of each item in an array.
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 …
Suggested Courses

649 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