1. 程式人生 > >基於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 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

<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/ 實時監控記憶體的活動情況。
效果圖