本文实例为大家分享了python实现内存监控系统的具体代码,供大家参考,具体内容如下
思路:通过系统命令或操作系统文件获取到内存信息(linux 内存信息存在/proc/meminfo文件中,mac os 通过命令vm_stat命令可以查看)
并将获取到信息保存到数据库中,通过web将数据实时的展示出来.(获取数据—展示数据)
1、后台数据采集(获取数据)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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数据中,新建表:
1
2
3
4
5
6
7
8
9
10
11
|
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实时展示折线图数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
from 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
< html lang = "en" > < head > < meta charset = "UTF-8" > < title >内存监控</ title > < script src = "http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js" ></ script > < script src = "http://cdn.hcharts.cn/highstock/highstock.js" ></ script > < script src = "http://cdn.hcharts.cn/highcharts/modules/exporting.js" ></ script > </ head > < body > < div id = "container" style = "min-width:400px;height:400px" ></ div > </ body > < script type = "text/javascript" > $(function () { $.getJSON('/data', function (data) { // Create the chart $('#container').highcharts('StockChart', { chart: { events: { load: function () { var chart = $('#container').highcharts(); var series = chart.series[0]; //隔1秒,请求一次/data,实时获取内存信息 setInterval(function () { $.getJSON("/data", function (res) { $.each(res, function (i, v) { series.addPoint(v); }); }); }, 1000); } } }, rangeSelector : { selected : 1 }, title : { text : 'AAPL Stock Price' }, series : [{ name : 'AAPL', data : data, tooltip: { valueDecimals: 2 } }] }); }); }); </ script > </ html > |
done.
运行后台数据采集,运行前台web,通过http://localhost:8888/ 实时监控内存的活动情况。
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/supersanya/article/details/52450125