|
@@ -28,8 +28,10 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.TreeMap;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
import java.util.function.BiConsumer;
|
|
|
|
|
|
+import static org.hamcrest.Matchers.containsString;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
import static org.hamcrest.Matchers.nullValue;
|
|
@@ -40,19 +42,19 @@ public class GrokTests extends ESTestCase {
|
|
|
|
|
|
public void testMatchWithoutCaptures() {
|
|
|
String line = "value";
|
|
|
- Grok grok = new Grok(basePatterns, "value");
|
|
|
+ Grok grok = new Grok(basePatterns, "value", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures(line);
|
|
|
assertEquals(0, matches.size());
|
|
|
}
|
|
|
|
|
|
public void testNoMatchingPatternInDictionary() {
|
|
|
- Exception e = expectThrows(IllegalArgumentException.class, () -> new Grok(Collections.emptyMap(), "%{NOTFOUND}"));
|
|
|
+ Exception e = expectThrows(IllegalArgumentException.class, () -> new Grok(Collections.emptyMap(), "%{NOTFOUND}", logger::warn));
|
|
|
assertThat(e.getMessage(), equalTo("Unable to find pattern [NOTFOUND] in Grok's pattern dictionary"));
|
|
|
}
|
|
|
|
|
|
public void testSimpleSyslogLine() {
|
|
|
String line = "Mar 16 00:01:25 evita postfix/smtpd[1713]: connect from camomile.cloud9.net[168.100.1.3]";
|
|
|
- Grok grok = new Grok(basePatterns, "%{SYSLOGLINE}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{SYSLOGLINE}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures(line);
|
|
|
assertEquals("evita", matches.get("logsource"));
|
|
|
assertEquals("Mar 16 00:01:25", matches.get("timestamp"));
|
|
@@ -64,7 +66,7 @@ public class GrokTests extends ESTestCase {
|
|
|
public void testSyslog5424Line() {
|
|
|
String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " +
|
|
|
"Hello, syslog.";
|
|
|
- Grok grok = new Grok(basePatterns, "%{SYSLOG5424LINE}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{SYSLOG5424LINE}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures(line);
|
|
|
assertEquals("191", matches.get("syslog5424_pri"));
|
|
|
assertEquals("1", matches.get("syslog5424_ver"));
|
|
@@ -79,13 +81,13 @@ public class GrokTests extends ESTestCase {
|
|
|
|
|
|
public void testDatePattern() {
|
|
|
String line = "fancy 12-12-12 12:12:12";
|
|
|
- Grok grok = new Grok(basePatterns, "(?<timestamp>%{DATE_EU} %{TIME})");
|
|
|
+ Grok grok = new Grok(basePatterns, "(?<timestamp>%{DATE_EU} %{TIME})", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures(line);
|
|
|
assertEquals("12-12-12 12:12:12", matches.get("timestamp"));
|
|
|
}
|
|
|
|
|
|
public void testNilCoercedValues() {
|
|
|
- Grok grok = new Grok(basePatterns, "test (N/A|%{BASE10NUM:duration:float}ms)");
|
|
|
+ Grok grok = new Grok(basePatterns, "test (N/A|%{BASE10NUM:duration:float}ms)", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures("test 28.4ms");
|
|
|
assertEquals(28.4f, matches.get("duration"));
|
|
|
matches = grok.captures("test N/A");
|
|
@@ -93,7 +95,7 @@ public class GrokTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testNilWithNoCoercion() {
|
|
|
- Grok grok = new Grok(basePatterns, "test (N/A|%{BASE10NUM:duration}ms)");
|
|
|
+ Grok grok = new Grok(basePatterns, "test (N/A|%{BASE10NUM:duration}ms)", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures("test 28.4ms");
|
|
|
assertEquals("28.4", matches.get("duration"));
|
|
|
matches = grok.captures("test N/A");
|
|
@@ -103,7 +105,7 @@ public class GrokTests extends ESTestCase {
|
|
|
public void testUnicodeSyslog() {
|
|
|
Grok grok = new Grok(basePatterns, "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " +
|
|
|
"%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " +
|
|
|
- "%{GREEDYDATA:syslog_message}");
|
|
|
+ "%{GREEDYDATA:syslog_message}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures("<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " +
|
|
|
"SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " +
|
|
|
"email@domain.no");
|
|
@@ -113,19 +115,19 @@ public class GrokTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testNamedFieldsWithWholeTextMatch() {
|
|
|
- Grok grok = new Grok(basePatterns, "%{DATE_EU:stimestamp}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{DATE_EU:stimestamp}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures("11/01/01");
|
|
|
assertThat(matches.get("stimestamp"), equalTo("11/01/01"));
|
|
|
}
|
|
|
|
|
|
public void testWithOniguramaNamedCaptures() {
|
|
|
- Grok grok = new Grok(basePatterns, "(?<foo>\\w+)");
|
|
|
+ Grok grok = new Grok(basePatterns, "(?<foo>\\w+)", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures("hello world");
|
|
|
assertThat(matches.get("foo"), equalTo("hello"));
|
|
|
}
|
|
|
|
|
|
public void testISO8601() {
|
|
|
- Grok grok = new Grok(basePatterns, "^%{TIMESTAMP_ISO8601}$");
|
|
|
+ Grok grok = new Grok(basePatterns, "^%{TIMESTAMP_ISO8601}$", logger::warn);
|
|
|
List<String> timeMessages = Arrays.asList(
|
|
|
"2001-01-01T00:00:00",
|
|
|
"1974-03-02T04:09:09",
|
|
@@ -149,7 +151,7 @@ public class GrokTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testNotISO8601() {
|
|
|
- Grok grok = new Grok(basePatterns, "^%{TIMESTAMP_ISO8601}$");
|
|
|
+ Grok grok = new Grok(basePatterns, "^%{TIMESTAMP_ISO8601}$", logger::warn);
|
|
|
List<String> timeMessages = Arrays.asList(
|
|
|
"2001-13-01T00:00:00", // invalid month
|
|
|
"2001-00-01T00:00:00", // invalid month
|
|
@@ -188,7 +190,7 @@ public class GrokTests extends ESTestCase {
|
|
|
|
|
|
String text = "wowza !!!Tal!!! - Tal";
|
|
|
String pattern = "%{EXCITED_NAME} - %{NAME}";
|
|
|
- Grok g = new Grok(bank, pattern, false);
|
|
|
+ Grok g = new Grok(bank, pattern, false, logger::warn);
|
|
|
|
|
|
assertEquals("(?<EXCITED_NAME_0>!!!(?<NAME_21>Tal)!!!) - (?<NAME_22>Tal)", g.toRegex(pattern));
|
|
|
assertEquals(true, g.match(text));
|
|
@@ -206,7 +208,7 @@ public class GrokTests extends ESTestCase {
|
|
|
Map<String, String> bank = new HashMap<>();
|
|
|
bank.put("NAME", "!!!%{NAME}!!!");
|
|
|
String pattern = "%{NAME}";
|
|
|
- new Grok(bank, pattern, false);
|
|
|
+ new Grok(bank, pattern, false, logger::warn);
|
|
|
});
|
|
|
assertEquals("circular reference in pattern [NAME][!!!%{NAME}!!!]", e.getMessage());
|
|
|
|
|
@@ -214,7 +216,7 @@ public class GrokTests extends ESTestCase {
|
|
|
Map<String, String> bank = new HashMap<>();
|
|
|
bank.put("NAME", "!!!%{NAME:name}!!!");
|
|
|
String pattern = "%{NAME}";
|
|
|
- new Grok(bank, pattern, false);
|
|
|
+ new Grok(bank, pattern, false, logger::warn);
|
|
|
});
|
|
|
assertEquals("circular reference in pattern [NAME][!!!%{NAME:name}!!!]", e.getMessage());
|
|
|
|
|
@@ -222,7 +224,7 @@ public class GrokTests extends ESTestCase {
|
|
|
Map<String, String> bank = new HashMap<>();
|
|
|
bank.put("NAME", "!!!%{NAME:name:int}!!!");
|
|
|
String pattern = "%{NAME}";
|
|
|
- new Grok(bank, pattern, false);
|
|
|
+ new Grok(bank, pattern, false, logger::warn);
|
|
|
});
|
|
|
assertEquals("circular reference in pattern [NAME][!!!%{NAME:name:int}!!!]", e.getMessage());
|
|
|
|
|
@@ -231,7 +233,7 @@ public class GrokTests extends ESTestCase {
|
|
|
bank.put("NAME1", "!!!%{NAME2}!!!");
|
|
|
bank.put("NAME2", "!!!%{NAME1}!!!");
|
|
|
String pattern = "%{NAME1}";
|
|
|
- new Grok(bank, pattern, false);
|
|
|
+ new Grok(bank, pattern, false, logger::warn);
|
|
|
});
|
|
|
assertEquals("circular reference in pattern [NAME2][!!!%{NAME1}!!!] back to pattern [NAME1]", e.getMessage());
|
|
|
|
|
@@ -241,7 +243,7 @@ public class GrokTests extends ESTestCase {
|
|
|
bank.put("NAME2", "!!!%{NAME3}!!!");
|
|
|
bank.put("NAME3", "!!!%{NAME1}!!!");
|
|
|
String pattern = "%{NAME1}";
|
|
|
- new Grok(bank, pattern, false);
|
|
|
+ new Grok(bank, pattern, false, logger::warn);
|
|
|
});
|
|
|
assertEquals("circular reference in pattern [NAME3][!!!%{NAME1}!!!] back to pattern [NAME1] via patterns [NAME2]",
|
|
|
e.getMessage());
|
|
@@ -254,7 +256,7 @@ public class GrokTests extends ESTestCase {
|
|
|
bank.put("NAME4", "!!!%{NAME5}!!!");
|
|
|
bank.put("NAME5", "!!!%{NAME1}!!!");
|
|
|
String pattern = "%{NAME1}";
|
|
|
- new Grok(bank, pattern, false);
|
|
|
+ new Grok(bank, pattern, false, logger::warn );
|
|
|
});
|
|
|
assertEquals("circular reference in pattern [NAME5][!!!%{NAME1}!!!] back to pattern [NAME1] " +
|
|
|
"via patterns [NAME2=>NAME3=>NAME4]", e.getMessage());
|
|
@@ -262,7 +264,7 @@ public class GrokTests extends ESTestCase {
|
|
|
|
|
|
public void testBooleanCaptures() {
|
|
|
String pattern = "%{WORD:name}=%{WORD:status:boolean}";
|
|
|
- Grok g = new Grok(basePatterns, pattern);
|
|
|
+ Grok g = new Grok(basePatterns, pattern, logger::warn);
|
|
|
|
|
|
String text = "active=true";
|
|
|
Map<String, Object> expected = new HashMap<>();
|
|
@@ -279,7 +281,7 @@ public class GrokTests extends ESTestCase {
|
|
|
bank.put("NUMBER", "(?:%{BASE10NUM})");
|
|
|
|
|
|
String pattern = "%{NUMBER:bytes:float} %{NUMBER:id:long} %{NUMBER:rating:double}";
|
|
|
- Grok g = new Grok(bank, pattern);
|
|
|
+ Grok g = new Grok(bank, pattern, logger::warn);
|
|
|
|
|
|
String text = "12009.34 20000000000 4820.092";
|
|
|
Map<String, Object> expected = new HashMap<>();
|
|
@@ -297,7 +299,7 @@ public class GrokTests extends ESTestCase {
|
|
|
bank.put("NUMBER", "(?:%{BASE10NUM})");
|
|
|
|
|
|
String pattern = "%{NUMBER:bytes:float} %{NUMBER:status} %{NUMBER}";
|
|
|
- Grok g = new Grok(bank, pattern);
|
|
|
+ Grok g = new Grok(bank, pattern, logger::warn);
|
|
|
|
|
|
String text = "12009.34 200 9032";
|
|
|
Map<String, Object> expected = new HashMap<>();
|
|
@@ -312,7 +314,7 @@ public class GrokTests extends ESTestCase {
|
|
|
String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " +
|
|
|
"\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " +
|
|
|
"Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"";
|
|
|
- Grok grok = new Grok(basePatterns, "%{COMBINEDAPACHELOG}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{COMBINEDAPACHELOG}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures(logLine);
|
|
|
|
|
|
assertEquals("31.184.238.164", matches.get("clientip"));
|
|
@@ -372,7 +374,7 @@ public class GrokTests extends ESTestCase {
|
|
|
String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " +
|
|
|
"HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}";
|
|
|
|
|
|
- Grok grok = new Grok(bank, pattern);
|
|
|
+ Grok grok = new Grok(bank, pattern, logger::warn);
|
|
|
|
|
|
Map<String, Object> expected = new HashMap<>();
|
|
|
expected.put("clientip", "83.149.9.216");
|
|
@@ -396,14 +398,14 @@ public class GrokTests extends ESTestCase {
|
|
|
public void testNoMatch() {
|
|
|
Map<String, String> bank = new HashMap<>();
|
|
|
bank.put("MONTHDAY", "(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])");
|
|
|
- Grok grok = new Grok(bank, "%{MONTHDAY:greatday}");
|
|
|
+ Grok grok = new Grok(bank, "%{MONTHDAY:greatday}", logger::warn);
|
|
|
assertThat(grok.captures("nomatch"), nullValue());
|
|
|
}
|
|
|
|
|
|
public void testMultipleNamedCapturesWithSameName() {
|
|
|
Map<String, String> bank = new HashMap<>();
|
|
|
bank.put("SINGLEDIGIT", "[0-9]");
|
|
|
- Grok grok = new Grok(bank, "%{SINGLEDIGIT:num}%{SINGLEDIGIT:num}");
|
|
|
+ Grok grok = new Grok(bank, "%{SINGLEDIGIT:num}%{SINGLEDIGIT:num}", logger::warn);
|
|
|
|
|
|
Map<String, Object> expected = new HashMap<>();
|
|
|
expected.put("num", "1");
|
|
@@ -430,7 +432,8 @@ public class GrokTests extends ESTestCase {
|
|
|
});
|
|
|
t.start();
|
|
|
};
|
|
|
- Grok grok = new Grok(basePatterns, grokPattern, MatcherWatchdog.newInstance(10, 200, System::currentTimeMillis, scheduler));
|
|
|
+ Grok grok = new Grok(basePatterns, grokPattern, MatcherWatchdog.newInstance(10, 200, System::currentTimeMillis, scheduler),
|
|
|
+ logger::warn);
|
|
|
Exception e = expectThrows(RuntimeException.class, () -> grok.captures(logLine));
|
|
|
run.set(false);
|
|
|
assertThat(e.getMessage(), equalTo("grok pattern matching was interrupted after [200] ms"));
|
|
@@ -470,24 +473,32 @@ public class GrokTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testUnsupportedBracketsInFieldName() {
|
|
|
- Grok grok = new Grok(basePatterns, "%{WORD:unsuppo(r)ted}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{WORD:unsuppo(r)ted}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures("line");
|
|
|
assertNull(matches);
|
|
|
}
|
|
|
|
|
|
public void testJavaClassPatternWithUnderscore() {
|
|
|
- Grok grok = new Grok(basePatterns, "%{JAVACLASS}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{JAVACLASS}", logger::warn);
|
|
|
assertThat(grok.match("Test_Class.class"), is(true));
|
|
|
}
|
|
|
|
|
|
public void testJavaFilePatternWithSpaces() {
|
|
|
- Grok grok = new Grok(basePatterns, "%{JAVAFILE}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{JAVAFILE}", logger::warn);
|
|
|
assertThat(grok.match("Test Class.java"), is(true));
|
|
|
}
|
|
|
|
|
|
+ public void testLogCallBack(){
|
|
|
+ AtomicReference<String> message = new AtomicReference<>();
|
|
|
+ Grok grok = new Grok(basePatterns, ".*\\[.*%{SPACE}*\\].*", message::set);
|
|
|
+ grok.match("[foo]");
|
|
|
+ //this message comes from Joni, so updates to Joni may change the expectation
|
|
|
+ assertThat(message.get(), containsString("regular expression has redundant nested repeat operator"));
|
|
|
+ }
|
|
|
+
|
|
|
private void assertGrokedField(String fieldName) {
|
|
|
String line = "foo";
|
|
|
- Grok grok = new Grok(basePatterns, "%{WORD:" + fieldName + "}");
|
|
|
+ Grok grok = new Grok(basePatterns, "%{WORD:" + fieldName + "}", logger::warn);
|
|
|
Map<String, Object> matches = grok.captures(line);
|
|
|
assertEquals(line, matches.get(fieldName));
|
|
|
}
|