话不多说,先来脚本

  1. #!/usr/bin/python
  2. import netifaces
  3. import requests
  4. import time
  5. import json
  6. import re
  7. import commands
  8. mac="GPU-machine" + ip #此处用你的ip
  9. print(mac)
  10. TEMP="GPU Current Temp"
  11. (status,output)=commands.getstatusoutput("nvidia-smi -q | grep 'GPU Current Temp' | cut -d' ' -f 24")
  12. print(status)
  13. print(output)
  14. #此处 'GPU Current Temp' 在函数内部,先用'',外部用“”。否则报错。就是外部'',内部"",则引用不了内部。报错。
  15. def push():
  16. ts = int(time.time())
  17. payload = [
  18. {
  19. "endpoint": mac, #若是字符,用“”或者''. 变量就直接用。
  20. "metric": "GPU-temperture",
  21. "timestamp": ts,
  22. "step": 20,
  23. "value": output,
  24. "counterType": "GAUGE",
  25. "tags": "idc=lg,loc=beijing",
  26. }
  27. ]
  28. r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(payload))
  29. print(r.text)
  30. print(mac)
  31. while True:
  32. push()
  33. time.sleep(1)

1 GPU温度

nvidia-smi -q -i 0 -d TEMPERATURE|grep ‘GPU Current’|cut -d” “ -f 24
-i0 看第0块GPU
这句命令是用来查看当前GPU的温度的

2 python中匹配字符, 需要用 re正则模块。 否则报错。

3 python中执行shell命令,不能直接运行,

第一想到的就是os.system,
os.system(‘cat /proc/cpuinfo’)
但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()
output = os.popen(‘cat /proc/cpuinfo’)
print output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)
尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
(status, output) = commands.getstatusoutput(‘cat /proc/cpuinfo’)
print status, output
参考链接(https://www.cnblogs.com/hei-hei-hei/p/7216434.html)

subprocess

commands模块是python2时用的, 结果我的环境是python3,所以新模块subprocess。

先试试Popen

  1. >>> tem=subprocess.Popen("nvidia-smi -q | grep 'GPU Current Temp' | cut -d' ' -f 24",shell=True,stdout=subprocess.PIPE,universal_newlines=True)
  2. >>> tem2,err=tem.communicate()
  3. >>> print(tem2)
  4. 36
  5. >>> print(tem2)
  6. 36

结果是得到了。 但是36下面有个 空行什么意思, 这就不方便传参了。

换一个subprocess.getstatusoutput

  1. >>> subprocess.getstatusoutput("nvidia-smi -q | grep 'GPU Current Temp' | cut -d' ' -f 24")
  2. (0, '36')
  3. >>> a,b=subprocess.getstatusoutput("nvidia-smi -q | grep 'GPU Current Temp' | cut -d' ' -f 24")
  4. >>> print(b)
  5. 36
  6. >>> print(b)
  7. 36
  8. >>> print(b)
  9. 36
  10. >>>

这就很好,