From 9316364e9deb206cff63afc0b8ca6f919b4f63e0 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 2 Jan 2023 14:20:48 +0200 Subject: initial script, but WIP --- README.md | 2 +- foostats.pl | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 foostats.pl diff --git a/README.md b/README.md index 13f65a6..b9e747d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # foostats -Small Perl script reporting anonymous site stats for my foo.zone web and gemini capsule. \ No newline at end of file +Small Perl script reporting anonymous site stats for my foo.zone web and gemini capsule running on OpenBSD using `httpd` webserver and `relayd`, `inetd` + `vger` for Gemini. diff --git a/foostats.pl b/foostats.pl new file mode 100644 index 0000000..2d82916 --- /dev/null +++ b/foostats.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl + +use v5.32; +use strict; +use warnings; +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use constant { + WWW_LOGS_GLOB => '/var/www/logs/access.log*', +}; + +use Data::Dumper; +use Time::Piece; +use Digest::SHA3 'sha3_512_base64'; +use PerlIO::gzip; + +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, $cb) { + my sub open_file ($path) { + my $flag = $path =~ /\.gz$/ ? '<:gzip' : '<'; + open my $file, $flag, $path or die $!; + return $file; + } + + for my $path (glob $glob) { + my $file = open_file $path; + $cb->(split / +/) while <$file>; + close $file; + } +} + +sub process_www_logs ($cb) { + my sub parse_date ($date) { + eval { + Time::Piece->strptime($date, '[%d/%b/%Y:%H:%M:%S')->strftime('%Y-%m-%d') + } + } + + my sub parse_line (@line) { + my ($ip_hash, $ip_proto) = anonymize_ip $line[1]; + { + host => $line[0], + ip_hash => $ip_hash, + ip_proto => $ip_proto, + date => parse_date($line[4]), + uripath => $line[7], + status => $line[9], + #line => \@line, + } + } + + process_lines WWW_LOGS_GLOB, sub (@line) { + $cb->(parse_line @line); + }; +} + +process_www_logs sub { + say Dumper @_; +}; + -- cgit v1.2.3