########################################################### # pu.sh - list or count portal users for the last X minutes # J. David Schronce - Tue Jan 1 01:24:35 CST 2008 ########################################################### # BACKTIME - how many minutes into the past to look for # portal users - maximum 59 # # An attempt is made here to standardize the web server # log directories. On my servers they have a consistant # beginning, which I call LOGDIR, followed by the Environment # name, and ending with "access.log" - which I call # LOGEXT. Hopefully you can find similarities for your # log files. # ########################################################### BACKTIME=20 LOGDIR=/apps/ihs/*/logs LOGEXT=access.log ########################################################### # End of User Defined Variables ########################################################### USAGE="\nUSAGE: $0 <-c | -u > [HH] [MM]\n -c: User Count\n -u: User List\n HH and MM are optional\n \n\n Examples:\n $0 -u prod will show all the users using PROD env in the last $BACKTIME minutes\n where $BACKTIME is the number of minutes defined in the BACKTIME variable\n \n $0 -c prod will show the number of unique users using PROD env in the\n last $BACKTIME minutes\n \n $0 -c prod 11 20 will show the number of unique users using PROD env in the\n $BACKTIME minutes preceeding 11:20 AM\n \n-c and -u are exclusive\n \n HH and MM must be used together\n" if [ $# -lt 2 ] then echo $USAGE exit fi if [ $# -eq 3 ] then echo $USAGE exit fi if [ $# -gt 4 ] then echo $USAGE exit fi ############################################################## # Validate Environment Name LAENV=$2 ############################################################## > /tmp/LIST.$$ MIN=`date +%M` HR=`date +%H` if [ $# -eq 4 ]; then HR=$3 MIN=$4 fi # calc minutes CHKMIN=`echo "$MIN - $BACKTIME" | bc` case $CHKMIN in ?) CHKMIN="0$CHKMIN";; esac if [ $CHKMIN -lt 0 ]; then CHKMIN=`echo "$CHKMIN + 60" | bc` CHKHR=`echo "$HR - 1" | bc` else CHKHR=$HR fi if [ $CHKHR -lt 0 ]; then CHKHR=23 fi case $CHKHR in ?) CHKHR="0$CHKHR" esac DATE=`date +%d/%b/%Y` ######################################################################## # List & count Users ######################################################################## # cycle thru times ######################################################################## STARTMIN=$CHKMIN STARTHR=$CHKHR if [ $CHKMIN -gt $MIN ]; then MAXMIN=`echo "$MIN + 60" | bc ` case $MAXMIN in ?) MAXMIN="0$MAXMIN";; esac else MAXMIN=$MIN fi while [ $CHKMIN -le $MAXMIN ] do if [ $CHKMIN -gt 59 ]; then RMIN=`echo "$CHKMIN - 60" | bc` RHR=`echo "$CHKHR + 1" | bc` else RMIN=$CHKMIN RHR=$CHKHR fi case $RHR in ?) RHR="0$RHR";; esac case $RMIN in ?) RMIN="0$RMIN";; esac # Write out user name if [ -f $LOGDIR/$LAENV.$LOGEXT ]; then grep "$DATE:$RHR:$RMIN" $LOGDIR/$LAENV.$LOGEXT | grep -v "\- \-" | awk '{print $3}' | sort -u >> /tmp/LIST.$$ fi CHKMIN=`echo "$CHKMIN + 1 " | bc` done HEADER="User(s) between $STARTHR:$STARTMIN and $HR:$MIN" if [ -f /tmp/LIST.$$ ]; then case $1 in -c) echo "`cat /tmp/LIST.$$ | grep -v ^$ | sort -u | wc -l` $HEADER";; -u) echo "\n$HEADER";cat /tmp/LIST.$$ | grep -v ^$ | sort -u;; *) echo $USAGE; exit;; esac # Cleanup rm /tmp/LIST.$$ else echo "0 $HEADER" fi