{"id":398,"date":"2026-03-06T09:45:24","date_gmt":"2026-03-06T01:45:24","guid":{"rendered":"https:\/\/www.liaoxinghui.com\/?p=398"},"modified":"2026-03-06T09:45:24","modified_gmt":"2026-03-06T01:45:24","slug":"bash-script-monitor-linux-server-resources-send-dingtalk-alerts","status":"publish","type":"post","link":"https:\/\/www.liaoxinghui.com\/?p=398","title":{"rendered":"\u4f7f\u7528Bash\u811a\u672c\u76d1\u63a7Linux\u670d\u52a1\u5668\u8d44\u6e90\u5e76\u81ea\u52a8\u53d1\u9001\u544a\u8b66\u5230\u9489\u9489"},"content":{"rendered":"<h1>\u4f7f\u7528Bash\u811a\u672c\u76d1\u63a7Linux\u670d\u52a1\u5668\u8d44\u6e90\u5e76\u81ea\u52a8\u53d1\u9001\u544a\u8b66\u5230\u9489\u9489<\/h1>\n<h2>1. \u4e1a\u52a1\u573a\u666f\u4e0e\u76ee\u6807<\/h2>\n<p>\u4e2d\u5c0f\u4f01\u4e1a\u901a\u5e38\u6709\u591a\u53f0Linux\u670d\u52a1\u5668\u8fd0\u884c\u5173\u952e\u4e1a\u52a1\uff0c\u5982Web\u670d\u52a1\u6216\u6570\u636e\u5e93\u3002\u624b\u52a8\u767b\u5f55\u670d\u52a1\u5668\u68c0\u67e5\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\u6548\u7387\u4f4e\u4e0b\uff0c\u5bb9\u6613\u9519\u8fc7\u5f02\u5e38\u3002\u672c\u65b9\u6848\u7684\u76ee\u6807\u662f\uff1a\u901a\u8fc7Bash\u811a\u672c\u81ea\u52a8\u76d1\u63a7CPU\u548c\u5185\u5b58\u4f7f\u7528\u7387\uff0c\u5f53\u8d85\u8fc7\u9608\u503c\uff08\u5982CPU&gt;80%\u3001\u5185\u5b58&gt;90%\uff09\u65f6\uff0c\u81ea\u52a8\u53d1\u9001\u544a\u8b66\u6d88\u606f\u5230\u9489\u9489\u7fa4\uff0c\u5b9e\u73b07&#215;24\u5c0f\u65f6\u65e0\u4eba\u503c\u5b88\u76d1\u63a7\uff0c\u63d0\u5347\u8fd0\u7ef4\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<h2>2. \u73af\u5883\u51c6\u5907\uff08uv + \u4f9d\u8d56\uff09<\/h2>\n<p>\u672c\u4efb\u52a1\u57fa\u4e8eBash\u811a\u672c\uff0c\u65e0\u9700uv\u6216Python\u4f9d\u8d56\uff0c\u4f46\u9700\u8981\u4ee5\u4e0b\u73af\u5883\uff1a<\/p>\n<ul>\n<li>Linux\u670d\u52a1\u5668\uff08\u5982Ubuntu 20.04+\u6216CentOS 7+\uff09<\/li>\n<li>Bash shell\uff08\u901a\u5e38\u9ed8\u8ba4\u5b89\u88c5\uff09<\/li>\n<li>curl\u547d\u4ee4\uff08\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u5230\u9489\u9489\uff09<\/li>\n<li>\u9489\u9489\u7fa4\u804a\u5e76\u521b\u5efa\u81ea\u5b9a\u4e49\u673a\u5668\u4ebaWebhook\uff08\u83b7\u53d6Webhook URL\uff09<\/li>\n<\/ul>\n<p>\u68c0\u67e5\u73af\u5883\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\"># \u68c0\u67e5Bash\u7248\u672c\nbash --version\n# \u68c0\u67e5curl\u662f\u5426\u5b89\u88c5\ncurl --version\n# \u5982\u679c\u6ca1\u6709curl\uff0c\u5b89\u88c5\u5b83\uff08\u4ee5Ubuntu\u4e3a\u4f8b\uff09\nsudo apt update &amp;&amp; sudo apt install curl -y<\/code><\/pre>\n<h2>3. \u6570\u636e\u8bf4\u660e\uff08\u771f\u5b9e\u6570\u636e\u53e3\u5f84\u6216\u6a21\u62df\u6570\u636e\u751f\u6210\u903b\u8f91\uff09<\/h2>\n<p>\u76d1\u63a7\u6570\u636e\u6765\u81eaLinux\u7cfb\u7edf\u7684\u5b9e\u65f6\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li><strong>CPU\u4f7f\u7528\u7387<\/strong>\uff1a\u901a\u8fc7<code>top<\/code>\u6216<code>\/proc\/stat<\/code>\u8ba1\u7b97\uff0c\u672c\u811a\u672c\u4f7f\u7528<code>top -bn1<\/code>\u83b7\u53d6\uff0c\u8f93\u51fa\u4e3a\u767e\u5206\u6bd4\uff08\u598285.5%\uff09\u3002<\/li>\n<li><strong>\u5185\u5b58\u4f7f\u7528\u7387<\/strong>\uff1a\u901a\u8fc7<code>free<\/code>\u547d\u4ee4\u83b7\u53d6\uff0c\u8ba1\u7b97\u5df2\u7528\u5185\u5b58\u5360\u603b\u5185\u5b58\u7684\u6bd4\u4f8b\uff0c\u8f93\u51fa\u4e3a\u767e\u5206\u6bd4\uff08\u598292.3%\uff09\u3002<\/li>\n<li><strong>\u9608\u503c\u8bbe\u7f6e<\/strong>\uff1aCPU\u9608\u503c\u9ed8\u8ba480%\uff0c\u5185\u5b58\u9608\u503c\u9ed8\u8ba490%\uff0c\u53ef\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u8c03\u6574\u3002<\/li>\n<li><strong>\u6a21\u62df\u6570\u636e<\/strong>\uff1a\u5982\u9700\u6d4b\u8bd5\uff0c\u53ef\u4e34\u65f6\u4fee\u6539\u811a\u672c\uff0c\u786c\u7f16\u7801\u9ad8\u4f7f\u7528\u7387\u503c\u6765\u89e6\u53d1\u544a\u8b66\u3002<\/li>\n<\/ul>\n<h2>4. \u8bad\u7ec3\/\u5b9e\u73b0\u6b65\u9aa4\uff08\u5b8c\u6574\u4ee3\u7801\uff09<\/h2>\n<p>\u521b\u5efa\u4e00\u4e2aBash\u811a\u672c<code>monitor_server.sh<\/code>\uff0c\u5b9e\u73b0\u76d1\u63a7\u548c\u544a\u8b66\u529f\u80fd\u3002<\/p>\n<pre><code class=\"lang-bash language-bash bash\">#!\/bin\/bash\n\n# \u9489\u9489Webhook URL\uff0c\u66ff\u6362\u4e3a\u4f60\u7684\u5b9e\u9645URL\nDINGTALK_WEBHOOK=&quot;https:\/\/oapi.dingtalk.com\/robot\/send?access_token=your_token_here&quot;\n\n# \u9608\u503c\u8bbe\u7f6e\nCPU_THRESHOLD=80  # CPU\u4f7f\u7528\u7387\u9608\u503c\uff0c\u767e\u5206\u6bd4\nMEM_THRESHOLD=90  # \u5185\u5b58\u4f7f\u7528\u7387\u9608\u503c\uff0c\u767e\u5206\u6bd4\n\n# \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\nCURRENT_TIME=$(date &quot;+%Y-%m-%d %H:%M:%S&quot;)\n\n# \u83b7\u53d6CPU\u4f7f\u7528\u7387\uff08\u4f7f\u7528top\u547d\u4ee4\uff0c\u53d6\u7b2c\u4e00\u884c\uff0c\u89e3\u6790\u7a7a\u95f2\u767e\u5206\u6bd4\u540e\u8ba1\u7b97\u4f7f\u7528\u7387\uff09\nCPU_INFO=$(top -bn1 | grep &quot;%Cpu(s)&quot;)\nCPU_IDLE=$(echo $CPU_INFO | awk -F &#039; &#039; &#039;{print $8}&#039; | cut -d &#039;.&#039; -f1)\nCPU_USAGE=$((100 - CPU_IDLE))\n\n# \u83b7\u53d6\u5185\u5b58\u4f7f\u7528\u7387\uff08\u4f7f\u7528free\u547d\u4ee4\uff0c\u8ba1\u7b97\u5df2\u7528\/\u603b\u5185\u5b58\u6bd4\u4f8b\uff09\nMEM_INFO=$(free | grep Mem)\nMEM_TOTAL=$(echo $MEM_INFO | awk &#039;{print $2}&#039;)\nMEM_USED=$(echo $MEM_INFO | awk &#039;{print $3}&#039;)\nMEM_USAGE=$((MEM_USED * 100 \/ MEM_TOTAL))\n\n# \u521d\u59cb\u5316\u544a\u8b66\u6d88\u606f\nALERT_MESSAGE=&quot;&quot;\n\n# \u68c0\u67e5CPU\u4f7f\u7528\u7387\u662f\u5426\u8d85\u8fc7\u9608\u503c\nif [ $CPU_USAGE -gt $CPU_THRESHOLD ]; then\n    ALERT_MESSAGE=&quot;CPU\u4f7f\u7528\u7387: ${CPU_USAGE}% (\u9608\u503c: ${CPU_THRESHOLD}%)&quot;\nfi\n\n# \u68c0\u67e5\u5185\u5b58\u4f7f\u7528\u7387\u662f\u5426\u8d85\u8fc7\u9608\u503c\nif [ $MEM_USAGE -gt $MEM_THRESHOLD ]; then\n    if [ -n &quot;$ALERT_MESSAGE&quot; ]; then\n        ALERT_MESSAGE=&quot;${ALERT_MESSAGE}\\n\u5185\u5b58\u4f7f\u7528\u7387: ${MEM_USAGE}% (\u9608\u503c: ${MEM_THRESHOLD}%)&quot;\n    else\n        ALERT_MESSAGE=&quot;\u5185\u5b58\u4f7f\u7528\u7387: ${MEM_USAGE}% (\u9608\u503c: ${MEM_THRESHOLD}%)&quot;\n    fi\nfi\n\n# \u5982\u679c\u6709\u544a\u8b66\uff0c\u53d1\u9001\u5230\u9489\u9489\nif [ -n &quot;$ALERT_MESSAGE&quot; ]; then\n    # \u6784\u5efa\u9489\u9489\u6d88\u606fJSON\n    MESSAGE=&quot;\u670d\u52a1\u5668\u8d44\u6e90\u544a\u8b66 - ${CURRENT_TIME}\\n${ALERT_MESSAGE}&quot;\n    JSON_DATA=&#039;{&quot;msgtype&quot;: &quot;text&quot;, &quot;text&quot;: {&quot;content&quot;: &quot;&#039;&quot;$MESSAGE&quot;&#039;&quot;}}&#039;\n\n    # \u53d1\u9001POST\u8bf7\u6c42\u5230\u9489\u9489Webhook\n    curl -s -H &quot;Content-Type: application\/json&quot; -X POST -d &quot;$JSON_DATA&quot; &quot;$DINGTALK_WEBHOOK&quot;\n\n    # \u8f93\u51fa\u65e5\u5fd7\uff08\u53ef\u9009\uff09\n    echo &quot;[$CURRENT_TIME] \u544a\u8b66\u5df2\u53d1\u9001: $ALERT_MESSAGE&quot; &gt;&gt; \/var\/log\/server_monitor.log\nelse\n    # \u65e0\u544a\u8b66\u65f6\u8bb0\u5f55\u6b63\u5e38\u65e5\u5fd7\uff08\u53ef\u9009\uff09\n    echo &quot;[$CURRENT_TIME] \u8d44\u6e90\u6b63\u5e38: CPU ${CPU_USAGE}%, \u5185\u5b58 ${MEM_USAGE}%&quot; &gt;&gt; \/var\/log\/server_monitor.log\nfi<\/code><\/pre>\n<p>\u4fdd\u5b58\u811a\u672c\u540e\uff0c\u8d4b\u4e88\u6267\u884c\u6743\u9650\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\">chmod +x monitor_server.sh<\/code><\/pre>\n<h2>5. \u8c03\u7528\u65b9\u5f0f\uff08\u79bb\u7ebf\u6279\u91cf + \u5355\u6761\u793a\u4f8b\uff0c\u81f3\u5c11\u4e00\u79cd\uff09<\/h2>\n<ul>\n<li><strong>\u5355\u6761\u793a\u4f8b<\/strong>\uff1a\u76f4\u63a5\u8fd0\u884c\u811a\u672c\u8fdb\u884c\u4e00\u6b21\u6027\u76d1\u63a7\u3002\n<pre><code class=\"lang-bash language-bash bash\">.\/monitor_server.sh<\/code><\/pre><\/li>\n<li><strong>\u79bb\u7ebf\u6279\u91cf<\/strong>\uff1a\u901a\u8fc7crontab\u914d\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff0c\u5b9e\u73b0\u5468\u671f\u6027\u76d1\u63a7\uff08\u5982\u6bcf\u5206\u949f\u4e00\u6b21\uff09\u3002\n<pre><code class=\"lang-bash language-bash bash\"># \u7f16\u8f91crontab\ncrontab -e\n# \u6dfb\u52a0\u4ee5\u4e0b\u884c\uff08\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\uff09\n* * * * * \/path\/to\/monitor_server.sh\n# \u4fdd\u5b58\u5e76\u9000\u51fa\uff0ccron\u4f1a\u81ea\u52a8\u8fd0\u884c<\/code><\/pre><\/li>\n<\/ul>\n<h2>6. \u6307\u6807\u8bf4\u660e<\/h2>\n<p>\u9700\u5173\u6ce8\u4ee5\u4e0b\u8fd0\u7ef4\u6307\u6807\uff1a<\/p>\n<ul>\n<li><strong>CPU\u4f7f\u7528\u7387<\/strong>\uff1a\u670d\u52a1\u5668\u5904\u7406\u4efb\u52a1\u7684\u7e41\u5fd9\u7a0b\u5ea6\uff0c\u8d85\u8fc7\u9608\u503c\u53ef\u80fd\u5bfc\u81f4\u670d\u52a1\u5ef6\u8fdf\u6216\u5d29\u6e83\u3002<\/li>\n<li><strong>\u5185\u5b58\u4f7f\u7528\u7387<\/strong>\uff1a\u670d\u52a1\u5668\u5185\u5b58\u5360\u7528\u6bd4\u4f8b\uff0c\u8fc7\u9ad8\u53ef\u80fd\u89e6\u53d1OOM\uff08\u5185\u5b58\u6ea2\u51fa\uff09\u5bfc\u81f4\u8fdb\u7a0b\u88ab\u6740\u6b7b\u3002<\/li>\n<li><strong>\u544a\u8b66\u51c6\u786e\u7387<\/strong>\uff1a\u811a\u672c\u89e6\u53d1\u544a\u8b66\u7684\u6b63\u786e\u6027\uff0c\u907f\u514d\u8bef\u62a5\uff08\u6b63\u5e38\u65f6\u544a\u8b66\uff09\u6216\u6f0f\u62a5\uff08\u5f02\u5e38\u65f6\u672a\u544a\u8b66\uff09\u3002\n\u9002\u7528\u573a\u666f\uff1a\u5b9e\u65f6\u76d1\u63a7\u670d\u52a1\u5668\u5065\u5eb7\u72b6\u6001\uff0c\u9002\u7528\u4e8eWeb\u670d\u52a1\u5668\u3001\u6570\u636e\u5e93\u7b49\u9700\u8981\u9ad8\u53ef\u7528\u6027\u7684\u73af\u5883\u3002<\/li>\n<\/ul>\n<h2>7. \u4e0a\u7ebf\u540e\u8bc4\u4f30<\/h2>\n<ul>\n<li><strong>\u79bb\u7ebf\u76d1\u63a7<\/strong>\uff1a\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6<code>\/var\/log\/server_monitor.log<\/code>\uff0c\u786e\u8ba4\u811a\u672c\u6267\u884c\u8bb0\u5f55\u548c\u8d44\u6e90\u6570\u636e\u3002<\/li>\n<li><strong>\u7ebf\u4e0a\u6307\u6807<\/strong>\uff1a\u89c2\u5bdf\u9489\u9489\u7fa4\u544a\u8b66\u6d88\u606f\u662f\u5426\u53ca\u65f6\u3001\u51c6\u786e\uff1b\u76d1\u63a7\u670d\u52a1\u5668\u5b9e\u9645\u6027\u80fd\uff0c\u5982\u901a\u8fc7<code>top<\/code>\u6216<code>htop<\/code>\u9a8c\u8bc1\u3002<\/li>\n<li><strong>\u5b9a\u671f\u7ef4\u62a4<\/strong>\uff1a\n<ul>\n<li>\u8c03\u6574\u9608\u503c\uff1a\u6839\u636e\u4e1a\u52a1\u8d1f\u8f7d\u53d8\u5316\uff0c\u4fee\u6539<code>CPU_THRESHOLD<\/code>\u6216<code>MEM_THRESHOLD<\/code>\u3002<\/li>\n<li>\u66f4\u65b0Webhook\uff1a\u9489\u9489\u673a\u5668\u4ebatoken\u53d8\u66f4\u65f6\u66f4\u65b0\u811a\u672c\u3002<\/li>\n<li>\u4f18\u5316\u811a\u672c\uff1a\u5982\u589e\u52a0\u78c1\u76d8\u76d1\u63a7\u3001\u7f51\u7edc\u76d1\u63a7\u7b49\u529f\u80fd\u3002<\/li>\n<\/ul><\/li>\n<\/ul>\n<h2>8. \u5e38\u89c1\u5751\u4e0e\u6392\u67e5<\/h2>\n<ul>\n<li><strong>\u811a\u672c\u6267\u884c\u6743\u9650\u95ee\u9898<\/strong>\uff1a\n<ul>\n<li>\u5751\uff1a\u8fd0\u884c\u811a\u672c\u65f6\u62a5<code>Permission denied<\/code>\u3002<\/li>\n<li>\u6392\u67e5\uff1a\u4f7f\u7528<code>chmod +x monitor_server.sh<\/code>\u6dfb\u52a0\u6267\u884c\u6743\u9650\u3002<\/li>\n<\/ul><\/li>\n<li><strong>\u9489\u9489API\u9650\u6d41<\/strong>\uff1a\n<ul>\n<li>\u5751\uff1a\u9891\u7e41\u544a\u8b66\u5bfc\u81f4\u9489\u9489Webhook\u88ab\u9650\u6d41\uff0c\u6d88\u606f\u53d1\u9001\u5931\u8d25\u3002<\/li>\n<li>\u6392\u67e5\uff1a\u964d\u4f4ecrontab\u9891\u7387\uff08\u5982\u6bcf5\u5206\u949f\u4e00\u6b21\uff09\uff0c\u6216\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u5ef6\u65f6\u903b\u8f91\u3002<\/li>\n<\/ul><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u5982\u4f55\u7f16\u5199\u4e00\u4e2aBash\u811a\u672c\uff0c\u5b9e\u65f6\u76d1\u63a7Linux\u670d\u52a1\u5668\u7684CPU\u548c\u5185\u5b58\u4f7f\u7528\u7387\uff0c\u5f53\u8d85\u8fc7\u9884\u8bbe\u9608\u503c\u65f6\u81ea\u52a8\u901a\u8fc7\u9489\u9489Webhook\u53d1\u9001\u544a\u8b66\u6d88\u606f\uff0c\u5e76\u914d\u7f6ecrontab\u5b9e\u73b0\u5b9a\u65f6\u76d1\u63a7\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,10],"tags":[103,102,89,16,91],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry","category-linux","category-10","tag-automation","tag-bash","tag-devops","tag-linux","tag-monitoring"],"views":148,"_links":{"self":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/398","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=398"}],"version-history":[{"count":3,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions\/405"}],"wp:attachment":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}