Spaces:
Sleeping
Sleeping
Update vps_monitor.py
Browse files- vps_monitor.py +26 -10
vps_monitor.py
CHANGED
@@ -6,6 +6,7 @@ import sys
|
|
6 |
from flask import Flask, jsonify, render_template_string
|
7 |
from threading import Thread
|
8 |
import logging
|
|
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
@@ -72,10 +73,18 @@ def check_and_run_script(config):
|
|
72 |
script_path = config['script_path']
|
73 |
script_name = os.path.basename(script_path)
|
74 |
|
75 |
-
check_command = f"ps
|
76 |
stdin, stdout, stderr = client.exec_command(check_command)
|
77 |
-
|
|
|
|
|
78 |
status = "Running"
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
else:
|
80 |
restart_command = f"/bin/sh {script_path}"
|
81 |
stdin, stdout, stderr = client.exec_command(restart_command)
|
@@ -83,9 +92,11 @@ def check_and_run_script(config):
|
|
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] = {
|
@@ -93,7 +104,8 @@ def check_and_run_script(config):
|
|
93 |
'status': status,
|
94 |
'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
|
95 |
'username': config['username'],
|
96 |
-
'script_name': script_name
|
|
|
97 |
}
|
98 |
|
99 |
except Exception as e:
|
@@ -104,7 +116,8 @@ def check_and_run_script(config):
|
|
104 |
'status': f"Error: {str(e)}",
|
105 |
'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
|
106 |
'username': config['username'],
|
107 |
-
'script_name': script_name
|
|
|
108 |
}
|
109 |
finally:
|
110 |
if client:
|
@@ -117,22 +130,23 @@ def check_all_vps():
|
|
117 |
check_and_run_script(config)
|
118 |
|
119 |
# 创建表格头
|
120 |
-
table = "
|
121 |
-
table += "| Index | Hostname | Script Name | Status | Last Check | Username |\n"
|
122 |
-
table += "
|
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 += "
|
136 |
|
137 |
logger.info("\n" + table)
|
138 |
|
@@ -148,6 +162,7 @@ def index():
|
|
148 |
<th>Status</th>
|
149 |
<th>Last Check</th>
|
150 |
<th>Username</th>
|
|
|
151 |
</tr>
|
152 |
{% for key, data in vps_status.items() %}
|
153 |
<tr>
|
@@ -157,6 +172,7 @@ def index():
|
|
157 |
<td>{{ data.status }}</td>
|
158 |
<td>{{ data.last_check }}</td>
|
159 |
<td>{{ data.username }}</td>
|
|
|
160 |
</tr>
|
161 |
{% endfor %}
|
162 |
</table>
|
|
|
6 |
from flask import Flask, jsonify, render_template_string
|
7 |
from threading import Thread
|
8 |
import logging
|
9 |
+
from datetime import datetime, timedelta
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
73 |
script_path = config['script_path']
|
74 |
script_name = os.path.basename(script_path)
|
75 |
|
76 |
+
check_command = f"ps -eo pid,etime,args | grep {script_name} | grep -v grep"
|
77 |
stdin, stdout, stderr = client.exec_command(check_command)
|
78 |
+
output = stdout.read().decode('utf-8').strip()
|
79 |
+
|
80 |
+
if output:
|
81 |
status = "Running"
|
82 |
+
# 解析运行时间
|
83 |
+
parts = output.split()
|
84 |
+
if len(parts) > 1:
|
85 |
+
runtime = parts[1]
|
86 |
+
else:
|
87 |
+
runtime = "Unknown"
|
88 |
else:
|
89 |
restart_command = f"/bin/sh {script_path}"
|
90 |
stdin, stdout, stderr = client.exec_command(restart_command)
|
|
|
92 |
|
93 |
if exit_status == 0:
|
94 |
status = "Restarted"
|
95 |
+
runtime = "Just started"
|
96 |
else:
|
97 |
error_output = stderr.read().decode('utf-8')
|
98 |
status = f"Error: {error_output}"
|
99 |
+
runtime = "N/A"
|
100 |
|
101 |
key = f"{config['hostname']}:{script_name}"
|
102 |
vps_status[key] = {
|
|
|
104 |
'status': status,
|
105 |
'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
|
106 |
'username': config['username'],
|
107 |
+
'script_name': script_name,
|
108 |
+
'runtime': runtime
|
109 |
}
|
110 |
|
111 |
except Exception as e:
|
|
|
116 |
'status': f"Error: {str(e)}",
|
117 |
'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
|
118 |
'username': config['username'],
|
119 |
+
'script_name': script_name,
|
120 |
+
'runtime': "N/A"
|
121 |
}
|
122 |
finally:
|
123 |
if client:
|
|
|
130 |
check_and_run_script(config)
|
131 |
|
132 |
# 创建表格头
|
133 |
+
table = "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+\n"
|
134 |
+
table += "| Index | Hostname | Script Name | Status | Last Check | Username | Runtime |\n"
|
135 |
+
table += "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+\n"
|
136 |
|
137 |
# 添加每个VPS的状态
|
138 |
for key, status in vps_status.items():
|
139 |
hostname, script_name = key.split(':')
|
140 |
+
table += "| {:<7} | {:<21} | {:<16} | {:<8} | {:<23} | {:<8} | {:<8} |\n".format(
|
141 |
status['index'],
|
142 |
hostname[:21],
|
143 |
script_name[:16],
|
144 |
status['status'][:8],
|
145 |
status['last_check'],
|
146 |
+
status['username'][:8],
|
147 |
+
status['runtime'][:8]
|
148 |
)
|
149 |
+
table += "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+\n"
|
150 |
|
151 |
logger.info("\n" + table)
|
152 |
|
|
|
162 |
<th>Status</th>
|
163 |
<th>Last Check</th>
|
164 |
<th>Username</th>
|
165 |
+
<th>Runtime</th>
|
166 |
</tr>
|
167 |
{% for key, data in vps_status.items() %}
|
168 |
<tr>
|
|
|
172 |
<td>{{ data.status }}</td>
|
173 |
<td>{{ data.last_check }}</td>
|
174 |
<td>{{ data.username }}</td>
|
175 |
+
<td>{{ data.runtime }}</td>
|
176 |
</tr>
|
177 |
{% endfor %}
|
178 |
</table>
|