This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_awstats.pl
executable file
·175 lines (133 loc) · 5.05 KB
/
update_awstats.pl
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
#!/usr/bin/env perl
use 5.012; # so readdir assigns to $_
use strict;
use warnings;
use Data::Dumper;
use File::Spec;
use FindBin qw($Bin);
my $appname = shift;
die("[ERROR] appname parameters is required\n") unless ($appname);
#
# File and folder paths
#
my $home_path = $ENV{'HOME'};
my $log_folder_path = File::Spec->catdir($home_path,'/logs/frontend');
die("[ERROR] unable to access: $log_folder_path\n") unless -x ($log_folder_path);
my $awstats_folder_path = File::Spec->catdir($home_path,'/webapps/',$appname);
die("[ERROR] unable to access: $awstats_folder_path\n") unless -x ($awstats_folder_path);
my $logresolvemerge_path = File::Spec->catfile($awstats_folder_path,'tools','logresolvemerge.pl');
die("[ERROR] log tool does not exist: $logresolvemerge_path\n") unless -x ($logresolvemerge_path);
my $awstats_path = File::Spec->catfile($awstats_folder_path,'cgi-bin','awstats.pl');
die("[ERROR] awstats does not exist: $awstats_path\n") unless -x ($awstats_path);
my $data_folder_path = File::Spec->catdir($awstats_folder_path,'data');
#
# Sites and URLs
#
my %url_for_log = (
# log name > domain name
);
my @log_names = &_log_names($log_folder_path);
foreach my $log_name (@log_names) {
$url_for_log{$log_name} = $log_name;
}
#
# ...allow customised log name to URL mapping with tab separated file
#
my $custom_path = File::Spec->catfile($Bin,'custom.txt');
if (-e $custom_path) {
open(CUSTOM,"<$custom_path") or die("[ERROR] unable to open $custom_path for reading: $!\n");
while(my $line = <CUSTOM>) {
if (($line !~ /^#/) and ($line =~ /^(?<hostname>.+)\t+(?<log>.+)$/)) {
if ($+{hostname} eq '.ignore') { # skip if log name is '.ignore'
delete($url_for_log{$+{log}});
} else {
$url_for_log{$+{log}} = $+{hostname};
}
}
}
close(CUSTOM) or die("[ERROR] unable to close $custom_path after read: $!\n");
}
# Invert and combine the sites
my %log_filenames_for_site;
foreach my $log_filename (sort keys %url_for_log) {
my $site = $url_for_log{$log_filename};
if (exists($log_filenames_for_site{$site})) {
push(@{$log_filenames_for_site{$site}},$log_filename);
} else {
$log_filenames_for_site{$site} = [$log_filename];
}
}
#
#
#
my $template_config;
while(<DATA>) { $template_config .= $_; }
foreach my $site (sort keys %log_filenames_for_site) {
# Ensure awstats configuration file exists
my $config_path = File::Spec->catfile($awstats_folder_path,'cgi-bin','awstats.'.$site.'.conf');
if (not -e $config_path) {
print "[INFO] creating required: ".$config_path."\n";
my $config_contents = $template_config;
$config_contents =~ s/HOME/$home_path/gsm;
$config_contents =~ s/APPNAME/$appname/gsm;
$config_contents =~ s/DOMAIN/$site/gsm;
open(CONFIG,">$config_path") or die("[ERROR] unable to create $config_path to write: $!\n");
print CONFIG $config_contents;
close(CONFIG) or die("[ERROR] unable to close $config_path after write: $!\n");
my $site_data_path = File::Spec->catdir($data_folder_path,$site);
mkdir($site_data_path) or die("[ERROR] unable to mkdir $site_data_path: $!\n");
}
# Merge logs and process
my @logmerge = ( $logresolvemerge_path );
my $log_filenames = $log_filenames_for_site{$site};
foreach my $log_filename (@{$log_filenames}) {
push(@logmerge,File::Spec->catfile($log_folder_path,'access_'.$log_filename.'.log'));
push(@logmerge,File::Spec->catfile($log_folder_path,'access_'.$log_filename.'.log.1'));
push(@logmerge,File::Spec->catfile($log_folder_path,'error_'.$log_filename.'.log'));
push(@logmerge,File::Spec->catfile($log_folder_path,'error_'.$log_filename.'.log.1'));
}
push(@logmerge,'>');
push(@logmerge,File::Spec->catfile($data_folder_path,'log-'.$site));
push(@logmerge,'||');
push(@logmerge,$logresolvemerge_path);
foreach my $log_filename (@{$log_filenames}) {
push(@logmerge,File::Spec->catfile($log_folder_path,'access_'.$log_filename.'.log'));
push(@logmerge,File::Spec->catfile($log_folder_path,'error_'.$log_filename.'.log'));
}
push(@logmerge,'>');
push(@logmerge,File::Spec->catfile($data_folder_path,'log-'.$site));
my $logmerge = join(' ',@logmerge);
system($logmerge);
my @processlog = (
$awstats_path,
'-config='.$site,
'-u',
);
my $processlog = join(' ',@processlog);
system($processlog);
}
sub _log_names {
my( $logs_path ) = @_;
# ...check log directory exists
die('[ERROR] log directory must exist: '.$logs_path) unless -d $logs_path;
die('[ERROR] log directory must be accessible: '.$logs_path) unless -x $logs_path;
# Iterate over files in log directory
my %path_for_log;
opendir(my $dh, $logs_path) or die('[ERROR] unable to read directory: '.$logs_path.': '.$!);
while(readdir($dh)) {
my $log_filename = $_;
# Filter out rotated logs and stdout/php logs
if (($_ !~ /_php\.log$/) and ($_ =~ /^access_(?<log>.+?)\.log$/)) {
my $log_name = $+{log};
$path_for_log{$log_name} = $logs_path.'/'.$log_filename;
}
}
closedir $dh;
return sort keys %path_for_log;
}
__DATA__
Include "awstats.conf"
SiteDomain="DOMAIN"
HostAliases="DOMAIN www.DOMAIN"
DirData="HOME/webapps/APPNAME/data/DOMAIN"
LogFile="HOME/webapps/APPNAME/data/log-DOMAIN"