summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-01-02 14:20:48 +0200
committerPaul Buetow <paul@buetow.org>2023-01-02 14:20:48 +0200
commit9316364e9deb206cff63afc0b8ca6f919b4f63e0 (patch)
tree82890ab998d187a0699442f998e01f1c40fa1d1e
parent9eea6793c7b6b56c857ac3bdad27af79fa737afe (diff)
initial script, but WIP
-rw-r--r--README.md2
-rw-r--r--foostats.pl66
2 files changed, 67 insertions, 1 deletions
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 @_;
+};
+