#!/bin/sh # # hitcount.cgi # WARNING: Depending on the log size, it can take time and cause other side effects. # DO NOT USE unless you know what you are doing with this. # Put it in the protected area. # # This script shows total hit counts of the past hour, simply using grep. # # Optionally, you can specify the hour to check in two styles (GET method). # http://yourdomain.com/protected/hitcount.cgi?14 # or # http://yourdomain.com/protected/hitcount.cgi?t=14 # This will check total hit from 14:00 to 14:59. # # http://yourdomain.com/protected/hitcount.cgi?-23 # or # http://yourdomain.com/protected/hitcount.cgi?t=-23 # This will check total hit of 23 hours ago. # It will work up to the number resulting 00:00 of yesterday's log. # Even if you input greter negative number, it will show 00:00 of yesterday. echo -e "Content-type: text/html\n\n" main (){ # Edit tags to show in any color, format you like. echo "
"
U=`id -u`
U=$(($U-500))
NOW=`date "+%Y:%H:%M"`
Y=${NOW%%:*}
HM=${NOW#*:}
H=${HM%:*}
M=${NOW##*:}
echo "Current time is $HM PST/PDT"
case "$1" in ));; '')ARG="${QUERY_STRING#t=}";;*)ARG="$1";;esac
#echo "ARG=$ARG"
PAST=''
YD='today '
case "$ARG" in
[0-9]) H="0$ARG" ;;
[01][0-9]|2[0-3]) H="$ARG" ;;
-[0-9]|-[0-4][0-9]) X='H=$(($H $ARG))';eval "$X" ;;
*) H=$(($H-1)) ;;
esac
case "$H" in ));; -*) PAST='.0';YD='yesterday ';H=$((24 + $H));;esac
case "$H" in ));;[1-9])H="0$H";;*[!0-9]*)H='00';;esac
#echo "H=$H , PAST=$PAST"
LOG="/log/access$PAST/${U%${U#?}}/${U}_access_log"
#echo "$LOG"
echo -n "Total hits for the hour $H $YD($H:00 to $H:59) = "
grep -c -E '^[^/]{17,45}/.../'"$Y:$H" "$LOG"
#echo "grep -c -E '^[^/]{17,45}/.../'$Y:$H $LOG"
}
allday () {
echo -n "Total hit of $YD(00:00 to $HM) = "
N=`wc -l $LOG`
N=${N#* }
echo ${N%%/*}
#wc -l $LOG
}
main 2>&1
# If you don't have a huge log file, and want to know the total hit of today,
# remove the '#' from the line bellow.
# allday 2>&1
# end