grok.asciidoc 12 KB

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