您的位置:首页 > 数据库 > SQL

flask+mysql+highcharts监控内存

2017-06-09 17:19 99 查看

agent.py

# -*- coding:utf-8 -*-
import time
import pymysql

conn = pymysql.connect(host='127.0.0.1',user='root',password='osyunwei',db='memory')
conn.autocommit(True)
cur = conn.cursor()

def getMem():
with open('/proc/meminfo') as f:
total = int(f.readline().split()[1])
free = int(f.readline().split()[1])
available = f.readline()
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) values(%s,%s)' %(mem_use/1024,t)
cur.execute(sql)

print mem_use / 1024

if __name__ =="__main__":
while True:
time.sleep(3)
getMem()


monitor.py

# -*- coding:utf-8 -*-

from flask import Flask,render_template,request
import pymysql
import json

conn = pymysql.connect(host='127.0.0.1',user='root',password='osyunwei',db='memory')
conn.autocommit(True)
cur = conn.cursor()

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/data/')
def data():
sql = 'select memory,time 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=8888,debug=True)


index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>内存监控</title>
</head>
<body>

<div id="container" style="min-width:400px;height:400px;"></div>

<script src="/static/jquery.js"></script>
<script src="/static/highstock.js"></script>
<script src="/static/exporting.js"></script>
<script>
$(function () {

Highcharts.setOptions({
global: {
useUTC: false
}
});
$.getJSON('/data', function (data) {

$('#container').highcharts('StockChart', {
rangeSelector : {
selected : 1
},
title : {
text : 'memory data'
},
series : [{
name : 'memory',
data : data,

}]
});
});
});
</script>

</body>
</html>


目录结构

.
├── agent.py
├── monitor.py
├── static
│   ├── exporting.js
│   ├── highstock.js
│   └── jquery.js
└── templates
└── index.html


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: