Spaces:
Sleeping
Sleeping
Update vps_monitor.py
Browse files- vps_monitor.py +13 -15
vps_monitor.py
CHANGED
@@ -23,9 +23,6 @@ logging.basicConfig(
|
|
23 |
)
|
24 |
logger = logging.getLogger()
|
25 |
|
26 |
-
# 记录脚本启动时间
|
27 |
-
script_start_time = datetime.now()
|
28 |
-
|
29 |
def get_vps_configs():
|
30 |
configs = []
|
31 |
index = 1
|
@@ -82,7 +79,7 @@ def check_and_run_script(config):
|
|
82 |
|
83 |
# 检查进程是否在运行
|
84 |
if last_pid:
|
85 |
-
check_command = f"ps -p {last_pid} -o pid
|
86 |
else:
|
87 |
check_command = f"ps aux | grep '{script_path}' | grep -v grep"
|
88 |
|
@@ -107,7 +104,7 @@ def check_and_run_script(config):
|
|
107 |
|
108 |
if new_pid.isdigit():
|
109 |
pid = new_pid
|
110 |
-
runtime = "
|
111 |
status = "Restarted"
|
112 |
logger.info(f"Script {script_name} restarted. New PID: {pid}")
|
113 |
else:
|
@@ -119,7 +116,7 @@ def check_and_run_script(config):
|
|
119 |
vps_status[key] = {
|
120 |
'index': config['index'],
|
121 |
'status': status,
|
122 |
-
'last_check':
|
123 |
'username': config['username'],
|
124 |
'script_name': script_name,
|
125 |
'runtime': runtime,
|
@@ -132,7 +129,7 @@ def check_and_run_script(config):
|
|
132 |
vps_status[key] = {
|
133 |
'index': config['index'],
|
134 |
'status': f"Error: {str(e)}",
|
135 |
-
'last_check':
|
136 |
'username': config['username'],
|
137 |
'script_name': script_name,
|
138 |
'runtime': "N/A",
|
@@ -156,15 +153,14 @@ def check_all_vps():
|
|
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 |
-
|
166 |
status['username'][:8],
|
167 |
-
status['runtime']
|
168 |
status['pid'][:5]
|
169 |
)
|
170 |
table += "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+-------+\n"
|
@@ -192,7 +188,7 @@ def index():
|
|
192 |
<td><a href="/status/{{ key }}">{{ key.split(':')[0] }}</a></td>
|
193 |
<td>{{ data.script_name }}</td>
|
194 |
<td>{{ data.status }}</td>
|
195 |
-
<td>{{
|
196 |
<td>{{ data.username }}</td>
|
197 |
<td>{{ data.runtime }}</td>
|
198 |
<td>{{ data.pid }}</td>
|
@@ -200,7 +196,7 @@ def index():
|
|
200 |
{% endfor %}
|
201 |
</table>
|
202 |
'''
|
203 |
-
return render_template_string(html, vps_status=vps_status
|
204 |
|
205 |
@app.route('/status/<path:key>')
|
206 |
def vps_status_detail(key):
|
@@ -211,12 +207,15 @@ def vps_status_detail(key):
|
|
211 |
|
212 |
@app.route('/health')
|
213 |
def health_check():
|
214 |
-
return jsonify({"status": "healthy", "uptime":
|
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,8 +236,7 @@ def main():
|
|
237 |
time.sleep(60)
|
238 |
heartbeat_count += 1
|
239 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
240 |
-
|
241 |
-
logger.info(f"Heartbeat: Script is still running. Uptime: {uptime}")
|
242 |
|
243 |
if __name__ == "__main__":
|
244 |
main()
|
|
|
23 |
)
|
24 |
logger = logging.getLogger()
|
25 |
|
|
|
|
|
|
|
26 |
def get_vps_configs():
|
27 |
configs = []
|
28 |
index = 1
|
|
|
79 |
|
80 |
# 检查进程是否在运行
|
81 |
if last_pid:
|
82 |
+
check_command = f"ps -p {last_pid} -o pid=,etime=,args="
|
83 |
else:
|
84 |
check_command = f"ps aux | grep '{script_path}' | grep -v grep"
|
85 |
|
|
|
104 |
|
105 |
if new_pid.isdigit():
|
106 |
pid = new_pid
|
107 |
+
runtime = "00:00"
|
108 |
status = "Restarted"
|
109 |
logger.info(f"Script {script_name} restarted. New PID: {pid}")
|
110 |
else:
|
|
|
116 |
vps_status[key] = {
|
117 |
'index': config['index'],
|
118 |
'status': status,
|
119 |
+
'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
|
120 |
'username': config['username'],
|
121 |
'script_name': script_name,
|
122 |
'runtime': runtime,
|
|
|
129 |
vps_status[key] = {
|
130 |
'index': config['index'],
|
131 |
'status': f"Error: {str(e)}",
|
132 |
+
'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
|
133 |
'username': config['username'],
|
134 |
'script_name': script_name,
|
135 |
'runtime': "N/A",
|
|
|
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 |
+
status['last_check'],
|
162 |
status['username'][:8],
|
163 |
+
status['runtime'],
|
164 |
status['pid'][:5]
|
165 |
)
|
166 |
table += "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+-------+\n"
|
|
|
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 |
{% 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 |
|
208 |
@app.route('/health')
|
209 |
def health_check():
|
210 |
+
return jsonify({"status": "healthy", "uptime": time.time() - start_time}), 200
|
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 |
time.sleep(60)
|
237 |
heartbeat_count += 1
|
238 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
239 |
+
logger.info(f"Heartbeat: Script is still running. Uptime: {heartbeat_count} minutes")
|
|
|
240 |
|
241 |
if __name__ == "__main__":
|
242 |
main()
|