简易的内存监控系统
本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客
文章源地址,还可以看到具体的代码,喜欢请加个星星
腾讯视频链接录制中间网出问题了,重启了一下,所以有两部分
视频1
视频2
本文的目的在于,尽可能用简单的代码,让大家了解内存监控的原理
主题思路
获取内存信息
存储信息
展现
后续扩展
加主机名,monitor部署在多台机器,不直接插数据库
通过http请求的方式,一台机器起flask专门存数据monitor
思路图
其实所有的监控项,包括内存数据,都是从文件中读取的,大家执行以下 cat /proc/meminfo就可以看到关于内存的信息,我们关注的是前四行,总内存,空闲内存,缓冲和缓存大小
计算内存占用量公式:
(总内存-空闲内存-缓冲-缓存)/1024Mb
代码呼之欲出 monitor.py
用with打开文件,可以自动关闭,比直接open优雅那么一丢丢
def getMem():
with open("/proc/meminfo") as f:
total = int(f.readline().split()[1])
free = int(f.readline().split()[1])
buffers = int(f.readline().split()[1])
cache = int(f.readline().split()[1])
mem_use = total-free-buffers-cache
print mem_use/1024
while True:
time.sleep(1)
getMem()
执行文件 python monitor.py,每一秒打印一条内存信息
[woniu@teach memory]$ python mointor.py
2920
2919
2919
2919
2919
我们可以写个很搓的测试代码,占用一点内存,看看数据会不会变
执行下面代码,能看到内存使用量明显多了几M
# test.py
s = "akdsakjhdjkashdjkhasjkdhasjkdhkjashdaskjhfoopnnm,ioqouiew"*100000
for i in s:
for j in s:
s.count(j)
获取内存数据done!
第二步存储数据库 我们选用mysql新建表格,我们需要两个字段,内存和时间 sql呼之欲出,简单粗暴
create memory(memory int,time int)
我们的 monitor.py就不能只打印内存信息了,要存储数据库啦,引入mysql模块,代码如下
import time
import MySQLdb as mysql
db = mysql.connect(user="reboot",passwd="reboot123",db="memory",host="localhost")
db.autocommit(True)
cur = db.cursor()
def getMem():
with open("/proc/meminfo") as f:
total = int(f.readline().split()[1])
free = int(f.readline().split()[1])
buffers = int(f.readline().split()[1])
cache = int(f.readline().split()[1])
mem_use = total-free-buffers-cache
t = int(time.time())
sql = "insert into memory (memory,time) value (%s,%s)"%(mem_use/1024,t)
cur.execute(sql)
print mem_use/1024
#print "ok"
while True:
time.sleep(1)
getMem()
比之前的多了拼接sql和执行的步骤,具体过程见视频,大家到数据库里执行一下下面的sql,就能看到我们辛辛苦苦获取的内存数据啦
select * from memory
我们的数据库里数据越来越多,怎么展示呢
我们需要flask
我们看下文件结构
.
├── flask_web.py web后端代码
├── mointor.py 监控数据获取
├── static 静态文件,第三方图表库
│ ├── exporting.js
│ ├── highstock.js
│ └── jquery.js
├── templates
│ └── index.html 展示前端页面
└── test.py 占用内存的测试代码
flask_web就是我们的web服务代码,template下面的html,就是前端展示的文件,static下面是第三方库
flask_web的代码如下
提供两个路由
根目录渲染文件index.html
/data路由去数据库插数据,返回json,供画图使用
from flask import Flask,render_template,request
import MySQLdb as mysql
con = mysql.connect(user="reboot",passwd="reboot123",host="localhost",db="memory")
con.autocommit(True)
cur = con.cursor()
app = Flask(__name__)
import json
@app.route("/")
def index():
return render_template("index.html")
@app.route("/data")
def data():
sql = "select * from memory"
cur.execute(sql)
arr = []
for i in cur.fetchall():
arr.append([i[1]*1000,i[0]])
return json.dumps(arr)
if __name__=="__main__":
app.run(host="0.0.0.0",port=9092,debug=True)
前端index.html
highstock的demo页面,copy过来,具体过程见视频
<span class="javascript"><span class="hljs-number">51</span>reboot</span>
hello world
具体观察数据结构的过程,见视频和demo链接,我们做的 就是把数据库里的数据,拼接成前端画图需要的数据,展现出来
这时候前端就能看到图表啦
我们并不仅限于此,如果想实时的看到内存,应该怎么搞呢查询数据时候增加一个时间戳当限制条件,再次查询时,只返回两次查询之间的增量数据
前端动态添加增量结点数据到图表中
代码呼之欲出
python
tmp_time = 0
@app.route("/data")
def data():
global tmp_time
if tmp_time>0:
sql = "select * from memory where time>%s" % (tmp_time/1000)
else:
sql = "select * from memory"
cur.execute(sql)
arr = []
for i in cur.fetchall():
arr.append([i[1]*1000,i[0]])
if len(arr)>0:
tmp_time = arr[-1][0]
return json.dumps(arr)
前端,3秒查一次增量数据
$.getJSON("/data", function (data) {
// Create the chart
$("#container").highcharts("StockChart", {
chart:{
events:{
load:function(){
var series = this.series[0]
setInterval(function(){
$.getJSON("/data",function(res){
$.each(res,function(i,v){
series.addPoint(v)
})
})
},3000)
}
}
},
rangeSelector : {
selected : 1
},
title : {
text : "AAPL Stock Price"
},
series : [{
name : "AAPL",
data : data,
tooltip: {
valueDecimals: 2
}
}]
});
});
done!两个文件都搞定,double kill!
效果
最终代码直接下载那个木看也行
监控文件monitor.py
import time
import MySQLdb as mysql
db = mysql.connect(user="reboot",passwd="reboot123",db="memory",host="localhost")
db.autocommit(True)
cur = db.cursor()
def getMem():
f = open("/proc/meminfo")
total = int(f.readline().split()[1])
free = int(f.readline().split()[1])
buffers = int(f.readline().split()[1])
cache = int(f.readline().split()[1])
mem_use = total-free-buffers-cache
t = int(time.time())
sql = "insert into memory (memory,time) value (%s,%s)"%(mem_use/1024,t)
cur.execute(sql)
print mem_use/1024
#print "ok"
while True:
time.sleep(1)
getMem()
flask
from flask import Flask,render_template,request
import MySQLdb as mysql
con = mysql.connect(user="reboot",passwd="reboot123",host="localhost",db="memory")
con.autocommit(True)
cur = con.cursor()
app = Flask(__name__)
import json
@app.route("/")
def index():
return render_template("index.html")
tmp_time = 0
@app.route("/data")
def data():
global tmp_time
if tmp_time>0:
sql = "select * from memory where time>%s" % (tmp_time/1000)
else:
sql = "select * from memory"
cur.execute(sql)
arr = []
for i in cur.fetchall():
arr.append([i[1]*1000,i[0]])
if len(arr)>0:
tmp_time = arr[-1][0]
return json.dumps(arr)
if __name__=="__main__":
app.run(host="0.0.0.0",port=9092,debug=True)
前端
<span class="javascript"><span class="hljs-number">51</span>reboot</span>
hello world
代码没有特别注意细节,希望大家喜欢。
欢迎大家关注个人公共号,高品质运维开发
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/91560.html
简易的内存监控系统 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 文章源地址,还可以看到具体的代码,喜欢请加个星星 腾讯视频链接 录制中间网出问题了,重启了一下,所以有两部分 视频1 视频2 本文的目的在于,尽可能用简单的代码,让大家了解内存监控的原理主题思路 获取内存信息 存储信息 展现 后续扩展 加主机名,monitor部署在多台机器,不直接插...
摘要:背景近邻算法的概述近邻算法的简介近邻算法是属于一个非常有效且易于掌握的机器学习算法,简单的说就是采用测量不同特征值之间距离的方法对数据进行分类的一个算法。完美的分类器的错误率为,而最差的分类器的错误率则为。 1 背景 1.1 k近邻算法的概述 (1)k近邻算法的简介 k-近邻算法是属于一个非...
摘要:性能测试除了需要监控内存占用流量等,还需要获取的电量数据,测试在可接受范围内,避免出现过度消耗电量的现象。这一栏显示了不同的充电方式对电量使用的影响。 本文由作者张迎贞授权网易云社区发布。 APP性能测试除了需要监控PCU、内存占用、流量等,还需要获取APP的电量数据,测试在可接受范围内,避免APP出现过度消耗电量的现象。手机有很多硬件模块:CPU,蓝牙,GPS,显示屏,Wifi,射频...
摘要:在本次课程中,着重讲解的是传统的机器学习技术及各种算法。回归对连续型数据进行预测趋势预测等除了分类之外,数据挖掘技术和机器学习技术还有一个非常经典的场景回归。 摘要: 什么是数据挖掘?什么是机器学习?又如何进行Python数据预处理?本文将带领大家一同了解数据挖掘和机器学习技术,通过淘宝商品案例进行数据预处理实战,通过鸢尾花案例介绍各种分类算法。 课程主讲简介:韦玮,企业家,资深IT领...
阅读 3249·2021-11-10 11:35
阅读 1335·2019-08-30 13:20
阅读 1158·2019-08-29 16:18
阅读 2177·2019-08-26 13:54
阅读 2194·2019-08-26 13:50
阅读 993·2019-08-26 13:39
阅读 2529·2019-08-26 12:08
阅读 1991·2019-08-26 10:37