summaryrefslogtreecommitdiff
path: root/bin/obsolete/ldap2analog.rb
blob: 533536054ca8d7c725b965ae64d78c50fc48bb99 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/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"

# defaults
$force_rebuild = false

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("-f", "--force"              , String, "Force rebuilding the cachefiles")                         { $force_rebuild = true }
	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')
@config_dir = @config['module']['analog']['config']
@analog_dns = @config['module']['analog']['dns']
@analog_caches = @config['module']['analog']['caches']
@analog_output = @config['module']['analog']['output']
@rmagic_dest = @config['module']['analog']['rmagic']

@analog_common = <<EOF
FILEEXCLUDE /web-stats/*
FILEEXCLUDE /phpmyadmin/*
ALL ON
WARNINGS -MR
TIMECOLS	bRP
WEEKCOLS	bRP
MONTHCOLS	bRP
WEEKCOLS	bRP
DAYREPCOLS	bRP
DAYSUMCOLS	bRP
HOURREPCOLS	bRP
HOURSUMCOLS	bRP
WEEKHOURCOLS	bRP
QUARTERREPCOLS	bRP
QUARTERSUMCOLS	bRP
FIVEREPCOLS	bRP
FIVESUMCOLS	bRP
HOSTCOLS		DERSsPpQqBbCc
ORGCOLS	DERSsPpQqBbCc
DOMCOLS	NDERrSsPpQqBbCc
DIRCOLS	NDERrSsPpQqBbCc
REFCOLS	NDERrSsPpQqBbCc
REFSITECOLS	NDERrSsPpQqBbCc
SEARCHQUERYCOLS	NDERrSsPpQqBbCc
SEARCHWORDCOLS	NDERrSsPpQqBbCc
INTSEARCHQUERYCOLS	NDERrSsPpQqBbCc
INTSEARCHWORDCOLS	NDERrSsPpQqBbCc
BROWREPCOLS	NDERrSsPpQqBbCc
BROWSUMCOLS	NDERrSsPpQqBbCc
OSCOLS	NDERrSsPpQqBbCc
VHOSTCOLS	NDERrSsPpQqBbCc
USERCOLS	NDERrSsPpQqBbCc
REQCOLS	NDERrSspqBbCc
TYPECOLS	NDERrSsBbCc
REDIRCOLS	NDERrSs
FAILCOLS	NDERrSs
REDIRHOSTCOLS	NDERrSs
FAILHOSTCOLS	NDERrSs
REDIRREFCOLS	NDERrSs
FAILREFCOLS	NDERrSs
REDIRVHOSTCOLS	NDERrSs
FAILVHOSTCOLS	NDERrSs
REDIRUSERCOLS	NDERrSs
FAILUSERCOLS	NDERrSs
STATUSCOLS	DERrSs
SIZECOLS	DERrSsPpQqBbCc
PROCTIMECOLS	DERrSsPpQqBbCc
EOF

files = {}
files['analog'] = {}
files['rmagic'] = {}

def mkdir(dir, mode, uid, gid)
	Dir.mkdir(dir, mode) unless File.exists?(dir)
#	File.chown(uid, gid, dir) == 1 or throw "chown #{dir} failed"
end

clients.each{ |c|
	c['vhosts'] = ldap.conn.search2(c['dn'][0], LDAP::LDAP_SCOPE_SUBTREE, '(&(objectclass=tnWebVHost)(tnHost='+@config['thishost']+'))')
	client_home = c['homeDirectory'][0] or throw "No home for #{d['dn'][0]}"
	c['vhosts'].each{ |vhost|
		server_name = vhost['tnWebVHostServerName'][0]
		server_admin = vhost['tnWebVHostWebmaster'][0]
		server_aliases = (vhost['tnWebVHostServerAlias'] or []).join(" ")
		config = []
		config << "LOGFILE none"
		config << "LOGFILE " + client_home + "/logs-for-stat/" + server_name + "-access.*"

		cache_file = @analog_caches + "/" + server_name + ".cache-%s"%(Time.now.strftime("%Y%m%d%S"))
		cache_file_cur = @analog_caches + "/" + server_name + ".cache-current"
		cache_file_old = @analog_caches + "/" + server_name + ".cache-old"
		File.delete(cache_file_old) if File.exist?(cache_file_old)
		File.rename(cache_file_cur, cache_file_old) if File.symlink?(cache_file_cur)
		File.symlink(cache_file, cache_file_cur)

		unless $force_rebuild then
			config << "CACHEFILE " + cache_file_old
		end
		config << "CACHEOUTFILE " + cache_file
		config << "HOSTNAME " + server_name
		config << "HOSTURL " + "http://" + server_name + "/"
		config << "OUTPUT COMPUTER"
		config << "OUTFILE " + @analog_output + "/" + server_name + ".dat"
		config << "DNS WRITE"
		config << "DNSFILE " + @analog_dns + "/cachefile"

		(vhost['tnWebVHostServerName']+(vhost['tnWebVHostServerAlias'] or [])).each { |host|
			config << "REFREPEXCLUDE http://" + host + "/*"
		}
		
		(vhost['tnWebVHostServerAlias'] or []).each { |host|
			config << "HOSTALIAS " + host + " " + server_name
		}

		config << "DNSLOCKFILE " + @analog_dns + "/cachefile.lock"
		files['analog'][ server_name ] = config.join("\n") + "\n" + @analog_common

		config = []
		config << "[statistics]"
		config << "File_In = " + @analog_output +"/"+ server_name + ".dat"
		config << "Frame_File_Out = index.html"
		config << "Reverse_Time = 1"
		config << "Verbose=WE"
		config << "Language=de"
		config << ""
		config << "[reports]"
		config << "Active_Column=R"
		#config << "GraphType = bar,pie"
		config << "Font = Helvetica, sans-serif"
		config << "Font = Helvetica, sans-serif"
		config << "Font_Color = #000000"
		config << "Title_Font = Helvetica"
		config << "Title_BG_Color = #000000"
		config << "Title_Font_Color = #FFFFFF"
		config << "Data_Font = Helvetica"
		config << "Data_Color_2=#EEEEEE"
		config << "Data_Header_Color = #9999CC"
		config << "Data_Total_Color = #CCCCCC"
		config << "#--- "
		config << "# Note how we use a directory (rather than a filename) to "
		config << "# tell it to put each reports in a separate file."
		config << "# (MAC: To use this change to ':manyfile:')"
		config << "File_Out = " + @rmagic_dest +"/"+ server_name + "/"

		config << ""
		config << "[GENERAL]"
		config << "Rows = ALL"
		#config << "Rows = SR, PR, FL, RR, NF, NH, BT"

		config << ""
		config << "[navigation]"
		config << "#Page_Font = Helvetica, sans-serif"
		config << "#Page_Font_Color = #000000"
		config << "#Page_BG_Color=#CCCCFF"
		config << "File_Out = navfile.html"
		config << ""
		config << "[website]"
		config << "Title = " + server_name + " Web Site Report"
		config << "Webmaster = webmaster@multi24.com"
		config << "Base_URL = http://" + server_name + "/"
		files['rmagic'][ server_name ] = config.join("\n") + "\n"
	}
}

Dir.entries( @rmagic_dest ).each{ |e|
	next if ((e =~ /^\./) != nil)
	next if files['rmagic'].has_key?( e )

	FileUtils.rm Dir.glob(@rmagic_dest+'/'+e+'/*')
	Dir.rmdir(@rmagic_dest+'/'+e)
}
%w(rmagic analog).each{ |tool|
	Dir.entries( @config_dir + "/" + tool ).each{ |e|
		next if ((e =~ /^\./) != nil)
		next if files[tool].has_key?( e )
		File.unlink( @config_dir + '/'+tool+'/' + e )
	}

	files[tool].each_pair{ |name, config|
		filename = @config_dir + '/' +tool+ '/' + name
		f = File.new( filename, "w" )
		f.write(config)
		f.close
	}
}