Re: [squid-users] squid log with date ext

From: Nicolás Ruiz <nicolas@dont-contact.us>
Date: Fri, 11 May 2007 12:51:45 -0400

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Adrian Chadd wrote:
> On Thu, May 10, 2007, Kinkie wrote:
>> On 5/10/07, groupslist@gmail.com <groupslist@gmail.com> wrote:
>>> Hi,
>>>
>>> Is it possible to generate squid log file with date extension
>>> (like /var/log/squid/access.log-`date +%Y%m%d` format) in real time (i mean
>>> it is not generated by logrotate) ?
>> Currently it's not possible.
>> You can rename old files after rotating them with "squid -k rotate";
>> it's a relatively simple exercise in shell scripting.
>
> And if someone writes it up I'd be happy to include it in the base distribution.
> The trick: use head -1 and tail -1 on the rotated logfile to figure out its
> time span, then rename the logfile to that..

I use the following combination of crontab and script to rename and
produce a calamaris report

# Rotate squid logs
0 4 * * * /usr/sbin/squid -k rotate
# And generate report of freshly rotated log
5 4 * * * /usr/local/bin/Daily_Calamaris

Daily_Calamaris:

#!/bin/bash
WHEN=`/bin/date -d yesterday +%Y%m%d`
LOGDIR="/var/spool/squid"
OUTDIR="/var/spool/squid"
LOGFILE="${LOGDIR}/squid_access.log.0"
NEWFILE="${LOGDIR}/squid_access.log.${WHEN}"
OUTFILE_TXT="${OUTDIR}/squid_report_${WHEN}.txt"
OUTFILE_HTML="${OUTDIR}/squid_report_${WHEN}.html"
/bin/mv ${LOGFILE} ${NEWFILE}
/usr/bin/nice -n 19 \
  /bin/cat ${NEWFILE} | \
  /usr/bin/calamaris -a -n --domain-report 50 \
  --requester-report 100 > ${OUTFILE_TXT}
/usr/bin/nice -n 19 \
  /bin/cat ${NEWFILE} | \
  /usr/bin/calamaris -F html -a -n --domain-report 50 \
  --requester-report 100 > ${OUTFILE_HTML}

- -------------------------------------------------
What Adrian is asking for could be done with something like (is there a
better way to get the time string than using perl?)

#!/bin/bash
LOGDIR="/var/spool/squid"
OUTDIR="/var/spool/squid"
LOGFILE="${LOGDIR}/squid_access.log.0"
FROM=`head -1 ${LOGFILE} | cut -d. -f1 | perl -e \
  '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = \
  localtime(<STDIN> - 4 * 60 * 60 ); \
  printf("%04d:%02d:%02d-%02d:%02d:%02d\n", \
         $year+1900, \
         $mon+1, \
         $mday, \
         $hour, \
         $min, \
         $sec)'`
TO=`tail -1 ${LOGFILE} | cut -d. -f1 | perl -e \
  '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = \
  localtime(<STDIN> - 4 * 60 * 60 ); \
  printf("%04d:%02d:%02d-%02d:%02d:%02d\n", \
         $year+1900, \
         $mon+1, \
         $mday, \
         $hour, \
         $min, \
         $sec)'`
NEWFILE="${LOGDIR}/squid_access.log.${FROM}--${TO}"
OUTFILE_TXT="${OUTDIR}/squid_report_${FROM}--${TO}.txt"
OUTFILE_HTML="${OUTDIR}/squid_report_${FROM}--${TO}.html"
/bin/mv ${LOGFILE} ${NEWFILE}
/usr/bin/nice -n 19 \
  /bin/cat ${NEWFILE} | \
  /usr/bin/calamaris -a -n --domain-report 50 \
  --requester-report 100 > ${OUTFILE_TXT}
/usr/bin/nice -n 19 \
  /bin/cat ${NEWFILE} | \
  /usr/bin/calamaris -F html -a -n --domain-report 50 \
  --requester-report 100 > ${OUTFILE_HTML}

- ----------------
Note that the server is in timezone GMT-4, and the server is using UTC,
that's why (a) the crontab runs at 4 am (midnight local time) and (b) I
have to compute the localtime for <STDIN> - 4 * 60 * 60.

Hope it helps

>
>
>
> Adrian
>
>

- --
A: Because it destroys the flow of conversation.
Q: Why is top posting dumb?
- --
Juan Nicolás Ruiz | Corporación Parque Tecnológico de Mérida
                     | Centro de Cálculo Cientifico ULA
nicolas@ula.ve | Avenida 4, Edif. Gral Masini, Ofic. B-32
+58-(0)274-252-4192 | Mérida - Edo. Mérida. Venezuela
PGP Key fingerprint = CDA7 9892 50F7 22F8 E379 08DA 9A3B 194B D641 C6FF
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGRJ8hmjsZS9ZBxv8RAl9lAJ41K0kjG2VfiM+Wls72N99ke7QIpQCbBmk0
89Wv/ADxXLJ/tt8OGGYSJP4=
=HjSv
-----END PGP SIGNATURE-----
Received on Fri May 11 2007 - 10:54:04 MDT

This archive was generated by hypermail pre-2.1.9 : Fri Jun 01 2007 - 12:00:05 MDT