From caf9c0b788e01702a94c3540a9e83e79628ce801 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Wed, 18 Oct 2006 14:06:23 +0000 Subject: fix -merge git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@211 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- bin/weblogs-merge | 76 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/bin/weblogs-merge b/bin/weblogs-merge index 8d2d35d..209f0e7 100755 --- a/bin/weblogs-merge +++ b/bin/weblogs-merge @@ -12,8 +12,10 @@ require 'optparse' require 'find' -$VERBOSE = nil -$NOTHING = nil +$VERBOSE = false +$NOTHING = false + +READBLOCKSIZE = 4096 def show_help(parser, code=0, io=STDOUT) io.puts parser @@ -21,19 +23,40 @@ def show_help(parser, code=0, io=STDOUT) end ARGV.options do |opts| opts.on_tail("-h", "--help", "Display this help screen") { show_help(opts) } - opts.on("-v", "--verbose" , nil, "Be verbose") { $VERBOSE = 1 } - opts.on("-n", "--do-nothing" , nil, "Do nothing") { $VERBOSE = 1; $NOTHING = 1 } + opts.on("-v", "--verbose" , nil, "Be verbose") { $VERBOSE = true } + opts.on("-n", "--do-nothing" , nil, "Do nothing") { $VERBOSE = true; $NOTHING = true } opts.parse! end root = '/srv/www/vhosts/' dir = "/logs-archive/" -year = Time.new.year.to_s -month = Time.new.month.to_s +this_year = Time.new.year.to_s +this_month = Time.new.month.to_s Dir.chdir(root) +def append_and_remove(sourcefile, targetfile) + puts " #{sourcefile} >> #{targetfile}" if $VERBOSE + next if $NOTHING + File.open( targetfile, "a" ) do |targetfh| + File.open( sourcefile, "r" ) do |sourcefh| + while r = sourcefh.read(READBLOCKSIZE) do + targetfh.write r + end + end + end + File.unlink(sourcefile) +end + +def getfilelist(dir) + files = Array.new + Find.find(dir) { |file| + files.push(file) if File.file?(file) + } + files.sort +end + Dir.glob("*").each{ |client| Dir.chdir(root) next unless File.stat( client ).directory? @@ -42,8 +65,8 @@ Dir.glob("*").each{ |client| Dir.chdir(client+dir) - if $VERBOSE then - puts 'pwd :'+root+"/"+client+"/"+dir + if $VERBOSE + puts 'pwd :'+root+"/"+client+"/"+dir end if ! File.writable?(root+"/"+client+"/"+dir) then @@ -51,28 +74,21 @@ Dir.glob("*").each{ |client| exit 1 end - files = Array.new - - Find.find(root+"/"+client+"/"+dir) { |file| - if File.file?(file) then - files.push(file); - end + getfilelist(root+"/"+client+"/"+dir).each { |sourcefile| + m = /(.*-(?:access|error).log.)(\d+)-(\d+).gz/.match(sourcefile) + next unless m + dummy, name, year, month = m.to_a + next if this_year == year + targetfile = "#{name}#{year}.gz" + append_and_remove(sourcefile, targetfile) } - files.sort! # sort by date (name in this case) - - files.each { |file| - if file =~ %r{(.*)-(access|error).log.(\d+)-(\d)+-(\d+)(-\d+)?.gz} then - if ($3 == year && $4 == month) then - next - else - store = [$1,$2,$3].join('-')+'.gz' - end - puts " #{file} >> #{store}" if $VERBOSE - if $NOTHING && system('cat '+file+' >>'+store) then - File.unlink(file) - end - else - puts 'did not match: '+file - end + + getfilelist(root+"/"+client+"/"+dir).each { |sourcefile| + m = /(.*-(?:access|error).log.)(\d+)-(\d+)-(\d+).gz/.match(sourcefile) + next unless m + dummy, name, year, month, day = m.to_a + next if this_year == year && this_month == month + targetfile = "#{name}#{year}-#{month}.gz" + append_and_remove(sourcefile, targetfile) } } -- cgit v1.2.3