加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python获取系统信息的代码

发布时间:2020-12-17 17:17:56 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import os,inspect,socket,time,pymysqlnum = 0class Mysql: def __init__(self,host,user,password,db): self.cnn = pymysql.connect(host=host,user

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import os,inspect,socket,time,pymysql
num = 0
class Mysql:
    def __init__(self,host,user,password,db):
        self.cnn = pymysql.connect(host=host,user=user,passwd=password,db=db,charset='utf8')
        self.cur= self.cnn.cursor()
 
    def run(self,sql):
        self.cur.execute(sql)
 
    def cmd(self,sql):
        self.cur.execute(sql)
        return self.cur.fetchall()
 
    def commit(self):
        self.cnn.commit()
 
    def close(self):
        self.cur.close()
        self.cnn.close()
 
class mon:
    def __init__(self):
        self.db_file='./db.json'
        self.data={}
 
    def getAmber(self):
        global num
        num+=1
        return num
 
    def getDate(self):
        return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
 
    def getProcess(self):
        return os.popen('ps -ef |wc -l').readlines()[0].split()[0]
 
    def getDisk(self):
        return os.popen("df -m |grep '/$'").readlines()[0].split()
 
    def getMem(self):
        return os.popen('free -m').readlines()[1].split()[1:4]
 
    def getSwap(self):
        return os.popen('free -m').readlines()[3].split()[1:]
 
    def getLoad(self):
        return os.popen('uptime').readlines()[0].split()[-3:]
 
    def getHost(self):
        return socket.gethostname()
 
    def getUser(self):
        return os.popen('uptime').readlines()[0].split()[3]
 
    def getRuntime(self):
        return os.popen('uptime').readlines()[0].split()[2]
 
    def getSystem(self):
        return os.popen('cat /etc/redhat-release').readlines()[0].split('n')[0]
 
    def getKerner(self):
        return os.popen('uname -r').readlines()[0].split('n')[0]
 
 
    def run(self):
        for fun in inspect.getmembers(self,predicate=inspect.ismethod):
            if fun[0][:3] == 'get':
                #print fun[1]()
                self.data[fun[0][3:]] = fun[1]()
        #print self.data
        return self.data
        #file(self.db_file,'a').write("%sn" % self.data)
        time.sleep(15)
if __name__ == "__main__":
    mysql=Mysql('192.168.0.58','root','123456','host')
    while 1:
        res= mon().run()
        memTotal=int(res['Mem'][0])
        diskTotal=int(res['Disk'][0])
        memFree=int(res['Mem'][2])
        diskFree=int(res['Disk'][2])
        cpuPercent=int(res['Process'])
        ip=os.popen("ifconfig |grep  'Bcast' |awk -F: '{print $2}'").readlines()[0].split()[0]
        print('-------------------',ip)
        ress=mysql.cmd('select * from hostinfo where ip="%s"'%ip)
        ress=list(ress[0])
        for i in ress[3:]:
            lists=eval(i)
            print(lists)
            lists['data'].pop(0)
            lists['data'].append(eval(lists['name']))
            sqls='update hostinfo set %s = "%s" where ip="%s"'%(str(lists['name']),lists,ip)
            mysql.cmd(sqls)
        mysql.commit()
        print('------------------------------------------------')
        time.sleep(30)

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读