flask+mysql+highcharts监控内存

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

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值