V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yaoqyqx
V2EX  ›  问与答

求大佬看下日本同学给我的代码

  •  
  •   yaoqyqx · 2019-11-16 09:50:14 +08:00 · 1113 次点击
    这是一个创建于 1833 天前的主题,其中的信息可能已经有所发展或是发生改变。
    是从服务器获取各种参数然后传回数据库,但我不是很懂 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()
    3 条回复    2019-11-18 12:29:39 +08:00
    wysnylc
        1
    wysnylc  
       2019-11-16 10:25:44 +08:00
    垃圾代码不分国界
    vinko
        2
    vinko  
       2019-11-18 08:22:53 +08:00 via Android
    这是什么东西?
    yaoqyqx
        3
    yaoqyqx  
    OP
       2019-11-18 12:29:39 +08:00
    @vinko
    我想做一个获取服务器的各种信息并且导入 SQL,但我不太能看懂他的代码,老哥有什么好办法吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1467 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:18 · PVG 01:18 · LAX 09:18 · JFK 12:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.