From 1b2b1db5d7c0240237fda8988ef419c5a01c3042 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Wed, 18 Oct 2006 13:11:27 +0000 Subject: Do weblogs git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@203 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- bin/weblogs-compress | 41 +++++++++++++++++++++++++++ bin/weblogs-merge | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/weblogs-rotate | 46 ++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100755 bin/weblogs-compress create mode 100755 bin/weblogs-merge create mode 100755 bin/weblogs-rotate diff --git a/bin/weblogs-compress b/bin/weblogs-compress new file mode 100755 index 0000000..f731e47 --- /dev/null +++ b/bin/weblogs-compress @@ -0,0 +1,41 @@ +#!/usr/bin/ruby + +require 'optparse' + +$VERBOSE = nil + +def show_help(parser, code=0, io=STDOUT) + io.puts parser + exit(code) +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.parse! +end + + +root = '/srv/www/vhosts' +Dir.chdir(root) + +Dir.glob("*").each{ |client| + Dir.chdir(root) + next unless File.stat( client ).directory? + next unless File.exist?( client + "/logs" ) + next unless File.stat( client + "/logs" ).directory? + + logroot = root+"/"+client+"/logs" + Dir.chdir(logroot) + Dir.glob("*.log.*").each{ |filename| + match = /^(.*)\.log\.[0-9]{4,4}-[0-9][0-9]-[0-9][0-9]$/.match(filename) + next unless match + + if File.exist?(filename+".gz") or + File.exist?(filename+".bz2") + STDERR.puts("Not compressing #{filename}: Target exists."); + else + puts "Compressing #{filename}" if $VERBOSE + system("gzip -9 #{filename}") or throw("system(gzip -9 #{filename}) failed.") + end + } +} diff --git a/bin/weblogs-merge b/bin/weblogs-merge new file mode 100755 index 0000000..553bc49 --- /dev/null +++ b/bin/weblogs-merge @@ -0,0 +1,79 @@ +#!/usr/bin/ruby + +# +# Copyright (c) 2004,2005 Florian Reitmeir +# based on a script from Peter Palfrader +# +# All rights reserved. +# +# MIT license. +# + +require 'optparse' +require 'find' + +$VERBOSE = nil +$NOTHING = nil + +def show_help(parser, code=0, io=STDOUT) + io.puts parser + exit(code) +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.parse! +end + +root = '/var/www/' +dir = "/logs-archive/" + +year = Time.new.year.to_s +month = Time.new.month.to_s + +Dir.chdir(root) + +Dir.glob("*").each{ |client| + Dir.chdir(root) + next unless File.stat( client ).directory? + next unless File.exist?( client + dir ) + next unless File.stat( client + dir ).directory? + + Dir.chdir(client+dir) + + if $VERBOSE then + puts 'pwd :'+root+client+dir + end + + if ! File.writable?(root+client+dir) then + puts 'log dir not writeable' + exit 1 + end + + files = Array.new + + Find.find(root+client+dir) { |file| + if File.file?(file) then + files.push(file); + end + } + 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 ' src: '+file if $VERBOSE + puts 'dest: '+store if $VERBOSE + if $NOTHING && system('cat '+file+' >>'+store) then + File.unlink(file) + end + else + puts 'did not match: '+file + end + } +} diff --git a/bin/weblogs-rotate b/bin/weblogs-rotate new file mode 100755 index 0000000..98e969f --- /dev/null +++ b/bin/weblogs-rotate @@ -0,0 +1,46 @@ +#!/usr/bin/ruby + +require 'optparse' + +$VERBOSE = nil + +def show_help(parser, code=0, io=STDOUT) + io.puts parser + exit(code) +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.parse! +end + + +root = '/srv/www/vhosts' +Dir.chdir(root) + +today = Time.new.strftime("%Y-%m-%d"); +Dir.glob("*").each{ |client| + Dir.chdir(root) + next unless File.stat( client ).directory? + next unless File.exist?( client + "/logs" ) + next unless File.stat( client + "/logs" ).directory? + + logroot = root+"/"+client+"/logs" + Dir.chdir(logroot) + Dir.glob("*.log").each{ |filename| + targetname = filename +"."+ today + + if File.exist?(targetname) or + File.exist?(targetname+".gz") or + File.exist?(targetname+".bz2") + STDERR.puts("Not moving #{filename}: Target exists."); + else + puts "Moving #{filename}" if $VERBOSE + File.rename( filename , targetname ); + end + } +} + +system ("/usr/sbin/apache2ctl graceful") or throw "system apache2ctl graceful failed" +## graceful means that apache might not close the old logfile immediately +## therefore we compress an hour later in a different script -- cgit v1.2.3