{"id":582,"date":"2020-09-03T16:56:21","date_gmt":"2020-09-03T08:56:21","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=582"},"modified":"2021-08-12T15:39:55","modified_gmt":"2021-08-12T07:39:55","slug":"logstash6-8-12%e5%8a%a8%e6%80%81%e7%94%9f%e6%88%90elasticsearch%e7%9a%84index%e7%9a%84%e6%ad%a3%e7%a1%ae%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=582","title":{"rendered":"logstash6.8.12\u52a8\u6001\u751f\u6210elasticsearch\u7684index\u7684\u6b63\u786e\u65b9\u6cd5"},"content":{"rendered":"<p>\u7f51\u4e0a\u6709\u5f88\u591a\u7684\u3010\u5047\u3011logstash\u52a8\u6001\u751f\u6210index\u7684\u6587\u7ae0\uff0c\u770b\u4e86\u5f88\u591a\uff0c\u6839\u672c\u4e0d\u7b26\u5408\u6211\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u6211\u51b3\u5b9a\u6765\u4e00\u7bc7\u5e72\u8d27\uff0c\u771f\u6b63\u7684\u89e3\u51b3\u95ee\u9898\u3002\u4eba\u72e0\u8bdd\u4e0d\u591a\uff0c\u4ee3\u7801\u76f4\u63a5\u4e0a\u3002\u6211\u662f\u4f7f\u7528\u5b98\u65b9\u63d0\u4f9b\u7684helm\u5305\u8fdb\u884c\u5b89\u88c5\u7684\uff0c\u5982\u679c\u60f3\u8981\u5b8c\u6574\u90e8\u7f72\u6587\u4ef6\uff0c\u53ef\u4ee5\u6765\u8054\u7cfb\u6211\u3002QQ:357244849<\/p>\n<hr \/>\n<h2>logstash\u7684\u5173\u952e\u914d\u7f6e.\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u7684\u76ee\u7684\u662f\u8fd9\u6837\u7684\uff1a<\/h2>\n<p>1.\u4f7f\u7528udp\u7aef\u53e39998 \u63a5\u53d7\u6765\u81ea\u6570\u636e\u6e90\u7684\u4fe1\u606f\uff1b<br \/>\n2.\u901a\u8fc7filter\u63d2\u4ef6\uff0c<strong><span style=\"color: #ff0000;\">\u5148\u628amessage\u8f6c\u6210json\u683c\u5f0f\uff0c\u8fd9\u4e00\u6b65\u5f88\u91cd\u8981\uff0c\u5f88\u591a\u6587\u7ae0\u91cc\u90fd\u4e0d\u63d0\u8fd9\u4e2a\uff1b<\/span><\/strong><br \/>\n3.\u5224\u65ad\u63a5\u53d7\u5230\u7684\u6570\u636e\u91cc\uff0c\u662f\u5426\u5305\u542bapp_name\u8fd9\u4e2akey\uff0c\u5982\u679c\u5305\u542b\uff0c\u5c31\u628aapp_name\u91cc\u7684\u503c\u63d0\u51fa\u6765\uff0c\u548c\u5f53\u524d\u65e5\u671f\u7ec4\u5408\u5230\u4e00\u8d77\uff0c\u4f5c\u4e3a\u4f20\u5165elasticsearch\u7684index\u540d\u5b57\u3002\u5982\u679c\u4e0d\u5305\u542b\uff0c\u5c31\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32non_index_log\u4f5c\u4e3aelasticsearch\u7684index\u540d\u5b57\uff1b<br \/>\n4.\u628a\u65e5\u5fd7\u8f6c\u53d1\u5230elasticsearch\u91cc\uff0c\u540c\u65f6\u6253\u5370\u5728\u63a7\u5236\u53f0\u91cc<\/p>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">    input {\n      udp {\n        port =&gt; 9998\n      }\n    }\n    filter {\n      json {\n        source =&gt; \"message\"\n      }\n      if [app_name] {\n        mutate {\n          add_field =&gt; {\n            \"index_name\" =&gt; \"%{app_name}-%{+YYYY.MM.dd}\"\n          }\n        }\n      } else {\n        mutate {\n          add_field =&gt; {\n            \"index_name\" =&gt;  \"non_index_log\"\n          }\n        }\n      }\n    }\n    output {\n        elasticsearch {\n           hosts =&gt; [\"http:\/\/elasticsearch-master:9200\"]\n           index =&gt; \"logstash-roc-%{index_name}\"\n           }\n        stdout { codec =&gt; rubydebug }\n    }<\/code><\/pre>\n<hr \/>\n<h2>\u4e3a\u4e86\u6d4b\u8bd5\u8fd9\u6837\u914d\u7f6e\u4e4b\u540e\u7684logstash\u6548\u679c\u5982\u4f55\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2apython\u4ee3\u7801<\/h2>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#  All rights reserved.\n#\n#  filename : example2.py\n#  description :\n#\n#  created by zhenwei.Li at 2020\/9\/2 15:52\nimport logging\nimport logstash\nimport sys\nhost = '192.168.0.12'\ntest_logger = logging.getLogger('python-logstash-logger')\ntest_logger.setLevel(logging.INFO)\ntest_logger.addHandler(logstash.LogstashHandler(host, 9998, version=1))\ntest_logger.error('python-logstash: test logstash error message.')\n# add extra field to logstash message\nextra = {\n    'app_name': 'temp-lizhenwei-python2',\n    'test_string': 'python version: ' + repr(sys.version_info),\n    'test_boolean': True,\n    'test_dict': {'a': 1, 'b': 'c'},\n    'test_float': 1.23,\n    'test_integer': 123,\n    'type': 'temp-lishuai-python3'\n}\ntest_logger.info('python-logstash: test extra fields', extra=extra)<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u53bb\u68c0\u67e5logstash\u7684\u63a7\u5236\u53f0\u662f\u5426\u4ea7\u751f\u4e86index_name\uff0c\u7406\u8bba\u4e0a\u6765\u8bf4\uff0c\u4f1a\u4ea7\u751f\u4e00\u4e2a<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">\"index_name\" =&gt; \"temp-lizhenwei-python2-2020.09.03\"\u548c\"index_name\" =&gt; \"non_index_log\"<\/code>\u5982\u4e0b\u56fe\u6240\u793a<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-583 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-36.png\" alt=\"\" width=\"594\" height=\"228\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-584 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-37.png\" alt=\"\" width=\"834\" height=\"363\" \/><\/p>\n<hr \/>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53bbkibana\u91cc\u9762\u68c0\u67e5\u4e00\u4e0belasticsearch\u7684index,\u786e\u5b9e\u4ea7\u751f\u4e86\u6211\u4eec\u9700\u8981\u7684\u4e24\u4e2aindex<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-585 size-large\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-38-1024x296.png\" alt=\"\" width=\"900\" height=\"260\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u4e0a\u6709\u5f88\u591a\u7684\u3010\u5047\u3011logstash\u52a8\u6001\u751f\u6210index\u7684\u6587\u7ae0\uff0c\u770b\u4e86\u5f88\u591a\uff0c\u6839\u672c\u4e0d\u7b26\u5408\u6211\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u6211\u51b3\u5b9a\u6765\u4e00\u7bc7\u5e72\u8d27 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-582","post","type-post","status-publish","format-standard","hentry","category-elasticsearch"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=582"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions"}],"predecessor-version":[{"id":877,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions\/877"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}