{"id":18393,"date":"2025-04-19T00:48:02","date_gmt":"2025-04-18T16:48:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/18393.html"},"modified":"2025-04-19T00:48:02","modified_gmt":"2025-04-18T16:48:02","slug":"%e4%bd%bf%e7%94%a8%e9%a3%9e%e4%b9%a6%e7%be%a4%e6%9c%ba%e5%99%a8%e4%ba%ba%e7%9b%91%e6%8e%a7%e6%9c%8d%e5%8a%a1%e5%99%a8gpu%e4%bd%bf%e7%94%a8%e7%8e%87","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/18393.html","title":{"rendered":"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387"},"content":{"rendered":"<p>\u76ee\u6807&#xff1a;\u5982\u679c\u670d\u52a1\u5668GPU\u7a7a\u7f6e&#xff0c;\u53ef\u4ee5\u53ca\u65f6\u63a8\u9001\u6d88\u606f\u5230\u98de\u4e66\u7fa4\u3002<\/p>\n<p>\u5176\u4ed6\u7c7b\u4f3c\u7684\u76d1\u63a7\u76ee\u6807\u4e5f\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p>\u6b65\u9aa4&#xff1a;<\/p>\n<p>(1) \u9996\u5148\u5728\u7fa4\u804a\u8bbe\u7f6e\u52a0\u5165\u673a\u5668\u4eba&#xff0c;\u590d\u5236webhook_url<\/p>\n<p>(2) \u5728\u670d\u52a1\u5668\u540e\u53f0\u8fd0\u884c\u5982\u4e0b\u4ee3\u7801\u3002\u6ce8\u610f\u66ff\u6362webhook_url<\/p>\n<p>&#034;&#034;&#034;<br \/>\nnohup python monitor_gpu.py my_server &gt; monitor_gpu.log 2&gt;&amp;1 &amp;<br \/>\n&#034;&#034;&#034;<br \/>\nimport sys<br \/>\nimport subprocess<br \/>\nimport requests<br \/>\nimport time<br \/>\nfrom datetime import datetime<\/p>\n<p>import logging<br \/>\nlogger &#061; logging.getLogger(__name__)<br \/>\nlogging.basicConfig(level&#061;logging.INFO)<\/p>\n<p># \u98de\u4e66\u673a\u5668\u4eba webhook \u5730\u5740<br \/>\nfeishu_webhook_url &#061; &#034;https:\/\/open.feishu.cn\/open-apis\/bot\/v2\/hook\/xxxxxxx&#034;<br \/>\nif len(sys.argv) &lt; 2:<br \/>\n    logger.error(f&#034;Usage: python {__file__} &lt;machine_id&gt;&#034;)<br \/>\n    sys.exit(1)<br \/>\nmachine_id &#061; sys.argv[1]<\/p>\n<p>def check_gpu_usage(util_threshold&#061;10):<br \/>\n    # \u6267\u884c nvidia-smi \u547d\u4ee4\u83b7\u53d6\u663e\u5361\u4f7f\u7528\u60c5\u51b5<br \/>\n    result &#061; subprocess.run([&#039;nvidia-smi&#039;, &#039;&#8211;query-gpu&#061;utilization.gpu,memory.used,memory.total&#039;, &#039;&#8211;format&#061;csv,noheader,nounits&#039;], stdout&#061;subprocess.PIPE)<br \/>\n    output &#061; result.stdout.decode(&#039;utf-8&#039;).strip()<\/p>\n<p>    # \u89e3\u6790\u8f93\u51fa<br \/>\n    utils &#061; []<br \/>\n    mems &#061; []<br \/>\n    for info in output.split(&#039;\\\\n&#039;):<br \/>\n        if info:<br \/>\n            gpu_utilization, memory_used, memory_total &#061; map(int, info.split(&#039;, &#039;))<br \/>\n            utils.append(gpu_utilization)<br \/>\n            mems.append(memory_used)<br \/>\n    gpu_utilization_avg &#061; sum(utils) \/ len(utils)<br \/>\n    memory_used_avg &#061; sum(mems) \/ len(mems)<\/p>\n<p>    # \u5224\u65ad\u662f\u5426\u7a7a\u95f2&#xff08;\u5047\u8bbe\u5229\u7528\u7387\u5c0f\u4e8e10%\u4e3a\u7a7a\u95f2&#xff09;<br \/>\n    if gpu_utilization_avg &lt; util_threshold:<br \/>\n        return True, f&#034;\u26a0\ufe0f {machine_id} GPU\u7a7a\u95f2&#xff1a;\u5e73\u5747\u5229\u7528\u7387 {gpu_utilization_avg}% \u663e\u5b58 {memory_used_avg\/1000} GB&#034;<br \/>\n    return False, f&#034;\u2705 {machine_id} GPUs are in usage.&#034;<\/p>\n<p>def send_feishu_message(message):<br \/>\n    headers &#061; {<br \/>\n        &#034;Content-Type&#034;: &#034;application\/json&#034;<br \/>\n    }<br \/>\n    data &#061; {<br \/>\n        &#034;timestamp&#034;: int(datetime.now().timestamp()),<br \/>\n        &#034;msg_type&#034;: &#034;text&#034;,<br \/>\n        &#034;content&#034;: {<br \/>\n            &#034;text&#034;: message<br \/>\n        }<br \/>\n    }<br \/>\n    t &#061; datetime.now().strftime(&#034;%Y-%m-%d %H:%M:%S&#034;)<br \/>\n    response &#061; requests.post(feishu_webhook_url, json&#061;data, headers&#061;headers)<br \/>\n    response.raise_for_status()<br \/>\n    result &#061; response.json()<br \/>\n    if result.get(&#034;code&#034;) and result.get(&#034;code&#034;) !&#061; 0:<br \/>\n        logger.info(f&#034;{t} \u53d1\u9001\\\\&#034;{message}\\\\&#034;\u5931\u8d25&#xff1a;{result[&#039;msg&#039;]}&#034;)<br \/>\n    else:<br \/>\n        logger.info(f&#034;{t} \u53d1\u9001\\\\&#034;{message}\\\\&#034;\u6210\u529f&#034;)<\/p>\n<p>send_feishu_message(f&#034;&#x1f389; {machine_id} GPU\u7a7a\u95f2\u72b6\u6001\u76d1\u63a7\u5df2\u5f00\u542f! \u521d\u59cb\u5316\u72b6\u6001\u4e3a&#xff1a;GPU\u6b63\u5728\u88ab\u4f7f\u7528\u4e2d&#034;)<br \/>\nstate_prev &#061; False<br \/>\nmax_retry &#061; 3<br \/>\nwhile True:<br \/>\n    # \u5355\u6b21\u6700\u591a\u68c0\u67e5k\u6b21&#xff0c;\u5168\u7a7a\u624d\u89c6\u4e3a\u7a7a<br \/>\n    is_idle, message &#061; None, None<br \/>\n    for _ in range(max_retry):<br \/>\n        is_idle, message &#061; check_gpu_usage()<br \/>\n        if not is_idle:<br \/>\n            break<br \/>\n        time.sleep(60)<\/p>\n<p>    if is_idle ^ state_prev:<br \/>\n        send_feishu_message(message)<br \/>\n    state_prev &#061; is_idle<br \/>\n    time.sleep(10 * 60)  # \u6bcf10\u5206\u949f\u68c0\u67e5\u4e00\u6b21<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\u3002(2) \u5728\u670d\u52a1\u5668\u540e\u53f0\u8fd0\u884c\u5982\u4e0b\u4ee3\u7801\u3002\u6ce8\u610f\u66ff\u6362webhook_url\u3002(1) \u9996\u5148\u5728\u7fa4\u804a\u8bbe\u7f6e\u52a0\u5165\u673a\u5668\u4eba\uff0c\u590d\u5236webhook_url\u3002\u76ee\u6807\uff1a\u5982\u679c\u670d\u52a1\u5668GPU\u7a7a\u7f6e\uff0c\u53ef\u4ee5\u53ca\u65f6\u63a8\u9001\u6d88\u606f\u5230\u98de\u4e66\u7fa4\u3002\u5176\u4ed6\u7c7b\u4f3c\u7684\u76d1\u63a7\u76ee\u6807\u4e5f\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\u5b9e\u73b0\u3002_\u98de\u4e66\u4ecb\u5165gpu api<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[43,1388],"topic":[],"class_list":["post-18393","post","type-post","status-publish","format-standard","hentry","category-server","tag-43","tag-1388"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/18393.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\u3002(2) \u5728\u670d\u52a1\u5668\u540e\u53f0\u8fd0\u884c\u5982\u4e0b\u4ee3\u7801\u3002\u6ce8\u610f\u66ff\u6362webhook_url\u3002(1) \u9996\u5148\u5728\u7fa4\u804a\u8bbe\u7f6e\u52a0\u5165\u673a\u5668\u4eba\uff0c\u590d\u5236webhook_url\u3002\u76ee\u6807\uff1a\u5982\u679c\u670d\u52a1\u5668GPU\u7a7a\u7f6e\uff0c\u53ef\u4ee5\u53ca\u65f6\u63a8\u9001\u6d88\u606f\u5230\u98de\u4e66\u7fa4\u3002\u5176\u4ed6\u7c7b\u4f3c\u7684\u76d1\u63a7\u76ee\u6807\u4e5f\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\u5b9e\u73b0\u3002_\u98de\u4e66\u4ecb\u5165gpu api\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/18393.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T16:48:02+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/18393.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/18393.html\",\"name\":\"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T16:48:02+00:00\",\"dateModified\":\"2025-04-18T16:48:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/18393.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/18393.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/18393.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/18393.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1k\u6b21\u3002(2) \u5728\u670d\u52a1\u5668\u540e\u53f0\u8fd0\u884c\u5982\u4e0b\u4ee3\u7801\u3002\u6ce8\u610f\u66ff\u6362webhook_url\u3002(1) \u9996\u5148\u5728\u7fa4\u804a\u8bbe\u7f6e\u52a0\u5165\u673a\u5668\u4eba\uff0c\u590d\u5236webhook_url\u3002\u76ee\u6807\uff1a\u5982\u679c\u670d\u52a1\u5668GPU\u7a7a\u7f6e\uff0c\u53ef\u4ee5\u53ca\u65f6\u63a8\u9001\u6d88\u606f\u5230\u98de\u4e66\u7fa4\u3002\u5176\u4ed6\u7c7b\u4f3c\u7684\u76d1\u63a7\u76ee\u6807\u4e5f\u53ef\u4ee5\u4fee\u6539\u4ee3\u7801\u5b9e\u73b0\u3002_\u98de\u4e66\u4ecb\u5165gpu api","og_url":"https:\/\/www.wsisp.com\/helps\/18393.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T16:48:02+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/18393.html","url":"https:\/\/www.wsisp.com\/helps\/18393.html","name":"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T16:48:02+00:00","dateModified":"2025-04-18T16:48:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/18393.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/18393.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/18393.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4f7f\u7528\u98de\u4e66\u7fa4\u673a\u5668\u4eba\u76d1\u63a7\u670d\u52a1\u5668GPU\u4f7f\u7528\u7387"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/18393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=18393"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/18393\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=18393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=18393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=18393"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=18393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}