grok.asciidoc 11 KB

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