| 1 | #! /bin/sh |
| 2 | |
| 3 | # Copyright (c) University of Cambridge, 1995 - 2007 |
| 4 | # See the file NOTICE for conditions of use and distribution. |
| 5 | |
| 6 | # This script takes the following command line arguments: |
| 7 | # -l dir Log file directory |
| 8 | # -k days Number of days to keep the log files |
| 9 | |
| 10 | # Except when they appear in comments, the following placeholders in this |
| 11 | # source are replaced when it is turned into a runnable script: |
| 12 | # |
| 13 | # CONFIGURE_FILE_USE_NODE |
| 14 | # CONFIGURE_FILE_USE_EUID |
| 15 | # CONFIGURE_FILE |
| 16 | # BIN_DIRECTORY |
| 17 | # EXICYCLOG_MAX |
| 18 | # COMPRESS_COMMAND |
| 19 | # COMPRESS_SUFFIX |
| 20 | # CHOWN_COMMAND |
| 21 | # CHGRP_COMMAND |
| 22 | # CHMOD_COMMAND |
| 23 | # TOUCH_COMMAND |
| 24 | # MV_COMMAND |
| 25 | # RM_COMMAND |
| 26 | |
| 27 | # PROCESSED_FLAG |
| 28 | |
| 29 | # This is a shell script for cycling exim main and reject log files. Each time |
| 30 | # it is run, the files get "shuffled down" by one, the current one (e.g. |
| 31 | # mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02, |
| 32 | # and so on, up to the limit configured here. When the number to keep is |
| 33 | # greater than 99 (not common, but some people do it), three digits are used |
| 34 | # (e.g. mainlog.001). The same shuffling happens to the reject logs. All |
| 35 | # renamed files with numbers greater than 1 are compressed. |
| 36 | |
| 37 | # This script should be called regularly (e.g. daily) by a root crontab |
| 38 | # entry of the form |
| 39 | |
| 40 | # 1 0 * * * /opt/exim/bin/exicyclog |
| 41 | |
| 42 | # The following lines are generated from Exim's configuration file when |
| 43 | # this source is built into a script, but you can subsequently edit them |
| 44 | # without rebuilding things, as long are you are careful not to overwrite |
| 45 | # the script in the next Exim rebuild/install. "Keep" is the number of old log |
| 46 | # files that are required to be kept. Its value can be overridden by the -k |
| 47 | # command line option. "Compress" and "suffix" define your chosen compression |
| 48 | # method. The others are provided because the location of certain commands |
| 49 | # varies from OS to OS. Sigh. |
| 50 | |
| 51 | keep=EXICYCLOG_MAX |
| 52 | compress=COMPRESS_COMMAND |
| 53 | suffix=COMPRESS_SUFFIX |
| 54 | |
| 55 | chgrp=CHGRP_COMMAND |
| 56 | chmod=CHMOD_COMMAND |
| 57 | chown=CHOWN_COMMAND |
| 58 | mv=MV_COMMAND |
| 59 | rm=RM_COMMAND |
| 60 | touch=TOUCH_COMMAND |
| 61 | |
| 62 | # End of editable lines |
| 63 | ######################################################################### |
| 64 | |
| 65 | # Sort out command line options. |
| 66 | |
| 67 | while [ $# -gt 0 ] ; do |
| 68 | case "$1" in |
| 69 | -l) log_file_path=$2 |
| 70 | shift |
| 71 | ;; |
| 72 | -k) keep=$2 |
| 73 | shift |
| 74 | ;; |
| 75 | *) echo "** exicyclog: unknown option $1" |
| 76 | exit 1 |
| 77 | ;; |
| 78 | esac |
| 79 | shift |
| 80 | done |
| 81 | |
| 82 | # Some operating systems have different versions in which the commands live |
| 83 | # in different places. We have a fudge that will search the usual suspects if |
| 84 | # requested. |
| 85 | |
| 86 | for cmd in chgrp chmod chown mv rm touch; do |
| 87 | eval "oldcmd=\$$cmd" |
| 88 | if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi |
| 89 | newcmd=$cmd |
| 90 | for dir in /bin /usr/bin /usr/sbin /usr/etc ; do |
| 91 | if [ -f $dir/$cmd ] ; then |
| 92 | newcmd=$dir/$cmd |
| 93 | break |
| 94 | fi |
| 95 | done |
| 96 | eval $cmd=$newcmd |
| 97 | done |
| 98 | |
| 99 | # See if this installation is using the esoteric "USE_EUID" feature of Exim, |
| 100 | # in which it uses the effective user id as a suffix for the configuration file |
| 101 | # name. In order for this to work, exicyclog must be run under the appropriate |
| 102 | # euid. |
| 103 | |
| 104 | if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then |
| 105 | euid=.`id -u` |
| 106 | fi |
| 107 | |
| 108 | # See if this installation is using the esoteric "USE_NODE" feature of Exim, |
| 109 | # in which it uses the host's name as a suffix for the configuration file name. |
| 110 | |
| 111 | if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then |
| 112 | hostsuffix=.`uname -n` |
| 113 | fi |
| 114 | |
| 115 | # Now find the configuration file name. This has got complicated because the |
| 116 | # CONFIGURE_FILE value may now be a list of files. The one that is used is the |
| 117 | # first one that exists. Mimic the code in readconf.c by testing first for the |
| 118 | # suffixed file in each case. |
| 119 | |
| 120 | set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End |
| 121 | CONFIGURE_FILE |
| 122 | End |
| 123 | ` |
| 124 | while [ "$config" = "" -a $# -gt 0 ] ; do |
| 125 | if [ -f "$1$euid$hostsuffix" ] ; then |
| 126 | config="$1$euid$hostsuffix" |
| 127 | elif [ -f "$1$euid" ] ; then |
| 128 | config="$1$euid" |
| 129 | elif [ -f "$1$hostsuffix" ] ; then |
| 130 | config="$1$hostsuffix" |
| 131 | elif [ -f "$1" ] ; then |
| 132 | config="$1" |
| 133 | fi |
| 134 | shift |
| 135 | done |
| 136 | |
| 137 | # Determine if the log file path is set, and where the spool directory is. |
| 138 | # Search for an exim_path setting in the configure file; otherwise use the bin |
| 139 | # directory. Call that version of Exim to find the spool directory and log file |
| 140 | # path, unless log_file_path was set above by a command line option. BEWARE: a |
| 141 | # tab character is needed in the command below. It has had a nasty tendency to |
| 142 | # get lost in the past. Use a variable to hold a space and a tab to keep the |
| 143 | # tab in one place. |
| 144 | |
| 145 | st=' ' |
| 146 | exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"` |
| 147 | if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi |
| 148 | |
| 149 | spool_directory=`$exim_path -C $config -bP spool_directory | sed 's/.*=[ ]*//'` |
| 150 | |
| 151 | if [ "$log_file_path" = "" ] ; then |
| 152 | log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[ ]*//'` |
| 153 | fi |
| 154 | |
| 155 | # If log_file_path contains only "syslog" then no Exim log files are in use. |
| 156 | # We can't cycle anything. Complain and give up. |
| 157 | |
| 158 | if [ "$log_file_path" = "syslog" ] ; then |
| 159 | echo "*** Exim is logging to syslog - no log files to cycle ***" |
| 160 | exit 1 |
| 161 | fi |
| 162 | |
| 163 | # Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect |
| 164 | # what remains. The simplistic regex originally used failed when a filename |
| 165 | # contained "syslog", so we have to use three less general ones, because sed |
| 166 | # doesn't have much power in its regexs. |
| 167 | |
| 168 | log_file_path=`echo "$log_file_path" | \ |
| 169 | sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'` |
| 170 | |
| 171 | # If log_file_path is empty, try and get the compiled in default by using |
| 172 | # /dev/null as the configuration file. |
| 173 | |
| 174 | if [ "$log_file_path" = "" ]; then |
| 175 | log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[ ]*//'` |
| 176 | log_file_path=`echo "$log_file_path" | \ |
| 177 | sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'` |
| 178 | fi |
| 179 | |
| 180 | # If log_file_path is still empty, the logs we are interested in are probably |
| 181 | # called "mainlog" and "rejectlog" in the directory called "log" in the spool |
| 182 | # directory. Otherwise we fish out the directory from the given path, and also |
| 183 | # the names of the logs. |
| 184 | |
| 185 | if [ "$log_file_path" = "" ]; then |
| 186 | logdir=$spool_directory/log |
| 187 | mainlog=mainlog |
| 188 | rejectlog=rejectlog |
| 189 | paniclog=paniclog |
| 190 | else |
| 191 | logdir=`echo $log_file_path | sed 's?/[^/]*$??'` |
| 192 | logbase=`echo $log_file_path | sed 's?^.*/??'` |
| 193 | mainlog=`echo $logbase | sed 's/%s/main/'` |
| 194 | rejectlog=`echo $logbase | sed 's/%s/reject/'` |
| 195 | paniclog=`echo $logbase | sed 's/%s/panic/'` |
| 196 | fi |
| 197 | |
| 198 | # Get into the log directory to do the business. |
| 199 | |
| 200 | cd $logdir || exit 1 |
| 201 | |
| 202 | # If there is no main log file, do nothing. |
| 203 | |
| 204 | if [ ! -f $mainlog ]; then exit; fi |
| 205 | |
| 206 | # Find out the owner and group of the main log file so that we can re-instate |
| 207 | # this on moved and compressed files, since some operating systems may change |
| 208 | # things. This is a tedious bit of code, but it should work both in operating |
| 209 | # systems where the -l option of ls gives the user and group, and those in which |
| 210 | # you need -lg. The condition is that, if the fifth field of the output from |
| 211 | # ls consists entirely of digits, then the third and fourth fields are the user |
| 212 | # and group. |
| 213 | |
| 214 | a=`ls -lg $mainlog` |
| 215 | b=`ls -l $mainlog` |
| 216 | |
| 217 | # These statements work fine in the Bourne or Korn shells, but not in Bash. |
| 218 | # So for the benefit of systems whose /bin/sh is really Bash, they have been |
| 219 | # changed to a messier form. |
| 220 | |
| 221 | # user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'` |
| 222 | # group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'` |
| 223 | |
| 224 | user=`echo "$a |
| 225 | $b |
| 226 | " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'` |
| 227 | |
| 228 | group=`echo "$a |
| 229 | $b |
| 230 | " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'` |
| 231 | |
| 232 | # Now do the job. First remove the files that have "fallen off the bottom". |
| 233 | # Look for both the compressed and uncompressed forms. |
| 234 | |
| 235 | if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi; |
| 236 | |
| 237 | if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi; |
| 238 | if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi; |
| 239 | |
| 240 | if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi; |
| 241 | if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi; |
| 242 | |
| 243 | if [ -f $paniclog.$keept ]; then $rm $paniclog.$keept; fi; |
| 244 | if [ -f $paniclog.$keept.$suffix ]; then $rm $paniclog.$keept.$suffix; fi; |
| 245 | |
| 246 | # Now rename all the previous old files by increasing their numbers by 1. |
| 247 | # When the number is less than 10, insert a leading zero. |
| 248 | |
| 249 | count=$keep |
| 250 | if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi |
| 251 | |
| 252 | while [ $count -gt 1 ]; do |
| 253 | old=`expr -- $count - 1` |
| 254 | if [ $keep -gt 99 ]; then |
| 255 | if [ $old -lt 10 ]; then oldt=00$old |
| 256 | elif [ $old -lt 100 ]; then oldt=0$old |
| 257 | else oldt=$old |
| 258 | fi |
| 259 | else |
| 260 | if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi; |
| 261 | fi |
| 262 | if [ -f $mainlog.$oldt ]; then |
| 263 | $mv $mainlog.$oldt $mainlog.$countt |
| 264 | elif [ -f $mainlog.$oldt.$suffix ]; then |
| 265 | $mv $mainlog.$oldt.$suffix $mainlog.$countt.$suffix |
| 266 | fi |
| 267 | if [ -f $rejectlog.$oldt ]; then |
| 268 | $mv $rejectlog.$oldt $rejectlog.$countt |
| 269 | elif [ -f $rejectlog.$oldt.$suffix ]; then |
| 270 | $mv $rejectlog.$oldt.$suffix $rejectlog.$countt.$suffix |
| 271 | fi |
| 272 | if [ -f $paniclog.$oldt ]; then |
| 273 | $mv $paniclog.$oldt $paniclog.$countt |
| 274 | elif [ -f $paniclog.$oldt.$suffix ]; then |
| 275 | $mv $paniclog.$oldt.$suffix $paniclog.$countt.$suffix |
| 276 | fi |
| 277 | count=$old |
| 278 | countt=$oldt |
| 279 | done |
| 280 | |
| 281 | # Now rename the current files as 01 or 001 if keeping more than 99 |
| 282 | |
| 283 | if [ $keep -gt 99 ]; then first=001; else first=01; fi |
| 284 | |
| 285 | if [ -f $mainlog ]; then |
| 286 | $mv $mainlog $mainlog.$first |
| 287 | $chown $user:$group $mainlog.$first |
| 288 | $touch $mainlog |
| 289 | $chown $user:$group $mainlog |
| 290 | $chmod 640 $mainlog |
| 291 | fi |
| 292 | |
| 293 | if [ -f $rejectlog ]; then |
| 294 | $mv $rejectlog $rejectlog.$first |
| 295 | $chown $user:$group $rejectlog.$first |
| 296 | $touch $rejectlog |
| 297 | $chown $user:$group $rejectlog |
| 298 | $chmod 640 $rejectlog |
| 299 | fi |
| 300 | |
| 301 | if [ -f $paniclog ]; then |
| 302 | $mv $paniclog $paniclog.$first |
| 303 | $chown $user:$group $paniclog.$first |
| 304 | $touch $paniclog |
| 305 | $chown $user:$group $paniclog |
| 306 | $chmod 640 $paniclog |
| 307 | fi |
| 308 | |
| 309 | # Now scan the (0)02 and later files, compressing where necessary, and |
| 310 | # ensuring that their owners and groups are correct. |
| 311 | |
| 312 | count=2; |
| 313 | |
| 314 | while [ $count -le $keep ]; do |
| 315 | if [ $keep -gt 99 ]; then |
| 316 | if [ $count -lt 10 ]; then countt=00$count |
| 317 | elif [ $count -lt 100 ]; then countt=0$count |
| 318 | else countt=$count |
| 319 | fi |
| 320 | else |
| 321 | if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi |
| 322 | fi |
| 323 | if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi |
| 324 | if [ -f $mainlog.$countt.$suffix ]; then |
| 325 | $chown $user:$group $mainlog.$countt.$suffix |
| 326 | fi |
| 327 | if [ -f $rejectlog.$countt ]; then $compress $rejectlog.$countt; fi |
| 328 | if [ -f $rejectlog.$countt.$suffix ]; then |
| 329 | $chown $user:$group $rejectlog.$countt.$suffix |
| 330 | fi |
| 331 | if [ -f $paniclog.$countt ]; then $compress $paniclog.$countt; fi |
| 332 | if [ -f $paniclog.$countt.$suffix ]; then |
| 333 | $chown $user:$group $paniclog.$countt.$suffix |
| 334 | fi |
| 335 | |
| 336 | count=`expr -- $count + 1` |
| 337 | done |
| 338 | |
| 339 | # End of exicyclog |