为了节省日志服务器的cpu,在awstats配置文件里我是这样写的

         LogFile="/path_to_log/%YYYY%MM%DD-24"

         意思就是只分析前一天的日志.但是没有想到awstats.pl这哥很不给力,每遇到跨月的时候总会扔给我一个错误:
 
 
  
  1. /path_to_awstats/awstats.pl -update -config=vod -lang=cn 
  2. Create/Update database for config "/etc/awstats/vod.conf" by AWStats version 6.95 (build 1.943) 
  3. From data in log file "/path_to_log/20110328"... 
  4. Error: Couldn't open server log file "/path_to_log/20110328" : No such file or directory 
  5. Setup ('/etc/awstats/vod.conf' file, web server or permissions) may be wrong. 
  6. Check config file, permissions and AWStats documentation (in 'docs' directory). 

         提示:No such file or directory,仔细一看,明明才3.1号,结果给的是3.28号.看来是perl拼日期的时候拼错了.网上搜索一番发现竟然没有人提到这样的问题,难道大家分析日志的时候都是直接分析"path_to_log/*"?不管了,想想解决办法.分析awstats的源代码?我是没那爱好,先看下awstats.pl的参数吧.我很惊喜的发现了-LogFile参数.

         -LogFile=x     to change log to analyze whatever is 'LogFile' in config file
           Be care to process log files in chronological order when updating statistics.

         于是试了一下:
 
 
  
  1. /path_to_awstats/awstats.pl -update -config=vod -lang=cn -LogFile=/path_to_log/20110228 
  2. Create/Update database for config "/etc/awstats/vod.conf" by AWStats version 6.95 (build 1.943) 
  3. From data in log file "/path_to_log/20110228"… 
  4. Phase 1 : First bypass old records, searching new record... 
  5. Direct access to last remembered record has fallen on another record. 
  6. So searching new records from beginning of log file... 
  7. Jumped lines in file: 0 
  8. Parsed lines in file: 2507347 
  9.  Found 0 dropped records, 
  10.  Found 1 corrupted records, 
  11.  Found 2507346 old records, 
  12.  Found 0 new qualified records. 

         不错,还真好使,那怎么得到正确的日期呢?

         先试下php:
 
 
  
  1. php -r 'var_dump(date("Ymd",time()-86400));' 
  2. string(8) "20110228" 

         再试一下shell:
 
 
  
  1. date +%Y%m%d -d yesterday 
  2. 20110228 

         余下的事儿就简单了,拼个字符串,然后执行一下就好了,我选择的是php:
 
 
  
  1. <?php 
  2. system('/path_to_awstats/awstats.pl -update -config=vod -lang=cn -LogFile='.date('Ymd',time()-86400)); 
  3. ?>