Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Potentially useful shell script to manage your logs

Potentially useful shell script to manage your logs
Thread Tools
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 16, 2005, 10:32 PM
 
I wanted to quickly perform queries on my apache access.log file to see how many of the visitors I was getting were going to the pages I wanted them to go. I also wanted to be able to view this at any location, and to be able to see this activity over time (as far back as a week). I decided I was going to do it in shell script, even though I've never done that before and knew next to nothing about it. So after a few hours, here it is for your convenience:

Code:
#!/bin/sh # # log_manager.sh v1.1 by itistoday # Description: An administrative utility that can be used to view relevant parts # of apache access.log files. Used with cron it can keep a trail of a fixed # number of logs to view. This can be modified for many practical purposes because # all it really does is execute external scripts and increments file numbers. # # The results of using it with cron can look something like this: # # [shell]$ ls | less # # potential-0.log # potential-1.log # potential-2.log # potential-3.log # potential-4.log # potential-5.log # services-0.log # services-1.log # services-2.log # services-3.log # services-4.log # services-5.log # ############### Config area ############### APACHE_LOG_PATH="path/to/access.log" # path to the access.log file NEW_LOG_PATH="path/to/new/logs" # path to new logs directory, no trailing slash! MAX_NUM_LOGS=51 # maximum number of new logs to keep per prefix ############# End Config area ############# if [ -z $1 ] ; then echo "Please provide a log prefix as an argument." echo "Be sure to provide the appropriate your_prefix.sh file as well" echo "in the same folder as this script." echo "It must take four arguments and look something like this:" echo "" echo "grep your_query \$1 > \$2/\$3-\$4.log" echo "" echo "The first argument passed is \$APACHE_LOG_PATH (edit this script to change)" echo "The second is \$NEW_LOG_PATH" echo "The third is the file prefix that you pass to this script" echo "The fourth is an incremental number this script passes automatically" exit fi log_prefix=$1 if [ ! -f ${log_prefix}.sh ] ; then echo "Error: ${log_prefix}.sh does not exist." echo "Please create it with the correct format." echo "" echo "Hint: to see an example run this script without arguments" exit fi num_files=`ls ${NEW_LOG_PATH}/${log_prefix}* 2>/dev/null | wc | awk '{print $1}'` sh ${log_prefix}.sh $APACHE_LOG_PATH $NEW_LOG_PATH $log_prefix $num_files if [ $num_files -ge $MAX_NUM_LOGS ] ; then # get oldest file and delete it, the rename the rest to one less oldest=`ls ${NEW_LOG_PATH}/${log_prefix}* | head -1` rm $oldest list_of_files=`ls ${NEW_LOG_PATH}/${log_prefix}*` i=0 for ff in $list_of_files ; do new_name="${NEW_LOG_PATH}/${log_prefix}-${i}.log" if [ -f $new_name ] ; then rm $new_name fi mv $ff $new_name i=$(( $i + 1 )) done fi
(Sorry about the lack of tabs, it's this board's fault)

It takes one argument, a "prefix". Here's an example:

Say you wanted to see all the people that accessed a certain web page, but only cared about the Mac users and didn't want to see yourself in the logs. You would make a file called macfilter.sh that contained this (where xxx.xxx.xxx.xxx is your IP address):
Code:
grep the_page.html $1 | grep Macintosh | grep -v xxx.xxx.xxx.xxx > $2/$3-$4.log
Make sure this file is in the same directory as this script. Edit the script config values to your liking, and then just run:
Code:
sh log_manager.sh macfilter
or make it executable to just do:
Code:
./log_manager.sh macfilter
You can then set this up to be executed by cron every 30 minutes, and presto! You've got a whole history of the visitors to this page, and if you have it put them in a folder for a website you can view it wherever you are. Sure it's crude, but it's also quick and dirty and gets the job done!

It was a great learning experience and hopefully someone other than me will find it useful
(Last edited by itistoday; Jul 16, 2005 at 11:49 PM. )
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 17, 2005, 02:39 PM
 
Alternatively you could also just run this command...
Code:
tail -f /path/to/apache.log | grep page.html >> path/to/new.log
and run it in the background. Heh... That'd probably be better... But it wouldn't work if your server automatically moves logs, archives them and then deletes them.
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 17, 2005, 02:57 PM
 
Ok, in addendum to my previous post, you can actually do this with tail. There are two versions of it, one for linux, and one for OS X. Linux version first:
Code:
tail --follow=name --retry path/to/apache.log | grep page.html >> path/to/new.log
On linux tail will output error messages when the file is deleted or renamed, if you don't want to see those run this:
Code:
tail --follow=name --retry path/to/apache.log 2>/dev/null | grep page.html >> path/to/new.log
And on OS X you use this argument to tail:
Code:
tail -F path/to/apache.log | grep page.html >> path/to/new.log
Cool stuff.
(Last edited by itistoday; Jul 17, 2005 at 03:10 PM. )
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:21 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2