summaryrefslogtreecommitdiff
path: root/bin/weblogs-merge
blob: 553bc494496cb300ce99b34c8a6f3df2f87a7a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/ruby

#
# Copyright (c) 2004,2005 Florian Reitmeir <florian@reitmeir.org>
# based on a script from Peter Palfrader <peter@palfrader.org>
#
# 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
	}
}