#!/usr/bin/ruby # # Copyright (c) 2004,2005 Florian Reitmeir # based on a script from Peter Palfrader # # 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 }