{"id":493,"date":"2026-03-09T10:55:07","date_gmt":"2026-03-09T02:55:07","guid":{"rendered":"https:\/\/www.liaoxinghui.com\/?p=493"},"modified":"2026-03-09T10:55:07","modified_gmt":"2026-03-09T02:55:07","slug":"automating-server-log-collection-analysis-python-elk-stack","status":"publish","type":"post","link":"https:\/\/www.liaoxinghui.com\/?p=493","title":{"rendered":"\u4f7f\u7528Python\u811a\u672c\u5bf9\u65e5\u5fd7\u8fdb\u884c\u6536\u96c6\u548c\u5206\u6790\u5e76\u96c6\u6210ELK Stack\u4ece\u96f6\u4e0a\u7ebf"},"content":{"rendered":"<h2>\u4e1a\u52a1\u573a\u666f<\/h2>\n<p>\u6211\u4eec\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u6709\u8d85\u8fc710\u53f0\u670d\u52a1\u5668\uff0c\u65e5\u5fd7\u5206\u6563\u5728\u5404\u8282\u70b9\uff0c\u5bfc\u81f4\u6545\u969c\u6392\u67e5\u6548\u7387\u4f4e\u4e0b\u3002\u4e1a\u52a1\u9700\u6c42\u5305\u62ec\u96c6\u4e2d\u6536\u96c6\u65e5\u5fd7\u5230ELK Stack\uff08Elasticsearch\u3001Logstash\u3001Kibana\uff09\u8fdb\u884c\u5b9e\u65f6\u5206\u6790\u548c\u544a\u8b66\uff0c\u4ee5\u652f\u6301\u8fd0\u7ef4\u56e2\u961f\u5feb\u901f\u54cd\u5e94\u6027\u80fd\u95ee\u9898\u3002\u9879\u76ee\u89c4\u6a21\u7ea6\u675f\u4e3a\u65e5\u5747\u65e5\u5fd7\u91cf\u7ea610GB\uff0c\u56e2\u961f\u534f\u4f5c\u8fb9\u754c\u6d89\u53ca\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u811a\u672c\u3001\u8fd0\u7ef4\u5de5\u7a0b\u5e08\u90e8\u7f72ELK\u7ec4\u4ef6\uff0c\u91cc\u7a0b\u7891\u8bbe\u5b9a\u4e3a\u4e24\u5468\u5185\u5b8c\u6210\u539f\u578b\u90e8\u7f72\u548c\u4e00\u4e2a\u6708\u5185\u5168\u9762\u4e0a\u7ebf\uff0c\u9a8c\u6536\u53e3\u5f84\u4ee5\u65e5\u5fd7\u6536\u96c6\u5ef6\u8fdf\u4f4e\u4e8e1\u79d2\u548c\u544a\u8b66\u51c6\u786e\u7387\u8fbe\u523095%\u4e3a\u51c6\u3002<\/p>\n<h2>\u5019\u9009\u65b9\u6848\u5bf9\u6bd4<\/h2>\n<p>\u56f4\u7ed5\u81ea\u52a8\u5316\u65e5\u5fd7\u7ba1\u7406\u76ee\u6807\uff0c\u6211\u4eec\u5bf9\u6bd4\u4e09\u79cd\u5019\u9009\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u65b9\u6848A\uff1a\u7eafPython\u811a\u672c<\/strong> \u2013 \u4f7f\u7528Python\u76f4\u63a5\u8bfb\u53d6\u670d\u52a1\u5668\u65e5\u5fd7\uff0c\u901a\u8fc7socket\u6216HTTP\u53d1\u9001\u5230\u4e2d\u592e\u5b58\u50a8\u5982\u6587\u4ef6\u7cfb\u7edf\u6216\u6570\u636e\u5e93\u3002<\/li>\n<li><strong>\u65b9\u6848B\uff1aELK Stack\u96c6\u6210Python\u811a\u672c<\/strong> \u2013 Python\u811a\u672c\u9884\u5904\u7406\u65e5\u5fd7\uff0c\u7136\u540e\u901a\u8fc7Filebeat\u6216Logstash\u63a5\u5165Elasticsearch\uff0c\u6700\u7ec8\u7531Kibana\u53ef\u89c6\u5316\u3002<\/li>\n<li><strong>\u65b9\u6848C\uff1a\u5546\u4e1aSaaS\u65e5\u5fd7\u670d\u52a1<\/strong> \u2013 \u5982Datadog\u6216Splunk Cloud\uff0c\u63d0\u4f9b\u6258\u7ba1\u670d\u52a1\uff0c\u65e0\u9700\u81ea\u5efa\u57fa\u7840\u8bbe\u65bd\u3002<\/li>\n<\/ul>\n<p>\u8bc4\u4f30\u7ef4\u5ea6\u5305\u62ec\u6210\u672c\uff08\u521d\u59cb\u6295\u5165\u548c\u8fd0\u7ef4\u5f00\u9500\uff09\u3001\u90e8\u7f72\u590d\u6742\u5ea6\uff08\u5b66\u4e60\u66f2\u7ebf\u548c\u914d\u7f6e\u65f6\u95f4\uff09\u3001\u53ef\u6269\u5c55\u6027\uff08\u652f\u6301\u670d\u52a1\u5668\u6570\u91cf\u589e\u957f\uff09\u3001\u548c\u7ef4\u62a4\u6210\u672c\uff08\u65e5\u5e38\u76d1\u63a7\u548c\u6545\u969c\u5904\u7406\uff09\u3002\u6700\u7ec8\u51b3\u7b56\u7406\u7531\uff1a\u65b9\u6848B\u80dc\u51fa\uff0c\u56e0\u4e3a\u5b83\u5e73\u8861\u4e86\u6210\u672c\uff08\u5f00\u6e90\u514d\u8d39\uff0c\u4f46\u9700\u786c\u4ef6\u6295\u5165\u7ea65000\u5143\u4eba\u6c11\u5e01\u6bcf\u6708\u7528\u4e8e\u5b58\u50a8\uff09\u548c\u53ef\u63a7\u6027\uff0c\u9002\u5408\u4e2d\u578b\u56e2\u961f\u81ea\u5efaELK\u6808\uff0c\u907f\u514dSaaS\u7684\u9ad8\u8ba2\u9605\u8d39\u7528\u548c\u7eafPython\u811a\u672c\u7684\u53ef\u6269\u5c55\u6027\u74f6\u9888\u3002\u8fc1\u79fb\u7b56\u7565\uff1a\u4ece\u65e7\u6709\u811a\u672c\u7cfb\u7edf\u9010\u6b65\u5207\u6362\uff0c\u5148\u5728\u975e\u5173\u952e\u670d\u52a1\u5668\u90e8\u7f72ELK\uff0c\u9a8c\u8bc1\u540e\u518d\u5168\u91cf\u8fc1\u79fb\u3002<\/p>\n<h2>\u5b9e\u73b0\u6b65\u9aa4<\/h2>\n<p>\u4efb\u52a1\u7c7b\u578b\u4e3a\u81ea\u52a8\u5316\u811a\u672c\uff0c\u7528\u4e8e\u65e5\u5fd7\u6536\u96c6\u548c\u6e05\u6d17\u3002\u4ee5\u4e0b\u662f\u5173\u952e\u6b65\u9aa4\uff1a<\/p>\n<h3>\u6b65\u9aa41\uff1a\u5b89\u88c5\u548c\u914d\u7f6eELK Stack<\/h3>\n<p>\u5047\u8bbe\u4f7f\u7528Ubuntu 20.04\u7cfb\u7edf\uff0c\u4ece\u96f6\u90e8\u7f72\u3002\u5b89\u88c5Elasticsearch\u3001Logstash\u548cKibana\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\"># \u5b89\u88c5Elasticsearch\nwget -qO - https:\/\/artifacts.elastic.co\/GPG-KEY-elasticsearch | sudo apt-key add -\nsudo apt-get install apt-transport-https\necho &quot;deb https:\/\/artifacts.elastic.co\/packages\/7.x\/apt stable main&quot; | sudo tee -a \/etc\/apt\/sources.list.d\/elastic-7.x.list\nsudo apt-get update &amp;&amp; sudo apt-get install elasticsearch\nsudo systemctl start elasticsearch\n\n# \u5b89\u88c5Logstash\u548cKibana\nsudo apt-get install logstash kibana\nsudo systemctl start logstash kibana<\/code><\/pre>\n<h3>\u6b65\u9aa42\uff1a\u7f16\u5199Python\u65e5\u5fd7\u6536\u96c6\u811a\u672c<\/h3>\n<p>\u811a\u672c\u4ece\u670d\u52a1\u5668\u65e5\u5fd7\u6587\u4ef6\u8bfb\u53d6\u3001\u6e05\u6d17\u5e76\u53d1\u9001\u5230Logstash\u3002\u53c2\u6570\u8bf4\u660e\uff1alog_path\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u8def\u5f84\uff0c\u4f8b\u5982\u201c\/var\/log\/app.log\u201d\uff1bbatch_size\u63a7\u5236\u6bcf\u6b21\u53d1\u9001\u7684\u65e5\u5fd7\u6761\u6570\uff0c\u9ed8\u8ba4100\u6761\u4ee5\u51cf\u5c11\u7f51\u7edc\u5f00\u9500\u3002<\/p>\n<pre><code class=\"lang-python language-python python\">import json\nimport time\nimport socket\n\ndef collect_logs(log_path, batch_size=100):\n    &quot;&quot;&quot;\n    \u81ea\u52a8\u5316\u811a\u672c\uff1a\u6536\u96c6\u548c\u6e05\u6d17\u65e5\u5fd7\uff0c\u53d1\u9001\u5230Logstash\u3002\n    \u8c03\u7528\u65b9\u5f0f\uff1apython collect_logs.py --log_path \/var\/log\/app.log\n    &quot;&quot;&quot;\n    logstash_host = &#039;localhost&#039;\n    logstash_port = 5044\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock.connect((logstash_host, logstash_port))\n\n    with open(log_path, &#039;r&#039;) as f:\n        logs = []\n        for line in f:\n            # \u6e05\u6d17\u65e5\u5fd7\uff1a\u79fb\u9664\u654f\u611f\u4fe1\u606f\u5982IP\u5730\u5740\uff0c\u5047\u8bbe\u7528\u6b63\u5219\u66ff\u6362\n            cleaned_line = line.replace(&#039;192.168.1.1&#039;, &#039;[REDACTED]&#039;)\n            logs.append({&quot;message&quot;: cleaned_line.strip(), &quot;timestamp&quot;: time.time()})\n            if len(logs) &gt;= batch_size:\n                send_to_logstash(sock, logs)\n                logs = []\n        if logs:\n            send_to_logstash(sock, logs)\n    sock.close()\n\ndef send_to_logstash(sock, logs):\n    for log in logs:\n        sock.sendall(json.dumps(log).encode(&#039;utf-8&#039;) + b&#039;\\n&#039;)\n\nif __name__ == &quot;__main__&quot;:\n    import argparse\n    parser = argparse.ArgumentParser()\n    parser.add_argument(&quot;--log_path&quot;, type=str, required=True)\n    parser.add_argument(&quot;--batch_size&quot;, type=int, default=100)\n    args = parser.parse_args()\n    collect_logs(args.log_path, args.batch_size)<\/code><\/pre>\n<h3>\u6b65\u9aa43\uff1a\u914d\u7f6eFilebeat\u548c\u5b9a\u65f6\u4efb\u52a1<\/h3>\n<p>Filebeat\u914d\u7f6e\u6587\u4ef6\u793a\u4f8b\uff08filebeat.yml\uff09\uff0c\u6307\u5b9a\u8f93\u5165\u65e5\u5fd7\u8def\u5f84\u548c\u8f93\u51fa\u5230Logstash\uff1a<\/p>\n<pre><code class=\"lang-yaml language-yaml yaml\">filebeat.inputs:\n- type: log\n  paths:\n    - \/var\/log\/*.log\n  fields:\n    app: myapp\n\noutput.logstash:\n  hosts: [&quot;localhost:5044&quot;]<\/code><\/pre>\n<p>\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\u4f7f\u7528cron\uff0c\u6bcf5\u5206\u949f\u8fd0\u884cPython\u811a\u672c\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\"># \u7f16\u8f91crontab\ncrontab -e\n# \u6dfb\u52a0\u884c\uff1a*\/5 * * * * \/usr\/bin\/python3 \/path\/to\/collect_logs.py --log_path \/var\/log\/app.log<\/code><\/pre>\n<h2>\u6307\u6807\u8bf4\u660e<\/h2>\n<p>\u6307\u6807\u4e0e\u81ea\u52a8\u5316\u811a\u672c\u4efb\u52a1\u7c7b\u578b\u5339\u914d\uff1a<\/p>\n<ul>\n<li><strong>\u65e5\u5fd7\u541e\u5410\u91cf<\/strong>\uff1a\u6bcf\u79d2\u5904\u7406\u7684\u65e5\u5fd7\u6761\u6570\uff0c\u76ee\u6807\u503c\u4e3a500\u6761\/\u79d2\u4ee5\u786e\u4fdd\u5b9e\u65f6\u6027\u3002<\/li>\n<li><strong>\u6536\u96c6\u5ef6\u8fdf<\/strong>\uff1a\u4ece\u65e5\u5fd7\u4ea7\u751f\u5230\u8fdb\u5165Elasticsearch\u7684\u65f6\u95f4\uff0c\u9608\u503c\u8bbe\u4e3a\u5c0f\u4e8e1\u79d2\u3002<\/li>\n<li><strong>\u5b58\u50a8\u4f7f\u7528\u7387<\/strong>\uff1a\u57fa\u4e8e\u65e5\u574710GB\u65e5\u5fd7\uff0c\u76d1\u63a7Elasticsearch\u7d22\u5f15\u5927\u5c0f\uff0c\u907f\u514d\u8d85\u8fc7\u78c1\u76d880%\u3002\n\u6570\u636e\u8bf4\u660e\uff1a\u65e5\u5fd7\u683c\u5f0f\u4e3aJSON\uff0c\u6bcf\u6761\u7ea61KB\uff1b\u8c03\u7528\u65b9\u5f0f\u901a\u8fc7Python\u811a\u672c\u53c2\u6570\u548ccron\u5b9a\u65f6\u89e6\u53d1\uff1b\u53c2\u6570\u8bf4\u660e\u5305\u62eclog_path\uff08\u5fc5\u9700\uff09\u3001batch_size\uff08\u53ef\u9009\uff0c\u9ed8\u8ba4100\uff09\u3002<\/li>\n<\/ul>\n<h2>\u4e0a\u7ebf\u540e\u8bc4\u4f30<\/h2>\n<p>\u4e0a\u7ebf\u540e\uff0c\u6211\u4eec\u8bc4\u4f30ELK Stack\u96c6\u6210\u6548\u679c\uff1a\u65e5\u5fd7\u6536\u96c6\u5b8c\u6574\u6027\u8fbe\u523098%\uff0c\u544a\u8b66\u51c6\u786e\u7387\u63d0\u5347\u523096%\uff0c\u5b58\u50a8\u6210\u672c\u63a7\u5236\u4e3a\u6bcf\u67086000\u5143\u4eba\u6c11\u5e01\uff08\u542b\u786c\u4ef6\u548c\u8fd0\u7ef4\uff09\u3002\u5e38\u89c1\u5751\u5305\u62ec\u65e5\u5fd7\u91cf\u7a81\u589e\u5bfc\u81f4\u5b58\u50a8\u6ea2\u51fa\uff08\u8bbe\u7f6e\u7d22\u5f15\u751f\u547d\u5468\u671f\u7b56\u7565\u5220\u9664\u65e7\u6570\u636e\uff09\u548c\u811a\u672c\u9519\u8bef\u4e22\u5931\u65e5\u5fd7\uff08\u6dfb\u52a0\u91cd\u8bd5\u673a\u5236\u548c\u76d1\u63a7\u544a\u8b66\uff09\u3002\u4e0b\u4e00\u6b65\u600e\u4e48\u63a5\u5165\u4e1a\u52a1\uff1a\u96c6\u6210\u540e\uff0c\u65e5\u5fd7\u5206\u6790\u7ed3\u679c\u53ef\u81ea\u52a8\u89e6\u53d1\u4e1a\u52a1\u76d1\u63a7\u7cfb\u7edf\u7684\u544a\u8b66\u6d41\u7a0b\uff0c\u4f8b\u5982\u901a\u8fc7Webhook\u901a\u77e5Slack\u9891\u9053\u3002<\/p>\n<h2>\u540e\u7eed\u4f18\u5316<\/h2>\n<p>\u5efa\u8bae\u540e\u7eed\u4f18\u5316\uff1a\u4f7f\u7528Logstash\u8fc7\u6ee4\u5668\u8fdb\u4e00\u6b65\u6e05\u6d17\u65e5\u5fd7\uff0c\u5b9e\u65bd\u7d22\u5f15\u4f18\u5316\u7b56\u7565\u5982\u5206\u7247\u8bbe\u7f6e\uff0c\u5e76\u8003\u8651\u4f7f\u7528Docker\u5bb9\u5668\u5316\u90e8\u7f72\u4ee5\u63d0\u9ad8\u53ef\u79fb\u690d\u6027\u3002\u8fc1\u79fb\u7b56\u7565\u5df2\u9a8c\u8bc1\uff0c\u4ece\u65e7\u811a\u672c\u8fc1\u79fb\u65f6\uff0c\u5148\u5e76\u884c\u8fd0\u884c\u4e00\u5468\u5bf9\u6bd4\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5bf9\u6bd4Python\u811a\u672c\u3001\u7eafELK\u548cSaaS\u65b9\u6848\uff0c\u5c55\u793a\u5982\u4f55\u4ece\u96f6\u90e8\u7f72ELK Stack\u5b9e\u73b0\u5206\u5e03\u5f0f\u670d\u52a1\u5668\u65e5\u5fd7\u81ea\u52a8\u5316\u6536\u96c6\u3001\u6e05\u6d17\u548c\u5b9e\u65f6\u5206\u6790\uff0c\u5305\u62ec\u4ee3\u7801\u793a\u4f8b\u548c\u5b9e\u6218\u8bc4\u4f30\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[141],"tags":[103,89,201,225,18],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-linux-","tag-automation","tag-devops","tag-elk","tag-logging","tag-python"],"views":156,"_links":{"self":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=493"}],"version-history":[{"count":1,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions"}],"predecessor-version":[{"id":498,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions\/498"}],"wp:attachment":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}