提交 41c1dc839be4a869059346eeb3370560fe32defe

作者 qianyingz
1 个父辈 a56d69a9

add feature#每天凌晨清理7天前监控数据

@@ -130,6 +130,7 @@ def create_app(): @@ -130,6 +130,7 @@ def create_app():
130 os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' 130 os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
131 131
132 # start_schedule() 132 # start_schedule()
  133 + start_schedule()
133 return app 134 return app
134 135
135 136
@@ -185,7 +185,7 @@ class DataManager(BlueprintApi): @@ -185,7 +185,7 @@ class DataManager(BlueprintApi):
185 @bp.route("/init", methods=["GET"]) 185 @bp.route("/init", methods=["GET"])
186 def init(): 186 def init():
187 username = 'admin' 187 username = 'admin'
188 - password = 'admin' 188 + password = SM3.encode('DMap@123')
189 if not User.query.filter_by(username=username).one_or_none(): 189 if not User.query.filter_by(username=username).one_or_none():
190 user = User(username=username, password=password, role='admin', 190 user = User(username=username, password=password, role='admin',
191 phone='', company='', position='', email='', 191 phone='', company='', position='', email='',
@@ -197,12 +197,13 @@ class DataManager(BlueprintApi): @@ -197,12 +197,13 @@ class DataManager(BlueprintApi):
197 return "创建默认用户成功" 197 return "创建默认用户成功"
198 else: 198 else:
199 return "默认用户已存在" 199 return "默认用户已存在"
200 - 200 +
201 @staticmethod 201 @staticmethod
202 - @bp.route("/test", methods=["GET"])  
203 - def test():  
204 - result = 'c78ca1de8139e62c99d4c4d2050ddd16'  
205 - result = AESHelper.encode('dataman2')  
206 - # result=DES.decode('U2FsdGVkX199Ncuh7+0/HVmonwM9Uz7SYnmF73wtW7c=')  
207 - result2 = AESHelper.decode(result)  
208 - return result2 202 + @bp.route("/translate", methods=["GET"])
  203 + def translate():
  204 + password = ['esri@123','admin','DMap@123','passwd']
  205 + result = {}
  206 + for p in password:
  207 + new_pwd = SM3.encode(p)
  208 + result[p] = new_pwd
  209 + return result
@@ -5,8 +5,9 @@ @@ -5,8 +5,9 @@
5 5
6 # import schedule 6 # import schedule
7 from flask import json 7 from flask import json
  8 +from sqlalchemy import true
8 from .models import MonitorHost, MonitorInfo 9 from .models import MonitorHost, MonitorInfo
9 -import datetime 10 +from datetime import datetime, timedelta
10 import math 11 import math
11 import uuid 12 import uuid
12 import schedule 13 import schedule
@@ -30,7 +31,6 @@ def pull_metric(): @@ -30,7 +31,6 @@ def pull_metric():
30 0, configure.SQLALCHEMY_DATABASE_URI) 31 0, configure.SQLALCHEMY_DATABASE_URI)
31 32
32 # 拉取服务器信息 33 # 拉取服务器信息
33 -  
34 hosts = sys_session.query( 34 hosts = sys_session.query(
35 MonitorHost.host) 35 MonitorHost.host)
36 for host in hosts: 36 for host in hosts:
@@ -61,7 +61,39 @@ def pull_metric(): @@ -61,7 +61,39 @@ def pull_metric():
61 # 获取数据并汇聚为1min的数据入库 61 # 获取数据并汇聚为1min的数据入库
62 # 结束 62 # 结束
63 except Exception as e: 63 except Exception as e:
64 - sys_session.rollback() 64 + # sys_session.rollback()
  65 + StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
  66 + finally:
  67 + if sys_session:
  68 + try:
  69 + sys_session.close()
  70 + except Exception as e:
  71 + StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
  72 + if sys_ds:
  73 + try:
  74 + sys_ds.Destroy()
  75 + except Exception as e:
  76 + StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
  77 +
  78 +
  79 +#每天清理7天前的数据
  80 +def monitor_vacuuming():
  81 + try:
  82 + current_date = datetime.now()
  83 + last_7_date = current_date+timedelta(days=-7)
  84 + current_hour = current_date.hour
  85 + if current_hour == 0:
  86 + sys_session = PGUtil.get_db_session(
  87 + configure.SQLALCHEMY_DATABASE_URI)
  88 + sys_ds = PGUtil.open_pg_data_source(
  89 + 0, configure.SQLALCHEMY_DATABASE_URI)
  90 + info_orm = sys_session.query(MonitorInfo).filter(
  91 + MonitorInfo.time_stamp < last_7_date)
  92 + count = info_orm.count()
  93 + info_orm.delete()
  94 + sys_session.commit()
  95 + StructurePrint().print("MonitorInfo清理%d条监控数据" % count, "info")
  96 + except Exception as e:
65 StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error") 97 StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
66 finally: 98 finally:
67 if sys_session: 99 if sys_session:
@@ -77,10 +109,13 @@ def pull_metric(): @@ -77,10 +109,13 @@ def pull_metric():
77 109
78 110
79 def start_schedule(): 111 def start_schedule():
80 - # # 1分钟巡检一次 112 +
81 try: 113 try:
82 StructurePrint().print("start_schedule") 114 StructurePrint().print("start_schedule")
  115 + # 1分钟巡检一次
83 schedule.every(1).minutes.do(pull_metric) 116 schedule.every(1).minutes.do(pull_metric)
  117 + # 每天00:00清理数据
  118 + schedule.every().day.at('00:00').do(monitor_vacuuming)
84 stop_run_continuously = run_continuously() 119 stop_run_continuously = run_continuously()
85 except Exception as e: 120 except Exception as e:
86 StructurePrint().print("start_schedule:"+e.__str__() + 121 StructurePrint().print("start_schedule:"+e.__str__() +
@@ -130,7 +165,7 @@ def get_sample_data(orginal, name, host): @@ -130,7 +165,7 @@ def get_sample_data(orginal, name, host):
130 165
131 166
132 def get_data(stamp, host, metrics_name, cur_data): 167 def get_data(stamp, host, metrics_name, cur_data):
133 - time_stamp = datetime.datetime.strptime( 168 + time_stamp = datetime.strptime(
134 cur_data['timestamp'], "%Y-%m-%d %H:%M:%S") 169 cur_data['timestamp'], "%Y-%m-%d %H:%M:%S")
135 date_stamp = time_stamp.strftime("%Y-%m-%d") 170 date_stamp = time_stamp.strftime("%Y-%m-%d")
136 guid = uuid.uuid1().__str__() 171 guid = uuid.uuid1().__str__()
注册登录 后发表评论