Apache Statistic Script

2006-05-01


Apache Statistic Bash Script

Alhtough, there are multiple utilities that can parse through apache statistics and tell you everything you wanted to know under the sun, I decided to create a simple bash script to tell me a few things that I wanted to know. Basically, it was who visited my site, and how many hits did I get from their IP.

The Script

Here is the script...

#!/bin/bash

firstdate="$(head -1 logfile | awk '{print $4}' | sed 's/\[//')"
lastdate="$(tail -1 logfile | awk '{print$4}' | sed 's/\[//')"
echo "Results of Analyzing Log File"
echo ""
echo "Start Date: $(echo $firstdate | sed 's/:/ at / ')"
echo "End Date : $(echo $lastdate | sed 's/:/ at /')"
echo ""
echo "Domain 1 Statistics"
cat logfile | grep domain1 | awk '[print$1}' | sort | uniq -c | sort
echo "Domain 2 Statistics"
cat logfile | grep domain2 | awk '[print$1}' | sort | uniq -c | sort

Crontab

Now, to receive an update on my website everyday, I added to my crontab...
30 22 * * * apache_stats.sh > out.txt
31 22 * * * mail ed AT rhatbox -s 'website statistics' < out.txt

Everyday at 10:31pm I get an update mailed to me with the IP's which accessed my website and how many times their IP appeared.