|
@@ -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__()
|