#coding=utf-8
import MySQLdb as mysql
import json
from app import RequestUtil
from flask import Flask, request, render_template
app = Flask(__name__)
db = mysql.connect(user="root", passwd="", db="monitor", charset="utf8", host="localhost")
db.autocommit(True)
c = db.cursor()
t1_time = 0
t2_time = 0
t3_time = 0
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
data = request.json
try:
sql = "INSERT INTO `stat` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`) VALUES('%s', '%d', '%d', '%d', '%s', '%d')" % (data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], int(data['Time']))
ret = c.execute(sql)
except mysql.IntegrityError:
pass
return "OK"
else:
try:
sql = "select * from host order by add_time desc"
c.execute(sql)
host_all = []
host_all = c.fetchall()
except:
pass
return render_template('index.html', host_all = host_all)
@app.route('/api/add', methods=['GET', 'POST'])
def add_host():
add_time = RequestUtil.now_datetime()
host = RequestUtil.get_parameter(request, 'host', '')
if host:
sql = "insert into host (ip, add_time) values ('%s', '%s')" % (host,add_time)
c.execute(sql)
# 提交到数据库执行
insert_id = c.lastrowid
if insert_id:
rst = {'success': 1, 'data': ''}
else:
rst = {'success': 0, 'data': 'add host error'}
return RequestUtil.object_2_dict(rst)
@app.route('/api/del', methods=['GET', 'POST'])
def del_host():
ip = RequestUtil.get_parameter(request, 'id', '')
if ip:
sql = "delete from host where id='%s'" %(id)
c.execute(sql)
delete_id = c.lastrowid
if delete_id:
rst = {'success': 1, 'data': ''}
else:
rst = {'success': 0, 'data': 'del redis error'}
return RequestUtil.object_2_dict(rst)
@app.route('/monitor/load', methods=['GET'])
def load():
ip = RequestUtil.get_parameter(request, 'ip', '')
return render_template('top.html',ip=ip)
@app.route('/monitor/mem_free', methods=['GET'])
def free():
ip = RequestUtil.get_parameter(request, 'ip', '')
return render_template('mem_free.html',ip=ip)
@app.route('/monitor/mem_used', methods=['GET'])
def used():
ip = RequestUtil.get_parameter(request, 'ip', '')
return render_template('mem_used.html',ip=ip)
@app.route('/test', methods=['GET'])
def test():
return flask.render_template("test.html")
@app.route('/test1', methods=['GET', 'POST'])
def test():
if request.method == 'POST':
print request.form.get('id')
return "ok"
else:
return request.form.get('abc')
@app.route('/data1')
def data1():
ip = RequestUtil.get_parameter(request, 'ip', '')
global t1_time
if t1_time>0:
sql = "select mem_usage,time from stat where time>%s and host='%s'" % (t1_time/1000,ip)
else:
sql = "select mem_usage,time from stat where host='%s'" %(ip)
c.execute(sql)
arr = []
for i in c.fetchall():
arr.append([i[1]*1000,i[0]])
if len(arr)>0:
t1_time = arr[-1][0]
return json.dumps(arr)
@app.route('/data2')
def data2():
ip = RequestUtil.get_parameter(request, 'ip', '')
global t2_time
if t2_time>0:
sql = "select mem_free,time from stat where time>%s and host='%s'" % (t2_time/1000,ip)
else:
sql = "select mem_free,time from stat where host='%s'" %(ip)
c.execute(sql)
arr = []
for i in c.fetchall():
arr.append([i[1]*1000,i[0]])
if len(arr)>0:
t2_time = arr[-1][0]
return json.dumps(arr)
@app.route('/data3')
def data3():
ip = RequestUtil.get_parameter(request, 'ip', '')
name = RequestUtil.get_parameter(request, 'name', '')
global t3_time
if t3_time>0:
sql = "select load_avg,time from stat where time>%s and host='%s'" % (t3_time/1000,ip)
else:
sql = "select load_avg,time from stat where host='%s'" %(ip)
c.execute(sql)
arr = []
brr = []
drr = []
for i in c.fetchall():
a = float(i[0].split(',')[0])
b = float(i[0].split(',')[1])
d = float(i[0].split(',')[2])
arr.append([i[1]*1000,a])
brr.append([i[1]*1000,b])
drr.append([i[1]*1000,d])
if len(arr)>0:
#t_time = arr[-1][0]
if name == "avg1":
return json.dumps(arr)
elif name == "avg5":
return json.dumps(brr)
else:
return json.dumps(drr)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True)
没有合适的资源?快使用搜索试试~ 我知道了~
python+flask做的简单的监控系统
共49个文件
js:31个
py:6个
html:6个
5星 · 超过95%的资源 需积分: 31 177 下载量 159 浏览量
2016-04-12
10:47:51
上传
评论 11
收藏 1.37MB ZIP 举报
温馨提示
用python+flask做的简单的监控系统,能对实时的显示系统的一些状态
资源推荐
资源详情
资源评论
收起资源包目录
monitor.zip (49个子文件)
monitor
flask_test.py 1KB
app
__init__.pyc 120B
RequestUtil.pyc 2KB
__init__.py 0B
RequestUtil.py 1KB
sql_unitl.py 2KB
templates
mem_free.html 392B
test.html 2KB
mem_used.html 392B
index.html 4KB
top.html 3KB
mon.html 488B
monitor.sql 4.22MB
static
highstock.js 206KB
jquery.js 278KB
jquery.min.js 90KB
memfree.js 2KB
highcharts.js 166KB
css
style.css 2KB
memused.js 2KB
hostadd.js 701B
chart
md5.js 6KB
jquery.min.js 90KB
redis_monitor.js 8KB
simpleStorage.js 11KB
echarts
echarts.js 355KB
.DS_Store 6KB
chart
funnel.js 9KB
map.js 441KB
scatter.js 29KB
line.js 13KB
tree.js 11KB
eventRiver.js 6KB
bar.js 10KB
treemap.js 9KB
radar.js 14KB
k.js 5KB
venn.js 9KB
heatmap.js 5KB
pie.js 12KB
force.js 29KB
wordCloud.js 35KB
gauge.js 9KB
chord.js 19KB
echarts-all.js 950KB
favicon.ico 1KB
exporting.js 8KB
monitor.py 2KB
flask_web.py 5KB
共 49 条
- 1
资源评论
- 我是一个大猪头2019-05-06拿来做测试,基本可用
- freelancer_ex2017-09-28基本没用到flask什么内容,前端也是自己画的,不是flask-bootstrap
唐小麻
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功