#!/bin/bash

if [ $# -lt 1 ]; then
	echo "Usage: $0 logdir"
	exit 1
fi

LOGDIR=$1

if ! [ -d "$LOGDIR" ]; then
	echo "Not a directory: $LOGDIR"
	exit 2
fi

find $LOGDIR/ -type f -name access.log -exec grep -HE '([0-9]{7,}|[3-9]{6,})$' "{}" \; \
| grep -E '\[[0-3]?[0-9]\/[A-Z][a-z]{2}\/20[2-9][0-9]:(0[8-9]|1[0-8]):[0-5][0-9]:[0-5][0-9] [+-][0-9]{4}\]' \
| sed ':a;s/^\(\([^"]*"[^"]*"[^"]*\)*[^"]*"[^"]*\) /\1\|\|/;ta' \
| sed ':a;s/^\([^[]*\[[^]]*\) /\1\|\|/;ta' \
| sort -k12 -n -r \
| sed -E 's/\|\|/ /g'
