Spaces:
Sleeping
Sleeping
Update vps_monitor.py
Browse files- vps_monitor.py +12 -10
vps_monitor.py
CHANGED
@@ -23,6 +23,9 @@ logging.basicConfig(
|
|
23 |
)
|
24 |
logger = logging.getLogger()
|
25 |
|
|
|
|
|
|
|
26 |
def get_vps_configs():
|
27 |
configs = []
|
28 |
index = 1
|
@@ -116,7 +119,7 @@ def check_and_run_script(config):
|
|
116 |
vps_status[key] = {
|
117 |
'index': config['index'],
|
118 |
'status': status,
|
119 |
-
'last_check':
|
120 |
'username': config['username'],
|
121 |
'script_name': script_name,
|
122 |
'runtime': runtime,
|
@@ -129,7 +132,7 @@ def check_and_run_script(config):
|
|
129 |
vps_status[key] = {
|
130 |
'index': config['index'],
|
131 |
'status': f"Error: {str(e)}",
|
132 |
-
'last_check':
|
133 |
'username': config['username'],
|
134 |
'script_name': script_name,
|
135 |
'runtime': "N/A",
|
@@ -153,12 +156,13 @@ def check_all_vps():
|
|
153 |
# 添加每个VPS的状态
|
154 |
for key, status in vps_status.items():
|
155 |
hostname, script_name = key.split(':')
|
|
|
156 |
table += "| {:<7} | {:<21} | {:<16} | {:<8} | {:<23} | {:<8} | {:<8} | {:<5} |\n".format(
|
157 |
status['index'],
|
158 |
hostname[:21],
|
159 |
script_name[:16],
|
160 |
status['status'][:8],
|
161 |
-
|
162 |
status['username'][:8],
|
163 |
status['runtime'][:8],
|
164 |
status['pid'][:5]
|
@@ -188,7 +192,7 @@ def index():
|
|
188 |
<td><a href="/status/{{ key }}">{{ key.split(':')[0] }}</a></td>
|
189 |
<td>{{ data.script_name }}</td>
|
190 |
<td>{{ data.status }}</td>
|
191 |
-
<td>{{ data.last_check }}</td>
|
192 |
<td>{{ data.username }}</td>
|
193 |
<td>{{ data.runtime }}</td>
|
194 |
<td>{{ data.pid }}</td>
|
@@ -196,7 +200,7 @@ def index():
|
|
196 |
{% endfor %}
|
197 |
</table>
|
198 |
'''
|
199 |
-
return render_template_string(html, vps_status=vps_status)
|
200 |
|
201 |
@app.route('/status/<path:key>')
|
202 |
def vps_status_detail(key):
|
@@ -207,15 +211,12 @@ def vps_status_detail(key):
|
|
207 |
|
208 |
@app.route('/health')
|
209 |
def health_check():
|
210 |
-
return jsonify({"status": "healthy", "uptime":
|
211 |
|
212 |
def run_flask():
|
213 |
app.run(host='0.0.0.0', port=8080)
|
214 |
|
215 |
def main():
|
216 |
-
global start_time
|
217 |
-
start_time = time.time()
|
218 |
-
|
219 |
logger.info("===== VPS monitoring script is starting =====")
|
220 |
|
221 |
flask_thread = Thread(target=run_flask)
|
@@ -236,7 +237,8 @@ def main():
|
|
236 |
time.sleep(60)
|
237 |
heartbeat_count += 1
|
238 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
239 |
-
|
|
|
240 |
|
241 |
if __name__ == "__main__":
|
242 |
main()
|
|
|
23 |
)
|
24 |
logger = logging.getLogger()
|
25 |
|
26 |
+
# 记录脚本启动时间
|
27 |
+
script_start_time = datetime.now()
|
28 |
+
|
29 |
def get_vps_configs():
|
30 |
configs = []
|
31 |
index = 1
|
|
|
119 |
vps_status[key] = {
|
120 |
'index': config['index'],
|
121 |
'status': status,
|
122 |
+
'last_check': (datetime.now() - script_start_time).total_seconds(),
|
123 |
'username': config['username'],
|
124 |
'script_name': script_name,
|
125 |
'runtime': runtime,
|
|
|
132 |
vps_status[key] = {
|
133 |
'index': config['index'],
|
134 |
'status': f"Error: {str(e)}",
|
135 |
+
'last_check': (datetime.now() - script_start_time).total_seconds(),
|
136 |
'username': config['username'],
|
137 |
'script_name': script_name,
|
138 |
'runtime': "N/A",
|
|
|
156 |
# 添加每个VPS的状态
|
157 |
for key, status in vps_status.items():
|
158 |
hostname, script_name = key.split(':')
|
159 |
+
last_check = timedelta(seconds=int(status['last_check']))
|
160 |
table += "| {:<7} | {:<21} | {:<16} | {:<8} | {:<23} | {:<8} | {:<8} | {:<5} |\n".format(
|
161 |
status['index'],
|
162 |
hostname[:21],
|
163 |
script_name[:16],
|
164 |
status['status'][:8],
|
165 |
+
str(last_check),
|
166 |
status['username'][:8],
|
167 |
status['runtime'][:8],
|
168 |
status['pid'][:5]
|
|
|
192 |
<td><a href="/status/{{ key }}">{{ key.split(':')[0] }}</a></td>
|
193 |
<td>{{ data.script_name }}</td>
|
194 |
<td>{{ data.status }}</td>
|
195 |
+
<td>{{ timedelta(seconds=data.last_check|int) }}</td>
|
196 |
<td>{{ data.username }}</td>
|
197 |
<td>{{ data.runtime }}</td>
|
198 |
<td>{{ data.pid }}</td>
|
|
|
200 |
{% endfor %}
|
201 |
</table>
|
202 |
'''
|
203 |
+
return render_template_string(html, vps_status=vps_status, timedelta=timedelta)
|
204 |
|
205 |
@app.route('/status/<path:key>')
|
206 |
def vps_status_detail(key):
|
|
|
211 |
|
212 |
@app.route('/health')
|
213 |
def health_check():
|
214 |
+
return jsonify({"status": "healthy", "uptime": (datetime.now() - script_start_time).total_seconds()}), 200
|
215 |
|
216 |
def run_flask():
|
217 |
app.run(host='0.0.0.0', port=8080)
|
218 |
|
219 |
def main():
|
|
|
|
|
|
|
220 |
logger.info("===== VPS monitoring script is starting =====")
|
221 |
|
222 |
flask_thread = Thread(target=run_flask)
|
|
|
237 |
time.sleep(60)
|
238 |
heartbeat_count += 1
|
239 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
240 |
+
uptime = datetime.now() - script_start_time
|
241 |
+
logger.info(f"Heartbeat: Script is still running. Uptime: {uptime}")
|
242 |
|
243 |
if __name__ == "__main__":
|
244 |
main()
|