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
8
# - Create AWStats config file
9
# See COPYING.TXT file about AWStats GNU General Public License.
10
#-------------------------------------------------------
11
# $Revision: 1.23 $ - $Author: eldy $ - $Date: 2004/01/13 13:14:41 $
14
#-------------------------------------------------------
15
# IF YOU ARE A PACKAGE BUILDER, CHANGE THIS TO MATCH YOUR PATH
16
# SO THAT THE CONFIGURE WILL WORK ON YOUR DISTRIB !!!
17
# Following path are the one
18
#-------------------------------------------------------
29
$AWSTATS_ICON_PATH='/usr/local/awstats/wwwroot/icon';
30
$AWSTATS_CSS_PATH='/usr/local/awstats/wwwroot/css';
31
$AWSTATS_CLASSES_PATH='/usr/local/awstats/wwwroot/classes';
32
$AWSTATS_CGI_PATH='/usr/local/awstats/wwwroot/cgi-bin';
33
$AWSTATS_MODEL_CONFIG='/etc/awstats/awstats.model.conf'; # Used only when configure ran on linux
34
$AWSTATS_DIRDATA_PATH='/var/lib/awstats'; # Used only when configure ran on linux
38
#-------------------------------------------------------
40
#-------------------------------------------------------
41
# For windows registry management
43
eval('use Win32::TieRegistry ( Delimiter=>"/", TiedRef=>\$reg )');
45
use vars qw/ $REVISION $VERSION /;
46
$REVISION='$Revision: 1.23 $'; $REVISION =~ /\s(.*)\s/; $REVISION=$1;
47
$VERSION="1.0 (build $REVISION)";
50
$DIR $PROG $Extension $Debug
56
# Possible dirs for Apache conf files
58
'C:/Program Files/Apache Group/Apache2/conf/httpd.conf',
59
'C:/Program Files/Apache Group/Apache/conf/httpd.conf',
60
'/etc/httpd/httpd.conf',
61
'/usr/local/apache/conf/httpd.conf',
62
'/usr/local/apache2/conf/httpd.conf'
66
$WebServerChanged $UseAlias $Step
67
%LogFormat %ConfToChange
77
#-------------------------------------------------------
79
#-------------------------------------------------------
81
#-------------------------------------------------------
83
#-------------------------------------------------------
85
print "Error: $_[0].\n";
89
#-------------------------------------------------------
91
#-------------------------------------------------------
93
my $level = $_[1] || 1;
94
if ($Debug >= $level) {
95
my $debugstring = $_[0];
96
if ($ENV{"GATEWAY_INTERFACE"}) { $debugstring =~ s/^ /   /; $debugstring .= "<br>"; }
97
print "DEBUG $level - ".time." : $debugstring\n";
102
#-------------------------------------------------------
103
# update_httpd_config
104
# Replace common to combined in Apache config file
105
#-------------------------------------------------------
106
sub update_httpd_config
109
if (! $file) { error("Call to update_httpd_config with wrong parameter"); }
111
open(FILE, $file) || error("Failed to open $file for update");
112
open(FILETMP, ">$file.tmp") || error("Failed to open $file.tmp for writing");
114
# $%conf contains param and values
118
# First, change values that are already present in old config file
125
# Remove comments not at beginning of line
129
if ($_ =~ /^CustomLog\s(.*)\scommon$/i) { $savline="CustomLog $1 combined"; }
132
print FILETMP "$savline";
138
# Move file to file.sav
139
if (rename("$file","$file.old")==0) {
140
error("Failed to make backup of current config file to $file.old");
143
# Move tmp file into config file
144
if (rename("$file.tmp","$file")==0) {
145
error("Failed to move tmp config file $file.tmp to $file");
151
#-------------------------------------------------------
152
# update_awstats_config
153
# Update an awstats model [to another one]
154
#-------------------------------------------------------
155
sub update_awstats_config
158
my $fileto=shift||"$file.tmp";
160
if (! $file) { error("Call to update_awstats_config with wrong parameter"); }
161
if ($file =~ /Developpements[\\\/]awstats/i) {
162
print " This is my dev area. Don't touch.\n";
164
} # To avoid script working in my dev area
166
open(FILE, $file) || error("Failed to open $file for update");
167
open(FILETMP, ">$fileto") || error("Failed to open $fileto for writing");
169
# $%conf contains param and values
173
# First, change values that are already present in old config file
180
# Remove comments not at beginning of line
183
# Extract param and value
184
my ($param,$value)=split(/=/,$_,2);
185
$param =~ s/^\s+//; $param =~ s/\s+$//;
187
$value =~ s/^[\s\'\"]+//; $value =~ s/[\s\'\"]+$//;
190
# cleanparam is param without begining #
191
my $cleanparam=$param; my $wascleaned=0;
192
if ($cleanparam =~ s/^#//) { $wascleaned=1; }
193
if (defined($ConfToChange{"$cleanparam"}) && $ConfToChange{"$cleanparam"}) { $savline = ($wascleaned?"#":"")."$cleanparam=\"".$ConfToChange{"$cleanparam"}."\"\n"; }
196
print FILETMP "$savline";
202
if ($fileto eq "$file.tmp") {
203
# Move file to file.sav
204
if (rename("$file","$file.old")==0) {
205
error("Failed to make backup of current config file to $file.old");
208
# Move tmp file into config file
209
if (rename("$fileto","$file")==0) {
210
error("Failed to move tmp config file $fileto to $file");
216
print " Config file $fileto created.\n";
223
#-------------------------------------------------------
225
#-------------------------------------------------------
226
($DIR=$0) =~ s/([^\/\\]+)$//; ($PROG=$1) =~ s/\.([^\.]*)$//; $Extension=$1;
227
$DIR||='.'; $DIR =~ s/([^\/\\])[\\\/]+$/$1/;
229
my $QueryString=""; for (0..@ARGV-1) { $QueryString .= "$ARGV[$_] "; }
230
if ($QueryString =~ /debug=/i) { $Debug=$QueryString; $Debug =~ s/.*debug=//; $Debug =~ s/&.*//; $Debug =~ s/ .*//; }
236
if ($ARGV[$_] =~ /^-*h/i) { $helpfound=1; last; }
237
if ($ARGV[$_] =~ /^-*awstatspath=([^\s\"]+)/i) { $AWSTATS_PATH=$1; last; }
239
# If AWSTATS_PATH was not forced on command line
240
if (! $AWSTATS_PATH) {
241
$AWSTATS_PATH=($DIR eq '.'?'..':$DIR);
242
$AWSTATS_PATH=~s/tools[\\\/]?$//;
243
$AWSTATS_PATH=~s/[\\\/]$//;
248
print "----- AWStats $PROG $VERSION (c) Laurent Destailleur -----\n";
249
print "$PROG is a tool to setup AWStats. It works with Apache only.\n";
250
print " - Get Apache config file from registry (ask if not found)\n";
251
print " - Change common log to combined (ask to confirm)\n";
252
print " - Add AWStats directives\n";
253
print " - Restart web server\n";
254
print " - Create AWStats config file\n";
256
print "Usage: $PROG.$Extension\n";
263
my ($nowsec,$nowmin,$nowhour,$nowday,$nowmonth,$nowyear) = localtime($nowtime);
264
if ($nowyear < 100) { $nowyear+=2000; } else { $nowyear+=1900; }
265
my $nowsmallyear=$nowyear;$nowsmallyear =~ s/^..//;
266
if (++$nowmonth < 10) { $nowmonth = "0$nowmonth"; }
267
if ($nowday < 10) { $nowday = "0$nowday"; }
268
if ($nowhour < 10) { $nowhour = "0$nowhour"; }
269
if ($nowmin < 10) { $nowmin = "0$nowmin"; }
270
if ($nowsec < 10) { $nowsec = "0$nowsec"; }
273
print "----- AWStats $PROG $VERSION (c) Laurent Destailleur -----\n";
274
print "This tool will help you to configure AWStats to analyze statistics for\n";
275
print "one web server. If you need to analyze load balanced servers, downloaded\n";
276
print "log files without web server, to analyze mail or ftp log files, or need\n";
277
print "to manage rotated logs, you will have to complete the config file manually\n";
278
print "according to your needs.\n";
279
print "Read the AWStats documentation (docs/index.html).\n";
283
if (-d "/etc" && -d "/home") { $OS='linux'; $CR=''; }
284
else { $OS='windows'; $CR="\r"; }
285
#print "Running OS detected: $OS (Perl $^[)\n";
286
print "\n-----> Running OS detected: $OS\n";
288
if ($OS eq 'linux') {
289
$AWSTATS_PATH=`pwd`; $AWSTATS_PATH =~ s/[\r\n]//;
290
$AWSTATS_PATH=~s/tools[\\\/]?$//;
291
$AWSTATS_PATH=~s/[\\\/]$//;
292
if ($AWSTATS_PATH ne '/usr/local/awstats') {
293
print "Warning: AWStats standard directory on Linux OS is '/usr/local/awstats'.\n";
294
print "If you want to use standard directory, you should first move all content\n";
295
print "of AWStats distribution from current directory:\n";
296
print "$AWSTATS_PATH\n";
297
print "to standard directory:\n";
298
print "/usr/local/awstats\n";
299
print "And then, run configure.pl from this location.\n";
300
print "Do you want to continue setup from this NON standard directory [yN] ? ";
302
while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
303
if ($bidon !~ /^y/i) {
304
print "configure.pl aborted.\n";
307
$AWSTATS_ICON_PATH="$AWSTATS_PATH/wwwroot/icon";
308
$AWSTATS_CSS_PATH="$AWSTATS_PATH/wwwroot/css";
309
$AWSTATS_CLASSES_PATH="$AWSTATS_PATH/wwwroot/classes";
310
$AWSTATS_CGI_PATH="$AWSTATS_PATH/wwwroot/cgi-bin";
313
elsif ($OS eq 'windows') {
314
# We do not use default values for awstats directives
315
# but thoose defined from AWSTATS_PATH
316
$AWSTATS_ICON_PATH="$AWSTATS_PATH/wwwroot/icon";
317
$AWSTATS_CSS_PATH="$AWSTATS_PATH/wwwroot/css";
318
$AWSTATS_CLASSES_PATH="$AWSTATS_PATH/wwwroot/classes";
319
$AWSTATS_CGI_PATH="$AWSTATS_PATH/wwwroot/cgi-bin";
324
# Detect web server path
325
# ----------------------
326
print "\n-----> Check for web server install\n";
327
my %ApachePath=(); # All Apache path found
328
my %ApacheConfPath=(); # All Apache config found
330
if ($OS eq 'linux') {
332
foreach my $conf (@WEBCONF) {
334
print " Found Web server Apache config file '$conf'\n";
335
$ApacheConfPath{"$conf"}=++$found;
339
if ($OS eq 'windows') {
340
$reg->Delimiter("/");
341
if ($tips=$reg->{"LMachine/Software/Apache Group/Apache/"}) {
342
# If Apache registry call successfull
344
foreach( sort keys %$tips ) {
345
my $path=$reg->{"LMachine/Software/Apache Group/Apache/$_/ServerRoot"};
347
if (-d "$path" && -s "$path/conf/httpd.conf") {
348
print " Found a Web server Apache install in '$path'\n";
349
$ApachePath{"$path"}=++$found;
350
$ApacheConfPath{"$path/conf/httpd.conf"}=++$found;
355
if (! scalar keys %ApacheConfPath) {
358
# Ask web server path
359
print "$PROG did not find your Apache web server path.\n";
361
if ($OS eq 'windows') {
362
print "\nPlease, enter full directory path of your Apache web server or\n";
363
print "'none' to skip this step if you don't have local web server.\n";
364
print "Example: /usr/local/apache\n";
365
print "Example: d:\\Program files\\apache group\\apache\n";
366
while ($bidon ne 'none' && ! -d "$bidon") {
367
print "Apache Web server path (CTRL+C to cancel):\n> ";
368
$bidon=<STDIN>; chomp $bidon;
369
if ($bidon && ! -d "$bidon" && $bidon ne 'none') { print " The directory '$bidon' does not exists.\n"; }
373
if ($bidon ne 'none') {
374
if ($bidon) { $ApachePath{"$bidon"}=1; }
376
print "\n".($bidon?"Now, enter":"Enter")." full config file path of you web server.\n";
377
print "Example: /etc/httpd/apache.conf\n";
378
print "Example: d:\\Program files\\apache group\\apache\\conf\\httpd.conf\n";
380
while (! -f "$bidon") {
381
print "Config file path (CTRL+C to cancel):\n> ";
382
$bidon=<STDIN>; chomp $bidon;
383
if (! -f "$bidon") { print " This file does not exists.\n"; }
385
$ApacheConfPath{"$bidon"}=1;
389
if (! scalar keys %ApacheConfPath) {
391
print "Your web server config file(s) could not be found.\n";
392
print "You will need to setup your web server manually to declare AWStats\n";
393
print "script as a CGI, if you want to build reports dynamically.\n";
394
print "See AWStats setup documentation (file docs/index.html)";
398
# Open Apache config file
399
# -----------------------
400
foreach my $key (keys %ApacheConfPath) {
401
print "\n-----> Check and complete web server config file '$key'\n";
402
# Read config file to search for awstats directives
403
my $commonchangedtocombined=0;
406
open(CONF,"<$key") || error("Failed to open config file '$key' for reading");
408
my $awstatsjsfound=0;
409
my $awstatsclassesfound=0;
410
my $awstatscssfound=0;
411
my $awstatsiconsfound=0;
412
my $awstatscgifound=0;
413
my $awstatsdirectoryfound=0;
415
if ($_ =~ /^CustomLog\s(.*)\scommon$/i) {
416
print "Warning: You Apache config file contains directives to write 'common' log files\n";
417
print "This means that some features can't work (os, browsers and keywords detection).\n";
418
print "Do you want me to setup Apache to write 'combined' log files [y/N] ? ";
420
while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
421
if ($bidon =~ /^y/i) {
423
update_httpd_config("$key");
425
$commonchangedtocombined=1;
429
if ($_ =~ /^CustomLog\s(.*)\scombined$/i) { $LogFormat{$key}=1; }
430
if ($_ =~ /Alias \/awstatsclasses/) { $awstatsclassesfound=1; }
431
if ($_ =~ /Alias \/awstatscss/) { $awstatscssfound=1; }
432
if ($_ =~ /Alias \/awstatsicons/) { $awstatsiconsfound=1; }
433
if ($_ =~ /ScriptAlias \/awstats\//) { $awstatscgifound=1; }
434
my $awstats_path_quoted=quotemeta($AWSTATS_PATH);
435
if ($_ =~ /Directory "$awstats_path_quoted\/wwwroot"/) { $awstatsdirectoryfound=1; }
439
if ($awstatsclassesfound && $awstatscssfound && $awstatsiconsfound && $awstatscgifound && $awstatsdirectoryfound) {
441
if ($commonchangedtocombined) { print " Common log files changed to combined.\n"; }
442
print " AWStats directives already present.\n";
446
# Add awstats directives
447
open(CONF,">>$key") || error("Failed to open config file '$key' for adding AWStats directives");
449
if (! $awstatsclassesfound || ! $awstatscssfound || ! $awstatsiconsfound || ! $awstatscgifound) {
452
print CONF "# Directives to allow use of AWStats as a CGI$CR\n";
455
if (! $awstatsclassesfound) {
456
print " Add 'Alias \/awstatsclasses \"$AWSTATS_CLASSES_PATH\/\"'\n";
457
print CONF "Alias \/awstatsclasses \"$AWSTATS_CLASSES_PATH\/\"$CR\n";
459
if (! $awstatscssfound) {
460
print " Add 'Alias \/awstatscss \"$AWSTATS_CSS_PATH\/\"'\n";
461
print CONF "Alias \/awstatscss \"$AWSTATS_CSS_PATH\/\"$CR\n";
463
if (! $awstatsiconsfound) {
464
print " Add 'Alias \/awstatsicons \"$AWSTATS_ICON_PATH\/\"'\n";
465
print CONF "Alias \/awstatsicons \"$AWSTATS_ICON_PATH\/\"$CR\n";
467
if (! $awstatscgifound) {
468
print " Add 'ScriptAlias \/awstats\/ \"$AWSTATS_CGI_PATH\/\"'\n";
469
print CONF "ScriptAlias \/awstats\/ \"$AWSTATS_CGI_PATH\/\"$CR\n";
471
if (! $awstatsdirectoryfound) {
472
print " Add '<Directory>' directive\n";
476
# This is to permit URL access to scripts/files in AWStats directory.
478
<Directory "$AWSTATS_PATH/wwwroot">
490
print " AWStats directives added to Apache config file.\n";
493
# Define model config file path
494
# -----------------------------
496
if ($OS eq 'linux') {
497
if (-f "$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf") {
498
$modelfile="$AWSTATS_PATH/wwwroot/cgi-bin/awstats.model.conf";
501
$modelfile="$AWSTATS_MODEL_CONFIG";
504
elsif ($OS eq 'windows') { $modelfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.model.conf"; }
505
else { $modelfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.model.conf"; }
507
# Update model config file
508
# ------------------------
509
print "\n-----> Update model config file '$modelfile'\n";
511
if ($OS eq 'linux') { $ConfToChange{'DirData'}="$AWSTATS_DIRDATA_PATH"; }
512
elsif ($OS eq 'windows') { $ConfToChange{'DirData'}='.'; }
513
else { $ConfToChange{'DirData'}='.'; }
515
$ConfToChange{'DirCgi'}='/awstats';
516
$ConfToChange{'DirIcons'}='/awstatsicons';
518
update_awstats_config("$modelfile");
519
print " File awstats.model.conf updated.\n";
521
# Ask if we need to create a config file
522
#---------------------------------------
525
print "\n-----> Need to create a new config file ?\n";
526
print "Do you want me to build a new AWStats config/profile\n";
527
print "file (required if first install) [y/N] ? ";
529
while ($bidon !~ /^[yN]/i) { $bidon=<STDIN>; }
530
if ($bidon =~ /^y/i) {
532
# Ask value for web site name
533
#----------------------------
534
print "\n-----> Define config file name to create\n";
535
print "What is the name of your web site or profile analysis ?\n";
536
print "Example: www.mysite.com\n";
537
print "Example: demo\n";
541
print "Your web site, virtual server or profile name:\n> ";
542
$bidon=<STDIN>; chomp $bidon;
543
if ($bidon =~ /\s/) { print " Space chars are not allowed.\n"; $bidon=''; }
547
# Define config file path
548
# -----------------------
549
if ($OS eq 'linux') { $configfile="/etc/awstats/awstats.$site.conf"; }
550
elsif ($OS eq 'windows') { $configfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.$site.conf"; }
551
else { $configfile="$AWSTATS_PATH\\wwwroot\\cgi-bin\\awstats.$site.conf"; }
553
if (-s "$configfile") {
554
print "Warning: A config file for this name already exists. Choose another one.\n";
558
# Create awstats.conf file
559
# ------------------------
560
print "\n-----> Create config file '$configfile'\n";
561
if (-s $configfile) { print " Config file already exists. No overwrite possible on existing config files.\n"; }
564
if ($OS eq 'linux') { $ConfToChange{'DirData'}="$AWSTATS_DIRDATA_PATH"; }
565
if ($OS eq 'windows') { $ConfToChange{'DirData'}='.'; }
567
$ConfToChange{'DirCgi'}='/awstats';
568
$ConfToChange{'DirIcons'}='/awstatsicons';
570
$ConfToChange{'SiteDomain'}="$site";
571
my $sitewithoutwww=lc($site); $sitewithoutwww =~ s/^www\.//i;
572
$ConfToChange{'HostAliases'}="$sitewithoutwww www.$sitewithoutwww 127.0.0.1 localhost";
573
update_awstats_config("$modelfile","$configfile");
579
# Restart Apache if change were made
580
# ----------------------------------
581
if ($WebServerChanged) {
582
if ($OS eq 'linux') {
583
print "\n-----> Restart Apache with '/sbin/service httpd restart'\n";
584
my $ret=`/sbin/service httpd restart`;
587
elsif ($OS eq 'windows') {
588
foreach my $key (keys %ApachePath) {
589
if (-f "$key/bin/Apache.exe") {
590
print "\n-----> Restart Apache with '\"$key/bin/Apache.exe\" -k restart'\n";
591
my $ret=`"$key/bin/Apache.exe" -k restart`;
596
foreach my $key (keys %ApachePath) {
597
if (-f "$key/bin/Apache.exe") {
598
print "\n-----> Restart Apache with '\"$key/bin/Apache.exe\" -k restart'\n";
599
my $ret=`"$key/bin/Apache.exe" -k restart`;
607
# Scan logorate for a log file
608
# If apache log has a logrotate log found, we create a config and add line in prerotate
615
# Schedule awstats update process
616
# -------------------------------
617
print "\n-----> Add update process inside a scheduler\n";
618
if ($OS eq 'linux') {
619
print "Sorry, configure.pl does not support automatic add to cron yet.\n";
620
print "You can do it manually by adding the following command to your cron:\n";
621
print "$AWSTATS_CGI_PATH/awstats -update -config=".($site?$site:"myvirtualserver")."\n";
622
print "Or if you have several config files and prefer having only one command:\n";
623
print "$AWSTATS_PATH/tools/awstats_updateall.pl now\n";
624
print "Press ENTER to continue... ";
627
elsif ($OS eq 'windows') {
628
print "Sorry, for windows users, if you want to have statistics to be\n";
629
print "updated on a regular basis, you have to add the update process\n";
630
print "in a scheduler task manually (See AWStats docs/index.html).\n";
631
print "Press ENTER to continue... ";
635
print "Sorry, if you want to have statistics to be\n";
636
print "updated on a regular basis, you have to add the update process\n";
637
print "in a scheduler task manually (See AWStats docs/index.html).\n";
638
print "Press ENTER to continue... ";
642
#print "\n-----> End of configuration\n";
645
print "A SIMPLE config file has been created: $configfile\n";
646
print "You should have a look inside to check and change manually main parameters.\n";
647
print "You can then manually update your statistics for '$site' with command:\n";
648
print "> perl awstats.pl -update -config=$site\n";
649
if (scalar keys %ApacheConfPath) {
650
print "You can also read your statistics for '$site' with URL:\n";
651
print "> http://localhost/awstats/awstats.pl?config=$site\n";
654
print "You can also build static report pages for '$site' with command:\n";
655
print "> perl awstats.pl -output=pagename -config=$site\n";
660
print "No config file was built. You can run this tool later to build as\n";
661
print "much config/profile files as you want.\n";
662
print "Once you have a config/profile file, for example 'awstats.demo.conf',\n";
663
print "You can manually update your statistics for 'demo' with command:\n";
664
print "> perl awstats.pl -update -config=demo\n";
665
if (scalar keys %ApacheConfPath) {
666
print "You can also read your statistics for 'demo' with URL:\n";
667
print "> http://localhost/awstats/awstats.pl?config=demo\n";
670
print "You can also build static report pages for 'demo' with command:\n";
671
print "> perl awstats.pl -output=pagename -config=demo\n";
675
print "Press ENTER to finish...\n";
679
0; # Do not remove this line