root/alternc/trunk/tools/top_ftp_users

Revision 2178, 7.4 kB (checked in by anarcat, 7 months ago)

correctly declare functions, we were missing parenthesis

  • Property svn:executable set to *
Line 
1 #!/bin/ksh
2 #
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.
7 # http://alternc.org
8 # ----------------------------------------------------------------------
9 # Based on:
10 # Valentin Lacambre's web hosting softwares: http://altern.org/
11 # ----------------------------------------------------------------------
12 # LICENSE
13 #
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.
18 #
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.
23 #
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 # ----------------------------------------------------------------------
29 #
30 PATH=""
31 PROG_NAME=top_ftp_users
32 PROG_VERSION=0.1.0
33 ALTERNC_ROOT=/var/alternc
34 ALTERNC_ETC=/etc/alternc
35 ALTERNC_LIB=/usr/lib/alternc
36 ALTERNC_CONF_FILE=$ALTERNC_ETC/local.sh
37 LOG_DIR=/var/log
38 LOG_FILE=$LOG_DIR/xferlog
39 TMP_ROOT=$ALTERNC_ROOT/tmp
40 RES_FILE=$TMP_ROOT/$PROG_NAME.res.$$
41 INTERMEDIATE_FILE=$TMP_ROOT/$PROG_NAME.int.$$
42 LOCK_FILE=/var/run/$PROG_NAME
43 export TEXTDOMAIN=alternc-admintools
44 YES=yes
45 NO=no
46
47 # Be sure to use the right programs
48 # and be sure they are there
49 awk=/usr/bin/awk
50 grep=/bin/grep
51 cat=/bin/cat
52 zcat=/bin/zcat
53 head=/usr/bin/head
54 id=/usr/bin/id
55 sort=/usr/bin/sort
56 rm=/bin/rm
57 sed=/bin/sed
58 cut=/usr/bin/cut
59 tail=/usr/bin/tail
60 ls=/bin/ls
61 gettext=/usr/bin/gettext
62 printf=/usr/bin/printf
63 lockfileremove=/usr/bin/lockfile-remove
64 lockfilecreate=/usr/bin/lockfile-create
65 lockfiletouch=/usr/bin/lockfile-touch
66
67 # Must have gettext first to display error messages
68 [ -x "$gettext" ] || { echo "Cannot execute $gettext"; exit 1 ; }
69
70 for i in $awk $grep $cat $zcat $head $id $sort $rm $sed $cut $tail $ls $printf $lockfileremove $lockfilecreate $lockfiletouch
71 do
72         [ -x "$i" ] || { echo "$($gettext "Unable to execute") ${i}."; exit 1 ; }
73 done
74
75
76 #-------------------------
77 set_messages()
78 #-------------------------
79 {
80         # Language-dependent messages
81         # Uses gettext and mo files.
82         # Don't change these messages, change the .po file instead.
83         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.")
84         NOT_FOUND_MSG=$($gettext "does not exist.")
85         NON_NUM_MSG=$($gettext "The \"number\" argument must be a number.")
86         NON_COMPATIBLE_MSG=$($gettext "The -n and -z options are not compatible.")
87         NON_NUM_MSG_FOR_N=$($gettext "The -n option requieres a number as argument.")
88         LOCKFILE_CREATION_FAILED=`$printf "$($gettext "%s is allready beeing executed.")" $PROG_NAME`
89         NON_ROOT_MSG=$($gettext "You have to be root (uid 0) to execute this program.")
90         HIT_RES_MSG=`$printf "$($gettext "Top %s ftp users, sorted by connection number (using gzipped logs: %s):")" $NB_USERS $($gettext "$USE_GZ_LOGS")`
91         SIZE_RES_MSG=`$printf "$($gettext "Top %s ftp users, sorted by size (using gzipped logs: %s):")" $NB_USERS $($gettext "$USE_GZ_LOGS")`
92         MISSING_CONF_FILE=`$printf "$($gettext "Can't find %s. Are you sure AlterncC is properly installed?")" $ALTERNC_CONF_FILE`
93         UNKNOWN_OPTION=$($gettext "Unknown %s option.")
94 }
95
96
97 #-------------------------
98 trap_EXIT()
99 #-------------------------
100 {
101         # Does some cleaning
102         $rm -f $RES_FILE
103         $rm -f $INTERMEDIATE_FILE
104         $lockfileremove $LOCK_FILE
105         exit
106 }
107 trap trap_EXIT INT KILL TERM QUIT ABRT STOP HUP
108
109
110 #-------------------------
111 # Main
112 #-------------------------
113 set_messages
114 # Must be root
115 [ "`$id -u`" != "0" ] && { echo $NON_ROOT_MSG ; exit 1 ; }
116
117 # Parse args
118 IS_N_PARAM=false
119 USE_GZ_LOGS="$NO"
120 N_PARAM=""
121 COMMAND=$cat
122 for ARG in "$@" ; do
123         [ "$IS_N_PARAM" = "true" ] && { shift ; IS_N_PARAM=false ; continue ; }
124         [ "`$printf "$ARG\n" | $cut -c1`" != "-" ] && [ "$IS_N_PARAM" = "false" ] &&  break
125
126         case $ARG in
127                 -h | --help        ) echo $PROG_NAME version $PROG_VERSION ; $printf "$USAGE\n" ; exit ;;
128                 -v | --version     ) echo $PROG_NAME version $PROG_VERSION ; exit ;;
129                 -z | --use-gz-logs ) USE_GZ_LOGS="$YES" ;;
130                 -n | --number=*    )
131                         if [ "$ARG" != "-n" ] ; then
132                                 N_PARAM=`echo $ARG | $cut -d"=" -f2`
133                         else
134                                 N_PARAM=$2
135                                 IS_N_PARAM=true
136                         fi
137                         [ `echo "$N_PARAM" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG_FOR_N" ; exit 1 ; }
138                         COMMAND="$tail -n $N_PARAM"
139                         ;;
140                 *) $printf "${UNKNOWN_OPTION}\n" $ARG ; exit 1 ;;
141         esac
142         shift
143 done
144
145 # -n and -z options are not compatible
146 [ "$USE_GZ_LOGS" = "$YES" ] && ! [ -z "$N_PARAM" ] && { echo $NON_COMPATIBLE_MSG ; exit 1 ; }
147 # Must have only 1 parameter
148 [ -z "$1" ] || [ "$#" -gt 1 ] && { $printf "$USAGE\n" ; exit 1 ; }
149 # Argument "number"  must be numeric
150 [ `echo "$1" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG" ; exit 1 ; } || NB_USERS=$1
151 # Parameter 1 must be numeric
152 [ `echo "$1" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG" ; exit 1 ; } || NB_USERS=$1
153 # These dirs/files must exist
154 ! [ -d "$LOG_DIR" ] && { echo "$LOG_DIR $NOT_FOUND_MSG" ; exit 1 ; }
155 ! [ -d "$TMP_ROOT" ] && { echo "$TMP_ROOT $NOT_FOUND_MSG" ; exit 1 ; }
156 ! [ -f "$LOG_FILE" ] && { echo "$LOG_FILE $NOT_FOUND_MSG" ; exit 1 ; }
157 # Have to get AlternC conf file :
158 ! [ -f "$ALTERNC_CONF_FILE" ] && { echo $MISSING_CONF_FILE ; exit 1 ; } || . $ALTERNC_CONF_FILE
159
160
161 # Prevents executing more than one shell at the same time
162 $lockfilecreate --retry 1 $LOCK_FILE
163 if [ $? != 0 ]
164 then
165         echo $LOCKFILE_CREATION_FAILED
166         exit 1
167 fi
168 $lockfiletouch $LOCK_FILE &
169 BADGER="$!"
170
171
172 # Does the stuff
173 set_messages
174 # Have to parse files one by one or else system wil go on knees
175 $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
176 for FILE in `$ls -1 $LOG_FILE.* | $grep -v "\.gz$"`; do
177         [ "$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
178 done
179 if [ "$USE_GZ_LOGS" = "$YES" ]
180 then
181         for GZLOG in $LOG_FILE.*.gz
182         do
183                 $zcat $GZLOG | $awk '{z=NF-4 ; account[$z]++ ; size[$z]+=$8} END {for (item in account) print item" "account[item]" "size[item]}' >> $RES_FILE
184         done
185 fi
186
187
188 # show results
189 $cat $RES_FILE | $awk '{account[$1]+=$2 ; size[$1]+=$3} END {for (item in account) print item" "account[item]" "size[item]}' > $INTERMEDIATE_FILE
190
191 echo $HIT_RES_MSG
192 $cat $INTERMEDIATE_FILE | $awk {'printf ("%20d %s\n", $2, $1)'} | $sort -gr | $head -n$NB_USERS
193 echo ""
194 echo $SIZE_RES_MSG
195 $cat $INTERMEDIATE_FILE | $awk {'printf ("%20d %s\n", $3, $1)'} | $sort -gr | $head -n$NB_USERS
196
197
198 # cleanly exit and remove temp files
199 kill "${BADGER}"
200 trap_EXIT
201
Note: See TracBrowser for help on using the browser.