你为什么Blog? 灵异停机事件的背后----microsoft的KB925902
四月 03

之前为了方便对apache的访问日志作统计分析,参考车东的文章用cronolog控制apache的访问日志轮循,从而方便了awstats对web服务器日志进行每天的分析。不过,这之后就带来了个麻烦,原来系统设置的logroate日志压缩就不起作用了,因为之前的web服务访问量不是很大、我又分配了相当的一部分硬盘空间来应付web的日志增长,所以就这么一直将就过来:想,没关系,反正还有空间,等哪天有空了,再加一个logroate的配置,直接把日志压缩了就是了。

直到前几天,︻┳═一 [zsk.akaka.com.cn]同志的一篇文字在抓虾的热文排行里面排到了第一页,于是web服务器的访问日志猛长,看来没法再将就了,于是,这项拖延了将近8个月的工程终于开工了......


本来是打算直接在logroate里面增添几条配置解决问题,不过后来上cronolog的网站看了下,发现官方有提供一个cronolog jumbo patch,可以让cronolog在分割日志之后对旧的log文件执行一个脚本,这就可以对旧的日志进行压缩了。看起来蛮好玩的,所以我就采取这个方案了。不过,这个jumbo-patch忘记了在生成新的Log日志的时候读取相关目录的umask设置,这个问题在cronolog的1.7.0-beta版本得到了改进,所以,各位还是直接使用crono-1.7.0-beta的版本吧。

  1. 制作cronolog 1.7.0-beta的rpm:
    1. 在cronolog的官网下载cronolog 1.7.0-beta的源码包
    2. 这个cronolog-1.7.0-beta的源码包内的spec文件存在着个小错误,要做rpm包的话,还得修改一下。对cronolog.spec文件所作的修改如下:
      1. diff -Nur /tmp/old/cronolog.spec /tmp/new/cronolog.spec
      2. --- /tmp/old/cronolog.spec      2007-04-03 12:07:59.000000000 +0800
      3. +++ /tmp/new/cronolog.spec      2007-04-03 12:12:38.000000000 +0800
      4. @@ -1,6 +1,6 @@
      5. %define name cronolog
      6. -%define version 1.7
      7. -%define release 1
      8. +%define version 1.7.0
      9. +%define release beta
      10. %define group System Networking/Daemons
      11.  
      12. Summary:       a flexible log file rotation program for Apache
      13. @@ -12,7 +12,7 @@
      14. Packager:      Andrew Ford <A.Ford@ford-mason.co.uk>
      15. URL:           http://www.ford-mason.co.uk/resources/cronolog/
      16.  
      17. -Source:                http://www.ford-mason.co.uk/resources/cronolog/cronolog-%version.tar.gz
      18. +Source:                http://www.ford-mason.co.uk/resources/cronolog/cronolog-%version-beta.tar.gz
      19.  
      20. BuildRoot:     /tmp/%{name}-root
    3. 制作rpm包

      1. cd /usr/src/trustix/sources
      2. cp /tmp/cronolog-1.7.0-beta.tar.gz .
      3. tar zxvf cronolog-1.7.0-beta.tar.gz
      4. cd /usr/src/trustix/specs
      5. cp /usr/src/trustix/sources/cronolog-1.7.0/cronolog.spec .
      6. patch -p2</tmp/cronolog_1.7.0-beta_spec.patch
      7. rpm -bb --target i686 /usr/src/trustix/specs/cronolog.spec
  2. 在Web服务器上安装新的cronolog,并安装相关环境:
    • 安装rpm

      1. rpm -ivh --force cronolog-1.7.0-beta.i686.rpm
  3. 修改apache的配置,使用cronolog轮循的日志使用gzip压缩输出,相关配置示例如下:
    1. CustomLog "|/usr/sbin/cronolog -r /usr/bin/gzip /var/log/httpd/XXX.XXX.com.cn-access_log.%Y%m%d" combined

    就是多加上个

    -r /usr/bin/gzip

    啦。

  4. 记得让apache重新读取一下新的配置文件
    1. service httpd reload

于是至此,以后就不必再为那些不断增长的庞大日志操心了;于是,这项拖拉了8个多月的懒惰工程就此收工了,好像真的也没有花费多少时间的说,是为懒惰故事之八。

附录:

cronolog-1.7.0-beta针对cronolog-1.6.2改进的功能列表

  • ability to launch a helper program on every log file after it is closed if multiple cronologs writing to the same file, only one helper is launched.
  • signal handling to ensure that every log file has a chance to be handled by that helper and that cronolog doesnt ever hang apache restarts
  • alarm() used so log files rotate on time period boundary even if there is no traffic
  • uid/gid patch from Isaac Wilcox minus the configure changes which broke the build for me.
  • the large file patch from matt lanier with tweaks from me to work with my own changes above.
  • incorporated a slightly different implementation of the SIGUSR1 patch from Prakash Kailasa
  • the s/stat/lstat/ fix from Victor Martinez

Leave a Reply