Skip to content

Commit 442f257

Browse files
author
Till Brehm
committed
Add IP anonymization option -p to vlogger.
1 parent 9e00bf0 commit 442f257

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

server/scripts/vlogger

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# 1.1 bugfix release
2929
# 1.2 support for mod_logio
3030
# 1.3 various contributed bugfixes
31-
# 1.3ISPconfig1 This local version has been modified for ISPConfig. Namely: "Added better error handling to vlogger script in case the MySQL database connection is not available."
31+
# 1.4 automatically creates two levels of subdirs.
3232
#
3333
#
3434
# TODO:
@@ -109,6 +109,9 @@ or "%m%d%Y-error.log". When using the -r option, the default becomes
109109
-d CONFIG
110110
Use the DBI usage tracker.
111111
112+
-p
113+
Privacy mode (delete last octet of ipv4 or last 4 sections of ipv6)
114+
112115
-h
113116
Displays help.
114117
@@ -148,10 +151,12 @@ use sigtrap qw(handler exitall HUP USR1 TERM INT PIPE);
148151
use Date::Format;
149152
use Getopt::Std;
150153
use IO::Handle;
154+
use File::Path qw(make_path);
155+
use File::Basename;
151156

152157
# get command line options
153158
our %OPTS;
154-
getopts( 'f:t:s:hu:g:aeivr:d:', \%OPTS );
159+
getopts( 'f:t:s:hu:g:aeipvr:d:', \%OPTS );
155160

156161
# print out version
157162
if ( $OPTS{'v'} ) {
@@ -171,9 +176,9 @@ if ( $OPTS{'h'} || !$ARGV[0] ) {
171176
# log directory
172177
my $LOGDIR;
173178
if ( $ARGV[0] ) {
174-
if ( !-d $ARGV[0] || -l $ARGV[0]) {
179+
if ( !-d $ARGV[0] ) {
175180
print STDERR
176-
"[vlogger] target directory $ARGV[0] does not exist or is a symlink - exiting.\n\n";
181+
"[vlogger] target directory $ARGV[0] does not exist - exiting.\n\n";
177182
exit;
178183
}
179184
$LOGDIR = $ARGV[0];
@@ -253,14 +258,9 @@ if ( $OPTS{'d'} ) {
253258
}
254259

255260
# test the connection
256-
eval {
257-
my $dbh = DBI->connect( $DBI_DSN, $DBI_USER, $DBI_PASS )
258-
or die "DBI Error: $!";
259-
$dbh->disconnect;
260-
};
261-
if ($@) {
262-
print "MySQL Connection problem\n";
263-
}
261+
my $dbh = DBI->connect( $DBI_DSN, $DBI_USER, $DBI_PASS )
262+
or die "DBI Error: $!";
263+
$dbh->disconnect;
264264

265265
# SIGALRM dumps the tracker hash
266266
$SIG{ALRM} = \&dump_tracker;
@@ -317,7 +317,7 @@ if ( $OPTS{'s'} ) {
317317

318318
# chroot to the logdir
319319
chdir($LOGDIR);
320-
#chroot("."); we better do not chroot as DBI requires to load a module on the fly -> error!
320+
chroot(".");
321321

322322
my %logs = ();
323323
my %tracker = ();
@@ -378,6 +378,12 @@ else {
378378
$0 = "vlogger (access log)";
379379
while ( my $log_line = <STDIN> ) {
380380

381+
382+
if ( $OPTS{'p'} ) {
383+
$log_line =~ s/^(\S*\s+\d+\.\d+\.\d+)\.\d+(\s+.*)/$1.0$2/;
384+
$log_line =~ s/^(\S*\s+[a-f0-9]*:[a-f0-9]*:[a-f0-9]*:[a-f0-9]*)(?::[a-f0-9]*){1,4}(\s+.*)/$1::0$2/;
385+
}
386+
381387
# parse out the first word (the vhost)
382388
my @this_line = split ( /\s/, $log_line );
383389
my ($vhost) = $this_line[0];
@@ -386,7 +392,6 @@ else {
386392
if ( $vhost =~ m#[/\\]# ) { $vhost = "default" }
387393
$vhost =~ /(.*)/o;
388394
$vhost = $1;
389-
$vhost = 'default' unless $vhost;
390395

391396
if ( $OPTS{'i'} ) {
392397
$reqsize = $this_line[1] + $this_line[2];
@@ -425,20 +430,19 @@ else {
425430
delete( $logs{$key} );
426431
}
427432

433+
my $filename = "${vhost}/" . time2str( $TEMPLATE, time() );
434+
my $filepath = dirname($filename);
435+
428436
# check if directory is there
429-
unless ( -d "${vhost}" ) {
430-
mkdir("${vhost}");
437+
unless ( -d $filepath ) {
438+
print "mkdir: " . $filepath . "\n";
439+
make_path($filepath)
440+
or die ( "can't mkdir $filepath" );
431441
}
432-
433-
# Dont log to symlinks
434-
if( -l "${vhost}/".time2str( $TEMPLATE, time() ) ) {
435-
die("Log target is a symlink: $LOGDIR/${vhost}/".time2str( $TEMPLATE, time() ));
436-
}
437442

438443
# open the file using the template
439-
open $vhost, ">>${vhost}/" . time2str( $TEMPLATE, time() )
440-
or die ( "can't open $LOGDIR/${vhost}/"
441-
. time2str( $TEMPLATE, time() ) );
444+
open $vhost, ">>".$filename
445+
or die ( "can't open $filename" );
442446

443447
# autoflush the handle unless -a
444448
if ( !$OPTS{'a'} ) {
@@ -465,9 +469,11 @@ else {
465469
$log_line =~ s/^\S*\s+//o;
466470
}
467471

468-
if ( $reqsize =~ m/^\d*$/ && $reqsize > 0 ) {
469-
$tracker{$vhost} += $reqsize;
470-
}
472+
if ($reqsize ne "-") {
473+
if ( $reqsize =~ m/\d|/ && $reqsize > 0 ) {
474+
$tracker{$vhost} += $reqsize;
475+
}
476+
}
471477

472478
print $vhost $log_line;
473479

@@ -514,37 +520,32 @@ sub open_errorlog {
514520

515521
# sub to update the database with the tracker data
516522
sub dump_tracker {
517-
eval {
518-
if ( keys(%tracker) > 0 ) {
523+
if ( keys(%tracker) > 0 ) {
519524
my $dbh = DBI->connect( $DBI_DSN, $DBI_USER, $DBI_PASS )
520525
or warn "DBI Error: $!";
521526
foreach my $key ( keys(%tracker) ) {
522-
my $ts = time2str( "%Y-%m-%d", time() );
527+
my $ts = time2str( "%m%d%Y", time() );
523528
my $sth =
524-
$dbh->prepare( "select * from web_traffic where hostname='" . $key
525-
. "' and traffic_date='" . $ts . "'" );
529+
$dbh->prepare( "select * from wwwusage where vhost='" . $key
530+
. "' and ldate='" . $ts . "'" );
526531
$sth->execute;
527532
if ( $sth->rows ) {
528533
my $query =
529-
"update web_traffic set traffic_bytes=traffic_bytes+"
534+
"update wwwusage set bytes=bytes+"
530535
. $tracker{$key}
531-
. " where hostname='" . $key
532-
. "' and traffic_date='" . $ts . "'";
536+
. " where vhost='" . $key
537+
. "' and ldate='" . $ts . "'";
533538
$dbh->do($query);
534539
}
535540
else {
536-
my $query = "insert into web_traffic (hostname, traffic_date, traffic_bytes) values ('$key', '$ts', '$tracker{$key}')";
541+
my $query = "insert into wwwusage (vhost, ldate, bytes) values ('$key', '$ts', '$tracker{$key}')";
537542
$dbh->do($query);
538543
}
539544
}
540545
$dbh->disconnect;
541546
%tracker = ();
542547
}
543548
alarm $DBI_DUMP;
544-
};
545-
if ($@) {
546-
print "Unable to store vlogger data in database\n";
547-
}
548549
}
549550

550551
# print usage info
@@ -562,6 +563,8 @@ sub usage {
562563
print " -s SYMLINK maintain a symlink to most recent file\n";
563564
print " -r SIZE rotate when file reaches SIZE\n";
564565
print " -d CONFIG use DBI usage tracker (see perldoc vlogger)\n";
566+
print " -p Privacy mode (delete last octet of ipv4 or\n";
567+
print " last 4 sections of ipv6)\n";
565568
print " -i extract mod_logio instead of filesize\n";
566569
print " -h display this help\n";
567570
print " -v output version information\n\n";

0 commit comments

Comments
 (0)