python 获取 GPU 温度,发送qq邮箱预警

一  发送qq邮件

这里用到了Python的两个包来发送邮件: smtplib 和 email 。
  Python 的 email 模块里包含了许多实用的邮件格式设置函数,可以用来创建邮件“包裹”。使用的 MIMEText 对象,为底层的 MIME(Multipurpose Internet MailExtensions,多用途互联网邮件扩展类型)协议传输创建了一封空邮件,最后通过高层的SMTP 协议发送出去。 MIMEText 对象 msg 包括收发邮箱地址、邮件正文和主题,Python 通过它就可以创建一封格式正确的邮件。smtplib 模块用来设置服务器连接的相关信息。

  要想通过QQ邮箱来发送邮件,需要开启QQ邮箱的设置-账户里SMTP服务,接下来会通过发送短信验证来获得授权码,有了授权码后就可以在代码里添加了。

 

 

 

 

 

二、Python 获取nvidia-smi中gpu相关的信息

import pynvml
import time
pynvml.nvmlInit()

def printNvidiaGPU(gpu_id):
    # get GPU temperature
    gpu_device = pynvml.nvmlDeviceGetHandleByIndex(gpu_id)

    temperature = pynvml.nvmlDeviceGetTemperature(gpu_device, pynvml.NVML_TEMPERATURE_GPU)
    # get GPU memory total
    totalMemory = pynvml.nvmlDeviceGetMemoryInfo(gpu_device).total
    # get GPU memory used
    usedMemory = pynvml.nvmlDeviceGetMemoryInfo(gpu_device).used

    performance = pynvml.nvmlDeviceGetPerformanceState(gpu_device)

    powerUsage = pynvml.nvmlDeviceGetPowerUsage(gpu_device)
    powerState = pynvml.nvmlDeviceGetPowerState(gpu_device)
    FanSpeed = pynvml.nvmlDeviceGetFanSpeed(gpu_device)
    PersistenceMode = pynvml.nvmlDeviceGetPersistenceMode(gpu_device)
    UtilizationRates = pynvml.nvmlDeviceGetUtilizationRates(gpu_device)

    print("MemoryInfo:{0}M/{1}M,使用率:{2}%".format("%.1f" % (usedMemory / 1024 / 1024), "%.1f" % (totalMemory / 1024 / 1024), "%.1f" % (usedMemory/totalMemory*100)))
    print("Temperature:{0}摄氏度".format(temperature))
    print("Performance:{0}".format(performance))
    print("PowerState: {0}".format(powerState))
    print("PowerUsage: {0}".format(powerUsage / 1000))
    print("FanSpeed: {0}".format(FanSpeed))
    print("PersistenceMode: {0}".format(PersistenceMode))
    print("UtilizationRates: {0}".format(UtilizationRates.gpu))
    time.sleep(1)
    
while (1):
    printNvidiaGPU(0) # 此处以0号gpu为例


三 , 以上两步 整合


# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

import pynvml
import time
pynvml.nvmlInit()

# 第三方 SMTP 服务
mail_host = "smtp.qq.com"  # 设置服务器
mail_user = "*********@qq.com"  # 用户名
mail_pass = "lzsnkoqjbit****"  # 口令,QQ邮箱是输入授权码,在qq邮箱设置 里用验证过的手机发送短信获得,不含空格

sender = '*********@qq.com'
receivers = ['*********@qq.com', '****@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱



def sendEmail(t):
    message = MIMEText(' ip:10.*.*.103 \nGPU 温度='+t, 'plain', 'utf-8')
    message['From'] = Header(sender, 'utf-8')
    message['To'] = Header(receivers[0], 'utf-8')
    subject = ' GPU温度告警 '
    message['Subject'] = Header(subject, 'utf-8')

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        smtpObj.quit()
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"邮件发送成功 温度="+t)
    except smtplib.SMTPException as e:
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"邮件发送成功 温度="+t,e)

def getNvidiaGPU(gpu_id):
    gpu_device = pynvml.nvmlDeviceGetHandleByIndex(gpu_id)
    temperature = pynvml.nvmlDeviceGetTemperature(gpu_device, pynvml.NVML_TEMPERATURE_GPU)
    return temperature

if __name__ == '__main__':
    # 此处以0号gpu为例
    while(1):
        tem = getNvidiaGPU(0)
        time.sleep(1)
        if(tem>=30):
            # sendEmail(str(tem))
            print("邮件发送成功")
        else:
            print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "温度=" + str(tem))

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值