-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_app.py
130 lines (99 loc) · 3.37 KB
/
flask_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask, jsonify, request, render_template
import datetime
from MyLib import *
app = Flask(__name__)
#############################
# Action
#############################
@app.route('/', methods = ["GET"])
def index():
req=request.args
last_number = req.get("last", "30")
return render_template("index.html", version=version, last_number=last_number)
@app.route('/show/<ip>', methods = ["GET"])
def show(ip):
# 获取参数
req=request.args
last_number = req.get("last", "30")
print("last_number=%s" % last_number)
return render_template("show.html", host_ip=ip, last_number=last_number)
#############################
# API section
#############################
@app.route('/api/')
def hello():
html="""
<p><a href="/">Go to Home page</a></p>
<h2>Help</h2>
<p class="light">http://10.10.117.156:8071/</p>
<p class="light">http://10.10.117.156:8071/?last=10</p>
"""
return html + "For json data, pls visit: http://10.10.117.156:%s/api_usage_data" % settings.get("sys", "port")
@app.route('/api/ip_list', methods=['GET'])
def get_ip_list():
sql = "select distinct(host_ip) FROM system_usage;"
results=db_query(sql)
return jsonify(results)
@app.route('/api/usage_data', methods=['GET'])
def get_latest_data():
"""
默认显示最新的30条信息
"""
# 获取查询参数
start_time = request.args.get('start')
end_time = request.args.get('end')
host_ip = request.args.get('ip', "")
last_number = int( request.args.get("last", "30") )
if start_time and end_time:
# 使用用户提供的时间段
query_start = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
query_end = datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S')
else:
# 默认使用过去3小时
#end_time = datetime.datetime.now()
#start_time = end_time - datetime.timedelta(hours=3)
#query_start = start_time
#query_end = end_time
query_start=datetime.datetime.now()
query_end=query_start - datetime.timedelta(hours=3)
# 查询指定时间段的数据
#query = "SELECT timestamp, cpu_usage, memory_usage FROM system_usage WHERE timestamp BETWEEN %s AND %s"
# 查询指定时间段的数据,并添加整数形式的时间戳
sql = """
SELECT
timestamp,
UNIX_TIMESTAMP(timestamp) AS unix_timestamp,
cpu_usage,
memory_usage,
host_ip,
hostname
FROM
system_usage
WHERE
1
"""
if start_time and end_time:
sql += " AND (timestamp BETWEEN '%s' AND '%s')" % (query_start, query_end)
if host_ip:
sql += " AND (host_ip = '%s')" % host_ip
sql += " ORDER BY timestamp DESC"
if start_time and end_time:
pass
else:
sql += " limit %d;" % last_number
#print(sql)
results=db_query(sql)
# 格式化为字典
data = []
for row in results:
data.append({
'timestamp': row[0].strftime('%Y-%m-%d %H:%M:%S'),
'unix_timestamp': row[1], # 整数形式的时间戳
'cpu_usage': row[2],
'memory_usage': row[3],
'host_ip': row[4],
'hostname': row[5]
})
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(settings.get("sys", "port")) )