前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python获取系统信息的代码

Python获取系统信息的代码

原创
作者头像
大师级码师
修改2021-11-02 13:33:03
5250
修改2021-11-02 13:33:03
举报
文章被收录于专栏:大师级码师大师级码师
代码语言:javascript
复制
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("%s\n" % 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)</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档