Alternc  latest
Alternc logiel libre pour l'hébergement
top_ftp_users
Go to the documentation of this file.
1 #!/bin/bash
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 echo "This script does not work with this ALternC version."
31 exit 1
32 
33 
34 
35 
36 PATH=""
37 PROG_NAME=top_ftp_users
38 PROG_VERSION=0.1.0
39 ALTERNC_ROOT=/var/alternc
40 ALTERNC_ETC=/etc/alternc
41 ALTERNC_LIB=/usr/lib/alternc
42 ALTERNC_CONF_FILE=$ALTERNC_ETC/local.sh
43 LOG_DIR=/var/log
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
50 YES=yes
51 NO=no
52 
53 # Be sure to use the right programs
54 # and be sure they are there
55 awk=/usr/bin/awk
56 grep=/bin/grep
57 cat=/bin/cat
58 zcat=/bin/zcat
59 head=/usr/bin/head
60 id=/usr/bin/id
61 sort=/usr/bin/sort
62 rm=/bin/rm
63 sed=/bin/sed
64 cut=/usr/bin/cut
65 tail=/usr/bin/tail
66 ls=/bin/ls
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
72 
73 # Must have gettext first to display error messages
74 [ -x "$gettext" ] || { echo "Cannot execute $gettext"; exit 1 ; }
75 
76 for i in $awk $grep $cat $zcat $head $id $sort $rm $sed $cut $tail $ls $printf $lockfileremove $lockfilecreate $lockfiletouch
77 do
78  [ -x "$i" ] || { echo "$($gettext "Unable to execute") ${i}."; exit 1 ; }
79 done
80 
81 
82 #-------------------------
83 set_messages()
84 #-------------------------
85 {
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.")
100 }
101 
102 
103 #-------------------------
104 trap_EXIT()
105 #-------------------------
106 {
107  # Does some cleaning
108  $rm -f $RES_FILE
109  $rm -f $INTERMEDIATE_FILE
110  $lockfileremove $LOCK_FILE
111  exit
112 }
113 trap trap_EXIT INT KILL TERM QUIT ABRT STOP HUP
114 
115 
116 #-------------------------
117 # Main
118 #-------------------------
119 set_messages
120 # Must be root
121 [ "`$id -u`" != "0" ] && { echo $NON_ROOT_MSG ; exit 1 ; }
122 
123 # Parse args
124 IS_N_PARAM=false
125 USE_GZ_LOGS="$NO"
126 N_PARAM=""
127 COMMAND=$cat
128 for ARG in "$@" ; do
129  [ "$IS_N_PARAM" = "true" ] && { shift ; IS_N_PARAM=false ; continue ; }
130  [ "`$printf "$ARG\n" | $cut -c1`" != "-" ] && [ "$IS_N_PARAM" = "false" ] && break
131 
132  case $ARG in
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" ;;
136  -n | --number=* )
137  if [ "$ARG" != "-n" ] ; then
138  N_PARAM=`echo $ARG | $cut -d"=" -f2`
139  else
140  N_PARAM=$2
141  IS_N_PARAM=true
142  fi
143  [ `echo "$N_PARAM" | $grep -c [^0-9]` != 0 ] && { echo "$NON_NUM_MSG_FOR_N" ; exit 1 ; }
144  COMMAND="$tail -n $N_PARAM"
145  ;;
146  *) $printf "${UNKNOWN_OPTION}\n" $ARG ; exit 1 ;;
147  esac
148  shift
149 done
150 
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
165 
166 
167 # Prevents executing more than one shell at the same time
168 $lockfilecreate --retry 1 $LOCK_FILE
169 if [ $? != 0 ]
170 then
171  echo $LOCKFILE_CREATION_FAILED
172  exit 1
173 fi
174 $lockfiletouch $LOCK_FILE &
175 BADGER="$!"
176 
177 
178 # Does the stuff
179 set_messages
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
184 done
185 if [ "$USE_GZ_LOGS" = "$YES" ]
186 then
187  for GZLOG in $LOG_FILE.*.gz
188  do
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
190  done
191 fi
192 
193 
194 # show results
195 $cat $RES_FILE | $awk '{account[$1]+=$2 ; size[$1]+=$3} END {for (item in account) print item" "account[item]" "size[item]}' > $INTERMEDIATE_FILE
196 
197 echo $HIT_RES_MSG
198 $cat $INTERMEDIATE_FILE | $awk {'printf ("%20d %s\n", $2, $1)'} | $sort -gr | $head -n$NB_USERS
199 echo ""
200 echo $SIZE_RES_MSG
201 $cat $INTERMEDIATE_FILE | $awk {'printf ("%20d %s\n", $3, $1)'} | $sort -gr | $head -n$NB_USERS
202 
203 
204 # cleanly exit and remove temp files
205 kill "${BADGER}"
206 trap_EXIT
207