The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. October 14, 2019, 06:22:09 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Parse apache log files from bash  (Read 4217 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: April 26, 2007, 11:49:45 PM »

I put the following code in a file called getref and call it from another script with "getref 192.168.0.1" it then searches the logs for "yesterday" for a couple of websites to see if there's any evidence that the lead came to us through an adwords ad.

Code:
#!/bin/bash

if [ ! $2 ]; then
  LINES=5
else
  LINES=$2
fi

YESTERDAY=`date -d yesterday +%d/%b/%Y`
ESCAPEDIP=`echo $1|sed -e "s/\./\\\\\./g"`
REGEX="$ESCAPEDIP".*$YESTERDAY

grep -q -E "$REGEX" /var/log/httpd/access_log

if [ $? == 0 ]; then
        echo
        echo "TLA Logfile match"
        echo
        RESULT=`grep -m 5 -E "$REGEX" /var/log/httpd/access_log`
        echo "$RESULT"|grep -q "\?kw="
        if [ $? == 0 ]; then
                echo Probable Adwords Lead
                echo
        fi
        echo "$RESULT"
        echo
fi

grep -q -E "$REGEX" /var/log/httpd/la-access_log

if [ $? == 0 ]; then
        echo
        echo "LA Logfile match"
        echo
        RESULT=`grep -m 5 -E "$REGEX" /var/log/httpd/la-access_log`
        echo "$RESULT"|grep -q "\?kw="
        if [ $? == 0 ]; then
                echo Probable Adwords Lead
                echo
        fi
        echo "$RESULT"
        echo
fi

if [ -z "$RESULT" ]; then

        grep -q -E "$REGEX" /var/log/httpd/access_log.1

        if [ $? == 0 ]; then
                echo
                echo "TLA Logfile match"
                echo
                RESULT=`grep -m 5 -E "$REGEX" /var/log/httpd/access_log.1`
                echo "$RESULT"|grep -q "\?kw="
                if [ $? == 0 ]; then
                        echo Probable Adwords Lead
                        echo
                fi
                echo "$RESULT"
                echo
        fi

        grep -q -E "$REGEX" /var/log/httpd/la-access_log.1

        if [ $? == 0 ]; then
                echo
                echo "LA Logfile match"
                echo
                RESULT=`grep -m 5 -E "$REGEX" /var/log/httpd/la-access_log.1`
                echo "$RESULT"|grep -q "\?kw="
                if [ $? == 0 ]; then
                        echo Probable Adwords Lead
                        echo
                fi
                echo "$RESULT"
                echo
        fi

        if [ -z "$RESULT" ]; then
                echo Not Found!
        fi

fi

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 27, 2007, 11:10:01 AM »

Nicely done ... particularly since I am not much of a shell scripter and this is easy reading to expand the skillz.

Danke!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!