chiark / gitweb /
Commit 2.4.5-5 as unpacked
[inn-innduct.git] / scripts / scanlogs.in
1 #! /bin/sh
2 # fixscript will replace this line with code to load innshellvars
3
4 ##  $Revision: 7770 $
5 ##  Summarize INN log files.
6 ##  Optional arguments:
7 ##      norotate        Do not rotate logfiles
8
9 ##  If you get an error from this line:
10 ##      sh -c 'egrep "`ls /etc`" /dev/null'
11 ##  then get a better egrep, like the FSF one.
12
13 ##  Directory where old log files are kept.
14 OLD=${MOST_LOGS}/OLD
15 ##  If you want to archive the active file, enable this line.
16 ACTIVEFILE=${ACTIVE}
17 ##  Number of lines of error in each category to report.
18 TOP=${TOP-20}
19 ##  NN log file.
20 NN=${PATHETC}/nn/Log
21
22 ##  Where these programs, if used, write their logs.
23 ##  We also have to find innfeed's log file.
24 INNFEEDLOG=`${AWK} '{gsub(/:|#/, " & ")} {if ($1 == "log-file" && $2 == ":") print $3}' ${PATHETC}/innfeed.conf`
25 INNFEED=
26 for F in "${INNFEEDLOG}" ; do
27     test -f "${MOST_LOGS}/${F}" && INNFEED="${INNFEED} ${MOST_LOGS}/${F}"
28 done
29 test -z "${INNFEED}" && test -f ${MOST_LOGS}/innfeed.log && INNFEED="${MOST_LOGS}/innfeed.log"
30
31
32 ##  Where nntpsend, if used, writes its log information.
33 NNTPSEND=${MOST_LOGS}/nntpsend.log
34 SENDNNTP=${MOST_LOGS}/send-nntp.log
35 SENDUUCP=${MOST_LOGS}/send-uucp.log
36 LIVEFILES="${INNFEED} ${NNTPSEND} ${SENDNNTP} ${SENDUUCP}"
37 ##  Where news.daily places expire output, unless noexplog was used.
38 EXPLOG=${MOST_LOGS}/expire.log
39
40 ##  If you divide your news syslog into separate files, list them here.
41 SYSLOG_CRIT=${MOST_LOGS}/news.crit
42 SYSLOG_ERR=${MOST_LOGS}/news.err
43 SYSLOG_NOTICE=${MOST_LOGS}/news.notice
44 SYSLOGS="${SYSLOG_CRIT} ${SYSLOG_ERR} ${SYSLOG_NOTICE}"
45
46 ##  Where tally control and unwanted processors are found.
47 TALLY_CONTROL=${PATHBIN}/tally.control
48 TALLY_UNWANTED=${PATHBIN}/tally.unwanted
49 UNWANTED_LOG=${MOST_LOGS}/unwanted.log
50 CONTROL_LOG=${MOST_LOGS}/control.log
51 CONTROL_DATA=
52 test -f ${MOST_LOGS}/newgroup.log && CONTROL_DATA=${MOST_LOGS}/newgroup.log
53 test -f ${MOST_LOGS}/rmgroup.log \
54         && CONTROL_DATA="${CONTROL_DATA} ${MOST_LOGS}/rmgroup.log"
55
56 ##  Build up the list of log files to process.
57 LOGS="${ERRLOG} ${EXPLOG} ${LOG} ${ACTIVEFILE} ${SYSLOGS} ${UNWANTED_LOG}"
58
59 for F in ${LIVEFILES} ; do
60     test -n "${F}" -a -f "${F}" && LOGS="${LOGS} ${F}"
61 done
62
63 test -n "${CONTROL_DATA}" && LOGS="${LOGS} ${CONTROL_LOG}"
64 for F in checkgroups default ihave newgroup rmgroup sendme sendsys \
65         senduuname version miscctl badcontrol failedpgp badpgp; do
66     test -f ${MOST_LOGS}/${F}.log && LOGS="${LOGS} ${MOST_LOGS}/${F}.log"
67 done
68
69 PROGNAME=scanlogs
70 LOCK=${LOCKS}/LOCK.${PROGNAME}
71
72 ##  Set defaults.
73 ROTATE=true
74
75 ##  Parse JCL.
76 for I
77 do
78     case "X${I}" in
79     Xnonn)
80         # Ignore this.
81         ;;
82     Xnorotate)
83         ROTATE=false
84         ;;
85     *)
86         echo "Unknown flag ${I}" 1>&2
87         exit 1
88         ;;
89     esac
90 done
91
92 ##  Make sure every log exists.
93 for F in ${LOGS} ; do
94     test ! -f ${F} && touch ${F}
95 done
96
97 ##  Temporary files.
98 T=${TMPDIR}/scan$$
99 PROBS=${TMPDIR}/scanlog$$
100 trap "rm -f ${T} ${PROBS}; exit 0" 0 1 2 3 15
101
102 ##  Rotate the logs?
103 if ${ROTATE} ; then
104     ##  Lock out others.
105     shlock -p $$ -f ${LOCK} || {
106         echo "$0: Locked by `cat ${LOCK}`"
107         exit 1
108     }
109     trap "rm -f ${T} ${PROBS} ${LOCK}; exit 0" 1 2 3 15
110
111     HERE=`pwd`
112     cd ${MOST_LOGS}
113     test ! -d ${OLD} && mkdir ${OLD}
114
115     ctlinnd -s logmode
116     PAUSED=false
117     ctlinnd -s pause "Flushing log and syslog files" 2>&1 && PAUSED=true
118     OUTPUT=`ctlinnd flushlogs 2>&1`
119     if [ "$OUTPUT" != "Ok" -a "$OUTPUT" != "In debug mode" ]; then
120         echo "$OUTPUT"
121         echo 'Cannot flush logs.'
122         rm -f ${LOCK}
123         exit 1
124     fi
125
126     ##  Make sure these .old files exist, in case innd is down.
127     for F in ${LOG} ${ERRLOG} ${EXPLOG} ; do
128         if [ ! -f ${F}.old ]; then
129             rm -f ${F}.old
130             cp ${F} ${F}.old
131             cat /dev/null >${F}
132         fi
133     done
134
135     ##  Copy syslog files, truncating old inode since syslog has it open.
136     for F in ${SYSLOGS}; do
137         rm -f ${F}.old
138         cp ${F} ${F}.old
139         cat /dev/null >${F}
140     done
141     ctlinnd -s logmode
142
143     ##  Make a copy of the active file.
144     if [ -n ${ACTIVEFILE} ] ; then
145         BASE=`basename ${ACTIVEFILE}`
146         rm -f ${OLD}/${BASE}.old
147         cp ${ACTIVEFILE} ${OLD}/${BASE}.old
148     fi
149
150     ##  These are live files, so use link rather than copy.
151     for F in ${LIVEFILES} ; do
152         if [ -f ${F} ]; then
153             rm -f ${F}.old ${F}.new
154             ln ${F} ${F}.old
155             touch ${F}.new
156             chmod 0660 ${F}.new
157             mv ${F}.new ${F}
158         fi
159     done
160
161     ##  Tally control messages if we logged them.
162     test -n "${CONTROL_DATA}" && cat ${CONTROL_DATA} | ${TALLY_CONTROL}
163
164     ${PAUSED} && ctlinnd -s go "Flushing log and syslog files" 2>&1
165
166     cd ${OLD}
167     for F in ${LOGS}; do
168         ##  Process the current (just-flushed) log
169         BASE=`basename ${F}`
170         rm -f ${OLD}/${BASE}
171         case ${F} in
172         ${SYSLOG_CRIT}|${ERRLOG}|${EXPLOG}|${LOG}|${SYSLOG_NOTICE})
173             ##  Make a link that can be deleted (since if not rotating
174             ##  we delete the copy that is made in ${TMPDIR}).
175             mv ${F}.old ${OLD}/${BASE}
176             rm -f ${OLD}/${BASE}.0
177             ln ${OLD}/${BASE} ${OLD}/${BASE}.0
178             ;;
179         ${ACTIVEFILE})
180             mv ${BASE}.old ${OLD}/${BASE}
181             ;;
182         ${SYSLOG_ERR})
183             mv ${F}.old ${OLD}/${BASE}
184             ;;
185         ${UNWANTED_LOG})
186             ##  Rotate and compress the file.
187             BASE=`basename ${F}`
188             if [ ! -f ${BASE} -a -f ../${BASE} ]; then
189                 cp ../${BASE} ${BASE}
190                 chmod 0440 ${BASE}
191             fi
192             if [ -f ${BASE} ]; then
193                 ${LOG_COMPRESS} <${BASE} >${BASE}.0${Z} && rm -f ${BASE}
194                 chmod 0440 ${BASE}.0${Z}
195
196                 ##  Do rotation.
197                 if [ X${LOGCYCLES} = X ]; then       
198                     LOGCYCLES=3
199                 fi
200                 EXT=${LOGCYCLES}
201                 rm -f ${BASE}.${LOGCYCLES}${Z}
202                 while [ ${EXT} -gt 0 ] ; do
203                     NEXT=${EXT}
204                     EXT=`expr ${EXT} - 1`
205                     test -f ${BASE}.${EXT}${Z} \
206                         && rm -f ${BASE}.${NEXT}${Z} \
207                         && mv ${BASE}.${EXT}${Z} ${BASE}.${NEXT}${Z}
208                 done
209             fi
210             ##  Innreport assumes where unwanted.log exists, so leave it
211             ##  and process later.
212             ;;
213         *)
214             if [ -f ${F}.old ]; then
215                 mv ${F}.old ${OLD}/${BASE}
216             else
217                 rm -f ${OLD}/${BASE} ${F}.new
218                 touch ${F}.new
219                 chmod 0660 ${F}.new
220                 ln ${F} ${F}.old
221                 mv ${F}.new ${F}
222                 mv ${F}.old ${OLD}/${BASE}
223             fi
224             ;;
225         esac
226     done
227     cd ${HERE}
228 else
229     ##  Don't use the real OLD directory, instead use TMPDIR
230     OLD=${TMPDIR}
231
232     ##  Make a snapshot of what we need for below.
233     ctlinnd -s pause "Snapshot log and syslog files" 2>&1
234     for F in ${SYSLOG_CRIT} ${ERRLOG} ${EXPLOG} ${LOG} ${SYSLOG_NOTICE} ; do
235         BASE=`basename ${F}`
236         rm -f ${OLD}/${BASE}.0
237         cp ${F} ${OLD}/${BASE}.0
238     done
239     ctlinnd -s go "Snapshot log and syslog files" 2>&1
240 fi
241
242 ##
243 ##  We now (finally!) have copies of the log files where we need them.
244 ##
245
246 ##  Display syslog critical messages.
247 BASE=`basename ${SYSLOG_CRIT}`
248 OLD_SYSLOG=${OLD}/${BASE}.0
249 if [ -s ${OLD_SYSLOG} ] ; then
250     echo Syslog critical messages:
251     cat ${OLD_SYSLOG}
252     echo ---------
253     echo ''
254 fi
255 rm -f ${OLD_SYSLOG}
256
257 ##  Display error log.
258 BASE=`basename ${ERRLOG}`
259 OLD_ERRLOG=${OLD}/${BASE}.0
260 if [ -s ${OLD_ERRLOG} ] ; then
261     echo Error log:
262     cat ${OLD_ERRLOG}
263     echo ---------
264     echo ''
265 fi
266 rm -f ${OLD_ERRLOG}
267
268 ##  Scan for various problems in articles we were offered or sent...
269 BASE=`basename ${LOG}`
270 OLD_LOG=${OLD}/${BASE}.0
271
272 ##  and summarize syslog information.
273 BASE=`basename ${SYSLOG_NOTICE}`
274 OLD_SYSLOG=${OLD}/${BASE}.0
275 if [ -s ${OLD_SYSLOG} -o -s ${OLD_LOG} ] ; then
276     ${PATHBIN}/innreport -f ${PATHETC}/innreport.conf ${OLD_SYSLOG} ${OLD_LOG}
277     echo ---------
278     echo ''
279 fi
280 rm -f ${OLD_LOG} ${OLD_SYSLOG}
281 if ${ROTATE} ; then
282     BASE=`basename ${UNWANTED_LOG}`
283     if [ -f ${UNWANTED_LOG}.old ]; then
284         mv ${UNWANTED_LOG}.old ${OLD}/${BASE}
285     else
286         rm -f ${OLD}/${BASE}
287         cp ${UNWANTED_LOG} ${OLD}/${BASE}
288         chmod 0660 ${OLD}/${BASE}
289     fi
290 fi
291
292 OLD_SYSLOG=${OLD}/${EXPLOG}.0
293 rm -f ${EXPLOG}
294
295 ##  Compress and rotate the logs.
296 if ${ROTATE} ; then
297     cd ${OLD}
298     if [ X${LOGCYCLES} = X ]; then       
299         LOGCYCLES=3
300     fi
301     for F in ${LOGS} ; do
302         ##  Skip if it's unwanted.log, since it's already rotated
303         if [ ${F} = ${UNWANTED_LOG} ]; then
304             continue
305         fi
306         ##  Skip if file doesn't exist.
307         BASE=`basename ${F}`
308         test -f ${BASE} || continue
309
310         ##  Compress the file.
311         ${LOG_COMPRESS} <${BASE} >${BASE}.0${Z} && rm -f ${BASE}
312         chmod 0440 ${BASE}.0${Z}
313
314         ##  Do rotation.
315         EXT=${LOGCYCLES}
316         rm -f ${BASE}.${LOGCYCLES}${Z}
317         while [ ${EXT} -gt 0 ] ; do
318             NEXT=${EXT}
319             EXT=`expr ${EXT} - 1`
320             test -f ${BASE}.${EXT}${Z} \
321                 && rm -f ${BASE}.${NEXT}${Z} \
322                 && mv ${BASE}.${EXT}${Z} ${BASE}.${NEXT}${Z}
323         done
324     done
325
326     ##  Remove lock.
327     rm -f ${LOCK}
328 fi
329
330 ##  All done.
331 exit 0