Important alert: (current site time 7/15/2013 8:57:24 PM EDT)
 

VB icon

Apache log search

Email
Submitted on: 7/10/2003 1:17:48 PM
By: Moth7 
Level: Intermediate
User Rating: By 2 Users
Compatibility: 5.0 (all versions), Active Perl specific
Views: 8506
 
     This code searches your apache access.log file (you need to alter the path in the code) based on criteria that you supply as STDIN (not args).
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Apache log search
= Description:This code searches your apache access.log file (you need to alter the path in the code) based on criteria that you supply as STDIN (not args).
= By: Moth7
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=487&lngWId=6=for details.=**************************************

#C:\Perl\Bin\Perl.exe 
# 
#Apache Log Manager 
# 
print "Apache Log Manager\n"; 
print "ds \[x\] - Find logs related to date \[x\] \n"; 
print "is \[x\] - Find logs related to ip \[x\]\n"; 
print "rs \[x\] - Find logs related to request \[x\]\n"; 
print "ss \[x\] - Find logs with string \[x\] in them\n"; 
&getui; 
sub getui 
{ 
print "Search type:"; 
$stype = <STDIN>; 
print "Search criteria:"; 
$scrit = <STDIN>; 
chop $stype; 
chop $scrit; 
DoSearch($stype,$scrit); 
} 
sub DoSearch 
{ 
$sc = substr($_[0],0,2); 
print "$sc\n"; 
if ($sc eq 'ds') 
{ 
 datesearch($_[1]);#Search for hits on specified Date 
} 
elsif ($sc eq 'is') 
{ 
 ipsearch($_[1]);#Search for hits by specified IP address 
} 
elsif ($sc eq 'rs') 
{ 
 requestsearch($_[1]);#Search for hits using specified request 
} 
else 
{ 
 stringsearch($_[1]);#No search defined-Generic search for specified string 
} 
} 
sub datesearch 
{ 
$sc = $_[0]; 
$count = 0; 
open (LOG,'C:\webserv\apache\logs\access.log'); #Replace with your log path 
@log = <LOG>; 
close(LOG); 
foreach $entry(@log) 
{ 
 $dbegin = index($entry,'['); 
 $dend = $dbegin + 12; 
 $data = substr($entry,$dbegin + 1,$dend); 
 if ($data =~ /$sc/) 
 { 
 chop $entry; 
 print "$entry\n"; 
 $count = $count +1; 
 } 
} 
print "$count entries\n"; 
} 
sub ipsearch 
{ 
$sc = $_[0]; 
$count = 0; 
open (LOG,'C:\webserv\apache\logs\access.log'); #Replace with your log path 
@log = <LOG>; 
close(LOG); 
foreach $entry(@log) 
{ 
 $iend = index($entry,' '); 
 $data = substr($entry,0,$dend -1); 
 if ($data =~ /$sc/) 
 { 
 chop $entry; 
 print "$entry\n"; 
 $count = $count +1; 
 } 
} 
print "$count entries\n"; 
} 
sub requestsearch 
{ 
$sc = $_[0]; 
$count = 0; 
open (LOG,'C:\webserv\apache\logs\access.log'); #Replace with your log path 
@log = <LOG>; 
close(LOG); 
foreach $entry(@log) 
{ 
 $rbegin = index($entry,"\""); 
 $rend = rindex($entry,"\" "); 
 $data = substr($entry,$dbegin + 1,$dend -1); 
 if ($data =~ /$sc/) 
 { 
 chop $entry; 
 print "$entry\n"; 
 $count = $count +1; 
 } 
} 
print "$count entries\n"; 
} 
sub stringsearch 
{ 
$sc = $_[0]; 
$count = 0; 
open (LOG,'C:\webserv\apache\logs\access.log'); #Replace with your log path 
@log = <LOG>; 
close(LOG); 
foreach $entry(@log) 
{ 
 if ($entry =~ /$sc/) 
 { 
 chop $entry; 
 print "$entry\n"; 
 $count = $count +1; 
 }
} 
print "$count entries\n"; 
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.