How to parse server logs and send it back to server as a request in PERL?

This is an example of my server logs.

"" "NULL-AUTH-USER" "29/Sep/2011:04:48:32 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:04:53:32 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:04:58:34 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:05:03:40 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:05:05:17 -0500" "GET /srcsearch/ HTTP/1.1" 200 12931
"" "NULL-AUTH-USER" "29/Sep/2011:05:05:17 -0500" "GET /favicon.ico HTTP/1.1" 404 1022
"" "NULL-AUTH-USER" "29/Sep/2011:05:05:17 -0500" "GET /favicon.ico HTTP/1.1" 404 1022
"" "NULL-AUTH-USER" "29/Sep/2011:05:05:17 -0500" "GET /favicon.ico HTTP/1.1" 404 1022
"" "NULL-AUTH-USER" "29/Sep/2011:05:05:23 -0500" "GET /srcsearch/ HTTP/1.1" 200 1232330
"" "NULL-AUTH-USER" "29/Sep/2011:05:05:54 -0500" "GET /srcsearch/ HTTP/1.1" 200 16379
"" "NULL-AUTH-USER" "29/Sep/2011:05:06:35 -0500" "GET /srcsearch/ HTTP/1.1" 200 12851
"" "NULL-AUTH-USER" "29/Sep/2011:05:08:41 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:05:12:11 -0500" "GET /srcsearch/ HTTP/1.1" 200 23033
"" "NULL-AUTH-USER" "29/Sep/2011:05:12:11 -0500" "GET /favicon.ico HTTP/1.1" 404 1022
"" "NULL-AUTH-USER" "29/Sep/2011:05:13:42 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:05:18:37 -0500" "GET /srcsearch/ HTTP/1.1" 200 11271
"" "NULL-AUTH-USER" "29/Sep/2011:05:18:49 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554
"" "NULL-AUTH-USER" "29/Sep/2011:05:18:58 -0500" "GET /srcsearch/ HTTP/1.1" 200 1140275
"" "NULL-AUTH-USER" "29/Sep/2011:05:19:06 -0500" "GET /srcsearch/ HTTP/1.1" 200 0

Open in new window

I want to write a parser that will extract only the part that is after GET till the first space character. Example from the first one is:
"" "NULL-AUTH-USER" "29/Sep/2011:04:48:32 -0500" "GET /srcsearch/ HTTP/1.0" 200 27554

Open in new window

Extracted part:


Open in new window

and the time stamp for the first one can be assumed as time=0

Then the others will be with respect to it. Which means:

The time stamp for the first request is :


Open in new window

The time stamp for the second request is :


Open in new window

So the delta is exactly 5 minutes. We will use this delta while sending request to the server.

Then append this URL to something like: 

Open in new window

Once I get this data I want to send these URLs to the server with the time difference which we calculated.

NOTE: As far as I know, CURL can be used for this purpose. I work on UNIX environment.

Or if there is an easier way to do it rather than CURL, that would be fine too.

Let me summarize it as a sudo code:

For each line that starts with ""  (x is a number and it can be anything. and its format may change like xxx.x.xxxxx.xx) extract the time stamp and find the difference between the previous one.

If it is the first one then assign the current time or zero to it.

Then extract the part which is right after GET till the next space. But don't get the space before and after the extracted part. 

then append this URL to something like: 

and send this entire thing to the server.

Open in new window

As a full short example:

send this request to the server

Open in new window

wait for 5 minutes

(because there is a 5 minute difference between the first two requests)

then send this one

Open in new window

and the process continues till the end of the file
Who is Participating?
parparovConnect With a Mentor Commented:
Assuming a 'Yes' on the previous Q, here is the code.
You will need to install LWP::Simple perl module if you haven't it already.


use strict;
use warnings;

use LWP::Simple;
use POSIX qw(mktime);

my $file;
$file = shift || die "Can't read file $file: $!";

open(F, $file);
my $BASE_URL = "";
my %MONTHS = qw(
	Jan 0 Feb 1 Mar 2 Apr 3 May 4 Jun 5 Jul 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11

sub get_timestamp ($$$$$$) {

	my ($sec, $min, $hou, $day, $mon, $yea) = @_;
	$yea -= 1900;
	$mon = $MONTHS{$mon};
	return mktime($sec, $min, $hou, $day, $mon, $yea, 0, 0);
my $ts0 = 0;

while (<F>) {
	if (/^\"\w+\.\w+\.\w+\.\w+\"\s+.*\s+\"(\d+)\/(\w{3})\/(\d+):(\d+):(\d+):(\d+)\s+.*\s+\"GET (\S+)/) {
#		print;
		my $day = $1;
		my $mon = $2;
		my $yea = $3;
		my $hou = $4;
		my $min = $5;
		my $sec = $6;
		my $url = $7;
		my $ts1 = get_timestamp($sec, $min, $hou, $day, $mon, $yea);
		my $to_sleep = $ts0 ? $ts1 - $ts0 : 0;
		print "Sleeping: $to_sleep\n";
		$ts0 = $ts1;
		print "At ", scalar(localtime), " get(\"$BASE_URL$url\");\n";

Open in new window

If the delta is zero, should the refetch be done at once?

TolgarAuthor Commented:
@parparov: my answer to your first question is yes. and thank you for your quick and prompt response.

I will try your code.

TolgarAuthor Commented:
I haven't checked it yet but let's say I don't have LWP::Simple module

But let's say  I found it and download it to my computer. Then how can I direct my Perl code to see this module?

Because, the perl is not locally installed on my machine.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.