show by type and mac
[eg5120] / parse-node-logs.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use POSIX qw(strftime);
6 use File::Slurp;
7 use DBD::Pg;
8 use Data::Dump qw(dump);
9 use autodie;
10
11 my $path = '/home/nodelogs/c405f97667784094bca5cfa52af0bcf1/';
12
13 # select _id,json->'received',time,to_timestamp((json->>'received')::int8/1000) - interval '1 hour' from nodelog ;
14
15 my $dbh = DBI->connect("dbi:Pg:dbname=eg5120", "dpavlin", "", { RaiseError => 1 });
16 warn "# truncate table nodelog";
17 $dbh->do( qq{ truncate table nodelog } ); # FIXME
18 my $sth = $dbh->prepare(qq{insert into nodelog (time,json) values (?,?)});
19
20 foreach my $filename (
21                 sort(glob("$path/node-red-out__2023-07-*")),
22                 "$path/node-red-out.log"
23         ) {
24         warn "# $filename";
25         open(my $log, '<', $filename);
26         my $in_json = 0;
27         my $json;
28         my $year = $1 if $filename =~ m/__(20\d\d)-/;
29         $year ||= (localtime)[5] + 1900;
30         my $time;
31         while(<$log>) {
32                 chomp;
33                 #warn "## $in_json -->$_<--\n";
34                 ## XXX debug 1 in node-red with logging to system console
35                 if ( m/(\d+ \w+)\s+(\d\d:\d\d:\d\d) - \Q[info] [debug:debug 1]\E/ ) {
36                         $time = $1 . ' ' . $year . ' ' . $2;
37                         #warn "# time $time\n";
38                 } elsif ( $_ eq '{' ) {
39                         $json .= $_;
40                         $in_json = 1;
41                         #warn "+ in_json";
42                 } elsif ( $in_json ) {
43
44                         s/\s(\S+):\s/"$1":/g;   # fix names quoting
45                         s/'/"/g;                # replace ' with "
46                         $json .= $_;
47
48                         if ( $_ eq '}' ) {
49                                 $in_json = 0;
50                                 #warn "- in_json";
51                                 warn ">>> $time [[[[ $json ]]]]";
52                                 write_file '/dev/shm/json', $json;
53                                 $sth->execute( $time, $json );
54                                 $json = '';
55                         }
56                 }
57
58         }
59 }
60