Skip to content

Instantly share code, notes, and snippets.

@thomaswitt
Created November 6, 2013 10:35
Show Gist options
  • Select an option

  • Save thomaswitt/7333993 to your computer and use it in GitHub Desktop.

Select an option

Save thomaswitt/7333993 to your computer and use it in GitHub Desktop.
Parse Apache Access Log for stats
#!/bin/sh
# Or try awk '{sum+=$10} END {print sum/1048576}'
#export LC_ALL=C
#env LC_ALL=en_US sort
unset LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME
bytes_in_mb=1048576
bytes_in_gb=1073741824
scriptbc() {
bc -q -l << EOF
scale=2
$*
quit
EOF
}
nicenumber() {
integer=$(echo $1 | cut -d. -f1)
decimal=$(echo $1 | cut -d. -f2)
if [ $decimal != $1 ]; then
result=",$decimal"
fi
thousands=$integer
while [ $thousands -gt 999 ]; do
remainder=$(($thousands % 1000))
while [ ${#remainder} -lt 3 ] ; do
remainder="0$remainder"
done
thousands=$(($thousands / 1000))
result=".${remainder}${result}"
done
nicenum="${thousands}${result}"
echo $nicenum
}
if [ $# -eq 0 -o ! -f "$1" ] ; then
echo "Usage: $(basename $0) logfile" >&2
exit 1
fi
firstdate="$(head -1 "$1" | awk '{print $4}' | sed 's/\[//')"
lastdate="$(tail -1 "$1" | awk '{print $4}' | sed 's/\[//')"
echo "Results of analyzing log file $1"
echo ""
echo " Start date: $(echo $firstdate|sed 's/:/ at /')"
echo " End date: $(echo $lastdate|sed 's/:/ at /')"
hits="$(wc -l < "$1" | sed 's/[^[:digit:]]//g')"
echo " Hits: $(nicenumber $hits) (total accesses)"
totalbytes="$(awk '{sum+=$10} END {print sum}' "$1")"
echo -n " Transferred: $(nicenumber $totalbytes) bytes "
if [ $totalbytes -gt $bytes_in_gb ] ; then
echo "($(nicenumber $(scriptbc $totalbytes / $bytes_in_gb)) GB)"
elif [ $totalbytes -gt $bytes_in_mb ] ; then
echo "($(scriptbc $totalbytes / $bytes_in_mb) MB)"
else
echo ""
fi
echo ""
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment