taito0 commited on
Commit
ee3434e
·
verified ·
1 Parent(s): d30f6c6

Update vps_monitor.py

Browse files
Files changed (1) hide show
  1. vps_monitor.py +23 -23
vps_monitor.py CHANGED
@@ -77,8 +77,9 @@ def check_and_run_script(config):
77
  status = "Restarted"
78
  logger.info(f"Script restarted on {config['hostname']}")
79
 
80
- vps_status[config['hostname']] = {
81
  'index': config['index'],
 
82
  'status': status,
83
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
84
  'username': config['username']
@@ -86,8 +87,9 @@ def check_and_run_script(config):
86
 
87
  except Exception as e:
88
  logger.error(f"Error occurred while checking VPS {config['index']} - {config['hostname']}: {str(e)}")
89
- vps_status[config['hostname']] = {
90
  'index': config['index'],
 
91
  'status': f"Error: {str(e)}",
92
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
93
  'username': config['username']
@@ -108,15 +110,14 @@ def check_all_vps():
108
  table += "+---------+-----------------------+----------+-------------------------+----------+\n"
109
 
110
  # 添加每个VPS的状态
111
- for config in vps_configs:
112
- hostname = config['hostname']
113
- status = vps_status.get(hostname, {})
114
  table += "| {:<7} | {:<21} | {:<8} | {:<23} | {:<8} |\n".format(
115
- status.get('index', ''),
116
- hostname[:21],
117
- status.get('status', '')[:8],
118
- status.get('last_check', ''),
119
- status.get('username', '')[:8]
120
  )
121
  table += "+---------+-----------------------+----------+-------------------------+----------+\n"
122
 
@@ -134,25 +135,24 @@ def index():
134
  <th>Last Check</th>
135
  <th>Username</th>
136
  </tr>
137
- {% for config in vps_configs %}
138
- {% set hostname = config['hostname'] %}
139
- {% set data = vps_status.get(hostname, {}) %}
140
  <tr>
141
- <td>{{ data.get('index', '') }}</td>
142
- <td><a href="/status/{{ hostname }}">{{ hostname }}</a></td>
143
- <td>{{ data.get('status', '') }}</td>
144
- <td>{{ data.get('last_check', '') }}</td>
145
- <td>{{ data.get('username', '') }}</td>
146
  </tr>
147
  {% endfor %}
148
  </table>
149
  '''
150
- return render_template_string(html, vps_configs=get_vps_configs(), vps_status=vps_status)
151
 
152
- @app.route('/status/<hostname>')
153
- def vps_status_detail(hostname):
154
- if hostname in vps_status:
155
- return jsonify(vps_status[hostname])
156
  else:
157
  return jsonify({"error": "VPS not found"}), 404
158
 
 
77
  status = "Restarted"
78
  logger.info(f"Script restarted on {config['hostname']}")
79
 
80
+ vps_status[config['index']] = {
81
  'index': config['index'],
82
+ 'hostname': config['hostname'],
83
  'status': status,
84
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
85
  'username': config['username']
 
87
 
88
  except Exception as e:
89
  logger.error(f"Error occurred while checking VPS {config['index']} - {config['hostname']}: {str(e)}")
90
+ vps_status[config['index']] = {
91
  'index': config['index'],
92
+ 'hostname': config['hostname'],
93
  'status': f"Error: {str(e)}",
94
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
95
  'username': config['username']
 
110
  table += "+---------+-----------------------+----------+-------------------------+----------+\n"
111
 
112
  # 添加每个VPS的状态
113
+ for index in sorted(vps_status.keys()):
114
+ status = vps_status[index]
 
115
  table += "| {:<7} | {:<21} | {:<8} | {:<23} | {:<8} |\n".format(
116
+ status['index'],
117
+ status['hostname'][:21],
118
+ status['status'][:8],
119
+ status['last_check'],
120
+ status['username'][:8]
121
  )
122
  table += "+---------+-----------------------+----------+-------------------------+----------+\n"
123
 
 
135
  <th>Last Check</th>
136
  <th>Username</th>
137
  </tr>
138
+ {% for index in vps_status.keys()|sort %}
139
+ {% set data = vps_status[index] %}
 
140
  <tr>
141
+ <td>{{ data['index'] }}</td>
142
+ <td><a href="/status/{{ data['index'] }}">{{ data['hostname'] }}</a></td>
143
+ <td>{{ data['status'] }}</td>
144
+ <td>{{ data['last_check'] }}</td>
145
+ <td>{{ data['username'] }}</td>
146
  </tr>
147
  {% endfor %}
148
  </table>
149
  '''
150
+ return render_template_string(html, vps_status=vps_status)
151
 
152
+ @app.route('/status/<int:index>')
153
+ def vps_status_detail(index):
154
+ if index in vps_status:
155
+ return jsonify(vps_status[index])
156
  else:
157
  return jsonify({"error": "VPS not found"}), 404
158