[py]监控内存并出图

监控内存出图

先将内存数据搞到数据库

已使用内存算法

used = int(total) - int(free) - int(butffers) - int(cache)

pymysql模块使用


db = ms.connect(host="localhost", user="root", password="123456", db="mem")
db.autocommit(True)
cur = db.cursor()

cur.execute(sql)

pymysql入库脚本

from time import sleep, time
import pymysql as ms

db = ms.connect(host="localhost", user="root", password="123456", db="mem")
db.autocommit(True)
cur = db.cursor()


def get_mem():
    with open("/proc/meminfo") as f:
        total = f.readline().split()[1]
        free = f.readline().split()[1]
        f.readline()
        butffers = f.readline().split()[1]
        cache = f.readline().split()[1]
    used = int(total) - int(free) - int(butffers) - int(cache)
    sql = 'insert into mem_used values (%d,%d)' % (used / 1024, time())
    cur.execute(sql)
    return int(used / 1024)


while True:
    mem = get_mem()
    print(mem)
    sleep(1)

posted @ 2018-01-24 11:10  mmaotai  阅读(200)  评论(0编辑  收藏  举报