flask+mysql+highcharts监控内存

来源:互联网 发布:mac app store创建id 编辑:程序博客网 时间:2024/05/23 02:03

agent.py

# -*- coding:utf-8 -*-import timeimport pymysqlconn = 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 / 1024if __name__ =="__main__":    while True:        time.sleep(3)        getMem()

monitor.py

# -*- coding:utf-8 -*-from flask import Flask,render_template,requestimport pymysqlimport jsonconn = 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

这里写图片描述

原创粉丝点击