是从服务器获取各种参数然后传回数据库,但我不是很懂 while(true)之后是怎样的一个流程。
import MySQLdb
import datetime
import subprocess
import time
import re
con = MySQLdb.connect(
user='root',
passwd='pass',
host='njPi',
db='oomori',
charset='utf8'
)
c = con.cursor()
res1_2_1_1 = [0]*16
res1_2_1_1_1 = [0]*16
values=[""]+[0]*75
args1 = ['sensors']
args1_1 = ['grep', 'Package']
args1_2 = ['grep', 'Core']
args2 = ['vmstat','-D']
args3 = ['vmstat','-s']
args4 = ['nvidia-smi', '-q', '-d', 'temperature']
args4_1 = ['grep', 'Current']
args5 = ['nvidia-smi', '-q', '-d', 'Power']
args5_1 = ['grep', 'Draw']
args6 = ['nvidia-smi', '-q', '-d', 'utilization']
args6_1 = ['grep', 'Gpu']
while(True):
values[0] = time.strftime('%Y-%m-%d %H:%M:%S')
res1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
res1_1 = subprocess.check_output(args1_1, stdin=res1.stdout)
res1 = subprocess.Popen(args1, stdout=subprocess.PIPE)
res1_2 = subprocess.check_output(args1_2, stdin=res1.stdout)
res1_1_1 = res1_1.split(b'+')
res1_1_1_1 = res1_1_1[1].split('°'.encode())[0]
res1_1_1_2 = res1_1_1[4].split('°'.encode())[0]
res1_2_1 = re.split('°'.encode(), res1_2)
for i in range (16):
res1_2_1_1[i]=res1_2_1[i*3]
res1_2_1_1_1[i] = res1_2_1_1[i].split(b'+')[1]
#print(res1_2_1_1_1)
res4 = subprocess.Popen(args4, stdout=subprocess.PIPE)
res4_1 = subprocess.check_output(args4_1, stdin=res4.stdout)
res4_1_1 = re.split('C'.encode(), res4_1)
res4_1_1_1 = res4_1_1[1].split(':'.encode())[1]
res5 = subprocess.Popen(args5, stdout=subprocess.PIPE)
res5_1 = subprocess.check_output(args5_1, stdin=res5.stdout)
res5_1_1 = re.split('W'.encode(), res5_1)
res5_1_1_1 = res5_1_1[0].split(':'.encode())[1]
res6 = subprocess.Popen(args6, stdout=subprocess.PIPE)
res6_1 = subprocess.check_output(args6_1, stdin=res6.stdout)
res6_1_1 = re.split('%'.encode(), res6_1)
res6_1_1_1 = res6_1_1[0].split(':'.encode())[1]
'''
print(res1_1_1_1)
print(res1_1_1_2)
print (res4_1_1_1)
print (res5_1_1_1)
print (res6_1_1_1)
'''
values[1] = res1_1_1_1.decode()
values[2] = res1_2_1_1_1[0].decode()
values[3] = res1_2_1_1_1[1].decode()
values[4] = res1_2_1_1_1[2].decode()
values[5] = res1_2_1_1_1[3].decode()
values[6] = res1_2_1_1_1[4].decode()
values[7] = res1_2_1_1_1[5].decode()
values[8] = res1_2_1_1_1[6].decode()
values[9] = res1_2_1_1_1[7].decode()
values[10] = res1_1_1_2.decode()
values[11] = res1_2_1_1_1[8].decode()
values[12] = res1_2_1_1_1[9].decode()
values[13] = res1_2_1_1_1[10].decode()
values[14] = res1_2_1_1_1[11].decode()
values[15] = res1_2_1_1_1[12].decode()
values[16] = res1_2_1_1_1[13].decode()
values[17] = res1_2_1_1_1[14].decode()
values[18] = res1_2_1_1_1[15].decode()
values[19] = res4_1_1_1.decode()
values[20] = res5_1_1_1.decode()
values[21] = res6_1_1_1.decode()
sql="INSERT INTO lmsensors VALUES" + "('" + values[0]+"',"+values[1]+","+values[2]+","+values[3]+","+values[4]+","+values[5]+","+values[6]+","+values[7]+","+values[8]+","+values[9]+","+values[10]+","+values[11]+","+values[12]+","+values[13]+","+values[14]+","+values[15]+","+values[16]+","+values[17]+","+values[18]+");"
c.execute(sql)
sql2="INSERT INTO GPU VALUES" + "('" + values[0]+"',"+values[19]+","+values[20]+","+values[21]+");"
c.execute(sql2)
'''
values[0] = datetime.datetime.now()
res2 = subprocess.check_output(args2)
print res2
values[0] = datetime.datetime.now()
res3 = subprocess.check_output(args3)
print res3
sql="INSERT INTO usages VALUES" + "('" + values[0]+"',"+values[19]+","+values[20]+","+values[21]+","+values[22]+","+values[23]+","+values[24]+","+values[25]+");"
c.execute(sql)
'''
con.commit()
time.sleep(3)
c.close()
con.close()