{"id":1379,"date":"2023-03-19T10:54:47","date_gmt":"2023-03-19T02:54:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1379"},"modified":"2023-04-28T21:12:41","modified_gmt":"2023-04-28T13:12:41","slug":"logstash-grok-configuration-debugging","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/logstash-grok-configuration-debugging\/","title":{"rendered":"Logstash grok\u914d\u7f6e\u8c03\u8bd5"},"content":{"rendered":"<p>grok\u662f\u4e00\u79cd\u91c7\u7528\u7ec4\u5408\u591a\u4e2a\u9884\u5b9a\u4e49\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u6765\u5339\u914d\u5206\u5272\u6587\u672c\u5e76\u6620\u5c04\u5230\u5173\u952e\u5b57\u7684\u5de5\u5177\u3002\u901a\u5e38\u7528\u6765\u5bf9\u65e5\u5fd7\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002logstash\u7684filter\u6a21\u5757\u4e2dgrok\u63d2\u4ef6\u662f\u5176\u5b9e\u73b0\u4e4b\u4e00\u3002<\/p>\n<p>logstash\u5185\u7f6e\u7684grok\u5339\u914d\u89c4\u5219\u53ef\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/logstash-plugins\/logstash-patterns-core\/blob\/master\/patterns\/grok-patterns\">https:\/\/github.com\/logstash-plugins\/logstash-patterns-core\/blob\/master\/patterns\/grok-patterns<\/a> \u3002grok\u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\u5339\u914d\u5b57\u6bb5\u89c4\u5219\uff0c\u53ef\u4ee5\u7075\u6d3b\u6ee1\u8db3\u6269\u5c55\u7684\u9700\u6c42\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u65e5\u5fd7\u65b9\u5f0f<\/h2>\n<pre><code>input {\n    ...\n}\n\nfilter {\n    ...\n}\n\noutput {\n    if &quot;_grokparsefailure&quot; in [tags] {\n        file { path =&gt; &quot;\/data\/logs\/logstash\/grok_failures.txt&quot; }  #\u89e3\u6790\u5931\u8d25\u65e5\u5fd7\n    } else {\n        elasticsearch {\n            hosts =&gt; [&quot;192.168.165.239:9200&quot;]\n            index =&gt; &quot;%{type}&quot;\n        }\n        stdout {\n           codec =&gt; rubydebug  #\u63a7\u5236\u53f0\u8f93\u51fa\u65e5\u5fd7\n        }\n    }\n}<\/code><\/pre>\n<p>\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7\u524d\u53f0\u542f\u52a8\u67e5\u770b\u63a7\u5236\u53f0\u8f93\u51fa\u65e5\u5fd7\uff1a<\/p>\n<pre><code># bin\/logstash -f config_file\/log.conf<\/code><\/pre>\n<h2>Kibana Dev Tools<\/h2>\n<h3>Console<\/h3>\n<p>\u67e5\u8be2ES\u91c7\u96c6\u7684\u6570\u636e\u683c\u5f0f\u662f\u5426\u7b26\u5408grok\u5207\u5272\u9884\u671f<\/p>\n<pre><code>GET \/appblog\/_search\n{\n  &quot;query&quot;: {\n    &quot;match_all&quot;: {\n\n    }\n  },\n  &quot;sort&quot;: [{ &quot;@timestamp&quot;: { &quot;order&quot; : &quot;desc&quot;} }]\n}<\/code><\/pre>\n<h3>Grok Debugger<\/h3>\n<p>Sample Data \u8f93\u5165\u65e5\u5fd7\u6837\u4f8b\uff0c\u5982<\/p>\n<pre><code>2019-05-25 15:23:32.009 [cn-appblog-provider-channel-gateway-alipay][ INFO ] [65117] [nio-8851-exec-8] [47a999cec484e6b5] [0ea76f03cdf92c57] [true] --- [cn.appblog.provider.channel.gateway.alipay.helper.XStreamHelper] [parseAlipayCreateReturn] [39] : This is log content<\/code><\/pre>\n<p>Grok Pattern \u8f93\u5165\u5339\u914d\u89c4\u5219\uff0c\u5982<\/p>\n<pre><code>%{TIME_STAMP_A:logtime}\\s+\\[%{APP_NAME:appname}\\]\\[\\s+%{LOG_LVL:loglvl}\\s+\\]\\s+\\[%{PROCESS_ID:pid}\\]\\s+\\[%{PROCESS_NAME:pname}\\]\\s+\\[%{TRACE_ID:traceid}\\]\\s+\\[%{SPAN_ID:spanid}\\]\\s+\\[%{SPAN_EXPORTABLE}\\]\\s+---\\s+\\[%{CLASS_PATH:classpath}\\]\\s+\\[%{METHOD_NAME:methodname}\\]\\s+\\[%{CODE_LINE:codeline}\\]\\s+:\\s+%{CONTENT:content}<\/code><\/pre>\n<p>Custom Patterns \u8f93\u5165\u81ea\u5b9a\u4e49\u89c4\u5219\uff0c\u5982<\/p>\n<pre><code>TIME_STAMP_A \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}.\\d{3}\nTIME_STAMP_T \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z\nTIME_STAMP_P \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\nTIME_STAMP_S \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2},\\d{3}\nHOST_NAME_PATTERN [a-zA-Z0-9._-]+\nAPP_NAME [a-zA-Z0-9._-]+\nLOG_LVL [a-zA-Z0-9._-]+\nCORRELATION_ID [0-9a-f-]{36}\nCIP ((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))\nID_PATTERN [0-9a-f\\-]{36}\nRPC_ID_PATTERN [0-9\\.]+\nAPP_OR_METHOD [\/a-zA-Z0-9._-]+\nTRACE_ID [0-9a-f]*\nSPAN_ID [0-9a-f]*\nPROCESS_ID \\d{0,5}\nPROCESS_NAME [a-zA-Z0-9._-]+\nSPAN_EXPORTABLE [a-z]{0,5}\nCLASS_PATH [a-zA-Z0-9._]+\nMETHOD_NAME [a-zA-Z0-9_]+\nCODE_LINE \\d{1,5}\nCONTENT [\\s\\S]*$<\/code><\/pre>\n<p>\u70b9\u51fb<code>Simulate<\/code>\uff0c\u5f97\u5230<code>Structured Data<\/code><\/p>\n<pre><code class=\"language-json\">{\n  &quot;traceid&quot;: &quot;47a999cec484e6b5&quot;,\n  &quot;classpath&quot;: &quot;cn.appblog.provider.channel.gateway.alipay.helper.XStreamHelper&quot;,\n  &quot;loglvl&quot;: &quot;INFO&quot;,\n  &quot;pname&quot;: &quot;nio-8851-exec-8&quot;,\n  &quot;pid&quot;: &quot;65117&quot;,\n  &quot;content&quot;: &quot;This is log content&quot;,\n  &quot;codeline&quot;: &quot;39&quot;,\n  &quot;spanid&quot;: &quot;0ea76f03cdf92c57&quot;,\n  &quot;appname&quot;: &quot;cn-appblog-provider-channel-gateway-alipay&quot;,\n  &quot;logtime&quot;: &quot;2019-05-25 15:23:32.009&quot;,\n  &quot;methodname&quot;: &quot;parseAlipayCreateReturn&quot;\n}<\/code><\/pre>\n<h2>\u914d\u7f6e\u793a\u4f8b<\/h2>\n<pre><code>2019-05-23 11:50:36.022 [cn-appblog-provider-channel-core][ INFO ] [21992] [nio-8888-exec-1] [143da285c068e5e1] [cb964a4c7b09ee0e] [true] --- [cn.appblog.provider.channel.core.helper.ChannelInfoHelper] [checkChannelInfo] [35] : ChannelPayRequest.checkChannelInfo [MerchantId: 142019050800009001, TransSerialNo: 122019052300016001, ChnlCode: alipay_offline_payment]<\/code><\/pre>\n<pre><code>input {\n    kafka {\n        bootstrap_servers =&gt; &quot;192.168.1.10:9092&quot;\n        topics =&gt; &quot;logstash&quot;\n        group_id =&gt; &quot;logstash&quot;\n        consumer_threads =&gt; 5\n        decorate_events =&gt; true\n        codec =&gt; json\n        type =&gt; &quot;thaipay&quot;\n        #auto_offset_reset =&gt; &quot;smallest&quot;\n        #reset_beginning =&gt; true\n   }\n}\n\nfilter {\n    if [type] == &quot;thaipay&quot; {\n        if [message] =~ &quot;^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}.\\d{3}\\s+\\[[a-zA-Z0-9._-]+\\]\\s*\\[\\s*[a-zA-Z0-9._-]+\\s*\\][\\s\\S]*$&quot; {\n            grok {\n                patterns_dir =&gt; &quot;\/data\/server\/logstash\/config_file\/patterns&quot;\n                #add_field =&gt; {&quot;logmatch&quot; =&gt; &quot;100001&quot;}\n                #match =&gt; { &quot;message&quot; =&gt; &quot;%{TIME_STAMP_A:logtime}&quot; }\n                #match =&gt; { &quot;message&quot; =&gt; &quot;%{TIME_STAMP_A:logtime}\\s+\\[%{APP_NAME:appname}\\]\\s+\\[%{LOG_LVL:loglvl}\\]&quot; }\n                #match =&gt; { &quot;message&quot; =&gt; &quot;%{TIME_STAMP_A:logtime}\\s+\\[%{APP_NAME:appname}\\]\\[\\s+%{LOG_LVL:loglvl}\\s+\\]\\s+\\[%{PROCESS_ID:pid}\\]\\s+\\[%{PROCESS_NAME:pname}\\]\\s+\\[%{TRACE_ID:traceid}\\]\\s+\\[%{SPAN_ID:spanid}\\]\\s+\\[%{SPAN_EXPORTABLE}\\]\\s+---\\s+\\[%{CLASS_PATH:classpath}\\]\\s+\\[%{METHOD_NAME:methodname}\\]\\s+\\[%{CODE_LINE:codeline}\\]&quot; }\n                match =&gt; { &quot;message&quot; =&gt; &quot;%{TIME_STAMP_A:logtime}\\s+\\[\\s*%{APP_NAME:appname}\\s*\\]\\[\\s*%{LOG_LVL:loglvl}\\s*\\]\\s+\\[\\s*%{PROCESS_ID:pid}\\s*\\]\\s+\\[\\s*%{PROCESS_NAME:pname}\\s*\\]\\s+\\[\\s*%{TRACE_ID:traceid}\\s*\\]\\s+\\[\\s*%{SPAN_ID:spanid}\\s*\\]\\s+\\[\\s*%{SPAN_EXPORTABLE}\\s*\\]\\s+---\\s+\\[\\s*%{CLASS_PATH:classpath}\\s*\\]\\s+\\[\\s*%{METHOD_NAME:methodname}\\s*\\]\\s+\\[\\s*%{CODE_LINE:codeline}\\s*\\]\\s+:\\s+%{CONTENT:content}&quot; }\n            }\n            #date {\n            #    match =&gt; [&quot;logtime&quot;, &quot;yyyy-MM-dd HH:mm:ss.SSS&quot;]\n            #    target =&gt; &quot;messagetime&quot;\n                #locale =&gt; &quot;en&quot;\n                #timezone =&gt; &quot;+00:00&quot;\n                #remove_field =&gt; [&quot;logtime&quot;]\n            #}\n        }\n    }\n}\n\noutput {\n    if &quot;_grokparsefailure&quot; in [tags] {\n        file { path =&gt; &quot;\/data\/logs\/logstash\/grok_failures.txt&quot; }\n    } else {\n        elasticsearch {\n            hosts =&gt; [&quot;192.168.1.10:9200&quot;]\n            index =&gt; &quot;%{type}&quot;\n        }\n        stdout {\n           codec =&gt; rubydebug\n        }\n    }\n}<\/code><\/pre>\n<pre><code>TIME_STAMP_A \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}.\\d{3}\nTIME_STAMP_T \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z\nTIME_STAMP_P \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\nTIME_STAMP_S \\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2},\\d{3}\nHOST_NAME_PATTERN [a-zA-Z0-9._-]+\nAPP_NAME [a-zA-Z0-9._-]+\nLOG_LVL [a-zA-Z0-9._-]+\nCORRELATION_ID [0-9a-f-]{36}\nCIP ((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))\nID_PATTERN [0-9a-f\\-]{36}\nRPC_ID_PATTERN [0-9\\.]+\nAPP_OR_METHOD [\/a-zA-Z0-9._-]+\nTRACE_ID [0-9a-f]*\nSPAN_ID [0-9a-f]*\nPROCESS_ID \\d{3,5}\nPROCESS_NAME [a-zA-Z0-9._-]+\nSPAN_EXPORTABLE [a-z]{0,5}\nCLASS_PATH [a-zA-Z0-9._]+\nMETHOD_NAME [a-zA-Z0-9_]+\nCODE_LINE \\d{1,5}\nCONTENT [\\s\\S]*$<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>grok\u662f\u4e00\u79cd\u91c7\u7528\u7ec4\u5408\u591a\u4e2a\u9884\u5b9a\u4e49\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u6765\u5339\u914d\u5206\u5272\u6587\u672c\u5e76\u6620\u5c04\u5230\u5173\u952e\u5b57\u7684\u5de5\u5177\u3002\u901a\u5e38\u7528\u6765\u5bf9\u65e5\u5fd7\u6570\u636e\u8fdb\u884c\u9884\u5904 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[345],"tags":[347,343],"class_list":["post-1379","post","type-post","status-publish","format-standard","hentry","category-elk","tag-grok","tag-logstash"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1379"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}