xjf6b commited on
Commit
8eb57c1
·
verified ·
1 Parent(s): 84f63ed

Update vps_monitor.py

Browse files
Files changed (1) hide show
  1. vps_monitor.py +53 -34
vps_monitor.py CHANGED
@@ -30,26 +30,39 @@ def get_vps_configs():
30
  if not hostname:
31
  break
32
 
33
- config = {
34
- 'index': index,
35
- 'hostname': hostname,
36
- 'username': os.environ.get(f'USERNAME_{index}'),
37
- 'password': os.environ.get(f'PASSWORD_{index}'),
38
- 'script_path': os.environ.get(f'SCRIPT_PATH_{index}')
39
- }
40
- configs.append(config)
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # 添加调试输出
43
- logger.info(f"Config {index}: {config}")
44
- print(f"Config {index}: {config}")
45
  sys.stdout.flush()
46
 
47
  index += 1
48
  return configs
49
 
50
  def check_and_run_script(config):
51
- logger.info(f"Checking VPS {config['index']}: {config['hostname']}")
52
- print(f"Checking VPS {config['index']}: {config['hostname']}")
53
  sys.stdout.flush()
54
  client = None
55
  try:
@@ -72,42 +85,46 @@ def check_and_run_script(config):
72
  stdin, stdout, stderr = client.exec_command(check_command)
73
  if stdout.read():
74
  status = "Running"
75
- logger.info(f"Script is running on {config['hostname']}")
76
  else:
77
- logger.info(f"Script not running on {config['hostname']}. Executing restart script.")
78
  restart_command = f"/bin/sh {script_path}" # 使用完整路径的sh
79
  stdin, stdout, stderr = client.exec_command(restart_command)
80
  exit_status = stdout.channel.recv_exit_status()
81
 
82
  if exit_status == 0:
83
  status = "Restarted"
84
- logger.info(f"Restart script executed successfully on {config['hostname']}")
85
  else:
86
  error_output = stderr.read().decode('utf-8')
87
  status = f"Error: {error_output}"
88
- logger.error(f"Error executing restart script on {config['hostname']}: {error_output}")
89
 
90
- vps_status[config['hostname']] = {
 
91
  'index': config['index'],
92
  'status': status,
93
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
94
- 'username': config['username']
 
95
  }
96
 
97
  except Exception as e:
98
- logger.error(f"Error occurred while checking VPS {config['index']} - {config['hostname']}: {str(e)}")
99
- vps_status[config['hostname']] = {
 
100
  'index': config['index'],
101
  'status': f"Error: {str(e)}",
102
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
103
- 'username': config['username']
 
104
  }
105
  finally:
106
  if client:
107
  client.close()
108
  logger.info(f"SSH connection closed for VPS {config['index']}: {config['hostname']}")
109
- logger.info(f"Finished checking VPS {config['index']}: {config['hostname']}")
110
- print(f"Finished checking VPS {config['index']}: {config['hostname']}")
111
  sys.stdout.flush()
112
 
113
  def check_all_vps():
@@ -118,8 +135,8 @@ def check_all_vps():
118
  for config in vps_configs:
119
  check_and_run_script(config)
120
  logger.info("Finished VPS check")
121
- for hostname, status in vps_status.items():
122
- print(f"VPS {status['index']} - {hostname}: Status: {status['status']}, Username: {status['username']}")
123
  sys.stdout.flush()
124
 
125
  @app.route('/')
@@ -130,14 +147,16 @@ def index():
130
  <tr>
131
  <th>Index</th>
132
  <th>Hostname</th>
 
133
  <th>Status</th>
134
  <th>Last Check</th>
135
  <th>Username</th>
136
  </tr>
137
- {% for hostname, data in vps_status.items() %}
138
  <tr>
139
  <td>{{ data.index }}</td>
140
- <td><a href="/status/{{ hostname }}">{{ hostname }}</a></td>
 
141
  <td>{{ data.status }}</td>
142
  <td>{{ data.last_check }}</td>
143
  <td>{{ data.username }}</td>
@@ -147,12 +166,12 @@ def index():
147
  '''
148
  return render_template_string(html, vps_status=vps_status)
149
 
150
- @app.route('/status/<hostname>')
151
- def vps_status_detail(hostname):
152
- if hostname in vps_status:
153
- return jsonify(vps_status[hostname])
154
  else:
155
- return jsonify({"error": "VPS not found"}), 404
156
 
157
  @app.route('/health')
158
  def health_check():
@@ -180,8 +199,8 @@ def main():
180
  print(f"Found {len(vps_configs)} VPS configurations")
181
  sys.stdout.flush()
182
  for config in vps_configs:
183
- logger.info(f"VPS configured: {config['hostname']}")
184
- print(f"VPS configured: {config['hostname']}")
185
  sys.stdout.flush()
186
 
187
  logger.info("Running initial VPS check")
 
30
  if not hostname:
31
  break
32
 
33
+ username = os.environ.get(f'USERNAME_{index}')
34
+ password = os.environ.get(f'PASSWORD_{index}')
35
+
36
+ script_paths = []
37
+ script_index = 1
38
+ while True:
39
+ script_path = os.environ.get(f'SCRIPT_PATHS_{index}_{script_index}')
40
+ if not script_path:
41
+ break
42
+ script_paths.append(script_path.strip())
43
+ script_index += 1
44
+
45
+ for script_path in script_paths:
46
+ config = {
47
+ 'index': index,
48
+ 'hostname': hostname,
49
+ 'username': username,
50
+ 'password': password,
51
+ 'script_path': script_path
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:
 
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}"
104
+ vps_status[key] = {
105
  'index': config['index'],
106
  'status': status,
107
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
108
+ 'username': config['username'],
109
+ 'script_name': script_name
110
  }
111
 
112
  except Exception as e:
113
+ logger.error(f"Error occurred while checking VPS {config['index']} - {config['hostname']} - {script_name}: {str(e)}")
114
+ key = f"{config['hostname']}:{script_name}"
115
+ vps_status[key] = {
116
  'index': config['index'],
117
  'status': f"Error: {str(e)}",
118
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
119
+ 'username': config['username'],
120
+ 'script_name': script_name
121
  }
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():
 
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('/')
 
147
  <tr>
148
  <th>Index</th>
149
  <th>Hostname</th>
150
+ <th>Script Name</th>
151
  <th>Status</th>
152
  <th>Last Check</th>
153
  <th>Username</th>
154
  </tr>
155
+ {% for key, data in vps_status.items() %}
156
  <tr>
157
  <td>{{ data.index }}</td>
158
+ <td><a href="/status/{{ key }}">{{ key.split(':')[0] }}</a></td>
159
+ <td>{{ data.script_name }}</td>
160
  <td>{{ data.status }}</td>
161
  <td>{{ data.last_check }}</td>
162
  <td>{{ data.username }}</td>
 
166
  '''
167
  return render_template_string(html, vps_status=vps_status)
168
 
169
+ @app.route('/status/<path:key>')
170
+ def vps_status_detail(key):
171
+ if key in vps_status:
172
+ return jsonify(vps_status[key])
173
  else:
174
+ return jsonify({"error": "VPS or script not found"}), 404
175
 
176
  @app.route('/health')
177
  def health_check():
 
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")