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