forked from thekrakken/java-grok
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrok.java
More file actions
461 lines (417 loc) · 13.1 KB
/
Grok.java
File metadata and controls
461 lines (417 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*******************************************************************************
* Copyright 2014 Anthony Corbacho and contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package io.thekraken.grok.api;
import static java.lang.String.format;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.thekraken.grok.api.exception.GrokException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@code Grok} parse arbitrary text and structure it.<br>
*
* {@code Grok} is simple API that allows you to easily parse logs
* and other files (single line). With {@code Grok},
* you can turn unstructured log and event data into structured data (JSON).
*<br>
* example:<br>
* <pre>
* Grok grok = Grok.create("patterns/patterns");
* grok.compile("%{USER}");
* Match gm = grok.match("root");
* gm.captures();
* </pre>
*
* @since 0.0.1
* @author anthonycorbacho
*/
public class Grok implements Serializable {
private static final Logger LOG = LoggerFactory.getLogger(Grok.class);
/**
* Named regex of the originalGrokPattern.
*/
private String namedRegex;
/**
* Map of the named regex of the originalGrokPattern
* with id = namedregexid and value = namedregex.
*/
private Map<String, String> namedRegexCollection;
/**
* Original {@code Grok} pattern (expl: %{IP}).
*/
private String originalGrokPattern;
/**
* Pattern of the namedRegex.
*/
private Pattern compiledNamedRegex;
/**
* {@code Grok} discovery.
*/
private Discovery disco;
/**
* {@code Grok} patterns definition.
*/
private Map<String, String> grokPatternDefinition;
/** only use in grok discovery. */
private String savedPattern;
/**
* automatic conversion of values
*/
private boolean automaticConversionEnabled = true;
/**
* Create Empty {@code Grok}.
*/
public static final Grok EMPTY = new Grok();
/**
* Create a new <i>empty</i>{@code Grok} object.
*/
public Grok() {
originalGrokPattern = StringUtils.EMPTY;
disco = null;
namedRegex = StringUtils.EMPTY;
compiledNamedRegex = null;
grokPatternDefinition = new TreeMap<String, String>();
namedRegexCollection = new TreeMap<String, String>();
savedPattern = StringUtils.EMPTY;
}
public String getSaved_pattern() {
return savedPattern;
}
public void setSaved_pattern(String savedpattern) {
this.savedPattern = savedpattern;
}
/**
* Create a {@code Grok} instance with the given patterns file and
* a {@code Grok} pattern.
*
* @param grokPatternPath Path to the pattern file
* @param grokExpression - <b>OPTIONAL</b> - Grok pattern to compile ex: %{APACHELOG}
* @return {@code Grok} instance
* @throws GrokException runtime expt
*/
public static Grok create(String grokPatternPath, String grokExpression)
throws GrokException {
if (StringUtils.isBlank(grokPatternPath)) {
throw new GrokException("{grokPatternPath} should not be empty or null");
}
Grok g = new Grok();
g.addPatternFromFile(grokPatternPath);
if (StringUtils.isNotBlank(grokExpression)) {
g.compile(grokExpression, false);
}
return g;
}
/**
* Create a {@code Grok} instance with the given grok patterns file.
*
* @param grokPatternPath : Path to the pattern file
* @return Grok
* @throws GrokException runtime expt
*/
public static Grok create(String grokPatternPath) throws GrokException {
return create(grokPatternPath, null);
}
/**
* Add custom pattern to grok in the runtime.
*
* @param name : Pattern Name
* @param pattern : Regular expression Or {@code Grok} pattern
* @throws GrokException runtime expt
**/
public void addPattern(String name, String pattern) throws GrokException {
if (StringUtils.isBlank(name)) {
throw new GrokException("Invalid Pattern name");
}
if (StringUtils.isBlank(pattern)) {
throw new GrokException("Invalid Pattern");
}
grokPatternDefinition.put(name, pattern);
}
/**
* Copy the given Map of patterns (pattern name, regular expression) to {@code Grok},
* duplicate element will be override.
*
* @param cpy : Map to copy
* @throws GrokException runtime expt
**/
public void copyPatterns(Map<String, String> cpy) throws GrokException {
if (cpy == null) {
throw new GrokException("Invalid Patterns");
}
if (cpy.isEmpty()) {
throw new GrokException("Invalid Patterns");
}
for (Map.Entry<String, String> entry : cpy.entrySet()) {
grokPatternDefinition.put(entry.getKey().toString(), entry.getValue().toString());
}
}
/**
* Get the current map of {@code Grok} pattern.
*
* @return Patterns (name, regular expression)
*/
public Map<String, String> getPatterns() {
return grokPatternDefinition;
}
/**
* Get the named regex from the {@code Grok} pattern. <br>
* See {@link #compile(String)} for more detail.
* @return named regex
*/
public String getNamedRegex() {
return namedRegex;
}
/**
* Add patterns to {@code Grok} from the given file.
*
* @param file : Path of the grok pattern
* @throws GrokException runtime expt
*/
public void addPatternFromFile(String file) throws GrokException {
File f = new File(file);
if (!f.exists()) {
throw new GrokException("Pattern not found");
}
if (!f.canRead()) {
throw new GrokException("Pattern cannot be read");
}
FileReader r = null;
try {
r = new FileReader(f);
addPatternFromReader(r);
} catch (FileNotFoundException e) {
throw new GrokException(e.getMessage());
} catch (@SuppressWarnings("hiding") IOException e) {
throw new GrokException(e.getMessage());
} finally {
try {
if (r != null) {
r.close();
}
} catch (IOException io) {
// TODO(anthony) : log the error
}
}
}
/**
* Add patterns to {@code Grok} from a Reader.
*
* @param r : Reader with {@code Grok} patterns
* @throws GrokException runtime expt
*/
public void addPatternFromReader(Reader r) throws GrokException {
BufferedReader br = new BufferedReader(r);
String line;
// We dont want \n and commented line
Pattern pattern = Pattern.compile("^([A-z0-9_]+)\\s+(.*)$");
try {
while ((line = br.readLine()) != null) {
Matcher m = pattern.matcher(line);
if (m.matches()) {
this.addPattern(m.group(1), m.group(2));
}
}
br.close();
} catch (IOException e) {
throw new GrokException(e.getMessage());
} catch (GrokException e) {
throw new GrokException(e.getMessage());
}
}
/**
* Disable automatic conversion of values
*/
public void disableAutomaticConversion() {
this.automaticConversionEnabled = false;
}
public boolean isAutomaticConversionEnabled() {
return automaticConversionEnabled;
}
/**
* Match the given <tt>log</tt> with the named regex.
* And return the json representation of the matched element
*
* @param log : log to match
* @return json representation og the log
*/
public String capture(String log){
Match match = match(log);
match.captures();
return match.toJson();
}
/**
* Match the given list of <tt>log</tt> with the named regex
* and return the list of json representation of the matched elements.
*
* @param logs : list of log
* @return list of json representation of the log
*/
public List<String> captures(List<String> logs){
List<String> matched = new ArrayList<String>();
for (String log : logs) {
Match match = match(log);
match.captures();
matched.add(match.toJson());
}
return matched;
}
/**
* Match the given <tt>text</tt> with the named regex
* {@code Grok} will extract data from the string and get an extence of {@link Match}.
*
* @param text : Single line of log
* @return Grok Match
*/
public Match match(String text) {
if (compiledNamedRegex == null || StringUtils.isBlank(text)) {
return Match.EMPTY;
}
Matcher m = compiledNamedRegex.matcher(text);
Match match = new Match();
if (m.find()) {
match.setSubject(text);
match.setGrok(this);
match.setMatch(m);
match.setStart(m.start(0));
match.setEnd(m.end(0));
}
return match;
}
/**
* Compile the {@code Grok} pattern to named regex pattern.
*
* @param pattern : Grok pattern (ex: %{IP})
* @throws GrokException runtime expt
*/
public void compile(String pattern) throws GrokException {
compile(pattern, false);
}
/**
* Compile the {@code Grok} pattern to named regex pattern.
*
* @param pattern : Grok pattern (ex: %{IP})
* @param namedOnly : Whether to capture named expressions only or not (i.e. %{IP:ip} but not ${IP})
* @throws GrokException runtime expt
*/
public void compile(String pattern, boolean namedOnly) throws GrokException {
if (StringUtils.isBlank(pattern)) {
throw new GrokException("{pattern} should not be empty or null");
}
namedRegex = pattern;
originalGrokPattern = pattern;
int index = 0;
/** flag for infinite recurtion */
int iterationLeft = 1000;
Boolean continueIteration = true;
// Replace %{foo} with the regex (mostly groupname regex)
// and then compile the regex
while (continueIteration) {
continueIteration = false;
if (iterationLeft <= 0) {
throw new GrokException("Deep recursion pattern compilation of " + originalGrokPattern);
}
iterationLeft--;
Matcher m = GrokUtils.GROK_PATTERN.matcher(namedRegex);
// Match %{Foo:bar} -> pattern name and subname
// Match %{Foo=regex} -> add new regex definition
if (m.find()) {
continueIteration = true;
Map<String, String> group = GrokUtils.namedGroups(m, m.group());
if (group.get("definition") != null) {
try {
addPattern(group.get("pattern"), group.get("definition"));
group.put("name", group.get("name") + "=" + group.get("definition"));
} catch (GrokException e) {
throw new RuntimeException(e);
}
}
int count = StringUtils.countMatches(namedRegex, "%{" + group.get("name") + "}");
for (int i = 0; i < count; i++) {
String definitionOfPattern = grokPatternDefinition.get(group.get("pattern"));
if (definitionOfPattern == null) {
throw new GrokException(format("No definition for key '%s' found, aborting",
group.get("pattern")));
}
String replacement = String.format("(?<name%d>%s)", index, definitionOfPattern);
if (namedOnly && group.get("subname") == null) {
replacement = String.format("(?:%s)", definitionOfPattern);
}
namedRegexCollection.put("name" + index,
(group.get("subname") != null ? group.get("subname") : group.get("name")));
namedRegex =
StringUtils.replace(namedRegex, "%{" + group.get("name") + "}", replacement,1);
// System.out.println(_expanded_pattern);
index++;
}
}
}
if (namedRegex.isEmpty()) {
throw new GrokException("Pattern not fount");
}
// Compile the regex
compiledNamedRegex = Pattern.compile(namedRegex);
}
/**
* {@code Grok} will try to find the best expression that will match your input.
* {@link Discovery}
*
* @param input : Single line of log
* @return the Grok pattern
*/
public String discover(String input) {
if (disco == null) {
disco = new Discovery(this);
}
return disco.discover(input);
}
/**
* Original grok pattern used to compile to the named regex.
*
* @return String Original Grok pattern
*/
public String getOriginalGrokPattern(){
return originalGrokPattern;
}
/**
* Get the named regex from the given id.
*
* @param id : named regex id
* @return String of the named regex
*/
public String getNamedRegexCollectionById(String id) {
return namedRegexCollection.get(id);
}
/**
* Get the full collection of the named regex.
*
* @return named RegexCollection
*/
public Map<String, String> getNamedRegexCollection() {
return namedRegexCollection;
}
}