author | Peter Gervai <grin@grin.hu> |
Thu, 30 Sep 2021 15:38:43 +0200 | |
changeset 9 | f1cb959b4603 |
parent 8 | 31c4ce4d9b73 |
child 10 | 72f6df1085b7 |
permissions | -rwxr-xr-x |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
1 |
#!/usr/bin/perl |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
2 |
# $Id: perlgrok.pl,v d6e64daafdc1 2021/09/29 23:46:59 grin $ |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
3 |
# |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
4 |
# grok parser in perl |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
5 |
# |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
6 |
# (c)Peter 'grin' Gervai, 2021; CC_BY-SA-4.0 / GPLv3+ |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
7 |
# grin*grin.hu |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
8 |
# |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
9 |
# This has been thrown together within half an hour, so... beware of the rough edges. |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
10 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
11 |
use warnings; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
12 |
use strict; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
13 |
use IO::File; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
14 |
use Getopt::Long; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
15 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
16 |
# pattern filename |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
17 |
my @fn_patterns = (); |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
18 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
19 |
# top pattern we resolve |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
20 |
my $top = "TEST"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
21 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
22 |
# debug level |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
23 |
my $DEBUG=1; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
24 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
25 |
my $fn_test = "-"; # STDIN |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
26 |
my $fn_fail; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
27 |
my $fn_parsed = "-"; # SDTOUT |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
28 |
my %input; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
29 |
my %processing; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
30 |
my $help = 0; |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
31 |
my $re_trace = 1; # check all regexp |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
32 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
33 |
my %xlate; # perl names don't support period in name, so we need to translate |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
34 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
35 |
GetOptions( |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
36 |
"input=s" => \$fn_test, |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
37 |
"pattern=s" => \@fn_patterns, |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
38 |
"top=s" => \$top, |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
39 |
"parsed=s" => \$fn_parsed, |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
40 |
"failed=s" => \$fn_fail, |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
41 |
"debug=i" => \$DEBUG, |
9 | 42 |
"retrace!" => \$re_trace, |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
43 |
"help" => \$help, |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
44 |
) or die "error in command line arguments"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
45 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
46 |
if( $help ) { |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
47 |
my $VER = '$Id: perlgrok.pl,v d6e64daafdc1 2021/09/29 23:46:59 grin $'; |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
48 |
print "$0 $VER (c) Peter 'grin' Gervai, 2021\n\n"; |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
49 |
print "Usage: $0 [--input input_file] [--pattern pattern_file] [--pattern another] [--top final_macro] \n"; |
9 | 50 |
print " [--parsed parsed_outfile] [--failed failed_lines_file] [--debug n] [--noretrace] [--help]\n"; |
51 |
print " input: input file to be processed; default stdin\n"; |
|
52 |
print " pattern: input grok-style pattern file; default patterns (name space pattern) (multiple possible)\n"; |
|
53 |
print " top: name of the pattern to match; default TEST\n"; |
|
54 |
print " parsed: output filename for parsed data; default stdout\n"; |
|
55 |
print " failed: output filename for lines not matched; default none\n"; |
|
56 |
print " debug: debug level 0 (none), 1 (error), 3 (warning), 7 (debug), 9 (trace)\n"; |
|
57 |
print " notrace: disable regexp syntax check per pattern\n"; |
|
58 |
print "\n\n"; |
|
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
59 |
exit; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
60 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
61 |
|
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
62 |
if( $#fn_patterns < 0 ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
63 |
push @fn_patterns, "patterns"; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
64 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
65 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
66 |
|
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
67 |
# read patterns |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
68 |
foreach my $fn_pattern (@fn_patterns) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
69 |
my $f = new IO::File "< $fn_pattern" or die "cannot open pattern $fn_pattern: $!"; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
70 |
&d(7, "Reading $fn_pattern pattern file contents"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
71 |
while( <$f> ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
72 |
chomp; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
73 |
# skip empty and comment lines |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
74 |
next if /^ *(#|$)/; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
75 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
76 |
# parse <name><space><content> |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
77 |
if( /^(\S+) +(.+)$/ ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
78 |
if( defined( $input{$1} ) ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
79 |
&d(1, "Warning: duplicate macro $1 in $fn_pattern; overwrites previous version!"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
80 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
81 |
$input{$1} = $2; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
82 |
} else { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
83 |
die "Patternfile $fn_pattern parse error:\n$_\n\n"; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
84 |
} |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
85 |
} |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
86 |
$f->close; |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
87 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
88 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
89 |
&d(5, "Start program"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
90 |
&resolve( $top, '<root>' ); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
91 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
92 |
my $res = $input{$top}; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
93 |
&d(9, "RESULT\n========\n${top} = $res" ); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
94 |
&d(9, "Pattern length=" . length($res) ); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
95 |
|
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
96 |
eval { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
97 |
my $test = "abc123"; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
98 |
$test =~ /$res/; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
99 |
}; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
100 |
if( $@ ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
101 |
print "ERR: $@\n\n"; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
102 |
exit; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
103 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
104 |
|
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
105 |
### read file |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
106 |
my $f = new IO::File "< $fn_test" or die "cannot open $fn_test: $!"; |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
107 |
my $ff; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
108 |
if( $fn_fail ) { $ff = new IO::File "> $fn_fail" or die "cannot open $fn_fail for writing: $!"; } |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
109 |
my $fout = new IO::File "> $fn_parsed" or die "cannot open output for parsed: $!"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
110 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
111 |
while( <$f> ) { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
112 |
chomp; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
113 |
&d(5, "LINE:$_"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
114 |
if( /$res/ ) { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
115 |
# there is a match, print parsed values |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
116 |
print $fout "$_\n"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
117 |
for my $k ( sort keys %+ ) { |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
118 |
print $fout "\t".&unxlate($k)." => '$+{$k}'\n"; |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
119 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
120 |
} else { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
121 |
if( $fn_fail ) { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
122 |
print $ff "$_\n" if $fn_fail ; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
123 |
} else { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
124 |
print "$_\n"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
125 |
print "\t-no match-\n"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
126 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
127 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
128 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
129 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
130 |
$fout->close; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
131 |
$f->close; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
132 |
$ff->close if $fn_fail; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
133 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
134 |
exit; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
135 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
136 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
137 |
######################################################################################################### |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
138 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
139 |
# fully resolve a macro line (recursively) |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
140 |
sub resolve { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
141 |
my ($key, $caller_key) = @_; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
142 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
143 |
&d( 7, "Called resolve for $key from $caller_key"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
144 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
145 |
# get macro data from %input |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
146 |
my $data = $input{$key}; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
147 |
if( $processing{$key} ) { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
148 |
# already processing this key: looping |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
149 |
die "Resolving loop in $key: already processing for $processing{$key}! Called from $caller_key."; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
150 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
151 |
$processing{$key} = $caller_key; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
152 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
153 |
&d( 7, "Resolve_all_macros loop in $key"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
154 |
# while we have unresolved macros, walk and resolve them all |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
155 |
while( $data =~ /%\{(?<k>[A-Z_0-9]+)(:(?<v>[_\w.\d]+))?\}/ ) { |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
156 |
my $r_key = $+{k}; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
157 |
my $r_name = $+{v} || ""; # this is just informational here |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
158 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
159 |
&d(7, "Resolve macro in $key for '$r_key' (name $r_name) in data /$data/" ); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
160 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
161 |
# do we know the macro? |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
162 |
if( defined( $input{ $r_key } ) ) { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
163 |
# resolve everything in the macro |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
164 |
&resolve( $r_key, $key ); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
165 |
# replace fully resolved macro in $data |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
166 |
&replace_all( \$data, $r_key ); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
167 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
168 |
} else { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
169 |
die "Unknown macro '$r_key'! Called from $caller_key."; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
170 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
171 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
172 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
173 |
# update macro in %input with fully resolved line |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
174 |
$input{$key} = $data; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
175 |
&d(7, "RESOLVED '$key'! Result=/$data/"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
176 |
|
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
177 |
if( $re_trace ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
178 |
# die if regexp is bad |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
179 |
"abc" =~ /$data/; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
180 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
181 |
|
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
182 |
# release loop protection for $key |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
183 |
delete $processing{$key}; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
184 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
185 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
186 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
187 |
## replace all macros with replacement [macro-free] regexp |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
188 |
sub replace_all { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
189 |
my ($data, $key) = @_; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
190 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
191 |
&d(7, "Replace_all $key in /$$data/"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
192 |
# while we have this macro in $data somewhere, repeat... |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
193 |
while( $$data =~ /\{${key}(:(?<v>[_\w.\d]+))?\}/ ) { |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
194 |
my $mname = $+{v}; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
195 |
my $inner = $input{$key}; # shall be already resolved, may be checked (FIXME) |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
196 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
197 |
&d(7, " Found '$key'"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
198 |
|
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
199 |
my $xname = $mname; |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
200 |
if( $mname ) { |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
201 |
# if it's not perl compatible, create a translation entry |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
202 |
if( $mname !~ /^[_A-Za-z][_A-Za-z0-9]*\z/ ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
203 |
$xname = &gen_xlate_entry($mname); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
204 |
} |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
205 |
# named macro, create a named capture group |
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
206 |
$$data =~ s/%\{${key}:${mname}\}/(?<${xname}>${inner})/g; |
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
207 |
&d(7, " Replace $key:$mname with named capture group /$inner/"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
208 |
} else { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
209 |
# unnamed macro, we just throw it in (trust no1 and parenthesize it) |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
210 |
$$data =~ s/%\{${key}\}/(?:${inner})/g; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
211 |
&d(7, " Replace $key with /$inner/"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
212 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
213 |
|
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
214 |
&sanitize_labels( $data ); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
215 |
|
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
216 |
&d(9, " Replacement result is /$$data/"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
217 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
218 |
&d(7, "REPLACED ALL '$key' in /$$data/"); |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
219 |
} |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
220 |
|
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
221 |
|
8
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
222 |
sub gen_xlate_entry { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
223 |
my ($mname) = @_; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
224 |
&d(7, "Generate a translation variable name for $mname"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
225 |
my $xname = "xlate_" . int(rand(1_000_000)); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
226 |
my $count = 100_000; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
227 |
while( defined( $xlate{$xname} ) ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
228 |
$xname = "xlate_" . int(rand(1_000_000)); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
229 |
die "Cannot generate unique translation for $mname in a _lot_ of steps" if --$count<=0; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
230 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
231 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
232 |
&d(7, "Generated $xname as the name for $mname"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
233 |
$xlate{$xname} = $mname; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
234 |
return $xname; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
235 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
236 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
237 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
238 |
# find ?<names> which perl incompatible, and xlate them forcibly |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
239 |
sub sanitize_labels { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
240 |
my ($data) = @_; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
241 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
242 |
if( $$data =~ /(?<!\\)\((?!\\)\?<([_A-Za-z][_A-Za-z0-9]*[^>_A-Za-z0-9])/ ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
243 |
&d(7, "Irregular name in $$data, create translation"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
244 |
# we have at least one irregular, get all labels |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
245 |
while( $$data =~ /(?<!\\)\((?!\\)\?<(.+?)>/g ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
246 |
my $label = $1; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
247 |
&d(9, "Got label $label (***BEFORE $` ##<->## $' AFTER***)"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
248 |
if( $label !~ /^[_A-Za-z][_A-Za-z0-9]*\z/ ) { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
249 |
# not perl compatible, translate and replace |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
250 |
my $xname = &gen_xlate_entry($label); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
251 |
$$data =~ s/(?<!\\)\((?!\\)\?<${label}>/(?<${xname}>/g; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
252 |
&d(5, "ForceReplaced label '$label' with '$xname' in /$$data/"); |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
253 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
254 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
255 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
256 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
257 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
258 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
259 |
sub unxlate { |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
260 |
my ($xkey) = @_; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
261 |
return $xlate{$xkey}; |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
262 |
} |
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
263 |
|
31c4ce4d9b73
perlgrok.pl: Fix patternfile; handle perl-incopatible labels
Peter Gervai <grin@grin.hu>
parents:
7
diff
changeset
|
264 |
|
6
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
265 |
# extremely low-key debug subsystem :-) |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
266 |
sub d { |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
267 |
my ($lvl, $s) = @_; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
268 |
return if $lvl > $DEBUG; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
269 |
print scalar(localtime) . " [$$] ($lvl) $s\n"; |
78c000fd2ee7
Add perlgrok.pl: a logstash/grok compatible perl parser
Peter Gervai <grin@grin.hu>
parents:
diff
changeset
|
270 |
} |