-
Notifications
You must be signed in to change notification settings - Fork 562
/
GrokParserStageSequenceBuilder.java
549 lines (507 loc) · 14 KB
/
GrokParserStageSequenceBuilder.java
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2016-2016 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2016 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.netmgt.syslogd;
import java.util.List;
import java.util.TimeZone;
import java.util.function.BiConsumer;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.syslogd.ParserStageSequenceBuilder.MatchInteger;
public abstract class GrokParserStageSequenceBuilder {
private static enum GrokState {
TEXT,
ESCAPE_PATTERN,
START_PATTERN,
PATTERN,
SEMANTIC,
END_PATTERN
}
private static enum GrokPattern {
STRING,
INTEGER,
MONTH
}
/**
* This enum contains all well-known syslog message fields.
*/
public static enum SemanticType {
/**
* Facility-priority integer.
*
* @see RFC 3164: PRI
* @see RFC 5424: PRIVAL
*/
facilityPriority,
/**
* Version.
*
* @see RFC 5424: VERSION
*/
version,
/**
* 4-digit year.
*
* @see ISO-8601
* @see RFC 3339
* @see RFC 5424: DATE-FULLYEAR
*/
year,
/**
* 2-digit month (1-12).
*
* @see ISO-8601
* @see RFC 3164
* @see RFC 3339
* @see RFC 5424: DATE-MONTH
*/
month,
/**
* 2-digit day of month (1-31).
*
* @see ISO-8601
* @see RFC 3164
* @see RFC 3339
* @see RFC 5424: DATE-MDAY
*/
day,
/**
* 2-digit hour of day (0-23).
*
* @see ISO-8601
* @see RFC 3164
* @see RFC 3339
* @see RFC 5424: TIME-HOUR
*/
hour,
/**
* 2-digit minute (0-59).
*
* @see ISO-8601
* @see RFC 3164
* @see RFC 3339
* @see RFC 5424: TIME-MINUTE
*/
minute,
/**
* 2-digit second (0-59).
*
* @see ISO-8601
* @see RFC 3164
* @see RFC 3339
* @see RFC 5424: TIME-SECOND
*/
second,
/**
* 1- to 6-digit fractional second value as a string.
* Note that the maximum resolution of this value is microseconds
* but we are storing the value in nanoseconds since nanosecond
* resolution is more prevalent in the Java time APIs.
*
* @see RFC 5424: TIME-SECFRAC
*/
secondFraction,
/**
* String timezone value.
*
* @see ISO-8601
* @see RFC 3339
* @see RFC 5424: TIME-OFFSET
*/
timezone,
/**
* String hostname (unqualified or FQDN), IPv4 address, or IPv6 address.
*
* @see RFC 5424: HOSTNAME
*/
hostname,
/**
* String process name.
*
* @see RFC 5424: APP-NAME
*/
processName,
/**
* String process name.
*
* @see RFC 5424: PROCID
*/
processId,
/**
* String message ID.
*
* @see RFC 5424: MSGID
*/
messageId,
/*
facility, // Cisco
priority, // Cisco
mnemonic, // Cisco
*/
/*
structuredData, // RFC5424 STRUCTURED-DATA
*/
/**
* Remaining string message.
*/
message
}
/*
Relevant EventBuilder fields:
// public EventBuilder setAlarmData(AlarmData alarmData);
public EventBuilder setDescription(String descr);
// public EventBuilder setDistPoller(String distPoller);
public EventBuilder setHost(String hostname);
// public EventBuilder setIfIndex(int ifIndex);
public EventBuilder setInterface(InetAddress ipAddress);
// public EventBuilder setIpInterface(OnmsIpInterface iface);
public EventBuilder setLogDest(String dest);
public EventBuilder setLogMessage(String content);
// public EventBuilder setMasterStation(String masterStation);
// public EventBuilder setMonitoredService(OnmsMonitoredService monitoredService);
// public EventBuilder setNode(OnmsNode node);
// public EventBuilder setNodeid(long nodeid);
public EventBuilder setParam(String parmName, String val);
public EventBuilder setParms(List<Parm> parms);
// public EventBuilder setService(String serviceName);
public EventBuilder setSeverity(String severity);
// public EventBuilder setSource(String source);
public EventBuilder setTime(Date date);
public EventBuilder setUei(String uei);
// public EventBuilder setUuid(String uuid);
SNMP-only:
// public EventBuilder setCommunity(String community);
// public EventBuilder setEnterpriseId(String enterprise);
// public EventBuilder setGeneric(int generic);
// public EventBuilder setSnmpHost(String snmpHost);
// public EventBuilder setSnmpTimeStamp(long timeStamp);
// public EventBuilder setSnmpVersion(String version);
// public EventBuilder setSpecific(int specific);
*/
/**
* This function maps {@link SemanticType} values of type int to {@link EventBuilder}
* calls.
*
* @param semanticString
* @return
*/
private static BiConsumer<ParserState,Integer> semanticIntegerToEventBuilder(String semanticString) {
SemanticType semanticType = null;
try {
semanticType = SemanticType.valueOf(semanticString);
} catch (IllegalArgumentException e) {
// Leave semanticType == null
}
if (semanticType == null) {
return (s,v) -> {
s.builder.setParam(semanticString, v);
};
} else {
switch(semanticType) {
case day:
return (s,v) -> {
s.builder.setDayOfMonth(v);
};
case facilityPriority:
return (s,v) -> {
String facilityTxt = SyslogFacility.getFacilityForCode(v).toString();
String priorityTxt = SyslogSeverity.getSeverityForCode(v).toString();
s.builder.setParam("service", facilityTxt);
s.builder.setParam("severity", priorityTxt);
s.builder.setUei("uei.opennms.org/syslogd/" + facilityTxt + "/" + priorityTxt);
};
case hour:
return (s,v) -> {
s.builder.setHourOfDay(v);
};
case minute:
return (s,v) -> {
s.builder.setMinute(v);
};
case month:
return (s,v) -> {
s.builder.setMonth(v);
};
case processId:
// processId can be an integer or string
return (s,v) -> {
s.builder.setParam("processid", v);
};
case second:
return (s,v) -> {
s.builder.setSecond(v);
};
// TODO: This should be handled as a string... this is only
// in here as a stopgap until we create a DIGITS pattern type.
case secondFraction:
return (s,v) -> {
s.builder.setMillisecond(v);
};
case version:
// Unique to this parser
return (s,v) -> {
s.builder.setParam("syslogversion", v);
};
case year:
return (s,v) -> {
s.builder.setYear(v);
};
default:
throw new IllegalArgumentException(String.format("Semantic type %s does not have an integer value", semanticString));
}
}
}
private static final BiConsumer<ParserState,String> NOOP = (s,v) -> {};
/**
* This function maps {@link SemanticType} values of type String to {@link EventBuilder}
* calls.
*
* @param semanticString
* @return
*/
private static BiConsumer<ParserState,String> semanticStringToEventBuilder(String semanticString) {
SemanticType semanticType = null;
try {
semanticType = SemanticType.valueOf(semanticString);
} catch (IllegalArgumentException e) {
// Leave semanticType == null
}
if (semanticType == null) {
return (s,v) -> {
s.builder.setParam(semanticString, v);
};
} else {
switch(semanticType) {
case hostname:
// TODO
return NOOP;
case message:
return (s,v) -> {
s.builder.setLogMessage(v);
// Using parms provides configurability.
s.builder.setParam("syslogmessage", v);
};
case messageId:
// Unique to this parser
return (s,v) -> {
s.builder.setParam("messageid", v);
};
case processId:
// processId can be an integer or string
return (s,v) -> {
s.builder.setParam("processid", v);
};
case processName:
return (s,v) -> {
s.builder.setParam("process", v);
};
case secondFraction:
return (s,v) -> {
// Convert the fraction value into a millisecond value since that
// is the best resolution that EventBuilder can handle
switch(v.length()) {
case 1:
s.builder.setMillisecond(MatchInteger.trimAndConvert(v) * 100);
break;
case 2:
s.builder.setMillisecond(MatchInteger.trimAndConvert(v) * 10);
break;
case 3:
s.builder.setMillisecond(MatchInteger.trimAndConvert(v));
break;
case 4:
s.builder.setMillisecond(MatchInteger.trimAndConvert(v) / 10);
break;
case 5:
s.builder.setMillisecond(MatchInteger.trimAndConvert(v) / 100);
break;
case 6:
s.builder.setMillisecond(MatchInteger.trimAndConvert(v) / 1000);
break;
}
};
case timezone:
return (s,v) -> {
// TODO: Make sure this works for all variants
s.builder.setTimeZone(TimeZone.getTimeZone(v));
};
default:
throw new IllegalArgumentException(String.format("Semantic type %s does not have a string value", semanticString));
}
}
}
// TODO: Figure out how to add a timestamp parm to the event
// bldr.addParam("timestamp", message.getSyslogFormattedDate());
public static List<ParserStage> parseGrok(String grok) {
GrokState state = GrokState.TEXT;
ParserStageSequenceBuilder factory = new ParserStageSequenceBuilder();
StringBuffer pattern = new StringBuffer();
StringBuffer semantic = new StringBuffer();
for (char c : grok.toCharArray()) {
switch(state) {
case TEXT:
switch(c) {
case '%':
state = GrokState.START_PATTERN;
continue;
case '\\':
state = GrokState.ESCAPE_PATTERN;
continue;
case ' ':
factory = factory.whitespace();
continue;
default:
factory = factory.character(c);
continue;
}
case ESCAPE_PATTERN:
switch(c) {
default:
factory = factory.character(c);
state = GrokState.TEXT;
continue;
}
case START_PATTERN:
switch(c) {
case '{':
state = GrokState.PATTERN;
continue;
default:
throw new IllegalStateException("Illegal character to start pattern");
}
case PATTERN:
switch(c) {
case ':':
state = GrokState.SEMANTIC;
continue;
default:
pattern.append(c);
continue;
}
case SEMANTIC:
switch(c) {
case '}':
state = GrokState.END_PATTERN;
continue;
default:
semantic.append(c);
continue;
}
case END_PATTERN:
final String patternString = pattern.toString();
final String semanticString = semantic.toString();
GrokPattern patternType = GrokPattern.valueOf(patternString);
switch(c) {
case '\\':
switch(patternType) {
case STRING:
// TODO: We need to peek forward to the escaped character and then do the same as the default case
throw new UnsupportedOperationException("Cannot support escape sequence directly after a pattern yet");
case INTEGER:
factory.integer(semanticIntegerToEventBuilder(semanticString));
break;
case MONTH:
factory.monthString(semanticIntegerToEventBuilder(semanticString));
break;
}
pattern = new StringBuffer();
semantic = new StringBuffer();
state = GrokState.ESCAPE_PATTERN;
continue;
case '%':
switch(patternType) {
case STRING:
// TODO: Can we handle this case?
throw new IllegalArgumentException(String.format("Invalid pattern: %s:%s does not have a trailing delimiter, cannot determine end of string", patternString, semanticString));
case INTEGER:
factory.integer(semanticIntegerToEventBuilder(semanticString));
break;
case MONTH:
factory.monthString(semanticIntegerToEventBuilder(semanticString));
break;
}
pattern = new StringBuffer();
semantic = new StringBuffer();
state = GrokState.START_PATTERN;
continue;
case ' ':
switch(patternType) {
case STRING:
factory.stringUntilWhitespace(semanticStringToEventBuilder(semanticString));
factory.whitespace();
break;
case INTEGER:
factory.intUntilWhitespace(semanticIntegerToEventBuilder(semanticString));
factory.whitespace();
break;
case MONTH:
factory.monthString(semanticIntegerToEventBuilder(semanticString));
factory.whitespace();
break;
}
break;
default:
switch(patternType) {
case STRING:
factory.stringUntil(String.valueOf(c), semanticStringToEventBuilder(semanticString));
factory.character(c);
break;
case INTEGER:
factory.integer(semanticIntegerToEventBuilder(semanticString));
factory.character(c);
break;
case MONTH:
factory.monthString(semanticIntegerToEventBuilder(semanticString));
factory.character(c);
break;
}
}
pattern = new StringBuffer();
semantic = new StringBuffer();
state = GrokState.TEXT;
continue;
}
}
// If we are in the process of ending a pattern, then tie it up with a bow
if (state == GrokState.END_PATTERN) {
final String patternString = pattern.toString();
final String semanticString = semantic.toString();
GrokPattern patternType = GrokPattern.valueOf(patternString);
switch(patternType) {
case STRING:
factory.terminal().string(semanticStringToEventBuilder(semanticString));
break;
case INTEGER:
factory.terminal().integer(semanticIntegerToEventBuilder(semanticString));
break;
case MONTH:
factory.terminal().monthString(semanticIntegerToEventBuilder(semanticString));
break;
}
}
return factory.getStages();
}
}