3 # $Id: top_ftp_users 22 2005-04-11 17:21:15Z jerome $
4 # ----------------------------------------------------------------------
5 # AlternC - Web Hosting System
6 # Copyright (C) 2002 by the AlternC Development Team.
8 # ----------------------------------------------------------------------
10 # Valentin Lacambre's web hosting softwares: http://altern.org/
11 # ----------------------------------------------------------------------
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License (GPL)
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # To read the license please visit http://www.gnu.org/copyleft/gpl.html
25 # ----------------------------------------------------------------------
26 # Original Author of file: Jerome Moinet
27 # Purpose of file: Parse the ftp logs to give the n most active users
28 # ----------------------------------------------------------------------
30 echo "This script does not work with this ALternC version."
37 PROG_NAME=top_ftp_users
39 ALTERNC_ROOT=/var/alternc
40 ALTERNC_ETC=/etc/alternc
41 ALTERNC_LIB=/usr/lib/alternc
42 ALTERNC_CONF_FILE=$ALTERNC_ETC/local.sh
44 LOG_FILE=$LOG_DIR/xferlog
45 TMP_ROOT=$ALTERNC_ROOT/tmp
46 RES_FILE=$TMP_ROOT/$PROG_NAME.res.$$
47 INTERMEDIATE_FILE=$TMP_ROOT/$PROG_NAME.int.$$
48 LOCK_FILE=/run/$PROG_NAME
49 export TEXTDOMAIN=alternc-admintools
53 # Be sure to use the right programs
54 # and be sure they are there
67 gettext=/usr/bin/gettext
68 printf=/usr/bin/printf
69 lockfileremove=/usr/bin/lockfile-remove
70 lockfilecreate=/usr/bin/lockfile-create
71 lockfiletouch=/usr/bin/lockfile-touch
73 # Must have gettext first to display error messages
74 [ -x "$gettext" ] || { echo "Cannot execute $gettext"; exit 1 ; }
76 for i in $awk $grep $cat $zcat $head $id $sort $rm $sed $cut $tail $ls $printf $lockfileremove $lockfilecreate $lockfiletouch
78 [ -x "$i" ] || { echo "$($gettext "Unable to execute") ${i}."; exit 1 ; }
82 #-------------------------
84 #-------------------------
86 # Language-dependent messages
87 # Uses gettext and mo files.
88 # Don't change these messages, change the .po file instead.
89 USAGE=$($gettext -e "Usage: top_ftp_users [ options ] number\n\ntop_ftp_users is a program that gives brief statistics\non ftp usage by parsing the ftp logs.\n\nOptions:\n -h, --help This help text.\n -v, --version Show version.\n -z, --use-gz-logs Parse gzipped and .1, ...n apache logs instead of just parsing the current log.\n -n, --number=NUMBER parse the NUMBER last lines of the current log.\n This option is not compatible with the --use-gz-logs option.\nSee the top_ftp_users(8) manual page for more information.")
90 NOT_FOUND_MSG=$($gettext "does not exist.")
91 NON_NUM_MSG=$($gettext "The \"number\" argument must be a number.")
92 NON_COMPATIBLE_MSG=$($gettext "The -n and -z options are not compatible.")
93 NON_NUM_MSG_FOR_N=$($gettext "The -n option requieres a number as argument.")
94 LOCKFILE_CREATION_FAILED=`$printf "$($gettext "%s is allready beeing executed.")" $PROG_NAME`
95 NON_ROOT_MSG=$($gettext "You have to be root (uid 0) to execute this program.")
96 HIT_RES_MSG=`$printf "$($gettext "Top %s ftp users, sorted by connection number (using gzipped logs: %s):")" $NB_USERS $($gettext "$USE_GZ_LOGS")`
97 SIZE_RES_MSG=`$printf "$($gettext "Top %s ftp users, sorted by size (using gzipped logs: %s):")" $NB_USERS $($gettext "$USE_GZ_LOGS")`
98 MISSING_CONF_FILE=`$printf "$($gettext "Can't find %s. Are you sure AlterncC is properly installed?")" $ALTERNC_CONF_FILE`
99 UNKNOWN_OPTION=$($gettext "Unknown %s option.")
103 #-------------------------
105 #-------------------------
109 $rm -f $INTERMEDIATE_FILE
110 $lockfileremove $LOCK_FILE
113 trap trap_EXIT INT KILL TERM QUIT ABRT STOP HUP
116 #-------------------------
118 #-------------------------
121 [ "`$id -u`" != "0" ] && { echo $NON_ROOT_MSG ; exit 1 ; }
129 [ "$IS_N_PARAM" = "true" ] && { shift ; IS_N_PARAM=false ; continue ; }
130 [ "`$printf "$ARG\n" | $cut -c1`" != "-" ] && [ "$IS_N_PARAM" = "false" ] && break
133 -h | --help ) echo $PROG_NAME version $PROG_VERSION ; $printf "$USAGE\n" ; exit ;;
134 -v | --version ) echo $PROG_NAME version $PROG_VERSION ; exit ;;
135 -z | --use-gz-logs ) USE_GZ_LOGS="$YES" ;;
137 if [ "$ARG" != "-n" ] ; then
138 N_PARAM=`echo $ARG | $cut -d"=" -f2`
143 [ `echo "$N_PARAM" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG_FOR_N" ; exit 1 ; }
144 COMMAND="$tail -n $N_PARAM"
146 *) $printf "${UNKNOWN_OPTION}\n" $ARG ; exit 1 ;;
151 # -n and -z options are not compatible
152 [ "$USE_GZ_LOGS" = "$YES" ] && ! [ -z "$N_PARAM" ] && { echo $NON_COMPATIBLE_MSG ; exit 1 ; }
153 # Must have only 1 parameter
154 [ -z "$1" ] || [ "$#" -gt 1 ] && { $printf "$USAGE\n" ; exit 1 ; }
155 # Argument "number" must be numeric
156 [ `echo "$1" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG" ; exit 1 ; } || NB_USERS=$1
157 # Parameter 1 must be numeric
158 [ `echo "$1" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG" ; exit 1 ; } || NB_USERS=$1
159 # These dirs/files must exist
160 ! [ -d "$LOG_DIR" ] && { echo "$LOG_DIR $NOT_FOUND_MSG" ; exit 1 ; }
161 ! [ -d "$TMP_ROOT" ] && { echo "$TMP_ROOT $NOT_FOUND_MSG" ; exit 1 ; }
162 ! [ -f "$LOG_FILE" ] && { echo "$LOG_FILE $NOT_FOUND_MSG" ; exit 1 ; }
163 # Have to get AlternC conf file :
164 ! [ -f "$ALTERNC_CONF_FILE" ] && { echo $MISSING_CONF_FILE ; exit 1 ; } || . $ALTERNC_CONF_FILE
167 # Prevents executing more than one shell at the same time
168 $lockfilecreate --retry 1 $LOCK_FILE
171 echo $LOCKFILE_CREATION_FAILED
174 $lockfiletouch $LOCK_FILE &
180 # Have to parse files one by one or else system wil go on knees
181 $COMMAND $LOG_FILE | $awk '{z=NF-4 ; account[$z]++ ; size[$z]+=$8} END {for (item in account) print item" "account[item]" "size[item]}' > $RES_FILE
182 for FILE in `$ls -1 $LOG_FILE.* | $grep -v "\.gz$"`; do
183 [ "$USE_GZ_LOGS" = "$YES" ] && [ -f $FILE ] && $cat $FILE | $awk '{z=NF-4 ; account[$z]++ ; size[$z]+=$8} END {for (item in account) print item" "account[item]" "size[item]}' >> $RES_FILE
185 if [ "$USE_GZ_LOGS" = "$YES" ]
187 for GZLOG in $LOG_FILE.*.gz
189 $zcat $GZLOG | $awk '{z=NF-4 ; account[$z]++ ; size[$z]+=$8} END {for (item in account) print item" "account[item]" "size[item]}' >> $RES_FILE
195 $cat $RES_FILE | $awk '{account[$1]+=$2 ; size[$1]+=$3} END {for (item in account) print item" "account[item]" "size[item]}' > $INTERMEDIATE_FILE
198 $cat $INTERMEDIATE_FILE | $awk {'printf ("%20d %s\n", $2, $1)'} | $sort -gr | $head -n$NB_USERS
201 $cat $INTERMEDIATE_FILE | $awk {'printf ("%20d %s\n", $3, $1)'} | $sort -gr | $head -n$NB_USERS
204 # cleanly exit and remove temp files