root/trunk/engine/ispcp-apache-logger

Revision 1341, 6.8 kB (checked in by scitech, 3 months ago)

apache logger problems

Line 
1 #!/usr/bin/perl
2
3 # ispCP ω (OMEGA) a Virtual Hosting Control Panel
4 # Copyright (c) 2001-2006 by moleSoftware GmbH
5 # http://www.molesoftware.com
6 # Copyright (c) 2006-2008 by isp Control Panel
7 # http://isp-control.net
8 #
9 #
10 # Based on vlogger - smarter logging for apache idea
11 # steve j. kondik  <shade@chemlab.org>
12 #
13 # License:
14 #    This program is free software; you can redistribute it and/or
15 #    modify it under the terms of the MPL Mozilla Public License
16 #    as published by the Free Software Foundation; either version 1.1
17 #    of the License, or (at your option) any later version.
18 #
19 #    This program is distributed in the hope that it will be useful,
20 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
21 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 #    MPL Mozilla Public License for more details.
23 #
24 #    You may have received a copy of the MPL Mozilla Public License
25 #    along with this program.
26 #
27 #    An on-line copy of the MPL Mozilla Public License can be found
28 #    http://www.mozilla.org/MPL/MPL-1.1.html
29 #
30 #
31 # The ispCP ω Home Page is at:
32 #
33 #    http://isp-control.net
34 #
35
36 use strict;
37 use warnings;
38 use sigtrap qw(handler exitall HUP USR1 TERM INT PIPE);
39 use Getopt::Std;
40
41 use FindBin;
42 use lib "$FindBin::Bin/";
43 require 'ispcp_common_code.pl';
44
45 our %OPTS;
46 getopts( 'e', \%OPTS );
47
48 my $MAXFILES = "33";
49 if ( $main::cfg{'APACHE_MAX_OPEN_LOG'} ) {
50         $MAXFILES = $main::cfg{'APACHE_MAX_OPEN_LOG'};
51 }
52
53 my $MAXLOGFILESIZE = 10485760; # 10Mb
54 if ( $main::cfg{'APACHE_MAX_LOG_FILE_SIZE'} ) {
55         $MAXLOGFILESIZE = $main::cfg{'APACHE_MAX_LOG_FILE_SIZE'};
56 }
57
58 my %timestamps = ();
59 my %combined_logs = ();
60 my %traff_logs = ();
61 my %access_logs = ();
62 my %error_logs = ();
63
64 sub checkFileExists;
65 sub DefaultLogs();
66 sub ErrorLogs();
67 sub checkFileSize;
68
69 ## Creating log files if possible ##
70
71 if ( !-d $main::cfg{'LOG_DIR'} ) {
72         print STDERR "[ispcp-apache-logger] target directory ".$main::cfg{'LOG_DIR'}." does not exist - logging on standard streams.\n";
73 } else {
74         checkFileSize("$main::cfg{'LOG_DIR'}/ispcp-apache-logger.stderr");
75         checkFileSize("$main::cfg{'LOG_DIR'}/ispcp-apache-logger.stdout");
76
77         my $res = open (STDERR, ">>", "$main::cfg{'LOG_DIR'}/ispcp-apache-logger.stderr");
78         if (!defined($res)) {
79                 print STDERR "[ispcp-apache-logger] Can't redirect STDERR\n";
80         } else {
81                 STDERR->autoflush(1);
82         }
83         $res = open (STDOUT, ">>", "$main::cfg{'LOG_DIR'}/ispcp-apache-logger.stdout");
84         if (!defined($res)) {
85                 print STDERR "[ispcp-apache-logger] Can't redirect STDOUT\n";
86         } else {
87                 STDOUT->autoflush(1);
88         }
89 }
90
91 if ( !-d $main::cfg{'APACHE_LOG_DIR'}) {
92         print STDERR "[ispcp-apache-logger] target directory ".$main::cfg{'APACHE_LOG_DIR'}." does not exist!!!\n";
93 } elsif ( !-d $main::cfg{'APACHE_USERS_LOG_DIR'}) {
94         print STDERR "[ispcp-apache-logger] target directory ".$main::cfg{'APACHE_USERS_LOG_DIR'}." does not exist!!!.\n";
95 } elsif ( $OPTS{'e'} ) {
96         ErrorLogs();
97 } else {
98         DefaultLogs();
99 }
100
101 while ( my $log_line = <STDIN> ){}
102
103 sub ErrorLogs(){
104         if (defined($main::engine_debug)){
105                 print STDOUT "[ispcp-apache-logger] Starting error log proccessing...\n";
106         }
107         while ( my $log_line = <STDIN> ) {
108                 my $vhost = 'default';
109
110                 if($log_line =~ m/($main::cfg{'APACHE_WWW_DIR'})/){
111                         ($vhost) = $log_line =~ m/$main::cfg{'APACHE_WWW_DIR'}\/([a-zA-Z0-9_\-\.]*)/;
112                 } else {
113                         if($log_line =~ m/($main::cfg{'PHP_STARTER_DIR'})/){
114                                 ($vhost) = $log_line =~ m/$main::cfg{'PHP_STARTER_DIR'}\/([a-zA-Z0-9_\-\.]*)/;
115                         }
116                 }
117
118                 if ( $vhost eq 'master' ){
119                         $vhost='default';
120                 }
121
122                 my $force_open=0;
123                 if ( !$error_logs{$vhost} ){
124                         $force_open=1;
125                         if ( (keys(%timestamps)+1) > $MAXFILES ) {
126                                 my ( $key, $value ) = sort { $timestamps{$a} <=> $timestamps{$b} } ( keys(%timestamps) );
127                                 close $error_logs{$key};
128                                 delete $error_logs{$key};
129                                 delete $timestamps{$key};
130                         }
131                 }
132
133                 checkFileExists($vhost, \%error_logs, $main::cfg{'APACHE_USERS_LOG_DIR'},"-error.log",$force_open);
134                 print { $error_logs{$vhost} } $log_line;
135         }
136 }
137
138 sub DefaultLogs(){
139         if (defined($main::engine_debug)){
140                 print STDOUT "[ispcp-apache-logger] Starting default log proccessing...\n";
141         }
142         while ( my $log_line = <STDIN> ) {
143                 my ($vhost, $size, $line) = $log_line =~ m/^(\S+) (\d+|-) (.*)$/s;
144                
145                 if( !defined($vhost) || !defined($size) ){
146                         print STDERR "[ispcp-apache-logger] Trouble line:\n\t$log_line\n";
147                 }
148                
149                 $vhost = lc($vhost) || "default";
150                 if ( $vhost =~ m#[/\\]# ) { $vhost = "default" }
151                 $vhost =~ /(.*)/o;
152                 $vhost = $1;
153                 $vhost = 'default' unless $vhost;
154
155                 my $force_open=0;
156                 if ( !$combined_logs{$vhost} || !$traff_logs{$vhost} || !$access_logs{$vhost} ){
157                         $force_open=1;
158                         if ( (keys(%timestamps)+1) > $MAXFILES ) {
159                                 my ( $key, $value ) = sort { $timestamps{$a} <=> $timestamps{$b} } ( keys(%timestamps) );
160                                 if(-e "$main::cfg{'APACHE_LOG_DIR'}/$key-combined.log" && defined($combined_logs{$key})){
161                                         close $combined_logs{$key};
162                                 }
163                                 if(-e "$main::cfg{'APACHE_LOG_DIR'}/$key-traf.log" && defined($traff_logs{$key})){
164                                         close $traff_logs{$key};
165                                 }
166                                 if(-e "$main::cfg{'APACHE_USERS_LOG_DIR'}/$key-access.log" && defined($access_logs{$key})){
167                                         close $access_logs{$key};
168                                 }
169                                 delete $combined_logs{$key};
170                                 delete $traff_logs{$key};
171                                 delete $access_logs{$key};
172                                 delete $timestamps{$key};
173                         }
174                 }
175
176                 checkFileExists($vhost, \%combined_logs, $main::cfg{'APACHE_LOG_DIR'},"-combined.log",$force_open);
177                 checkFileExists($vhost, \%traff_logs, $main::cfg{'APACHE_LOG_DIR'},"-traf.log",$force_open);
178                 checkFileExists($vhost, \%access_logs, $main::cfg{'APACHE_USERS_LOG_DIR'},"-access.log",$force_open);
179
180                 $log_line=$line;
181
182                 print { $combined_logs{$vhost} } $log_line;
183                 print { $access_logs{$vhost} } $log_line;
184                 if ( $size ne '-' && $size != 0 ){
185                         print { $traff_logs{$vhost} } "$size\n";
186                 }
187         }
188 }
189
190 exit;
191
192 sub checkFileSize(){
193         my ($file)=@_;
194         if( -e $file && ((my $filesize = -s $file) > $MAXLOGFILESIZE)) {
195                 unlink($file);
196         }
197 }
198
199 sub checkFileExists(){
200         my ($local_vhost, $hash, $path, $postpend,$force)=@_;
201         if (!(-e "$path/$local_vhost$postpend") || $force eq '1'){
202                 my $res = open ($hash->{$local_vhost}, ">>", "$path/$local_vhost$postpend");
203                 if (!defined($res)) {
204                         print STDERR "[ispcp-apache-logger] Can't open $path/$local_vhost$postpend\n";
205                 } else {
206                         $hash->{$local_vhost}->autoflush(1);
207                         $timestamps{$local_vhost}=time();
208                 }
209         }
210 }
211
212 sub exitall {
213         if ( $OPTS{'e'} ) {
214                 foreach my $key ( keys %error_logs ) {
215                         close $key;
216                 }
217                 if (defined($main::engine_debug)){
218                         print STDOUT "[ispcp-apache-logger] Ending error log proccessing...\n";
219                 }
220         } else {
221                 foreach my $key ( keys %combined_logs ) {
222                         close $key;
223                 }
224                 %combined_logs = ();
225                 foreach my $key ( keys %traff_logs ) {
226                         close $key;
227                 }
228                 %traff_logs = ();
229                 foreach my $key ( keys %access_logs ) {
230                         close $key;
231                 }
232                 %access_logs = ();
233                 if (defined($main::engine_debug)){
234                         print STDOUT "[ispcp-apache-logger] Ending default log proccessing...\n";
235                 }
236         }
237 }
Note: See TracBrowser for help on using the browser.