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/ 实时监控内存的活动情况。

效果图

本文python实现内存监控系统到此结束。岁月,是一首诗,一首蕴含丰富哲理的诗,岁月是一峰骆驼,驮着无数人的梦想。谢谢大家支持!

标签: 监控系统 python