{"id":9200,"date":"2022-07-25T15:57:14","date_gmt":"2022-07-25T20:57:14","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=9200"},"modified":"2022-07-25T16:09:46","modified_gmt":"2022-07-25T21:09:46","slug":"logstash-filtering-data-with-ruby","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=9200","title":{"rendered":"Logstash &#8211; Filtering data with Ruby"},"content":{"rendered":"\n<p>I&#8217;ve been working on forking log data into two different indices based on an element contained within the record &#8212; if the filename being sent includes the string &#8220;BASELINE&#8221;, then the data goes into the baseline index, otherwise it goes into the scan index. The data being ingested has the file name in &#8220;@fields.myfilename&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"402\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook-1024x402.png\" alt=\"\" class=\"wp-image-9201\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook-1024x402.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook-300x118.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook-768x301.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook-750x294.png 750w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2022\/07\/Elk-Logstash-HowFieldsLook.png 1047w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>It took a while to figure out how to get the value from the current data &#8212; event.get(&#8216;[@fields][myfilename]&#8217;) to get the @fields.myfilename value. <\/p>\n\n\n\n<p>The following logstash config accepts JSON inputs, parses the underscore-delimited filename into fields, replaces the dashes with underscores as KDL doesn&#8217;t handle dashes and wildcards in searches, and adds a flag to any record that <em>should<\/em> be a baseline. In the output section, that flag is then used to publish data to the appropriate index based on the baseline flag value. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ninput {\n  tcp {\n    port =&gt; 5055\n    codec =&gt; json\n  }\n}\nfilter {\n        # Sample file name: scan_ABCDMIIWO0Y_1-A-5-L2_BASELINE.json\n        ruby {  code =&gt; &quot;\n                        strfilename = event.get(&#039;&#x5B;@fields]&#x5B;myfilename]&#039;)\n                        arrayfilebreakout = strfilename.split(&#039;_&#039;)\n                        event.set(&#039;hostname&#039;, arrayfilebreakout&#x5B;1])\n                        event.set(&#039;direction&#039;,arrayfilebreakout&#x5B;2])\n                        event.set(&#039;parseablehost&#039;, strfilename.gsub(&#039;-&#039;,&#039;_&#039;))\n\n                        if strfilename.downcase =~ \/baseline\/\n                                event.set(&#039;baseline&#039;, 1)\n                        end&quot; }\n}\noutput {\n        if &#x5B;baseline] == 1 {\n                elasticsearch {\n                        action =&gt; &quot;index&quot;\n                        hosts =&gt; &#x5B;&quot;https:\/\/elastic.example.com:9200&quot;]\n                        ssl =&gt; true\n                        cacert =&gt; &#x5B;&quot;\/path\/to\/logstash\/config\/certs\/My_Chain.pem&quot;]\n                        ssl_certificate_verification =&gt; true\n                        # Credentials go here\n                        index =&gt; &quot;ljr-baselines&quot;\n                }\n        }\n        else{\n              elasticsearch {\n                        action =&gt; &quot;index&quot;\n                        hosts =&gt; &#x5B;&quot;https:\/\/elastic.example.com:9200&quot;]\n                        ssl =&gt; true\n                        cacert =&gt; &#x5B;&quot;\/path\/to\/logstash\/config\/certs\/My_Chain.pem&quot;]\n                        ssl_certificate_verification =&gt; true\n                        # Credentials go here\n                        index =&gt; &quot;ljr-scans-%{+YYYY.MM.dd}&quot;\n                }\n        }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on forking log data into two different indices based on an element contained within the record &#8212; if the filename being sent includes the string &#8220;BASELINE&#8221;, then the data goes into the baseline index, otherwise it goes into the scan index. The data being ingested has the file name in &#8220;@fields.myfilename&#8221; It &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1588],"tags":[1590,1589,1643,1681],"class_list":["post-9200","post","type-post","status-publish","format-standard","hentry","category-elk","tag-elasticsearch","tag-elk","tag-logstash","tag-ruby"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/9200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9200"}],"version-history":[{"count":2,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/9200\/revisions"}],"predecessor-version":[{"id":9205,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/9200\/revisions\/9205"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}