Spaces:
Sleeping
Sleeping
Update vps_monitor.py
Browse files- vps_monitor.py +32 -9
vps_monitor.py
CHANGED
@@ -52,13 +52,18 @@ def get_vps_configs():
|
|
52 |
}
|
53 |
configs.append(config)
|
54 |
|
|
|
55 |
logger.info(f"Config {index}: {hostname}, {username}, {script_paths}")
|
|
|
|
|
56 |
|
57 |
index += 1
|
58 |
return configs
|
59 |
|
60 |
def check_and_run_script(config):
|
61 |
logger.info(f"Checking VPS {config['index']}: {config['hostname']} - {config['script_path']}")
|
|
|
|
|
62 |
client = None
|
63 |
try:
|
64 |
client = paramiko.SSHClient()
|
@@ -75,25 +80,24 @@ def check_and_run_script(config):
|
|
75 |
script_path = config['script_path']
|
76 |
script_name = os.path.basename(script_path)
|
77 |
|
|
|
78 |
check_command = f"ps aux | grep {script_name} | grep -v grep"
|
79 |
stdin, stdout, stderr = client.exec_command(check_command)
|
80 |
if stdout.read():
|
81 |
status = "Running"
|
82 |
logger.info(f"Script {script_name} is running on {config['hostname']}")
|
83 |
else:
|
84 |
-
logger.
|
85 |
-
restart_command = f"/bin/sh {script_path}"
|
86 |
stdin, stdout, stderr = client.exec_command(restart_command)
|
87 |
exit_status = stdout.channel.recv_exit_status()
|
88 |
|
89 |
if exit_status == 0:
|
90 |
status = "Restarted"
|
91 |
logger.info(f"Restart script {script_name} executed successfully on {config['hostname']}")
|
92 |
-
output = stdout.read().decode('utf-8')
|
93 |
-
logger.info(f"Restart output: {output}")
|
94 |
else:
|
95 |
error_output = stderr.read().decode('utf-8')
|
96 |
-
status = "Error"
|
97 |
logger.error(f"Error executing restart script {script_name} on {config['hostname']}: {error_output}")
|
98 |
|
99 |
key = f"{config['hostname']}:{script_name}"
|
@@ -120,15 +124,20 @@ def check_and_run_script(config):
|
|
120 |
client.close()
|
121 |
logger.info(f"SSH connection closed for VPS {config['index']}: {config['hostname']}")
|
122 |
logger.info(f"Finished checking VPS {config['index']}: {config['hostname']} - {script_name}")
|
|
|
|
|
123 |
|
124 |
def check_all_vps():
|
125 |
logger.info("Starting VPS check")
|
|
|
|
|
126 |
vps_configs = get_vps_configs()
|
127 |
for config in vps_configs:
|
128 |
check_and_run_script(config)
|
129 |
logger.info("Finished VPS check")
|
130 |
for key, status in vps_status.items():
|
131 |
-
|
|
|
132 |
|
133 |
@app.route('/')
|
134 |
def index():
|
@@ -175,24 +184,37 @@ def main():
|
|
175 |
global start_time
|
176 |
start_time = time.time()
|
177 |
|
178 |
-
|
|
|
|
|
179 |
|
180 |
flask_thread = Thread(target=run_flask)
|
181 |
flask_thread.start()
|
182 |
logger.info("Flask server started in background")
|
|
|
|
|
183 |
|
184 |
vps_configs = get_vps_configs()
|
185 |
logger.info(f"Found {len(vps_configs)} VPS configurations")
|
|
|
|
|
186 |
for config in vps_configs:
|
187 |
logger.info(f"VPS configured: {config['hostname']} - {config['script_path']}")
|
|
|
|
|
188 |
|
189 |
logger.info("Running initial VPS check")
|
|
|
|
|
190 |
check_all_vps()
|
191 |
|
192 |
schedule.every(15).minutes.do(check_all_vps)
|
193 |
logger.info("Scheduled VPS check every 15 minutes")
|
|
|
|
|
194 |
|
195 |
-
|
|
|
196 |
|
197 |
heartbeat_count = 0
|
198 |
while True:
|
@@ -200,7 +222,8 @@ def main():
|
|
200 |
time.sleep(60)
|
201 |
heartbeat_count += 1
|
202 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
203 |
-
|
|
|
204 |
|
205 |
if __name__ == "__main__":
|
206 |
main()
|
|
|
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()
|
|
|
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 |
+
logger.info(f"Script {script_name} not running on {config['hostname']}. Executing restart script.")
|
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}"
|
|
|
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 |
logger.info("Finished VPS check")
|
138 |
for key, status in vps_status.items():
|
139 |
+
print(f"VPS {status['index']} - {key}: Status: {status['status']}, Username: {status['username']}, Script: {status['script_name']}")
|
140 |
+
sys.stdout.flush()
|
141 |
|
142 |
@app.route('/')
|
143 |
def index():
|
|
|
184 |
global start_time
|
185 |
start_time = time.time()
|
186 |
|
187 |
+
print("===== VPS monitoring script is starting =====")
|
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 |
+
print("===== VPS monitoring script is running =====")
|
217 |
+
sys.stdout.flush()
|
218 |
|
219 |
heartbeat_count = 0
|
220 |
while True:
|
|
|
222 |
time.sleep(60)
|
223 |
heartbeat_count += 1
|
224 |
if heartbeat_count % 5 == 0: # 每5分钟输出一次心跳信息
|
225 |
+
print(f"Heartbeat: Script is still running. Uptime: {heartbeat_count} minutes")
|
226 |
+
sys.stdout.flush()
|
227 |
|
228 |
if __name__ == "__main__":
|
229 |
main()
|