grok.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. [[grok-processor]]
  2. === Grok processor
  3. ++++
  4. <titleabbrev>Grok</titleabbrev>
  5. ++++
  6. Extracts structured fields out of a single text field within a document. You choose which field to
  7. extract matched fields from, as well as the grok pattern you expect will match. A grok pattern is like a regular
  8. expression that supports aliased expressions that can be reused.
  9. This tool is perfect for syslog logs, apache and other webserver logs, mysql logs, and in general, any log format
  10. that is generally written for humans and not computer consumption.
  11. This processor comes packaged with many
  12. https://github.com/elastic/elasticsearch/blob/{branch}/libs/grok/src/main/resources/patterns[reusable patterns].
  13. If you need help building patterns to match your logs, you will find the
  14. {kibana-ref}/xpack-grokdebugger.html[Grok Debugger] tool quite useful!
  15. The https://grokconstructor.appspot.com[Grok Constructor] is also a useful tool.
  16. [[grok-basics]]
  17. ==== Grok Basics
  18. Grok sits on top of regular expressions, so any regular expressions are valid in grok as well.
  19. The regular expression library is Oniguruma, and you can see the full supported regexp syntax
  20. https://github.com/kkos/oniguruma/blob/master/doc/RE[on the Oniguruma site].
  21. Grok works by leveraging this regular expression language to allow naming existing patterns and combining them into more
  22. complex patterns that match your fields.
  23. The syntax for reusing a grok pattern comes in three forms: `%{SYNTAX:SEMANTIC}`, `%{SYNTAX}`, `%{SYNTAX:SEMANTIC:TYPE}`.
  24. The `SYNTAX` is the name of the pattern that will match your text. For example, `3.44` will be matched by the `NUMBER`
  25. pattern and `55.3.244.1` will be matched by the `IP` pattern. The syntax is how you match. `NUMBER` and `IP` are both
  26. patterns that are provided within the default patterns set.
  27. The `SEMANTIC` is the identifier you give to the piece of text being matched. For example, `3.44` could be the
  28. duration of an event, so you could call it simply `duration`. Further, a string `55.3.244.1` might identify
  29. the `client` making a request.
  30. The `TYPE` is the type you wish to cast your named field. `int`, `long`, `double`, `float` and `boolean` are supported types for coercion.
  31. For example, you might want to match the following text:
  32. [source,txt]
  33. --------------------------------------------------
  34. 3.44 55.3.244.1
  35. --------------------------------------------------
  36. You may know that the message in the example is a number followed by an IP address. You can match this text by using the following
  37. Grok expression.
  38. [source,txt]
  39. --------------------------------------------------
  40. %{NUMBER:duration} %{IP:client}
  41. --------------------------------------------------
  42. [[using-grok]]
  43. ==== Using the Grok Processor in a Pipeline
  44. [[grok-options]]
  45. .Grok Options
  46. [options="header"]
  47. |======
  48. | Name | Required | Default | Description
  49. | `field` | yes | - | The field to use for grok expression parsing
  50. | `patterns` | yes | - | An ordered list of grok expression to match and extract named captures with. Returns on the first expression in the list that matches.
  51. | `pattern_definitions` | no | - | A map of pattern-name and pattern tuples defining custom patterns to be used by the current processor. Patterns matching existing names will override the pre-existing definition.
  52. | `trace_match` | no | false | when true, `_ingest._grok_match_index` will be inserted into your matched document's metadata with the index into the pattern found in `patterns` that matched.
  53. | `ignore_missing` | no | false | If `true` and `field` does not exist or is `null`, the processor quietly exits without modifying the document
  54. include::common-options.asciidoc[]
  55. |======
  56. Here is an example of using the provided patterns to extract out and name structured fields from a string field in
  57. a document.
  58. [source,console]
  59. --------------------------------------------------
  60. POST _ingest/pipeline/_simulate
  61. {
  62. "pipeline": {
  63. "description" : "...",
  64. "processors": [
  65. {
  66. "grok": {
  67. "field": "message",
  68. "patterns": ["%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes:int} %{NUMBER:duration:double}"]
  69. }
  70. }
  71. ]
  72. },
  73. "docs":[
  74. {
  75. "_source": {
  76. "message": "55.3.244.1 GET /index.html 15824 0.043"
  77. }
  78. }
  79. ]
  80. }
  81. --------------------------------------------------
  82. This pipeline will insert these named captures as new fields within the document, like so:
  83. [source,console-result]
  84. --------------------------------------------------
  85. {
  86. "docs": [
  87. {
  88. "doc": {
  89. "_index": "_index",
  90. "_id": "_id",
  91. "_source" : {
  92. "duration" : 0.043,
  93. "request" : "/index.html",
  94. "method" : "GET",
  95. "bytes" : 15824,
  96. "client" : "55.3.244.1",
  97. "message" : "55.3.244.1 GET /index.html 15824 0.043"
  98. },
  99. "_ingest": {
  100. "timestamp": "2016-11-08T19:43:03.850+0000"
  101. }
  102. }
  103. }
  104. ]
  105. }
  106. --------------------------------------------------
  107. // TESTRESPONSE[s/2016-11-08T19:43:03.850\+0000/$body.docs.0.doc._ingest.timestamp/]
  108. [[custom-patterns]]
  109. ==== Custom Patterns
  110. The Grok processor comes pre-packaged with a base set of patterns. These patterns may not always have
  111. what you are looking for. Patterns have a very basic format. Each entry has a name and the pattern itself.
  112. You can add your own patterns to a processor definition under the `pattern_definitions` option.
  113. Here is an example of a pipeline specifying custom pattern definitions:
  114. [source,js]
  115. --------------------------------------------------
  116. {
  117. "description" : "...",
  118. "processors": [
  119. {
  120. "grok": {
  121. "field": "message",
  122. "patterns": ["my %{FAVORITE_DOG:dog} is colored %{RGB:color}"],
  123. "pattern_definitions" : {
  124. "FAVORITE_DOG" : "beagle",
  125. "RGB" : "RED|GREEN|BLUE"
  126. }
  127. }
  128. }
  129. ]
  130. }
  131. --------------------------------------------------
  132. // NOTCONSOLE
  133. [[trace-match]]
  134. ==== Providing Multiple Match Patterns
  135. Sometimes one pattern is not enough to capture the potential structure of a field. Let's assume we
  136. want to match all messages that contain your favorite pet breeds of either cats or dogs. One way to accomplish
  137. this is to provide two distinct patterns that can be matched, instead of one really complicated expression capturing
  138. the same `or` behavior.
  139. Here is an example of such a configuration executed against the simulate API:
  140. [source,console]
  141. --------------------------------------------------
  142. POST _ingest/pipeline/_simulate
  143. {
  144. "pipeline": {
  145. "description" : "parse multiple patterns",
  146. "processors": [
  147. {
  148. "grok": {
  149. "field": "message",
  150. "patterns": ["%{FAVORITE_DOG:pet}", "%{FAVORITE_CAT:pet}"],
  151. "pattern_definitions" : {
  152. "FAVORITE_DOG" : "beagle",
  153. "FAVORITE_CAT" : "burmese"
  154. }
  155. }
  156. }
  157. ]
  158. },
  159. "docs":[
  160. {
  161. "_source": {
  162. "message": "I love burmese cats!"
  163. }
  164. }
  165. ]
  166. }
  167. --------------------------------------------------
  168. response:
  169. [source,console-result]
  170. --------------------------------------------------
  171. {
  172. "docs": [
  173. {
  174. "doc": {
  175. "_index": "_index",
  176. "_id": "_id",
  177. "_source": {
  178. "message": "I love burmese cats!",
  179. "pet": "burmese"
  180. },
  181. "_ingest": {
  182. "timestamp": "2016-11-08T19:43:03.850+0000"
  183. }
  184. }
  185. }
  186. ]
  187. }
  188. --------------------------------------------------
  189. // TESTRESPONSE[s/2016-11-08T19:43:03.850\+0000/$body.docs.0.doc._ingest.timestamp/]
  190. Both patterns will set the field `pet` with the appropriate match, but what if we want to trace which of our
  191. patterns matched and populated our fields? We can do this with the `trace_match` parameter. Here is the output of
  192. that same pipeline, but with `"trace_match": true` configured:
  193. ////
  194. Hidden setup for example:
  195. [source,console]
  196. --------------------------------------------------
  197. POST _ingest/pipeline/_simulate
  198. {
  199. "pipeline": {
  200. "description" : "parse multiple patterns",
  201. "processors": [
  202. {
  203. "grok": {
  204. "field": "message",
  205. "patterns": ["%{FAVORITE_DOG:pet}", "%{FAVORITE_CAT:pet}"],
  206. "trace_match": true,
  207. "pattern_definitions" : {
  208. "FAVORITE_DOG" : "beagle",
  209. "FAVORITE_CAT" : "burmese"
  210. }
  211. }
  212. }
  213. ]
  214. },
  215. "docs":[
  216. {
  217. "_source": {
  218. "message": "I love burmese cats!"
  219. }
  220. }
  221. ]
  222. }
  223. --------------------------------------------------
  224. ////
  225. [source,console-result]
  226. --------------------------------------------------
  227. {
  228. "docs": [
  229. {
  230. "doc": {
  231. "_index": "_index",
  232. "_id": "_id",
  233. "_source": {
  234. "message": "I love burmese cats!",
  235. "pet": "burmese"
  236. },
  237. "_ingest": {
  238. "_grok_match_index": "1",
  239. "timestamp": "2016-11-08T19:43:03.850+0000"
  240. }
  241. }
  242. }
  243. ]
  244. }
  245. --------------------------------------------------
  246. // TESTRESPONSE[s/2016-11-08T19:43:03.850\+0000/$body.docs.0.doc._ingest.timestamp/]
  247. In the above response, you can see that the index of the pattern that matched was `"1"`. This is to say that it was the
  248. second (index starts at zero) pattern in `patterns` to match.
  249. This trace metadata enables debugging which of the patterns matched. This information is stored in the ingest
  250. metadata and will not be indexed.
  251. [[grok-processor-rest-get]]
  252. ==== Retrieving patterns from REST endpoint
  253. The Grok Processor comes packaged with its own REST endpoint for retrieving which patterns the processor is packaged with.
  254. [source,console]
  255. --------------------------------------------------
  256. GET _ingest/processor/grok
  257. --------------------------------------------------
  258. The above request will return a response body containing a key-value representation of the built-in patterns dictionary.
  259. [source,js]
  260. --------------------------------------------------
  261. {
  262. "patterns" : {
  263. "BACULA_CAPACITY" : "%{INT}{1,3}(,%{INT}{3})*",
  264. "PATH" : "(?:%{UNIXPATH}|%{WINPATH})",
  265. ...
  266. }
  267. --------------------------------------------------
  268. // NOTCONSOLE
  269. By default, the API returns patterns in the order they are read from disk. This
  270. sort order preserves groupings of related patterns. For example, all patterns
  271. related to parsing Linux syslog lines stay grouped together.
  272. You can use the optional boolean `s` query parameter to sort returned patterns
  273. by key name instead.
  274. [source,console]
  275. --------------------------------------------------
  276. GET _ingest/processor/grok?s
  277. --------------------------------------------------
  278. The API returns the following response.
  279. [source,js]
  280. --------------------------------------------------
  281. {
  282. "patterns" : {
  283. "BACULA_CAPACITY" : "%{INT}{1,3}(,%{INT}{3})*",
  284. "BACULA_DEVICE" : "%{USER}",
  285. "BACULA_DEVICEPATH" : "%{UNIXPATH}",
  286. ...
  287. }
  288. --------------------------------------------------
  289. // NOTCONSOLE
  290. This can be useful to reference as the built-in patterns change across versions.
  291. [[grok-watchdog]]
  292. ==== Grok watchdog
  293. Grok expressions that take too long to execute are interrupted and
  294. the grok processor then fails with an exception. The grok
  295. processor has a watchdog thread that determines when evaluation of
  296. a grok expression takes too long and is controlled by the following
  297. settings:
  298. [[grok-watchdog-options]]
  299. .Grok watchdog settings
  300. [options="header"]
  301. |======
  302. | Name | Default | Description
  303. | `ingest.grok.watchdog.interval` | 1s | How often to check whether there are grok evaluations that take longer than the maximum allowed execution time.
  304. | `ingest.grok.watchdog.max_execution_time` | 1s | The maximum allowed execution of a grok expression evaluation.
  305. |======
  306. [[grok-debugging]]
  307. ==== Grok debugging
  308. It is advised to use the {kibana-ref}/xpack-grokdebugger.html[Grok Debugger] to debug grok patterns. From there you can test one or more
  309. patterns in the UI against sample data. Under the covers it uses the same engine as ingest node processor.
  310. Additionally, it is recommended to enable debug logging for Grok so that any additional messages may also be seen in the Elasticsearch
  311. server log.
  312. [source,js]
  313. --------------------------------------------------
  314. PUT _cluster/settings
  315. {
  316. "transient": {
  317. "logger.org.elasticsearch.ingest.common.GrokProcessor": "debug"
  318. }
  319. }
  320. --------------------------------------------------
  321. // NOTCONSOLE