• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python实现内存监控系统

python 搞代码 4年前 (2022-01-08) 34次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了python实现内存监控系统,通过系统命令或操作系统文件获取到内存信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现内存监控系统的具体代码,供大家参考,具体内容如下

思路:通过系统命令或操作系统文件获取到内存信息(linux 内存信息存在/proc/meminfo文件中,mac os 通过命令vm_stat命令可以查看)

并将获取到信息保存到数据库中,通过web将数据实时的展示出来.(获取数据―展示数据)

1、后台数据采集(获取数据)

 import subprocess import re import MySQLdb as mysql import time import socket #获取mysql数据游标,通过游标操作数据库 db = mysql.connect(user="root", passwd="123456",host="localhost", db="EBANK", charset="utf8") db.autocommit(True) cur = db.cursor() """ Mac系统各应用程序占内存信息 """ def processesUseMeminfo(): ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0] processLines = ps.split('\n') print processLines sep = re.compile('[\s]+') rssTotal = 0 # kB for row in range(1,len(processLines)): rowText = processLines[row].strip() rowElements = sep.split(rowText) try: rss = float(rowElements[0]) * 1024 except: rss = 0 # ignore... rssTotal += rss return rssTotal """ Mac内存活动信息 """ def processVM(): vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0] vmLines = vm.split('\n') sep = re.compile(':[\s]+') vmStats = {} for row in range(1,len(vmLines)-2): rowText = vmLines[row].strip() rowElements = sep.split(rowText) vmStats[(rowElements[0])] = int(rowElements[1].strip('\.'))/1024 return vmStats """ 执行更新数据库中内存信息,供web展示内存的实时数据 """ erval = 0 def execute(): '''更新内存活动信息''' global erval try: ip = socket.gethostbyname(socket.gethostname()) #获取本机ip #ip = '10.21.8.10' vmStats = processVM() wired = vmStats['Pages wired down'] active = vmStats['Pages active'] free = vmStats['Pages free'] inactive = vmStats['Pages inactive'] t = int(time.time()) sql = "insert into stat(host,mem_free,mem_usage,mem_total,load_avg,time) VALUES ('%s','%d','%d','%d','%d','%d')"\ %(ip,wired,active,free,inactive,t) print sql cur.execute(sql) erval += 1 if erval > 50: del_sql = "delete from stat where time <%d "%t print '执行数据清理.',del_sql cur.execute(del_sql) erval = 0 except Exception , message : print '获取内存信息异常:',message #pass finally: pass '''更新''' #TODO #rssTotal = processesUseMeminfo() #死循环不停的读取内存,每一秒钟插入一条新的内存信息 while True: time.sleep(1) execute() print 'none.'

获取到数据保存到MySQL数据中,新建表:

 CREATE TABLE `stat` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `host` varchar(256) DEFAULT NULL, `mem_free` int(11) DEFAULT NULL, `mem_usage` int(11) DEFAULT NULL, `mem_total` int(11) DEFAULT NULL, `load_avg` varchar(128) DEFAULT NULL, `time` bigint(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `host` (`host`(255)) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

2、前台web采用flask应用框架,通过highstock实时展示折线图数据

 from<i style="color:transparent">来源gaodai$ma#com搞$代*码*网</i> flask import Flask, request, render_template import json import MySQLdb as mysql app = Flask(__name__) db = mysql.connect(user="root", passwd="123456",host="localhost", db="EBANK", charset="utf8") db.autocommit(True) cur = db.cursor() @app.route("/") def index(): return render_template("monitor.html") tmp_time = 0 @app.route("/data") def getdata(): '''第一次查询全量数据,后面只查询增量数据''' global tmp_time if tmp_time > 0 : sql = "select time,mem_free from stat where time >%s" %(tmp_time) else: sql = "select time,mem_free from stat" cur.execute(sql) datas = [] for i in cur.fetchall(): datas.append([i[0], i[1]]) if len(datas) > 0 : tmp_time = datas[-1][0] return json.dumps(datas) if __name__ == "__main__": app.run(host='0.0.0.0',port=8888,debug=True)

新建一个monitor.html

   <title>内存监控</title> <div id="container" style="min-width:400px;height:400px"></div>

done.

运行后台数据采集,运行前台web,通过http://localhost:8888/ 实时监控内存的活动情况。

效果图

以上就是python实现内存监控系统的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python实现内存监控系统

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址