summaryrefslogtreecommitdiff
path: root/bin/ldap2awstats
blob: c71be994d6e8411d78e1cbbb93ace8cf49e97f53 (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
80
81
82
#!/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 "ldap"
require "getoptlong"
require "myldap"
require "yaml"
require "fileutils"
require "optparse"

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.parse!
end

@config = YAML::load( File.open( '/etc/noreply/config' ) )
ldap = MyLDAP.new(@config, "ldap2apache")
clients = ldap.conn.search2(@config['basedn'], LDAP::LDAP_SCOPE_SUBTREE, 'objectclass=tnClient')

files = {}

clients.each{ |c|
	c['vhosts'] = ldap.conn.search2(c['dn'][0], LDAP::LDAP_SCOPE_SUBTREE, '(&(objectclass=tnWebVHost)(tnHost='+@config['thishost']+'))')
	client_name = c['o'][0] or throw "No name (o) for #{d['dn'][0]}"
	c['vhosts'].each{ |vhost|
		server_name = vhost['tnWebVHostServerName'][0]
		server_admin = vhost['tnWebVHostWebmaster'][0]
		server_aliases = (vhost['tnWebVHostServerAlias'] or []).join(" ")

		property = {}
		if vhost['tnWebVHostProperties']
			vhost['tnWebVHostProperties'].each{ |prop|
				(key, val) = prop.split('=', 2)
				property[key] = val
			}
		end
		next if property['stats']=='no'


		config = []
		config << 'Include "/etc/awstats/awstats.conf"'
		config << 'LogFile="/usr/local/bin/weblogs-cat-and-remove '+client_name+' '+server_name+' |"'
		config << 'SiteDomain="'+server_name+'"'
		config << 'HostAliases="'+server_aliases+' '+server_name+'"'

		files[ 'awstats.'+server_name+'.conf'  ] = config.join("\n") + "\n"
	}
}

Dir.entries( '/etc/awstats/' ).each{ |e|
	next if ((e =~ /^\./) != nil)
	next if ((e =~ /^awstats.conf/) != nil)
 
	filename = '/etc/awstats/' + e
 
	unless files.has_key?(e) then
		File.unlink(filename)
	end
}

files.each_pair{ |file, conf|
	filename = '/etc/awstats/' + file

	on_disk = FileTest.exists?(filename) ?  File.read(filename) : nil
	next if on_disk == conf
	f = File.new( filename, "w" )
	f.write(conf)
	f.close
 
}