linux服务器显卡监控脚本

简介: linux服务器显卡监控脚本
  • 前期准备:
    pip install pynvml

1.watch_nvidia

#参数:nvidia_ids:显卡id   min_memory:最小可用显存 GB
def watch_nvidia(nvidia_ids,min_memory):
  flag = [1 for i in nvidia_ids]
  for i in nvidia_ids:
    handle = pynvml.nvmlDeviceGetHandleByIndex(i)
    meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
    #遍历每块卡的剩余显存
    print("card {} free memory is {}GB".format(i,meminfo.free * 1.0 /(1024**3)))
    if meminfo.free * 1.0 /(1024**3) > min_memory:
      flag[i-1]=0
    else:
      flag[i-1]=1#统计符合要求的卡的数量
  if 0 in flag:
    free_num = 0
    for i in flag:
      if i == 0:
        free_num += 1
    return free_num#返回符合要求的卡的数量
  else:
    print("no free card!")
    return -1

2.send_msg

#发送邮箱信息
#target_email:接受信息的邮箱,msg:发送的消息
def send_msg(target_email,msg):
  sender = 'xxxxx@163.com'  #发送信息的邮箱
  receivers = [target_email]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
  # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
  message = MIMEText(msg, 'plain', 'utf-8')
  subject = 'nvidia显卡监控'
  message['Subject'] = Header(subject, 'utf-8')
  #server = smtplib.SMTP('smtp.163.com', 587)
  server = smtplib.SMTP_SSL('smtp.163.com')#这部分需要去发送的邮箱账号去开启IMAP服务,获取登录授权码
  server.connect('smtp.163.com',465)
  #server.starttls()
  #server.ehlo()
  #xxxxxx是获取的登录授权码
  server.login(sender, "xxxxxx") 
  server.sendmail(sender, receivers, message.as_string()) 
  server.quit()


59.png


3.完整脚本nvidia.py

#-*-coding:GBK -*- 
import pynvml
pynvml.nvmlInit()
import time
import os
#from send_email import send_msg
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def send_msg(target_email,msg):
  sender = 'xxxxx@163.com'
  receivers = [target_email]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
  # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
  message = MIMEText(msg, 'plain', 'utf-8')
  subject = 'nvidia显卡监控'
  message['Subject'] = Header(subject, 'utf-8')
  #server = smtplib.SMTP('smtp.163.com', 587)
  server = smtplib.SMTP_SSL('smtp.163.com')
  server.connect('smtp.163.com',465)
  #server.starttls()
  #server.ehlo()
  server.login(sender, "xxxxxxx") 
  server.sendmail(sender, receivers, message.as_string()) 
  server.quit()
  #smtpObj = smtplib.SMTP("localhost",1025)
  #try:   
  #    smtpObj.sendmail(sender, receivers, message.as_string())
  #    print("邮件发送成功")
  #except smtplib.SMTPException:
  #    print("Error: 无法发送邮件")
def watch_nvidia(nvidia_ids,min_memory):
  flag = [1 for i in nvidia_ids]
  for i in nvidia_ids:
    handle = pynvml.nvmlDeviceGetHandleByIndex(i)
    meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
    print("card {} free memory is {}GB".format(i,meminfo.free * 1.0 /(1024**3)))
    if meminfo.free * 1.0 /(1024**3) > min_memory:
      flag[i-1]=0
    else:
      flag[i-1]=1
  if 0 in flag:
    free_num = 0
    for i in flag:
      if i == 0:
        free_num += 1
    return free_num
  else:
    print("no free card!")
    return -1
nvidia_ids = [0,1] # 显卡id
min_memory = 8 # 最小可用显存 GB
while True:
  flag = watch_nvidia(nvidia_ids,min_memory)
  if flag >= 1:
    send_msg("xxxxxxx@bjtu.edu.cn","{}张显卡空闲".format(flag))
    #os.system("sh veri.sh") # your command
    break
  time.sleep(10)

4.后台运行

nohup python nvidia.py >nvidia.out&
目录
相关文章
|
16天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
19天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 <<EOF binary put file.txt quit EOF ```
28 4
|
23天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
1天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
39 2
|
3天前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
13 0
|
5天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
9天前
|
监控 Linux
linux监控指定进程
请注意,以上步骤提供了一种基本的方式来监控指定进程。根据你的需求,你可以选择使用不同的工具和参数来获取更详细的进程信息。
14 0
|
9天前
|
监控 Linux 网络安全
linux中启动rpc.rstat监控
请注意,rpc.rstatd服务通常用于收集远程系统的性能统计信息,例如CPU利用率、内存使用等。在使用rpc.rstatd服务之前,你应该确保了解其功能、用法和安全性,并根据需要进行适当的配置和调整。
8 0
|
11天前
|
Linux 数据安全/隐私保护
Linux基础与服务器架构综合小实践
【4月更文挑战第9天】Linux基础与服务器架构综合小实践
1235 8
|
12天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0