1 rizwank 1.1 #!/usr/bin/perl
2 #-------------------------------------------------------
3 # This script configures AWStats so that it works immediately.
4 # - Get Apache config file from registry (ask if not found)
5 # - Change common log to combined (ask to confirm)
6 # - Add AWStats directives
7 # - Restart web server
8 # - Create AWStats config file
9 # See COPYING.TXT file about AWStats GNU General Public License.
10 #-------------------------------------------------------
11 # $Revision: 1.4 $ - $Author: eldy $ - $Date: 2005/01/15 22:42:58 $
12 require 5.005;
13
14 use strict;
15
16
17 #-------------------------------------------------------
18 # IF YOU ARE A PACKAGE BUILDER, CHANGE THIS TO MATCH YOUR PATH
19 # SO THAT THE CONFIGURE WILL WORK ON YOUR DISTRIB !!!
20 # Following path are the one
21 #-------------------------------------------------------
22 rizwank 1.1 use vars qw/
23 $AWSTATS_PATH
24 $AWSTATS_ICON_PATH
25 $AWSTATS_CSS_PATH
26 $AWSTATS_CLASSES_PATH
27 $AWSTATS_CGI_PATH
28 $AWSTATS_MODEL_CONFIG
29 $AWSTATS_DIRDATA_PATH
30 /;
31 $AWSTATS_PATH='';
32 $AWSTATS_ICON_PATH='/usr/local/awstats/wwwroot/icon';
33 $AWSTATS_CSS_PATH='/usr/local/awstats/wwwroot/css';
34 $AWSTATS_CLASSES_PATH='/usr/local/awstats/wwwroot/classes';
35 $AWSTATS_CGI_PATH='/usr/local/awstats/wwwroot/cgi-bin';
36 $AWSTATS_MODEL_CONFIG='/etc/awstats/awstats.model.conf'; # Used only when configure ran on linux
37 $AWSTATS_DIRDATA_PATH='/var/lib/awstats'; # Used only when configure ran on linux
38
39
40
41 #-------------------------------------------------------
42 # Defines
43 rizwank 1.1 #-------------------------------------------------------
44 # For windows registry management
45 my $reg;
46 eval('use Win32::TieRegistry ( Delimiter=>"/", TiedRef=>\$reg )');
47
48 use vars qw/ $REVISION $VERSION /;
49 $REVISION='$Revision: 1.4 $'; $REVISION =~ /\s(.*)\s/; $REVISION=$1;
50 $VERSION="1.0 (build $REVISION)";
51
52 use vars qw/
53 $DIR $PROG $Extension $Debug
54 /;
55
56 use vars qw/
57 @WEBCONF
58 /;
59 # Possible dirs for Apache conf files
60 @WEBCONF=(
61 'C:/Program Files/Apache Group/Apache2/conf/httpd.conf',
62 'C:/Program Files/Apache Group/Apache/conf/httpd.conf',
63 '/etc/httpd/httpd.conf',
64 rizwank 1.1 '/usr/local/apache/conf/httpd.conf',
65 '/usr/local/apache2/conf/httpd.conf'
66 );
67
68 use vars qw/
69 $WebServerChanged $UseAlias $Step
70 %LogFormat %ConfToChange
71 %OSLib
72 /;
73 $WebServerChanged=0;
74 $UseAlias=0;
75 %LogFormat=();
76 %ConfToChange=();
77 %OSLib=('linux'=>'Linux, BSD or Unix','macosx'=>'Mac OS','windows'=>'Windows');
78 $Step=0;
79
80
81
82 #-------------------------------------------------------
83 # Functions
84 #-------------------------------------------------------
85 rizwank 1.1
86 #-------------------------------------------------------
87 # error
88 #-------------------------------------------------------
89 sub error {
90 print "Error: $_[0].\n";
91 exit 1;
92 }
93
94 #-------------------------------------------------------
95 # debug
96 #-------------------------------------------------------
97 sub debug {
98 my $level = $_[1] || 1;
99 if ($Debug >= $level) {
100 my $debugstring = $_[0];
101 if ($ENV{"GATEWAY_INTERFACE"}) { $debugstring =~ s/^ /   /; $debugstring .= "<br>"; }
102 print "DEBUG $level - ".time." : $debugstring\n";
103 }
104 0;
105 }
106 rizwank 1.1
107 #-------------------------------------------------------
108 # update_httpd_config
109 # Replace common to combined in Apache config file
110 #-------------------------------------------------------
111 sub update_httpd_config
112 {
113 my $file=shift;
114 if (! $file) { error("Call to update_httpd_config with wrong parameter"); }
115
116 open(FILE, $file) || error("Failed to open $file for update");
117 open(FILETMP, ">$file.tmp") || error("Failed to open $file.tmp for writing");
118
119 # $%conf contains param and values
120 my %confchanged=();
121 my $conflinenb = 0;
122
123 # First, change values that are already present in old config file
124 while(<FILE>) {
125 my $savline=$_;
126
127 rizwank 1.1 chomp $_; s/\r//;
128 $conflinenb++;
129
130 # Remove comments not at beginning of line
131 $_ =~ s/\s#.*$//;
132
133 # Change line
134 if ($_ =~ /^CustomLog\s(.*)\scommon$/i) { $savline="CustomLog $1 combined"; }
135
136 # Write line
137 print FILETMP "$savline";
138 }
139
140 close(FILE);
141 close(FILETMP);
142
143 # Move file to file.sav
144 if (rename("$file","$file.old")==0) {
145 error("Failed to make backup of current config file to $file.old");
146 }
147
148 rizwank 1.1 # Move tmp file into config file
149 if (rename("$file.tmp","$file")==0) {
150 error("Failed to move tmp config file $file.tmp to $file");
151 }
152
153 return 0;
154 }
155
156 #-------------------------------------------------------
157 # update_awstats_config
158 # Update an awstats model [to another one]
159 #-------------------------------------------------------
160 sub update_awstats_config
161 {
162 my $file=shift;
163 my $fileto=shift||"$file.tmp";
164
165 if (! $file) { error("Call to update_awstats_config with wrong parameter"); }
166 if ($file =~ /Developpements[\\\/]awstats/i) {
167 print " This is my dev area. Don't touch.\n";
168 return;
169 rizwank 1.1 } # To avoid script working in my dev area
170
171 open(FILE, $file) || error("Failed to open '$file' for read");
172 open(FILETMP, ">$fileto") || error("Failed to open '$fileto' for writing");
173
174 # $%conf contains param and values
175 my %confchanged=();
176 my $conflinenb = 0;
177
178 # First, change values that are already present in old config file
179 while(<FILE>) {
180 my $savline=$_;
181
182 chomp $_; s/\r//;
183 $conflinenb++;
184
185 # Remove comments not at beginning of line
186 $_ =~ s/\s#.*$//;
187
188 # Extract param and value
189 my ($param,$value)=split(/=/,$_,2);
190 rizwank 1.1 $param =~ s/^\s+//; $param =~ s/\s+$//;
191 $value =~ s/#.*$//;
192 $value =~ s/^[\s\'\"]+//; $value =~ s/[\s\'\"]+$//;
193
194 if ($param) {
195 # cleanparam is param without begining #
196 my $cleanparam=$param; my $wascleaned=0;
197 if ($cleanparam =~ s/^#//) { $wascleaned=1; }
198 if (defined($ConfToChange{"$cleanparam"}) && $ConfToChange{"$cleanparam"}) { $savline = ($wascleaned?"#":"")."$cleanparam=\"".$ConfToChange{"$cleanparam"}."\"\n"; }
199 }
200 # Write line
201 print FILETMP "$savline";
202 }
203
204 close(FILE);
205 close(FILETMP);
206
207 if ($fileto eq "$file.tmp") {
208 # Move file to file.sav
209 if (rename("$file","$file.old")==0) {
210 error("Failed to make backup of current config file to $file.old");
211 rizwank 1.1 }
212
213 # Move tmp file into config file
214 if (rename("$fileto","$file")==0) {
215 error("Failed to move tmp config file $fileto to $file");
216 }
217 # Remove .old file
218 unlink "$file.old";
219 }
220 else {
221 print " Config file $fileto created.\n";
222 }
223 return 0;
224 }
225
226
227
228 #-------------------------------------------------------
229 # MAIN
230 #-------------------------------------------------------
231 ($DIR=$0) =~ s/([^\/\\]+)$//; ($PROG=$1) =~ s/\.([^\.]*)$//; $Extension=$1;
232 rizwank 1.1 $DIR||='.'; $DIR =~ s/([^\/\\])[\\\/]+$/$1/;
233
234 my $QueryString=""; for (0..@ARGV-1) { $QueryString .= "$ARGV[$_] "; }
235 if ($QueryString =~ /debug=/i) { $Debug=$QueryString; $Debug =~ s/.*debug=//; $Debug =~ s/&.*//; $Debug =~ s/ .*//; }
236
237 my $helpfound=0;
238 my $OS='';
239 my $CR='';
240 for (0..@ARGV-1) {
241 if ($ARGV[$_] =~ /^-*h/i) { $helpfound=1; last; }
242 if ($ARGV[$_] =~ /^-*awstatspath=([^\s\"]+)/i) { $AWSTATS_PATH=$1; last; }
243 }
244 # If AWSTATS_PATH was not forced on command line
245 if (! $AWSTATS_PATH) {
246 $AWSTATS_PATH=($DIR eq '.'?'..':$DIR);
247 $AWSTATS_PATH=~s/tools[\\\/]?$//;
248 $AWSTATS_PATH=~s/[\\\/]$//;
249 }
250 # On utilise le format de spearateur / partout (dans Apache et appels Perl)
251 $AWSTATS_PATH =~ s/\\/\//g;
252
253 rizwank 1.1 # Show usage help
254 if ($helpfound) {
255 print "----- AWStats $PROG $VERSION (c) Laurent Destailleur -----\n";
256 print "$PROG is a tool to setup AWStats. It works with Apache only.\n";
257 print " - Detect Apache config file (ask if not found)\n";
258 print " - Change common log to combined (ask to confirm)\n";
259 print " - Add AWStats directives\n";
260 print " - Restart web server\n";
261 print " - Create one AWStats config file (if asked)\n";
262 print "\n";
263 print "Usage: $PROG.$Extension\n";
264 print "\n";
265 exit 0;
266 }
267
268 # Get current time
269 my $nowtime=time;
270 my ($nowsec,$nowmin,$nowhour,$nowday,$nowmonth,$nowyear) = localtime($nowtime);
271 if ($nowyear < 100) { $nowyear+=2000; } else { $nowyear+=1900; }
272 my $nowsmallyear=$nowyear;$nowsmallyear =~ s/^..//;
273 if (++$nowmonth < 10) { $nowmonth = "0$nowmonth"; }
274 rizwank 1.1 if ($nowday < 10) { $nowday = "0$nowday"; }
275 if ($nowhour < 10) { $nowhour = "0$nowhour"; }
276 if ($nowmin < 10) { $nowmin = "0$nowmin"; }
277 if ($nowsec < 10) { $nowsec = "0$nowsec"; }
278
279 print "\n";
280 print "----- AWStats $PROG $VERSION (c) Laurent Destailleur -----\n";
281 print "This tool will help you to configure AWStats to analyze statistics for\n";
282 print "one web server. You can try to use it to let it do all that is possible\n";
283 print "in AWStats setup, however following the step by step manual setup\n";
284 print "documentation (docs/index.html) is often a better idea. Above all if:\n";
285 print "- You are not an administrator user,\n";
286 print "- You want to analyze downloaded log files without web server,\n";
287 print "- You want to analyze mail or ftp log files instead of web log files,\n";
288 print "- You need to analyze load balanced servers log files,\n";
289 print "- You want to 'understand' all possible ways to use AWStats...\n";
290 print "Read the AWStats documentation (docs/index.html).\n";
291
292 # Detect OS type
293 # --------------
294 if ("$^O" =~ /linux/i || (-d "/etc" && -d "/var" && "$^O" !~ /cygwin/i)) { $OS='linux'; $CR=''; }
295 rizwank 1.1 elsif (-d "/etc" && -d "/Users") { $OS='macosx'; $CR=''; }
296 elsif ("$^O" =~ /cygwin/i || "$^O" =~ /win32/i) { $OS='windows'; $CR="\r"; }
297 if (! $OS) {
298 print "configure.pl was not able to detect your OS. You must configure AWStats\n";
299 print "manually following the setup documentation (docs/index.html).\n";
300 print "configure.pl aborted.\n";
301 exit 1;
302 }
303
304 #print "Running OS detected: $OS (Perl $^[)\n";
305 print "\n-----> Running OS detected: $OSLib{$OS}\n";
306
307 if ($OS eq 'linux') {
308 $AWSTATS_PATH=`pwd`; $AWSTATS_PATH =~ s/[\r\n]//;
309 $AWSTATS_PATH=~s/tools[\\\/]?$//;
310 $AWSTATS_PATH=~s/[\\\/]$//;
311 if ($AWSTATS_PATH ne '/usr/local/awstats') {
312 print "Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.\n";
313 print "If you want to use standard directory, you should first move all content\n";
314 print "of AWStats distribution from current directory:\n";
315 print "$AWSTATS_PATH\n";
316 rizwank 1.1 print "to standard directory:\n";
317 print "/usr/local/awstats\n";
318 print "And then, run configure.pl from this location.\n";
319 print "Do you want to continue setup from this NON standard directory [yN] ? ";
320 my $bidon='';
321 while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
322 if ($bidon !~ /^y/i) {
323 print "configure.pl aborted.\n";
324 exit 1;
325 }
326 $AWSTATS_ICON_PATH="$AWSTATS_PATH/wwwroot/icon";
327 $AWSTATS_CSS_PATH="$AWSTATS_PATH/wwwroot/css";
328 $AWSTATS_CLASSES_PATH="$AWSTATS_PATH/wwwroot/classes";
329 $AWSTATS_CGI_PATH="$AWSTATS_PATH/wwwroot/cgi-bin";
330 }
331 }
332 elsif ($OS eq 'macosx') {
333 $AWSTATS_PATH=`pwd`; $AWSTATS_PATH =~ s/[\r\n]//;
334 $AWSTATS_PATH=~s/tools[\\\/]?$//;
335 $AWSTATS_PATH=~s/[\\\/]$//;
336 if ($AWSTATS_PATH ne '/Library/WebServer/awstats') {
337 rizwank 1.1 print "Warning: AWStats standard directory on Mac OS X is '/Library/WebServer/awstats'.\n";
338 print "If you want to use standard directory, you should first move all content\n";
339 print "of AWStats distribution from current directory:\n";
340 print "$AWSTATS_PATH\n";
341 print "to standard directory:\n";
342 print "/Library/WebServer/awstats\n";
343 print "And then, run configure.pl from this location.\n";
344 print "Do you want to continue setup from this NON standard directory [yN] ? ";
345 my $bidon='';
346 while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
347 if ($bidon !~ /^y/i) {
348 print "configure.pl aborted.\n";
349 exit 1;
350 }
351 $AWSTATS_ICON_PATH="$AWSTATS_PATH/wwwroot/icon";
352 $AWSTATS_CSS_PATH="$AWSTATS_PATH/wwwroot/css";
353 $AWSTATS_CLASSES_PATH="$AWSTATS_PATH/wwwroot/classes";
354 $AWSTATS_CGI_PATH="$AWSTATS_PATH/wwwroot/cgi-bin";
355 }
356 }
357 elsif ($OS eq 'windows') {
358 rizwank 1.1 # We do not use default values for awstats directives
359 # but thoose defined from AWSTATS_PATH
360 $AWSTATS_ICON_PATH="$AWSTATS_PATH/wwwroot/icon";
361 $AWSTATS_CSS_PATH="$AWSTATS_PATH/wwwroot/css";
362 $AWSTATS_CLASSES_PATH="$AWSTATS_PATH/wwwroot/classes";
363 $AWSTATS_CGI_PATH="$AWSTATS_PATH/wwwroot/cgi-bin";
364 }
365
366
367
368 # Detect web server path
369 # ----------------------
370 print "\n-----> Check for web server install\n";
371 my %ApachePath=(); # All Apache path found (used on windows only)
372 my %ApacheConfPath=(); # All Apache config found
373 my $tips;
374 if ($OS eq 'linux' || $OS eq 'macosx') {
375 my $found=0;
376 foreach my $conf (@WEBCONF) {
377 if (-s "$conf") {
378 print " Found Web server Apache config file '$conf'\n";
379 rizwank 1.1 $ApacheConfPath{"$conf"}=++$found;
380 }
381 }
382 }
383 if ($OS eq 'windows' && "$^O" !~ /cygwin/i) {
384 $reg->Delimiter("/");
385 if ($tips=$reg->{"LMachine/Software/Apache Group/Apache/"}) {
386 # If Apache registry call successfull
387 my $found=0;
388 foreach( sort keys %$tips ) {
389 my $path=$reg->{"LMachine/Software/Apache Group/Apache/$_/ServerRoot"};
390 $path=~s/[\\\/]$//;
391 if (-d "$path" && -s "$path/conf/httpd.conf") {
392 print " Found a Web server Apache install in '$path'\n";
393 $ApachePath{"$path"}=++$found;
394 $ApacheConfPath{"$path/conf/httpd.conf"}=++$found;
395 }
396 }
397 }
398 }
399 if (! scalar keys %ApacheConfPath) {
400 rizwank 1.1 my $bidon='';
401
402 if ($OS eq 'windows') {
403 # Ask web server path (need to restart on windows)
404 print "$PROG did not find your Apache web main runtime.\n";
405
406 print "\nPlease, enter full directory path of your Apache web server or\n";
407 print "'none' to skip this step if you don't have local web server or\n";
408 print "don't have permission to change its setup.\n";
409 print "Example: c:\\Program files\\apache group\\apache\n";
410 while ($bidon ne 'none' && ! -d "$bidon") {
411 print "Apache Web server path ('none' to skip):\n> ";
412 $bidon=<STDIN>; chomp $bidon;
413 if ($bidon && ! -d "$bidon" && $bidon ne 'none') { print "- The directory '$bidon' does not exists.\n"; }
414 }
415 }
416
417 if ($bidon ne 'none') {
418 if ($bidon) { $ApachePath{"$bidon"}=1; }
419
420 print "\n".($bidon?"Now, enter":"Enter")." full config file path of your Web server.\n";
421 rizwank 1.1 print "Example: /etc/httpd/httpd.conf\n";
422 print "Example: /usr/local/apache2/conf/httpd.conf\n";
423 print "Example: c:\\Program files\\apache group\\apache\\conf\\httpd.conf\n";
424 $bidon='';
425 while ($bidon ne 'none' && ! -f "$bidon") {
426 print "Config file path ('none' to skip web server setup):\n> ";
427 $bidon=<STDIN>; chomp $bidon;
428 if ($bidon && ! -f "$bidon" && $bidon ne 'none') { print "- This file does not exists.\n"; }
429 }
430 if ($bidon ne 'none') {
431 $ApacheConfPath{"$bidon"}=1;
432 }
433 }
434
435 }
436
437 if (! scalar keys %ApacheConfPath) {
438 print "\n";
439 print "Your web server config file(s) could not be found.\n";
440 print "You will need to setup your web server manually to declare AWStats\n";
441 print "script as a CGI, if you want to build reports dynamically.\n";
442 rizwank 1.1 print "See AWStats setup documentation (file docs/index.html)";
443 print "\n";
444 }
445
446 # Open Apache config file
447 # -----------------------
448 foreach my $key (keys %ApacheConfPath) {
449 print "\n-----> Check and complete web server config file '$key'\n";
450 # Read config file to search for awstats directives
451 my $commonchangedtocombined=0;
452 READ:
453 $LogFormat{$key}=4;
454 open(CONF,"<$key") || error("Failed to open config file '$key' for reading");
455 binmode CONF;
456 my $awstatsjsfound=0;
457 my $awstatsclassesfound=0;
458 my $awstatscssfound=0;
459 my $awstatsiconsfound=0;
460 my $awstatscgifound=0;
461 my $awstatsdirectoryfound=0;
462 while(<CONF>) {
463 rizwank 1.1 if ($_ =~ /^CustomLog\s(.*)\scommon$/i) {
464 print "Warning: You Apache config file contains directives to write 'common' log files\n";
465 print "This means that some features can't work (os, browsers and keywords detection).\n";
466 print "Do you want me to setup Apache to write 'combined' log files [y/N] ? ";
467 my $bidon='';
468 while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
469 if ($bidon =~ /^y/i) {
470 close CONF;
471 update_httpd_config("$key");
472 $WebServerChanged=1;
473 $commonchangedtocombined=1;
474 goto READ;
475 }
476 }
477 if ($_ =~ /^CustomLog\s(.*)\scombined$/i) { $LogFormat{$key}=1; }
478 if ($_ =~ /Alias \/awstatsclasses/) { $awstatsclassesfound=1; }
479 if ($_ =~ /Alias \/awstatscss/) { $awstatscssfound=1; }
480 if ($_ =~ /Alias \/awstatsicons/) { $awstatsiconsfound=1; }
481 if ($_ =~ /ScriptAlias \/awstats\//) { $awstatscgifound=1; }
482 my $awstats_path_quoted=quotemeta($AWSTATS_PATH);
483 if ($_ =~ /Directory "$awstats_path_quoted\/wwwroot"/) { $awstatsdirectoryfound=1; }
484 rizwank 1.1 }
485 close CONF;
486
487 if ($awstatsclassesfound && $awstatscssfound && $awstatsiconsfound && $awstatscgifound && $awstatsdirectoryfound) {
488 $UseAlias=1;
489 if ($commonchangedtocombined) { print " Common log files changed to combined.\n"; }
490 print " AWStats directives already present.\n";
491 next;
492 }
493
494 # Add awstats directives
495 open(CONF,">>$key") || error("Failed to open config file '$key' for adding AWStats directives");
496 binmode CONF;
497 if (! $awstatsclassesfound || ! $awstatscssfound || ! $awstatsiconsfound || ! $awstatscgifound) {
498 print CONF "$CR\n";
499 print CONF "#$CR\n";
500 print CONF "# Directives to allow use of AWStats as a CGI$CR\n";
501 print CONF "#$CR\n";
502 }
503 if (! $awstatsclassesfound) {
504 print " Add 'Alias \/awstatsclasses \"$AWSTATS_CLASSES_PATH\/\"'\n";
505 rizwank 1.1 print CONF "Alias \/awstatsclasses \"$AWSTATS_CLASSES_PATH\/\"$CR\n";
506 }
507 if (! $awstatscssfound) {
508 print " Add 'Alias \/awstatscss \"$AWSTATS_CSS_PATH\/\"'\n";
509 print CONF "Alias \/awstatscss \"$AWSTATS_CSS_PATH\/\"$CR\n";
510 }
511 if (! $awstatsiconsfound) {
512 print " Add 'Alias \/awstatsicons \"$AWSTATS_ICON_PATH\/\"'\n";
513 print CONF "Alias \/awstatsicons \"$AWSTATS_ICON_PATH\/\"$CR\n";
514 }
515 if (! $awstatscgifound) {
516 print " Add 'ScriptAlias \/awstats\/ \"$AWSTATS_CGI_PATH\/\"'\n";
517 print CONF "ScriptAlias \/awstats\/ \"$AWSTATS_CGI_PATH\/\"$CR\n";
518 }
519 if (! $awstatsdirectoryfound) {
520 print " Add '<Directory>' directive\n";
521 print CONF "$CR\n";
522 print CONF <<EOF;
523 #
524 # This is to permit URL access to scripts/files in AWStats directory.
525 #
526 rizwank 1.1 <Directory "$AWSTATS_PATH/wwwroot">
527 Options None
528 AllowOverride None
529 Order allow,deny
530 Allow from all
531 </Directory>
532
533 EOF
534 }
535 close CONF;
536 $UseAlias=1;
537 $WebServerChanged=1;
538 print " AWStats directives added to Apache config file.\n";
539 }
540
541 # Define model config file path
542 # -----------------------------
543 my $modelfile='';
544 if ($OS eq 'linux') {
545 if (-f "$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf") {
546 $modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf";
547 rizwank 1.1 }
548 else {
549 $modelfile="$AWSTATS_MODEL_CONFIG";
550 if (! -s $modelfile || ! -w $modelfile) { $modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf"; }
551 }
552 }
553 elsif ($OS eq "macosx") {
554 $modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf";
555 }
556 elsif ($OS eq 'windows') { $modelfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.model.conf"; }
557 else { $modelfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.model.conf"; }
558
559 # Update model config file
560 # ------------------------
561 if (-s $modelfile && -w $modelfile) {
562 print "\n-----> Update model config file '$modelfile'\n";
563 %ConfToChange=();
564 if ($OS eq 'linux' || $OS eq "macosx") { $ConfToChange{'DirData'}="$AWSTATS_DIRDATA_PATH"; }
565 elsif ($OS eq 'windows') { $ConfToChange{'DirData'}='.'; }
566 else { $ConfToChange{'DirData'}='.'; }
567 if ($UseAlias) {
568 rizwank 1.1 $ConfToChange{'DirCgi'}='/awstats';
569 $ConfToChange{'DirIcons'}='/awstatsicons';
570 }
571 update_awstats_config("$modelfile");
572 print " File awstats.model.conf updated.\n";
573 }
574
575 # Ask if we need to create a config file
576 #---------------------------------------
577 my $site='';
578 my $configfile='';
579 print "\n-----> Need to create a new config file ?\n";
580 print "Do you want me to build a new AWStats config/profile\n";
581 print "file (required if first install) [y/N] ? ";
582 my $bidon='';
583 while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
584 if ($bidon =~ /^y/i) {
585
586 # Ask value for web site name
587 #----------------------------
588 print "\n-----> Define config file name to create\n";
589 rizwank 1.1 print "What is the name of your web site or profile analysis ?\n";
590 # TODO Add example that use value found in ServerName ?
591 print "Example: www.mysite.com\n";
592 print "Example: demo\n";
593 ASKCONFIG:
594 my $bidon='';
595 while (! $bidon) {
596 print "Your web site, virtual server or profile name:\n> ";
597 $bidon=<STDIN>; chomp $bidon;
598 if ($bidon =~ /\s/) { print " Space chars are not allowed.\n"; $bidon=''; }
599 }
600 $site=$bidon;
601
602 # Define config file path
603 # -----------------------
604 if ($OS eq 'linux') {
605 print "\n-----> Define config file path\n";
606 print "In which directory do you plan to store your config file(s) ?\n";
607 print "Default: /etc/awstats\n";
608 my $bidon='';
609 print "Directory path to store config file(s) (Enter for default):\n> ";
610 rizwank 1.1 $bidon=<STDIN>; chomp $bidon;
611 if (! $bidon) { $bidon = "/etc/awstats"; }
612 my $configdir=$bidon;
613 if (! -d $configdir) {
614 # Create the directory for config files
615 my $mkdirok=mkdir "$configdir", 0755;
616 if (! $mkdirok) { error("Failed to create directory '$configdir', required to store config files."); }
617 }
618 $configfile="$configdir/awstats.$site.conf";
619 }
620 elsif ($OS eq "macosx") { $configfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.$site.conf"; }
621 elsif ($OS eq 'windows') { $configfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.$site.conf"; }
622 else { $configfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.$site.conf"; }
623
624 if (-s "$configfile") {
625 print "Warning: A config file for this name already exists. Choose another one.\n";
626 goto ASKCONFIG;
627 }
628
629 # Create awstats.conf file
630 # ------------------------
631 rizwank 1.1 print "\n-----> Create config file '$configfile'\n";
632 if (-s $configfile) { print " Config file already exists. No overwrite possible on existing config files.\n"; }
633 else {
634 %ConfToChange=();
635 # TODO Ask path of log file to change LogFile parameter
636 if ($OS eq 'linux' || $OS eq "macosx") { $ConfToChange{'DirData'}="$AWSTATS_DIRDATA_PATH"; }
637 if ($OS eq 'windows') { $ConfToChange{'DirData'}='.'; }
638 if ($UseAlias) {
639 $ConfToChange{'DirCgi'}='/awstats';
640 $ConfToChange{'DirIcons'}='/awstatsicons';
641 }
642 $ConfToChange{'SiteDomain'}="$site";
643 my $sitewithoutwww=lc($site); $sitewithoutwww =~ s/^www\.//i;
644 $ConfToChange{'HostAliases'}="$sitewithoutwww www.$sitewithoutwww 127.0.0.1 localhost";
645 update_awstats_config("$modelfile","$configfile");
646 }
647
648 }
649
650
651 # Restart Apache if change were made
652 rizwank 1.1 # ----------------------------------
653 if ($WebServerChanged) {
654 if ($OS eq 'linux') {
655 if (-f "/etc/debian_version") {
656 # We are on debian
657 my $command="/etc/init.d/apache restart";
658 print "\n-----> Restart Web server with '$command'\n";
659 my $ret=`$command`;
660 print "$ret";
661 } elsif (-x "/sbin/service") {
662 # We are not on debian
663 my $command="/sbin/service httpd restart";
664 print "\n-----> Restart Web server with '$command'\n";
665 my $ret=`$command`;
666 print "$ret";
667 } else {
668 print "\n-----> Don't forget to restart manually your web server\n";
669 }
670 }
671 elsif ($OS eq 'macosx') {
672 print "\n-----> Restart Web server with '/usr/sbin/apachectl restart'\n";
673 rizwank 1.1 my $ret=`/usr/sbin/apachectl restart`;
674 print "$ret";
675 }
676 elsif ($OS eq 'windows') {
677 foreach my $key (keys %ApachePath) {
678 if (-f "$key/bin/Apache.exe") {
679 print "\n-----> Restart Apache with '\"$key/bin/Apache.exe\" -k restart'\n";
680 my $ret=`"$key/bin/Apache.exe" -k restart`;
681 }
682 }
683 }
684 else {
685 foreach my $key (keys %ApachePath) {
686 if (-f "$key/bin/Apache.exe") {
687 print "\n-----> Restart Apache with '\"$key/bin/Apache.exe\" -k restart'\n";
688 my $ret=`"$key/bin/Apache.exe" -k restart`;
689 }
690 }
691 }
692 }
693
694 rizwank 1.1
695 # TODO
696 # Scan logorate for a log file
697 # If apache log has a logrotate log found, we create a config and add line in prerotate
698 # prerotate
699 # ...
700 # endscript
701 # If not found
702
703
704 # Schedule awstats update process
705 # -------------------------------
706 print "\n-----> Add update process inside a scheduler\n";
707 if ($OS eq 'linux' || $OS eq "macosx") {
708 print "Sorry, configure.pl does not support automatic add to cron yet.\n";
709 print "You can do it manually by adding the following command to your cron:\n";
710 print "$AWSTATS_CGI_PATH/awstats.pl -update -config=".($site?$site:"myvirtualserver")."\n";
711 print "Or if you have several config files and prefer having only one command:\n";
712 print "$AWSTATS_PATH/tools/awstats_updateall.pl now\n";
713 print "Press ENTER to continue... ";
714 $bidon=<STDIN>;
715 rizwank 1.1 }
716 elsif ($OS eq 'windows') {
717 print "Sorry, for Windows users, if you want to have statistics to be\n";
718 print "updated on a regular basis, you have to add the update process\n";
719 print "in a scheduler task manually (See AWStats docs/index.html).\n";
720 print "Press ENTER to continue... ";
721 $bidon=<STDIN>;
722 }
723 else {
724 print "Sorry, if you want to have statistics to be\n";
725 print "updated on a regular basis, you have to add the update process\n";
726 print "in a scheduler task manually (See AWStats docs/index.html).\n";
727 print "Press ENTER to continue... ";
728 $bidon=<STDIN>;
729 }
730
731 #print "\n-----> End of configuration\n";
732 print "\n\n";
733 if ($site) {
734 print "A SIMPLE config file has been created: $configfile\n";
735 print "You should have a look inside to check and change manually main parameters.\n";
736 rizwank 1.1 print "You can then manually update your statistics for '$site' with command:\n";
737 print "> perl awstats.pl -update -config=$site\n";
738 if (scalar keys %ApacheConfPath) {
739 print "You can also read your statistics for '$site' with URL:\n";
740 print "> http://localhost/awstats/awstats.pl?config=$site\n";
741 }
742 else {
743 print "You can also build static report pages for '$site' with command:\n";
744 print "> perl awstats.pl -output=pagetype -config=$site\n";
745 }
746 print "\n";
747 }
748 else {
749 print "No config file was built. You can run this tool later to build as\n";
750 print "much config/profile files as you want.\n";
751 print "Once you have a config/profile file, for example 'awstats.demo.conf',\n";
752 print "You can manually update your statistics for 'demo' with command:\n";
753 print "> perl awstats.pl -update -config=demo\n";
754 if (scalar keys %ApacheConfPath) {
755 print "You can also read your statistics for 'demo' with URL:\n";
756 print "> http://localhost/awstats/awstats.pl?config=demo\n";
757 rizwank 1.1 }
758 else {
759 print "You can also build static report pages for 'demo' with command:\n";
760 print "> perl awstats.pl -output=pagetype -config=demo\n";
761 }
762 print "\n";
763 }
764 print "Press ENTER to finish...\n";
765 $bidon=<STDIN>;
766
767
768 0; # Do not remove this line
|