重磅!!!监控分布式NVIDIA-GPU状态

简介:Uptime Kuma是一个易于使用的自托管监控工具,它的界面干净简洁,部署和使用都非常方便,用来监控GPU是否在占用,非常美观。

历史攻略:

docker应用:搭建uptime-kuma监控站点

win下持续观察nvidia-smi

Python:查看windows下GPU的使用情况、利用率

使用Supervisor部署Sanic应用

操作步骤:

1、容器搭建Uptime Kuma。详见 - 历史攻略链接1

2、安装nvidia-smi。详见 - 历史攻略链接2

3、搭建sanic服务端:主要是写访问nvidia-smi的一个接口。

4、配置Uptime Kuma。

安装依赖:

复制代码
pip install paramiko
pip install sanic

案例源码:

复制代码
# -*- coding: utf-8 -*-
# time: 2024/4/23 20:15
# file: server.py
# 公众号: 玩转测试开发

import re
import paramiko
import datetime
from sanic import Sanic
from sanic.response import json


class ParamikoTool(object):
    def __init__(self, user, password, host, port=22, timeout=60):
        self.user = user
        self.password = password
        self.host = host
        self.port = port
        self.timeout = timeout

    def send_command(self, command):
        print(f"send command:{command}")
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(self.host, self.port, self.user, self.password)
        stdin, stdout, stderr = ssh.exec_command(command)
        out = stdout.readlines()
        err = stderr.readlines()
        ssh.close()
        out_result = "".join(out)
        err_result = "".join(err)

        result = out_result + err_result
        print(result)

        return result


app = Sanic("MyHelloWorldApp")


@app.post("/")
async def hello_world(request):
    data = request.json
    print(f"data:{data}")

    get_command = dict()

    get_command["user"] = data["user"]
    get_command["password"] = data["password"]
    get_command["host"] = data["host"]

    if data.get("port") is None:
        get_command["port"] = 22

    else:
        get_command["port"] = data["port"]

    if data.get("timeout") is None:
        get_command["timeout"] = 60

    else:
        get_command["timeout"] = data["timeout"]

    user = get_command["user"]
    password = get_command["password"]
    host = get_command["host"]

    pt = ParamikoTool(user=user, password=password, host=host)
    smi_data = pt.send_command("nvidia-smi")
    utilization_rate = float(re.findall("MiB \|(.*?)%", smi_data)[0])
    card_used = True if utilization_rate > 0 else False

    if card_used:
        # 如果已经使用则,返回异常。否则正常返回
        return BaseException
    else:
        server_data = {
            "card_used": card_used,
            "date": str(datetime.datetime.now())[:19],
        }
        del pt

        return json(server_data)


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8009, auto_reload=True)

运行接口服务端:python server.py 或者参考详见 - 历史攻略链接4

Uptime Kuma配置监控项:多个机器的卡就发起多个监控项,填入对应账号密码即可。

主界面效果:

服务器接口响应情况:

小结:同理可以监控各类服务,进程,端口,占用。本质是:通过启动一个接口服务,将Uptime Kuma监控平台的接口请求,先指向这个服务接口,接口通过paramiko的方式,在对应的服务器执行对应的命令,解析这个命令,然后返回给Uptime Kuma平台。

相关推荐
张3蜂6 小时前
Gunicorn深度解析:Python WSGI服务器的王者
服务器·python·gunicorn
岁岁种桃花儿6 小时前
SpringCloud从入门到上天:分布式和微服务基础
分布式·spring cloud·微服务
杭州泽沃电子科技有限公司8 小时前
为电气风险定价:如何利用监测数据评估工厂的“电气安全风险指数”?
人工智能·安全
Godspeed Zhao10 小时前
自动驾驶中的传感器技术24.3——Camera(18)
人工智能·机器学习·自动驾驶
顾北1211 小时前
MCP协议实战|Spring AI + 高德地图工具集成教程
人工智能
wfeqhfxz258878211 小时前
毒蝇伞品种识别与分类_Centernet模型优化实战
人工智能·分类·数据挖掘
上海锟联科技12 小时前
DAS 系统 250MSPS 是否足够?——来自上海锟联科技的专业解析
分布式·科技·分布式光纤传感·光频域反射·das
rayufo12 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
中杯可乐多加冰12 小时前
RAG 深度实践系列(七):从“能用”到“好用”——RAG 系统优化与效果评估
人工智能·大模型·llm·大语言模型·rag·检索增强生成
珠海西格电力科技12 小时前
微电网系统架构设计:并网/孤岛双模式运行与控制策略
网络·人工智能·物联网·系统架构·云计算·智慧城市