Skip to content

Instantly share code, notes, and snippets.

@xeromorph
Created December 19, 2011 03:25
Show Gist options
  • Select an option

  • Save xeromorph/1495247 to your computer and use it in GitHub Desktop.

Select an option

Save xeromorph/1495247 to your computer and use it in GitHub Desktop.
apache access stats [bash]
#!/bin/bash
filename='/var/log/apache2/pogoda/access.log.1'
SEDFORMAT='s/([0-9]+)\/(.+)\/([0-9]+):([0-9]+:[0-9]+:[0-9]+)/\2 \1 \4 \3/'
PREV_EPOCH=$(date +%s -d "$(head -n1 $filename|grep -P -o '(\d+/\w{3}/\d{4}(:\d{2}){3})'|sed -r -e 's/([0-9]+)\/(.+)\/([0-9]+):([0-9]+:[0-9]+:[0-9]+)/\2 \1 \4 \3/')")
COUNT=0
SUM=0
grep -P -v '\.(js|css|png|gif|jpe?g)' $filename |while read LINE; do
DATESTR=$(echo $LINE|grep -P -o '(\d+/\w{3}/\d{4}(:\d{2}){3})')
DATETIME=$(echo $DATESTR|sed -r -e 's/([0-9]+)\/(.+)\/([0-9]+):([0-9]+:[0-9]+:[0-9]+)/\2 \1 \4 \3/')
EPOCH=$(LANG=en_US.UTF-8 date +%s -d "$DATETIME")
LAPSE=$(($EPOCH - $PREV_EPOCH))
SUM=$(($SUM + $LAPSE))
let "COUNT++"
PREV_EPOCH=$EPOCH
done
echo '$SUM/$COUNT'|bc -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment