python搭建系统监控平台(psutil)

2020-10-30  本文已影响0人  阿尼奥赛哟

psutil模块参数详解:https://www.cnblogs.com/iamjianghao/p/11894623.html

1. 读取服务器各项指标参数,存入数据库

import psutil

import time

import pymysql

db= pymysql.connect(user="root",passwd="Root@1997",db="test",host="172.16.22.196",port=3306)

db.autocommit(True)

cur= db.cursor()

def getinfo():

    mem= psutil.virtual_memory()

    memtotal= mem.total

    memfree= mem.free

    mempercent= mem.percent

    memused= mem.used

    cpu= psutil.cpu_percent(1)

return memtotal, memfree, memused, mempercent, cpu

if __name__== "__main__":

    while True:

        try:

            memtotal, memfree, memused, mempercent, cpu= getinfo()

           t=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

           sql= "insert into test (mem_free,mem_usage,mem_total,mempercent,cpu,time) value (%f,%f,%f,%s,%s,'%s')" % (

           memfree/1073741824,memused/1073741824,memtotal/1073741824, mempercent, cpu, t)

           cur.execute(sql)

           time.sleep(10)

except Exception as e:

            print(e)

结果:

上一篇 下一篇

猜你喜欢

热点阅读