• 重磅!!!监控分布式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
    
    • 1
    • 2

    案例源码:

    # -*- 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

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

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

    图片

    主界面效果:

    图片

    服务器接口响应情况:

    图片

    图片

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

    图片

  • 相关阅读:
    学习vite的核心原理
    阿里 OSS鉴权访问文件
    题目:2706.购买两块巧克力
    如何在快应用中定义一个全局对象
    js实现图片懒加载
    RadonDB MySQL Kubernetes 2.2.1 发布!
    卷积神经网络(CNN)网络结构及模型原理介绍
    【C++】模拟实现STL容器:vector
    openstack nova 源码分析
    【Java】672. 灯泡开关 Ⅱ
  • 原文地址:https://blog.csdn.net/hzblucky1314/article/details/138202857