common-log-format-example.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. [[common-log-format-example]]
  2. == Example: Parse logs in the Common Log Format
  3. ++++
  4. <titleabbrev>Example: Parse logs</titleabbrev>
  5. ++++
  6. In this example tutorial, you’ll use an <<ingest,ingest pipeline>> to parse
  7. server logs in the {wikipedia}/Common_Log_Format[Common Log Format] before
  8. indexing. Before starting, check the <<ingest-prerequisites,prerequisites>> for
  9. ingest pipelines.
  10. The logs you want to parse look similar to this:
  11. [source,log]
  12. ----
  13. 212.87.37.154 - - [30/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\"
  14. 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
  15. AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"
  16. ----
  17. // NOTCONSOLE
  18. These logs contain an IP address, timestamp, and user agent. You want to give
  19. these three items their own field in {es} for faster searches and
  20. visualizations. You also want to know where the request is coming from.
  21. . In {kib}, open the main menu and click **Stack Management** > **Ingest Node
  22. Pipelines**.
  23. +
  24. [role="screenshot"]
  25. image::images/ingest/ingest-pipeline-list.png[Kibana's Ingest Node Pipelines list view,align="center"]
  26. . Click **Create a pipeline**.
  27. . Provide a name and description for the pipeline.
  28. . Add a <<grok-processor,grok processor>> to parse the log message:
  29. .. Click **Add a processor** and select the **Grok** processor type.
  30. .. Set the field input to `message` and enter the following <<grok-basics,grok
  31. pattern>>:
  32. +
  33. [source,grok]
  34. ----
  35. %{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \[%{HTTPDATE:@timestamp}\] "%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}
  36. ----
  37. // NOTCONSOLE
  38. +
  39. .. Click **Add** to save the processor.
  40. . Add processors to map the date, IP, and user agent fields. Map the appropriate
  41. field to each processor type:
  42. +
  43. --
  44. * <<date-processor,**Date**>>: `@timestamp`
  45. * <<geoip-processor,**GeoIP**>>: `source.ip`
  46. * <<user-agent-processor,**User agent**>>: `user_agent`
  47. In the **Date** processor, specify the date format you want to use:
  48. `dd/MMM/yyyy:HH:mm:ss Z`.
  49. In the **GeoIP** processor, specify the target field as `source.geo`.
  50. Your form should look similar to this:
  51. [role="screenshot"]
  52. image::images/ingest/ingest-pipeline-processor.png[Processors for Ingest Node Pipelines,align="center"]
  53. The four processors will run sequentially: +
  54. Grok > Date > GeoIP > User agent +
  55. You can reorder processors using the arrow icons.
  56. Alternatively, you can click the **Import processors** link and define the
  57. processors as JSON:
  58. [source,js]
  59. ----
  60. {
  61. include::common-log-format-example.asciidoc[tag=common-log-pipeline]
  62. }
  63. ----
  64. // NOTCONSOLE
  65. ////
  66. [source,console]
  67. ----
  68. PUT /_ingest/pipeline/my-pipeline
  69. {
  70. // tag::common-log-pipeline[]
  71. "processors": [
  72. {
  73. "grok": {
  74. "field": "message",
  75. "patterns": ["%{IPORHOST:source.ip} %{USER:user.id} %{USER:user.name} \\[%{HTTPDATE:@timestamp}\\] \"%{WORD:http.request.method} %{DATA:url.original} HTTP/%{NUMBER:http.version}\" %{NUMBER:http.response.status_code:int} (?:-|%{NUMBER:http.response.body.bytes:int}) %{QS:http.request.referrer} %{QS:user_agent}"]
  76. }
  77. },
  78. {
  79. "date": {
  80. "field": "@timestamp",
  81. "formats": [ "dd/MMM/yyyy:HH:mm:ss Z" ]
  82. }
  83. },
  84. {
  85. "geoip": {
  86. "field": "source.ip",
  87. "target_field": "source.geo"
  88. }
  89. },
  90. {
  91. "user_agent": {
  92. "field": "user_agent"
  93. }
  94. }
  95. ]
  96. // end::common-log-pipeline[]
  97. }
  98. ----
  99. ////
  100. --
  101. . To test the pipeline, click **Add documents**.
  102. . In the **Documents** tab, provide a sample document for testing:
  103. +
  104. [source,js]
  105. ----
  106. [
  107. {
  108. "_source": {
  109. "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
  110. }
  111. }
  112. ]
  113. ----
  114. // NOTCONSOLE
  115. . Click **Run the pipeline** and verify the pipeline worked as expected.
  116. . If everything looks correct, close the panel, and then click **Create
  117. pipeline**.
  118. +
  119. You’re now ready to index the logs data to a <<data-streams,data stream>>.
  120. . Create an <<index-templates,index template>> with
  121. <<create-a-data-stream-template,data stream enabled>>.
  122. +
  123. [source,console]
  124. ----
  125. PUT /_index_template/my-data-stream-template
  126. {
  127. "index_patterns": [ "my-data-stream*" ],
  128. "data_stream": { },
  129. "priority": 500
  130. }
  131. ----
  132. // TEST[continued]
  133. . Index a document with the pipeline you created.
  134. +
  135. [source,console]
  136. ----
  137. POST /my-data-stream/_doc?pipeline=my-pipeline
  138. {
  139. "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\""
  140. }
  141. ----
  142. // TEST[s/my-pipeline/my-pipeline&refresh=wait_for/]
  143. // TEST[continued]
  144. . To verify, search the data stream to retrieve the document. The following
  145. search uses <<common-options-response-filtering,`filter_path`>> to return only
  146. the <<mapping-source-field,document source>>.
  147. +
  148. --
  149. [source,console]
  150. ----
  151. GET /my-data-stream/_search?filter_path=hits.hits._source
  152. ----
  153. // TEST[continued]
  154. The API returns:
  155. [source,console-result]
  156. ----
  157. {
  158. "hits": {
  159. "hits": [
  160. {
  161. "_source": {
  162. "@timestamp": "2099-05-05T16:21:15.000Z",
  163. "http": {
  164. "request": {
  165. "referrer": "\"-\"",
  166. "method": "GET"
  167. },
  168. "response": {
  169. "status_code": 200,
  170. "body": {
  171. "bytes": 3638
  172. }
  173. },
  174. "version": "1.1"
  175. },
  176. "source": {
  177. "ip": "212.87.37.154",
  178. "geo": {
  179. "continent_name": "Europe",
  180. "region_iso_code": "DE-BE",
  181. "city_name": "Berlin",
  182. "country_iso_code": "DE",
  183. "country_name": "Germany",
  184. "region_name": "Land Berlin",
  185. "location": {
  186. "lon": 13.4978,
  187. "lat": 52.411
  188. }
  189. }
  190. },
  191. "message": "212.87.37.154 - - [05/May/2099:16:21:15 +0000] \"GET /favicon.ico HTTP/1.1\" 200 3638 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"",
  192. "url": {
  193. "original": "/favicon.ico"
  194. },
  195. "user": {
  196. "name": "-",
  197. "id": "-"
  198. },
  199. "user_agent": {
  200. "original": "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\"",
  201. "os": {
  202. "name": "Mac OS X",
  203. "version": "10.11.6",
  204. "full": "Mac OS X 10.11.6"
  205. },
  206. "name": "Chrome",
  207. "device": {
  208. "name": "Mac"
  209. },
  210. "version": "52.0.2743.116"
  211. }
  212. }
  213. }
  214. ]
  215. }
  216. }
  217. ----
  218. --
  219. ////
  220. [source,console]
  221. ----
  222. DELETE /_data_stream/*
  223. DELETE /_index_template/*
  224. ----
  225. // TEST[continued]
  226. ////