提交 7b2111885cbed6083385f8669a106246c6d50802

作者 qianyingz
1 个父辈 23a19983

补充提交

1 -from datetime import datetime, timedelta  
2 -  
3 -from sqlalchemy.sql.functions import func  
4 -from .models import MonitorLog, db  
5 -from sqlalchemy import and_  
6 -from app.util.component.ApiTemplate import ApiTemplate  
7 -  
8 -  
9 -class Api(ApiTemplate):  
10 - api_name = "统计指标"  
11 -  
12 - def process(self):  
13 -  
14 - # 返回结果  
15 - res = {}  
16 - res["data"] = {}  
17 - logs = []  
18 - try:  
19 - server = self.para.get("server") # server  
20 - # metrics_type = self.para.get("metrics_type")  
21 - grain = self.para.get("grain")  
22 - count = int(self.para.get("count"))  
23 - start_time = self.para.get("start")  
24 - to_time = self.para.get("to")  
25 -  
26 - cur_now = datetime.now()  
27 - start_datetime = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")  
28 - end_datetime = datetime.strptime(to_time, "%Y-%m-%d %H:%M:%S")  
29 -  
30 - if grain == "day":  
31 - # 粒度是一天  
32 - format = "%Y-%m-%d"  
33 - logs = db.session.query(MonitorLog.date_stamp.label("key"), func.avg(MonitorLog.cpu_usage).label("cpu_usage"),  
34 - func.avg(MonitorLog.total_mem).label(  
35 - "total_mem"),  
36 - func.avg(MonitorLog.available_mem).label(  
37 - "available_mem"),  
38 - func.avg(MonitorLog.used_mem).label(  
39 - "used_mem"),  
40 - func.avg(MonitorLog.disk).label(  
41 - "disk"),  
42 - func.avg(MonitorLog.disk_usage).label(  
43 - "disk_usage"),  
44 - func.avg(MonitorLog.net_recv).label(  
45 - "net_recv"),  
46 - func.avg(MonitorLog.net_send).label("net_send")).filter(and_(  
47 - MonitorLog.date_stamp <= end_datetime.strftime(  
48 - format),  
49 - MonitorLog.date_stamp > start_datetime.strftime(  
50 - format),  
51 - MonitorLog.server == server)).group_by(MonitorLog.date_stamp).order_by(MonitorLog.date_stamp.asc()).all()  
52 -  
53 - keys = []  
54 - keys_map = {}  
55 - while start_datetime <= end_datetime:  
56 - keys.append(self.get_monitor_log(  
57 - start_datetime.strftime('%Y-%m-%d')))  
58 - keys_map[start_datetime.strftime('%Y-%m-%d')] = len(keys)-1  
59 - start_datetime += timedelta(days=1)  
60 -  
61 - res["data"] = self.get_result(logs, keys, keys_map)  
62 - elif grain == "minu_five":  
63 - # 粒度是5分钟  
64 - cur_minu = int(end_datetime.strftime("%M")) % 5  
65 - logs = db.session.query(MonitorLog.hour_stamp.label("hour_stamp"),  
66 - MonitorLog.f_minu_stamp.label(  
67 - "minu_stamp"),  
68 - func.avg(MonitorLog.cpu_usage).label(  
69 - "cpu_usage"),  
70 - func.avg(MonitorLog.total_mem).label(  
71 - "total_mem"),  
72 - func.avg(MonitorLog.available_mem).label(  
73 - "available_mem"),  
74 - func.avg(MonitorLog.used_mem).label(  
75 - "used_mem"),  
76 - func.avg(MonitorLog.disk).label(  
77 - "disk"),  
78 - func.avg(MonitorLog.disk_usage).label(  
79 - "disk_usage"),  
80 - func.avg(MonitorLog.net_recv).label(  
81 - "net_recv"),  
82 - func.avg(MonitorLog.net_send).label("net_send")).filter(and_(  
83 - MonitorLog.time_stamp <= "{}:00".format(  
84 - end_datetime.strftime("%Y-%m-%d %H:%M")),  
85 - MonitorLog.time_stamp >= "{}:00".format(  
86 - start_datetime.strftime("%Y-%m-%d %H:%M")),  
87 - MonitorLog.server == server)).group_by(MonitorLog.date_stamp, MonitorLog.hour_stamp, MonitorLog.f_minu_stamp).order_by(  
88 - MonitorLog.hour_stamp.asc(), MonitorLog.f_minu_stamp.asc()).all()  
89 -  
90 - keys = []  
91 - keys_map = {}  
92 - while start_datetime <= end_datetime:  
93 - key = start_datetime.strftime('%H:%M')  
94 - keys.append(self.get_monitor_log(key))  
95 - keys_map[key] = len(keys)-1  
96 - start_datetime += timedelta(minutes=5)  
97 -  
98 - logs = list(map(lambda log:  
99 - {"key": "%02d:%02d" % (log.hour_stamp, log.minu_stamp*5+cur_minu),  
100 - "cpu_usage": log.cpu_usage, "total_mem": log.total_mem,  
101 - "available_mem": log.available_mem, "used_mem": log.used_mem, "disk": log.disk, "disk_usage": log.disk_usage,  
102 - "net_recv": log.net_recv, "net_send": log.net_send},  
103 - logs))  
104 -  
105 - res["data"] = self.get_result_from_list(logs, keys, keys_map)  
106 - elif grain == "minu_ten":  
107 - # 粒度是10分钟  
108 - cur_minu = int(end_datetime.strftime("%M")) % 10  
109 - logs = db.session.query(MonitorLog.hour_stamp.label("hour_stamp"),  
110 - MonitorLog.d_minu_stamp.label(  
111 - "minu_stamp"),  
112 - func.avg(MonitorLog.cpu_usage).label(  
113 - "cpu_usage"),  
114 - func.avg(MonitorLog.total_mem).label(  
115 - "total_mem"),  
116 - func.avg(MonitorLog.available_mem).label(  
117 - "available_mem"),  
118 - func.avg(MonitorLog.used_mem).label(  
119 - "used_mem"),  
120 - func.avg(MonitorLog.disk).label(  
121 - "disk"),  
122 - func.avg(MonitorLog.disk_usage).label(  
123 - "disk_usage"),  
124 - func.avg(MonitorLog.net_recv).label(  
125 - "net_recv"),  
126 - func.avg(MonitorLog.net_send).label("net_send")).filter(and_(  
127 - MonitorLog.time_stamp <= "{}:00".format(  
128 - end_datetime.strftime("%Y-%m-%d %H:%M")),  
129 - MonitorLog.time_stamp >= "{}:00".format(  
130 - start_datetime.strftime("%Y-%m-%d %H:%M")),  
131 - MonitorLog.server == server)).group_by(MonitorLog.date_stamp,  
132 - MonitorLog.hour_stamp,  
133 - MonitorLog.d_minu_stamp).order_by(MonitorLog.hour_stamp.asc(), MonitorLog.d_minu_stamp.asc()).all()  
134 - keys = []  
135 - keys_map = {}  
136 - while start_datetime <= end_datetime:  
137 - key = start_datetime.strftime('%H:%M')  
138 - keys.append(self.get_monitor_log(key))  
139 - keys_map[key] = len(keys)-1  
140 - start_datetime += timedelta(minutes=10)  
141 -  
142 - logs = list(map(lambda log:  
143 - {"key": "%02d:%02d" % (log.hour_stamp, log.minu_stamp*10+cur_minu),  
144 - "cpu_usage": log.cpu_usage, "total_mem": log.total_mem,  
145 - "available_mem": log.available_mem, "used_mem": log.used_mem, "disk": log.disk, "disk_usage": log.disk_usage,  
146 - "net_recv": log.net_recv, "net_send": log.net_send},  
147 - logs))  
148 -  
149 - res["data"] = self.get_result_from_list(logs, keys, keys_map)  
150 - elif grain == "hour":  
151 - # 粒度是一小时  
152 - logs = db.session.query(MonitorLog.hour_stamp.label("key"), func.avg(MonitorLog.cpu_usage).label("cpu_usage"),  
153 - func.avg(MonitorLog.total_mem).label(  
154 - "total_mem"),  
155 - func.avg(MonitorLog.available_mem).label(  
156 - "available_mem"),  
157 - func.avg(MonitorLog.used_mem).label(  
158 - "used_mem"),  
159 - func.avg(MonitorLog.disk).label(  
160 - "disk"),  
161 - func.avg(MonitorLog.disk_usage).label(  
162 - "disk_usage"),  
163 - func.avg(MonitorLog.net_recv).label(  
164 - "net_recv"),  
165 - func.avg(MonitorLog.net_send).label("net_send")).filter(and_(  
166 - MonitorLog.time_stamp <= "{}:59:59".format(  
167 - end_datetime.strftime("%Y-%m-%d %H")),  
168 - MonitorLog.time_stamp >= "{}:59:59".format(  
169 - start_datetime.strftime("%Y-%m-%d %H")),  
170 - MonitorLog.server == server)).group_by(MonitorLog.hour_stamp).order_by(MonitorLog.hour_stamp.asc()).all()  
171 - keys = []  
172 - keys_map = {}  
173 - while start_datetime <= end_datetime:  
174 - key = int(start_datetime.strftime('%H'))  
175 - keys.append(self.get_monitor_log("%02d时" % key))  
176 - keys_map[str(key)] = len(keys)-1  
177 - start_datetime += timedelta(hours=1)  
178 -  
179 - res["data"] = self.get_result(logs, keys, keys_map)  
180 - else:  
181 - # 按分钟统计  
182 - logs = db.session.query(MonitorLog.hour_stamp.label("hour_stamp"), MonitorLog.minu_stamp.label("minu_stamp"), func.avg(MonitorLog.cpu_usage).label("cpu_usage"),  
183 - func.avg(MonitorLog.total_mem).label(  
184 - "total_mem"),  
185 - func.avg(MonitorLog.available_mem).label(  
186 - "available_mem"),  
187 - func.avg(MonitorLog.used_mem).label("used_mem"),  
188 - func.avg(MonitorLog.disk).label(  
189 - "disk"),  
190 - func.avg(MonitorLog.disk_usage).label(  
191 - "disk_usage"),  
192 - func.avg(MonitorLog.net_recv).label(  
193 - "net_recv"),  
194 - func.avg(MonitorLog.net_send).label("net_send")).filter(and_(  
195 - MonitorLog.time_stamp <= end_datetime.strftime("%Y-%m-%d %H:%M:%S"),  
196 - MonitorLog.time_stamp >= start_datetime.strftime("%Y-%m-%d %H:%M:%S"),  
197 - MonitorLog.server == server)).group_by(MonitorLog.hour_stamp, MonitorLog.minu_stamp).order_by(  
198 - MonitorLog.hour_stamp.asc(), MonitorLog.minu_stamp.asc()).all()  
199 - keys = []  
200 - keys_map = {}  
201 - while start_datetime <= end_datetime:  
202 - key = start_datetime.strftime('%H:%M')  
203 - keys.append(self.get_monitor_log(key))  
204 - keys_map[key] = len(keys)-1  
205 - start_datetime += timedelta(minutes=1)  
206 -  
207 - logs = list(map(lambda log:  
208 - {"key": "%02d:%02d" % (log.hour_stamp, log.minu_stamp),  
209 - "cpu_usage": log.cpu_usage, "total_mem": log.total_mem,  
210 - "available_mem": log.available_mem, "used_mem": log.used_mem, "disk": log.disk, "disk_usage": log.disk_usage,  
211 - "net_recv": log.net_recv, "net_send": log.net_send},  
212 - logs))  
213 -  
214 - res["data"] = self.get_result_from_list(logs, keys, keys_map)  
215 - res["result"] = True  
216 - except Exception as e:  
217 - raise e  
218 - return res  
219 -  
220 - api_doc = {  
221 - "tags": ["监控接口"],  
222 - "parameters": [  
223 - {"name": "server",  
224 - "in": "query",  
225 - "type": "string",  
226 - "description": "服务器地址"},  
227 - {"name": "grain",  
228 - "in": "query",  
229 - "type": "string",  
230 - "description": "指标粒度:minu:分钟,minu_five:5分钟,minu_ten:10分钟,hour:1小时,day:每天"},  
231 - {"name": "count",  
232 - "in": "query",  
233 - "type": "string",  
234 - "description": "个数"},  
235 - {"name": "to",  
236 - "in": "query",  
237 - "type": "string",  
238 - "description": "查询终止时间"},  
239 - {"name": "start",  
240 - "in": "query",  
241 - "type": "string",  
242 - "description": "查询起始时间"}  
243 - ],  
244 - "responses": {  
245 - 200: {  
246 - "schema": {  
247 - "properties": {  
248 - }  
249 - }  
250 - }  
251 - }  
252 - }  
253 -  
254 - def get_monitor_log(self, key):  
255 - return {"key": key,  
256 - "cpu_usage": None, "total_mem": None,  
257 - "available_mem": None, "used_mem": None, "disk": None, "disk_usage": None,  
258 - "net_recv": None, "net_send": None}  
259 -  
260 - def get_result(self, logs, keys, keys_map):  
261 - keys_map_key=keys_map.keys()  
262 - for log in logs:  
263 - if str(log.key) in keys_map_key:  
264 - tmp = keys[keys_map[str(log.key)]]  
265 - if tmp != None:  
266 - tmp['cpu_usage'] = log.cpu_usage  
267 - tmp['total_mem'] = log.total_mem  
268 - tmp['available_mem'] = log.available_mem  
269 - tmp['used_mem'] = log.used_mem  
270 - tmp['disk'] = log.disk  
271 - tmp['disk_usage'] = log.disk_usage  
272 - tmp['net_recv'] = log.net_recv  
273 - tmp['net_send'] = log.net_send  
274 - return keys  
275 -  
276 - def get_result_from_list(self, logs, keys, keys_map):  
277 - keys_map_key=keys_map.keys()  
278 - for log in logs:  
279 - if str(log["key"]) in keys_map_key:  
280 - tmp = keys[keys_map[str(log["key"])]]  
281 - if tmp != None:  
282 - tmp['cpu_usage'] = log["cpu_usage"]  
283 - tmp['total_mem'] = log["total_mem"]  
284 - tmp['available_mem'] = log["available_mem"]  
285 - tmp['used_mem'] = log["used_mem"]  
286 - tmp['disk'] = log["disk"]  
287 - tmp['disk_usage'] = log["disk_usage"]  
288 - tmp['net_recv'] = log["net_recv"]  
289 - tmp['net_send'] = log["net_send"]  
290 - return keys  
1 # coding=utf-8 1 # coding=utf-8
2 -# author: 4N  
3 -#createtime: 2021/6/11  
4 -#email: nheweijun@sina.com  
5 -  
6 - 2 +# author: resioR
  3 +#createtime: 2021/12/1
  4 +#email: qianyingz@chinadci.com
7 from datetime import datetime 5 from datetime import datetime
8 from time import time 6 from time import time
9 from sqlalchemy import Column, Integer, String, ForeignKey, Text, DateTime, Time, Float, Binary,TIMESTAMP 7 from sqlalchemy import Column, Integer, String, ForeignKey, Text, DateTime, Time, Float, Binary,TIMESTAMP
@@ -11,29 +9,6 @@ from sqlalchemy.orm import relationship @@ -11,29 +9,6 @@ from sqlalchemy.orm import relationship
11 from sqlalchemy.sql.expression import column 9 from sqlalchemy.sql.expression import column
12 from app.models import db 10 from app.models import db
13 11
14 -  
15 -class MonitorLog(db.Model):  
16 - '''  
17 - '''  
18 - __tablename__ = "dmap_monitor_log"  
19 - guid = Column(String(256), primary_key=True)  
20 - server = Column(String(256))  
21 - date_stamp = Column(Text)  
22 - hour_stamp = Column(Integer)  
23 - minu_stamp = Column(Integer)  
24 - d_minu_stamp = Column(Integer) # 10min粒度 1-6  
25 - f_minu_stamp = Column(Integer) # 5min粒度 1~12  
26 - time_stamp = Column(DateTime) # 创建时间戳  
27 - cpu_usage = Column(Float)  
28 - total_mem = Column(Float)  
29 - available_mem = Column(Float)  
30 - used_mem = Column(Float)  
31 - disk = Column(Float)  
32 - disk_usage = Column(Float)  
33 - net_recv = Column(Float)  
34 - net_send = Column(Float)  
35 -  
36 -  
37 class MonitorHost(db.Model): 12 class MonitorHost(db.Model):
38 ''' 13 '''
39 监控服务器配置 14 监控服务器配置
@@ -45,18 +20,6 @@ class MonitorHost(db.Model): @@ -45,18 +20,6 @@ class MonitorHost(db.Model):
45 password = Column(String(256)) 20 password = Column(String(256))
46 type = Column(String(256)) 21 type = Column(String(256))
47 host_name = Column(String(256)) 22 host_name = Column(String(256))
48 -  
49 -  
50 -class MonitorCpu(db.Model):  
51 - '''  
52 - '''  
53 - __tablename__ = "dmap_monitor_cpu"  
54 - guid = Column(String(256), primary_key=True)  
55 - server = Column(String(256))  
56 - date_stamp = Column(Text)  
57 - time_stamp = Column(TIMESTAMP) # 创建时间戳  
58 - stamp = Column(Text) # P0,P50,P90,P95,P99,P100  
59 - value = Column(Float)  
60 23
61 class MonitorInfo(db.Model): 24 class MonitorInfo(db.Model):
62 ''' 25 '''
  1 +# coding=utf-8
  2 +# author: resioR
  3 +#createtime: 2021/12/1
  4 +#email: qianyingz@chinadci.com
  5 +
1 # import schedule 6 # import schedule
2 from flask import json 7 from flask import json
3 from .models import MonitorHost, MonitorInfo 8 from .models import MonitorHost, MonitorInfo
注册登录 后发表评论