Python implements memory monitoring system:

Obtain memory information from system commands or operating system files

(The Linux memory information is stored in the /proc/meminfo file, which can be viewed by running the vm_stat command on the MAC OS.)

Save the obtained information in the database, and display the data in real time through the Web.(Obtain data – display data)

1. Background data collection (data acquisition)

Import subprocess import re import MySQLdb as mysql import time import socket mysql.connect(user="root", passwd="123456",host="localhost", db="EBANK", Char ="utf8") db.autocommit(True) cur = db.cursor() "" def processesUseMeminfo(): char ="utf8") db. 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 """ 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 """ erval = 0 def execute(): Update memory activity information global erval try: # 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 'Execute data.', del_SQL cur.execute(del_SQL) erval = 0 except Exception ,message: print 'error ',message #pass finally: Pass "update" #TODO #rssTotal = processesUseMeminfo() time.sleep(1) execute() print 'none.'Copy the code

MySQL > select * from ‘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;
Copy the code

2. The front web adopts flask application framework to display line chart data in real time through 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)
Copy the code

Create a new monitor.html

< HTML lang="en"> <head> <meta charset="UTF-8"> <title> Memory monitoring </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]; // Request once every 1 second /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>Copy the code

done.

Run background data collection, run the front web, through http://localhost:8888/ real-time monitoring of memory activities.