From 02b2148c995ca0a9b53821b134283b51399d83c5 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 4 Jul 2023 09:22:07 +0200 Subject: [PATCH] parse node-red logs to backfill data --- parse-node-logs.pl | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 parse-node-logs.pl diff --git a/parse-node-logs.pl b/parse-node-logs.pl new file mode 100755 index 0000000..1e1b6e9 --- /dev/null +++ b/parse-node-logs.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use POSIX qw(strftime); +use File::Slurp; +use DBD::Pg; +use Data::Dump qw(dump); +use autodie; + +my $dbh = DBI->connect("dbi:Pg:dbname=eg5120", "dpavlin", "", { RaiseError => 1 }); +$dbh->do( qq{ truncate table nodelog } ); # FIXME +my $sth = $dbh->prepare(qq{insert into nodelog (time,json) values (?,?)}); + +foreach my $filename ( sort(glob('node-logs/node-red-out__2023-07-*')), 'node-logs/node-red-out.log' ) { + warn "# $filename"; + open(my $log, '<', $filename); + my $in_json = 0; + my $json; + my $year = $1 if $filename =~ m/__(20\d\d)-/; + $year ||= (localtime)[5] + 1900; + my $time; + while(<$log>) { + chomp; + #warn "## $in_json -->$_<--\n"; + if ( m/(\d+ \w+)\s+(\d\d:\d\d:\d\d) - \Q[info] [debug:debug 1]\E/ ) { + $time = $1 . ' ' . $year . ' ' . $2; + #warn "# time $time\n"; + } elsif ( $_ eq '{' ) { + $json .= $_; + $in_json = 1; + #warn "+ in_json"; + } elsif ( $in_json ) { + + s/\s(\S+):\s/"$1":/g; # fix names quoting + s/'/"/g; # replace ' with " + $json .= $_; + + if ( $_ eq '}' ) { + $in_json = 0; + #warn "- in_json"; + warn ">>> $time [[[[ $json ]]]]"; + write_file '/dev/shm/json', $json; + $sth->execute( $time, $json ); + $json = ''; + } + } + + } +} -- 2.20.1