summaryrefslogtreecommitdiff
path: root/bin/weblogs-cat-and-remove
blob: bc9489f7d97e439e60ea76dfddbe9f99ab55dadd (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
#!/usr/bin/ruby

require 'optparse'

ROOT = '/srv/www/vhosts'
FORSTATS_DIR = "logs-for-stat"
READBLOCKSIZE = 4096

@unlink = true

def show_help(parser, code=0, io=STDOUT)
	io.puts "Usage: #{$0} [-n] <clientname> <vhostname>"
	io.puts parser
	exit(code)
end
ARGV.options do |opts|
	opts.on_tail("-h", "--help", "Display this help screen") { show_help(opts) }
	opts.on("-n", "--no-remove"  , "Do not remove files after printing them") { @unlink = false }
	opts.parse!
end

show_help(ARGV.options, 1, STDERR) unless ARGV.length == 2
client = ARGV.shift
vhostname = ARGV.shift


root_forstats_dir = ROOT+"/"+client+'/'+FORSTATS_DIR

[ROOT, ROOT+"/"+client, ROOT+"/"+client+'/'+FORSTATS_DIR].each do |dir|
	unless File.exist?( dir )
		STDERR.puts "#{dir} does not exist"
		exit 1
	end
	unless File.stat( dir ).directory?
		STDERR.puts "#{dir} is not a directory (or does not exist or something)"
		exit 1
	end
	Dir.chdir(dir)
end

Dir.glob("#{vhostname}-access.log.*").sort.each do |filename|
	File.open( filename, "r" ) do |f|
		while r = f.read(READBLOCKSIZE) do
			STDOUT.write r
		end
	end
	File.unlink(filename) if @unlink
end