-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvscp.pl
More file actions
executable file
·183 lines (163 loc) · 6.06 KB
/
vscp.pl
File metadata and controls
executable file
·183 lines (163 loc) · 6.06 KB
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
#!/usr/bin/perl -w
#
# vscp.pl pipes cache hits between varnishncsa and cPanel's splitlogs
# binary for domlog and bytelog writeouts. This helps cPanel track
# bandwidth usage, and include previously missed cache hits in
# statistical log processing. Such as Logaholic, AWStats, Webalizer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
# Interval in seconds for /etc/userdatadomains checks
my $userdata_poll_frequency = "60";
# Print piped requests to STDOUT
my $debug = '0';
# Custom Port header set in Varnish for the request target (usually 80/443).
# It's is mainly used for differentiating SSL downgraded requests.
my $varnish_port_header = 'X-Port';
# Client IP header set in Varnish. Usually X-Forwarded-For (beware of multiple IP entries if using the default xforwardedfor)
my $varnish_ip_header = 'X-Real-IP';
# Service name for init.d
$0 = "vscpd";
chomp( my $piped_logging =
`grep ^enable_piped_logs\=1 /var/cpanel/cpanel.config` );
if ( !$piped_logging ) {
print qq{
Piped logging is currently disabled. To enable follow the steps below to enable.
Log into WHM, and follow this sequence to the right place:
- Service Configuration >> Apache Configuration >> Piped Log Configuration
- Enable piped Apache logging, save it and let it rebuild the configuration.
For more information on piped logging in WHM:
https://documentation.cpanel.net/display/ALD/Apache+Configuration
https://www.liquidweb.com/kb/how-and-why-enabling-apaches-piped-logging/
};
exit;
}
use Sys::Hostname;
my $host = hostname;
my $vncsa_pid = open( VNCSA,
'varnishncsa -q "HIT" -F "%b:::%{HOST}i:::%{'.$varnish_port_header.'}i %{'.$varnish_ip_header.'}i %l %u %t \"%m %U%q %H\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" |'
) or die "Couldn't fork: $!\n";
my $access_log = ( -f "/etc/cpanel/ea4/is_ea4" ) ? "/etc/apache2/logs/access_log" : "/usr/local/apache/logs/access_log";
my $split_logs = open( SPLITLOGS,
"| /usr/local/cpanel/bin/splitlogs --main=${host} --mainout=${access_log}"
) or die "Couldn't fork: $!\n";
my $split_bytes =
open( SPLITBYTES,
"| /usr/local/cpanel/bin/splitlogs --main=${host} --suffix=-bytes_log" )
or die "Couldn't fork: $!\n";
# Disable buffering
$| = 1;
select((select(VNCSA), $|=1)[0]);
select((select(SPLITLOGS), $|=1)[0]);
select((select(SPLITBYTES), $|=1)[0]);
my %domlogs;
my $loaded_md5 = "";
my $check_time = time();
print localtime()." Listening for requests to parse and relay..\n";
while (<VNCSA>) {
chomp $_;
&get_userdata;
my ( $bytes, $domain, $rlog ) = split /:::/, $_;
my $domlog = &get_log($domain);
if ($domlog) {
print "TOLOGPIPE: $domlog:$rlog\n" if ($debug);
print SPLITLOGS "$domlog:$rlog\n";
print "TOBYTEPIPE: $domlog $bytes .\n" if ($debug);
print SPLITBYTES "$domlog $bytes .\n";
}
}
close(VNCSA);
# Subroutines
sub get_userdata() {
return if ( time() < $check_time );
chomp( my $check_md5 = `md5sum /etc/userdatadomains|awk '{print\$1}'` );
if ( $check_md5 =~ /^$loaded_md5$/m ) {
$check_time = time() + $userdata_poll_frequency;
return;
}
%domlogs = ();
open( my $udfh, '-|', "sed -e 's/://g' /etc/userdatadomains" ) or die $!;
while ( my $userdata = <$udfh> ) {
chomp $userdata;
my $user;
my ( $udom, $owner, $type, $dom, $rest ) = split /==/, $userdata;
( $udom, $user ) = split / /, $udom;
my $d = $udom;
$d =~ s/^\*\./_wildcard_./;
if ( $type =~ /addon/ ) {
if ( $udom =~ /^\*\./m ) {
$domlogs{$udom} = $d;
}
else {
$domlogs{$udom} = $dom;
$domlogs{$dom} = $dom;
}
}
elsif ( $type =~ /main/ ) {
if ( $udom =~ /^\*\./m ) {
$domlogs{$udom} = $d;
}
else {
$domlogs{$udom} = $dom;
}
}
elsif ( $type =~ /sub/ ) {
if ( $udom =~ /^\*\./m ) {
$domlogs{$udom} = $d;
}
else {
$domlogs{$udom} = $udom;
}
}
}
# Set domlogs for IP based requests, going by the first VirtualHost entry (mimic Apache behavior)
my %by_ip;
my ($ip,$port,$sname);
chomp(my $http = `grep ^apache_port /var/cpanel/cpanel.config|cut -d: -f2`);
chomp(my $https_port = `grep ^apache_ssl_port /var/cpanel/cpanel.config|cut -d: -f2`);
open( my $ap_scan,'-|', "egrep -A1 \"<VirtualHost .*:($http|$https_port)\" /etc/httpd/conf/httpd.conf" ) or die $!;
while (my $apdata = <$ap_scan> ) {
chomp $apdata;
if ($apdata =~ /<VirtualHost (\d{1,3}\.){3}\d{1,3}:/m) {
(my $bind) = ($apdata =~ /(?<=Host ).*(?=\>)/g);
($ip,$port) = split /:/,$bind;
}
if ($apdata =~ /ServerName/m) {
($sname) = ($apdata =~ /(?<=ServerName ).*/g);
}
if (($ip) && ($sname)) {
if (!$by_ip{$ip}) {
$by_ip{$ip} = $domlogs{$sname};
$domlogs{$ip} = $domlogs{$sname};
}
($ip,$sname) = ();
}
}
$loaded_md5 = $check_md5;
$check_time = time() + $userdata_poll_frequency;
}
sub get_log() {
my $check = shift;
$check =~ s/^www\.//g;
if ( $domlogs{$check} ) {
return $domlogs{$check};
}
else {
$check =~ s/^[\w-]+\./\*\./; # strip subdomain for wildcard check
if ( $domlogs{$check} ) {
return $domlogs{$check};
}
}
}