Spaces:
Sleeping
Sleeping
Update vps_monitor.py
Browse files- vps_monitor.py +23 -45
vps_monitor.py
CHANGED
@@ -52,24 +52,16 @@ def get_vps_configs():
|
|
52 |
}
|
53 |
configs.append(config)
|
54 |
|
55 |
-
# 添加调试输出
|
56 |
-
logger.info(f"Config {index}: {hostname}, {username}, {script_paths}")
|
57 |
-
print(f"Config {index}: {hostname}, {username}, {script_paths}")
|
58 |
-
sys.stdout.flush()
|
59 |
-
|
60 |
index += 1
|
61 |
return configs
|
62 |
|
63 |
def check_and_run_script(config):
|
64 |
logger.info(f"Checking VPS {config['index']}: {config['hostname']} - {config['script_path']}")
|
65 |
-
print(f"Checking VPS {config['index']}: {config['hostname']} - {config['script_path']}")
|
66 |
-
sys.stdout.flush()
|
67 |
client = None
|
68 |
try:
|
69 |
client = paramiko.SSHClient()
|
70 |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
71 |
|
72 |
-
logger.info(f"Connecting to {config['hostname']}")
|
73 |
client.connect(
|
74 |
hostname=config['hostname'],
|
75 |
username=config['username'],
|
@@ -80,25 +72,20 @@ def check_and_run_script(config):
|
|
80 |
script_path = config['script_path']
|
81 |
script_name = os.path.basename(script_path)
|
82 |
|
83 |
-
# 检查脚本是否正在运行 (使用 ps 命令,适用于FreeBSD)
|
84 |
check_command = f"ps aux | grep {script_name} | grep -v grep"
|
85 |
stdin, stdout, stderr = client.exec_command(check_command)
|
86 |
if stdout.read():
|
87 |
status = "Running"
|
88 |
-
logger.info(f"Script {script_name} is running on {config['hostname']}")
|
89 |
else:
|
90 |
-
|
91 |
-
restart_command = f"/bin/sh {script_path}" # 使用完整路径的sh
|
92 |
stdin, stdout, stderr = client.exec_command(restart_command)
|
93 |
exit_status = stdout.channel.recv_exit_status()
|
94 |
|
95 |
if exit_status == 0:
|
96 |
status = "Restarted"
|
97 |
-
logger.info(f"Restart script {script_name} executed successfully on {config['hostname']}")
|
98 |
else:
|
99 |
error_output = stderr.read().decode('utf-8')
|
100 |
status = f"Error: {error_output}"
|
101 |
-
logger.error(f"Error executing restart script {script_name} on {config['hostname']}: {error_output}")
|
102 |
|
103 |
key = f"{config['hostname']}:{script_name}"
|
104 |
vps_status[key] = {
|
@@ -122,22 +109,32 @@ def check_and_run_script(config):
|
|
122 |
finally:
|
123 |
if client:
|
124 |
client.close()
|
125 |
-
logger.info(f"SSH connection closed for VPS {config['index']}: {config['hostname']}")
|
126 |
-
logger.info(f"Finished checking VPS {config['index']}: {config['hostname']} - {script_name}")
|
127 |
-
print(f"Finished checking VPS {config['index']}: {config['hostname']} - {script_name}")
|
128 |
-
sys.stdout.flush()
|
129 |
|
130 |
def check_all_vps():
|
131 |
logger.info("Starting VPS check")
|
132 |
-
print("Starting VPS check")
|
133 |
-
sys.stdout.flush()
|
134 |
vps_configs = get_vps_configs()
|
135 |
for config in vps_configs:
|
136 |
check_and_run_script(config)
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
for key, status in vps_status.items():
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
@app.route('/')
|
143 |
def index():
|
@@ -184,37 +181,19 @@ def main():
|
|
184 |
global start_time
|
185 |
start_time = time.time()
|
186 |
|
187 |
-
|
188 |
-
sys.stdout.flush()
|
189 |
-
logger.info("===== VPS monitoring script started =====")
|
190 |
|
191 |
flask_thread = Thread(target=run_flask)
|
192 |
flask_thread.start()
|
193 |
logger.info("Flask server started in background")
|
194 |
-
print("Flask server started in background")
|
195 |
-
sys.stdout.flush()
|
196 |
-
|
197 |
-
vps_configs = get_vps_configs()
|
198 |
-
logger.info(f"Found {len(vps_configs)} VPS configurations")
|
199 |
-
print(f"Found {len(vps_configs)} VPS configurations")
|
200 |
-
sys.stdout.flush()
|
201 |
-
for config in vps_configs:
|
202 |
-
logger.info(f"VPS configured: {config['hostname']} - {config['script_path']}")
|
203 |
-
print(f"VPS configured: {config['hostname']} - {config['script_path']}")
|
204 |
-
sys.stdout.flush()
|
205 |
|
206 |
logger.info("Running initial VPS check")
|
207 |
-
print("Running initial VPS check")
|
208 |
-
sys.stdout.flush()
|
209 |
check_all_vps()
|
210 |
|
211 |
schedule.every(15).minutes.do(check_all_vps)
|
212 |
logger.info("Scheduled VPS check every 15 minutes")
|
213 |
-
print("Scheduled VPS check every 15 minutes")
|
214 |
-
sys.stdout.flush()
|
215 |
|
216 |
-
|
217 |
-
sys.stdout.flush()
|
218 |
|
219 |
heartbeat_count = 0
|
220 |
while True:
|
@@ -222,8 +201,7 @@ def main():
|
|
222 |
time.sleep(60)
|
223 |
heartbeat_count += 1
|
224 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
225 |
-
|
226 |
-
sys.stdout.flush()
|
227 |
|
228 |
if __name__ == "__main__":
|
229 |
main()
|
|
|
52 |
}
|
53 |
configs.append(config)
|
54 |
|
|
|
|
|
|
|
|
|
|
|
55 |
index += 1
|
56 |
return configs
|
57 |
|
58 |
def check_and_run_script(config):
|
59 |
logger.info(f"Checking VPS {config['index']}: {config['hostname']} - {config['script_path']}")
|
|
|
|
|
60 |
client = None
|
61 |
try:
|
62 |
client = paramiko.SSHClient()
|
63 |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
64 |
|
|
|
65 |
client.connect(
|
66 |
hostname=config['hostname'],
|
67 |
username=config['username'],
|
|
|
72 |
script_path = config['script_path']
|
73 |
script_name = os.path.basename(script_path)
|
74 |
|
|
|
75 |
check_command = f"ps aux | grep {script_name} | grep -v grep"
|
76 |
stdin, stdout, stderr = client.exec_command(check_command)
|
77 |
if stdout.read():
|
78 |
status = "Running"
|
|
|
79 |
else:
|
80 |
+
restart_command = f"/bin/sh {script_path}"
|
|
|
81 |
stdin, stdout, stderr = client.exec_command(restart_command)
|
82 |
exit_status = stdout.channel.recv_exit_status()
|
83 |
|
84 |
if exit_status == 0:
|
85 |
status = "Restarted"
|
|
|
86 |
else:
|
87 |
error_output = stderr.read().decode('utf-8')
|
88 |
status = f"Error: {error_output}"
|
|
|
89 |
|
90 |
key = f"{config['hostname']}:{script_name}"
|
91 |
vps_status[key] = {
|
|
|
109 |
finally:
|
110 |
if client:
|
111 |
client.close()
|
|
|
|
|
|
|
|
|
112 |
|
113 |
def check_all_vps():
|
114 |
logger.info("Starting VPS check")
|
|
|
|
|
115 |
vps_configs = get_vps_configs()
|
116 |
for config in vps_configs:
|
117 |
check_and_run_script(config)
|
118 |
+
|
119 |
+
# 创建表格头
|
120 |
+
table = "+---------+-----------------------+------------------+----------+-------------------------+----------+\n"
|
121 |
+
table += "| Index | Hostname | Script Name | Status | Last Check | Username |\n"
|
122 |
+
table += "+---------+-----------------------+------------------+----------+-------------------------+----------+\n"
|
123 |
+
|
124 |
+
# 添加每个VPS的状态
|
125 |
for key, status in vps_status.items():
|
126 |
+
hostname, script_name = key.split(':')
|
127 |
+
table += "| {:<7} | {:<21} | {:<16} | {:<8} | {:<23} | {:<8} |\n".format(
|
128 |
+
status['index'],
|
129 |
+
hostname[:21],
|
130 |
+
script_name[:16],
|
131 |
+
status['status'][:8],
|
132 |
+
status['last_check'],
|
133 |
+
status['username'][:8]
|
134 |
+
)
|
135 |
+
table += "+---------+-----------------------+------------------+----------+-------------------------+----------+\n"
|
136 |
+
|
137 |
+
logger.info("\n" + table)
|
138 |
|
139 |
@app.route('/')
|
140 |
def index():
|
|
|
181 |
global start_time
|
182 |
start_time = time.time()
|
183 |
|
184 |
+
logger.info("===== VPS monitoring script is starting =====")
|
|
|
|
|
185 |
|
186 |
flask_thread = Thread(target=run_flask)
|
187 |
flask_thread.start()
|
188 |
logger.info("Flask server started in background")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
logger.info("Running initial VPS check")
|
|
|
|
|
191 |
check_all_vps()
|
192 |
|
193 |
schedule.every(15).minutes.do(check_all_vps)
|
194 |
logger.info("Scheduled VPS check every 15 minutes")
|
|
|
|
|
195 |
|
196 |
+
logger.info("===== VPS monitoring script is running =====")
|
|
|
197 |
|
198 |
heartbeat_count = 0
|
199 |
while True:
|
|
|
201 |
time.sleep(60)
|
202 |
heartbeat_count += 1
|
203 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
204 |
+
logger.info(f"Heartbeat: Script is still running. Uptime: {heartbeat_count} minutes")
|
|
|
205 |
|
206 |
if __name__ == "__main__":
|
207 |
main()
|