diff options
| author | Paul Buetow <paul@buetow.org> | 2023-01-07 01:10:52 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-01-07 01:10:52 +0200 |
| commit | bd20223f11aaedd9096b0dddcc8aaca50a3baf94 (patch) | |
| tree | e9cdcfb6aa5e779348b57e85ae9678c55cfa1822 /foostats.pl | |
| parent | 9f776a3e82a036352710dbf3c218280ade8ee5b1 (diff) | |
use package
Diffstat (limited to 'foostats.pl')
| -rw-r--r-- | foostats.pl | 196 |
1 files changed, 98 insertions, 98 deletions
diff --git a/foostats.pl b/foostats.pl index 9e7fa35..0c8c595 100644 --- a/foostats.pl +++ b/foostats.pl @@ -6,123 +6,123 @@ use warnings; use feature 'signatures'; no warnings 'experimental::signatures'; -use constant { - GEMINI_LOGS_GLOB => '/var/log/daemon*', - WWW_LOGS_GLOB => '/var/www/logs/access.log*', -}; - -use Data::Dumper; -use Time::Piece; -use Digest::SHA3 'sha3_512_base64'; -use PerlIO::gzip; -use Time::Piece; -use File::stat; - -sub anonymize_ip ($ip) { - my $ip_proto = (index $ip, ':') == -1 ? 'ipv4' : 'ipv6'; - my $ip_hash = sha3_512_base64 $ip; - return ($ip_hash, $ip_proto); -} - -sub process_lines ($glob, $callback) { - my sub open_file ($path) { - my $flag = $path =~ /\.gz$/ ? '<:gzip' : '<'; - open my $file, $flag, $path or die $!; - return $file; - } +package Foostats::Tokenizer { + use constant { + GEMINI_LOGS_GLOB => '/var/log/daemon*', + WWW_LOGS_GLOB => '/var/www/logs/access.log*', + }; - for my $path (glob $glob) { - my $file = open_file $path; - $callback->($path, split / +/) while <$file>; - close $file; + use Data::Dumper; + use Time::Piece; + use Digest::SHA3 'sha3_512_base64'; + use PerlIO::gzip; + use Time::Piece; + use File::stat; + + sub anonymize_ip ($ip) { + my $ip_proto = (index $ip, ':') == -1 ? 'ipv4' : 'ipv6'; + my $ip_hash = sha3_512_base64 $ip; + return ($ip_hash, $ip_proto); } -} -sub process_www_logs ($callback) { - my sub parse_date ($date) { - eval { - Time::Piece->strptime($date, '[%d/%b/%Y:%H:%M:%S')->strftime('%Y-%m-%d') + sub read_lines ($glob, $callback) { + my sub open_file ($path) { + my $flag = $path =~ /\.gz$/ ? '<:gzip' : '<'; + open my $file, $flag, $path or die $!; + return $file; } - } - my sub parse_line (@line) { - my ($ip_hash, $ip_proto) = anonymize_ip $line[1]; - { - proto => 'http/s', - host => $line[0], - ip_hash => $ip_hash, - ip_proto => $ip_proto, - date => parse_date($line[4]), - uripath => $line[7], - status => $line[9], - #line => \@line, + for my $path (glob $glob) { + my $file = open_file $path; + $callback->($path, split / +/) while <$file>; + close $file; } } - process_lines WWW_LOGS_GLOB, sub ($logfile_path, @line) { - $callback->(parse_line @line); - }; -} + sub parse_www_logs ($callback) { + my sub parse_date ($date) { + eval { + Time::Piece->strptime($date, '[%d/%b/%Y:%H:%M:%S')->strftime('%Y-%m-%d') + } + } -sub process_gemini_logs ($callback) { - my sub year ($logfile_path) { - localtime( stat($logfile_path)->mtime )->strftime('%Y') - } + my sub parse_line (@line) { + my ($ip_hash, $ip_proto) = anonymize_ip $line[1]; + { + proto => 'http/s', + host => $line[0], + ip_hash => $ip_hash, + ip_proto => $ip_proto, + date => parse_date($line[4]), + uripath => $line[7], + status => $line[9], + } + } - sub parse_date ($year, @line) { - my $timestr = "$year $line[0] $line[1]"; - Time::Piece->strptime($timestr, '%Y %b %d')->strftime('%Y-%m-%d') + read_lines WWW_LOGS_GLOB, sub ($logfile_path, @line) { + $callback->(parse_line @line); + }; } - my sub parse_vger_line ($year, @line) { - my $full_path = $line[5]; - $full_path =~ s/"//g; - my ($proto, undef, $host, $uripath) = split '/', $full_path, 4; - $uripath = '' unless defined $uripath; - { - proto => 'gemini', - host => $host, - uripath => "/$uripath", - status => $line[6], - date => parse_date($year, @line), - time => $line[2], + sub parse_gemini_logs ($callback) { + my sub year ($logfile_path) { + localtime( stat($logfile_path)->mtime )->strftime('%Y') } - } - my sub parse_relayd_line ($year, @line) { - my ($ip_hash, $ip_proto) = anonymize_ip $line[12]; - { - ip_hash => $ip_hash, - ip_proto => $ip_proto, - date => parse_date($year, @line), - time => $line[2], + my sub parse_date ($year, @line) { + my $timestr = "$year $line[0] $line[1]"; + Time::Piece->strptime($timestr, '%Y %b %d')->strftime('%Y-%m-%d'); } - } - # Expect one vger and one relayd log line per event! So collect - # both events (one from one log line each) and then merge the result hash! - my ($vger, $relayd) = ({}, {}); - process_lines GEMINI_LOGS_GLOB, sub ($logfile_path, @line) { - my $year = year $logfile_path; - - if ($line[4] eq 'vger:') { - $vger = parse_vger_line $year, @line; - } elsif ($line[5] eq 'relay' and index($line[6], 'gemini') == 0) { - $relayd = parse_relayd_line $year, @line; + my sub parse_vger_line ($year, @line) { + my $full_path = $line[5]; + $full_path =~ s/"//g; + my ($proto, undef, $host, $uripath) = split '/', $full_path, 4; + $uripath = '' unless defined $uripath; + { + proto => 'gemini', + host => $host, + uripath => "/$uripath", + status => $line[6], + date => parse_date($year, @line), + time => $line[2], + } } - if (%$vger and %$relayd and $vger->{time} eq $relayd->{time}) { - $callback->({ %$vger, %$relayd }); - $vger = $relayd = {}; + my sub parse_relayd_line ($year, @line) { + my ($ip_hash, $ip_proto) = anonymize_ip $line[12]; + { + ip_hash => $ip_hash, + ip_proto => $ip_proto, + date => parse_date($year, @line), + time => $line[2], + } } - }; -} -sub process_logs { - my sub foo { say Dumper @_ }; - process_www_logs \&foo; - process_gemini_logs \&foo; -} + # Expect one vger and one relayd log line per event! So collect + # both events (one from one log line each) and then merge the result hash! + my ($vger, $relayd) = ({}, {}); + read_lines GEMINI_LOGS_GLOB, sub ($logfile_path, @line) { + my $year = year $logfile_path; + + if ($line[4] eq 'vger:') { + $vger = parse_vger_line $year, @line; + } elsif ($line[5] eq 'relay' and index($line[6], 'gemini') == 0) { + $relayd = parse_relayd_line $year, @line; + } + + if (%$vger and %$relayd and $vger->{time} eq $relayd->{time}) { + $callback->({ %$vger, %$relayd }); + $vger = $relayd = {}; + } + }; + } -process_logs; + sub parse_logs { + my sub foo { say Dumper @_ }; + parse_www_logs \&foo; + parse_gemini_logs \&foo; + } + parse_logs; +} |
