File size: 8,090 Bytes
84f63ed
 
 
 
 
 
 
 
d732277
84f63ed
 
 
 
 
 
a7ec032
 
 
 
 
 
 
 
84f63ed
 
 
 
 
 
 
 
 
 
8eb57c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84f63ed
 
 
 
 
8eb57c1
84f63ed
 
 
 
 
 
 
 
 
 
 
 
 
 
1dba9ff
 
 
 
 
 
 
 
 
 
84f63ed
 
d732277
 
1dba9ff
d732277
1dba9ff
 
 
d732277
1dba9ff
 
 
 
84f63ed
1dba9ff
 
84f63ed
1dba9ff
84f63ed
1dba9ff
 
d732277
1dba9ff
 
84f63ed
1dba9ff
d732277
1dba9ff
 
84f63ed
8eb57c1
84f63ed
 
 
8eb57c1
d732277
1dba9ff
 
84f63ed
 
 
8eb57c1
 
 
84f63ed
a7ec032
84f63ed
8eb57c1
d732277
1dba9ff
 
84f63ed
 
 
 
 
 
a7ec032
84f63ed
 
 
1021fca
 
1dba9ff
 
 
1021fca
 
8eb57c1
1021fca
1dba9ff
1021fca
 
 
 
 
d732277
1dba9ff
 
1021fca
1dba9ff
1021fca
 
84f63ed
 
 
 
 
 
 
 
 
8eb57c1
84f63ed
 
 
d732277
1dba9ff
84f63ed
8eb57c1
84f63ed
 
8eb57c1
 
84f63ed
 
 
d732277
1dba9ff
84f63ed
 
 
 
 
 
8eb57c1
 
 
 
84f63ed
8eb57c1
84f63ed
 
 
 
 
 
 
 
 
 
 
 
1021fca
84f63ed
 
 
 
 
 
 
 
 
 
 
1021fca
84f63ed
 
 
 
 
 
 
1021fca
84f63ed
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import paramiko
import schedule
import time
import os
import sys
from flask import Flask, jsonify, render_template_string
from threading import Thread
import logging
from datetime import datetime, timedelta

app = Flask(__name__)

vps_status = {}

# 设置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.StreamHandler(sys.stderr)
    ]
)
logger = logging.getLogger()

def get_vps_configs():
    configs = []
    index = 1
    while True:
        hostname = os.environ.get(f'HOSTNAME_{index}')
        if not hostname:
            break
        
        username = os.environ.get(f'USERNAME_{index}')
        password = os.environ.get(f'PASSWORD_{index}')
        
        script_paths = []
        script_index = 1
        while True:
            script_path = os.environ.get(f'SCRIPT_PATHS_{index}_{script_index}')
            if not script_path:
                break
            script_paths.append(script_path.strip())
            script_index += 1
        
        for script_path in script_paths:
            config = {
                'index': index,
                'hostname': hostname,
                'username': username,
                'password': password,
                'script_path': script_path
            }
            configs.append(config)
        
        index += 1
    return configs

def check_and_run_script(config):
    logger.info(f"Checking VPS {config['index']}: {config['hostname']} - {config['script_path']}")
    client = None
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        
        client.connect(
            hostname=config['hostname'],
            username=config['username'],
            password=config['password'],
            port=22
        )
        
        script_path = config['script_path']
        script_name = os.path.basename(script_path)
        key = f"{config['hostname']}:{script_name}"
        
        # 获取上次保存的 PID(如果有)
        last_pid = vps_status.get(key, {}).get('pid', None)
        
        # 检查进程是否在运行
        if last_pid:
            check_command = f"ps -p {last_pid} -o pid,etime,args"
        else:
            check_command = f"ps aux | grep '{script_path}' | grep -v grep"
        
        stdin, stdout, stderr = client.exec_command(check_command)
        output = stdout.read().decode('utf-8').strip()
        
        if output and (last_pid or script_path in output):
            parts = output.split()
            if last_pid:
                pid = last_pid
                runtime = parts[1] if len(parts) > 1 else "Unknown"
            else:
                pid = parts[1] if len(parts) > 1 else "Unknown"
                runtime = parts[9] if len(parts) > 9 else "Unknown"
            status = "Running"
            logger.info(f"Script {script_name} is running. PID: {pid}, Runtime: {runtime}")
        else:
            logger.info(f"Script {script_name} not running. Attempting to restart.")
            restart_command = f"nohup /bin/sh {script_path} > /dev/null 2>&1 & echo $!"
            stdin, stdout, stderr = client.exec_command(restart_command)
            new_pid = stdout.read().decode('utf-8').strip()
            
            if new_pid.isdigit():
                pid = new_pid
                runtime = "Just started"
                status = "Restarted"
                logger.info(f"Script {script_name} restarted. New PID: {pid}")
            else:
                pid = "N/A"
                runtime = "N/A"
                status = "Restart Failed"
                logger.error(f"Failed to restart script {script_name}")
        
        vps_status[key] = {
            'index': config['index'],
            'status': status,
            'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
            'username': config['username'],
            'script_name': script_name,
            'runtime': runtime,
            'pid': pid
        }
        
    except Exception as e:
        logger.error(f"Error occurred while checking VPS {config['index']} - {config['hostname']} - {script_name}: {str(e)}")
        key = f"{config['hostname']}:{script_name}"
        vps_status[key] = {
            'index': config['index'],
            'status': f"Error: {str(e)}",
            'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
            'username': config['username'],
            'script_name': script_name,
            'runtime': "N/A",
            'pid': "N/A"
        }
    finally:
        if client:
            client.close()

def check_all_vps():
    logger.info("Starting VPS check")
    vps_configs = get_vps_configs()
    for config in vps_configs:
        check_and_run_script(config)
    
    # 创建表格头
    table = "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+-------+\n"
    table += "| Index   | Hostname              | Script Name      | Status   | Last Check              | Username | Runtime  | PID   |\n"
    table += "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+-------+\n"
    
    # 添加每个VPS的状态
    for key, status in vps_status.items():
        hostname, script_name = key.split(':')
        table += "| {:<7} | {:<21} | {:<16} | {:<8} | {:<23} | {:<8} | {:<8} | {:<5} |\n".format(
            status['index'],
            hostname[:21],
            script_name[:16],
            status['status'][:8],
            status['last_check'],
            status['username'][:8],
            status['runtime'][:8],
            status['pid'][:5]
        )
        table += "+---------+-----------------------+------------------+----------+-------------------------+----------+----------+-------+\n"
    
    logger.info("\n" + table)

@app.route('/')
def index():
    html = '''
    <h1>VPS Status Overview</h1>
    <table border="1">
        <tr>
            <th>Index</th>
            <th>Hostname</th>
            <th>Script Name</th>
            <th>Status</th>
            <th>Last Check</th>
            <th>Username</th>
            <th>Runtime</th>
            <th>PID</th>
        </tr>
        {% for key, data in vps_status.items() %}
        <tr>
            <td>{{ data.index }}</td>
            <td><a href="/status/{{ key }}">{{ key.split(':')[0] }}</a></td>
            <td>{{ data.script_name }}</td>
            <td>{{ data.status }}</td>
            <td>{{ data.last_check }}</td>
            <td>{{ data.username }}</td>
            <td>{{ data.runtime }}</td>
            <td>{{ data.pid }}</td>
        </tr>
        {% endfor %}
    </table>
    '''
    return render_template_string(html, vps_status=vps_status)

@app.route('/status/<path:key>')
def vps_status_detail(key):
    if key in vps_status:
        return jsonify(vps_status[key])
    else:
        return jsonify({"error": "VPS or script not found"}), 404

@app.route('/health')
def health_check():
    return jsonify({"status": "healthy", "uptime": time.time() - start_time}), 200

def run_flask():
    app.run(host='0.0.0.0', port=8080)

def main():
    global start_time
    start_time = time.time()
    
    logger.info("===== VPS monitoring script is starting =====")
    
    flask_thread = Thread(target=run_flask)
    flask_thread.start()
    logger.info("Flask server started in background")

    logger.info("Running initial VPS check")
    check_all_vps()

    schedule.every(15).minutes.do(check_all_vps)
    logger.info("Scheduled VPS check every 15 minutes")
    
    logger.info("===== VPS monitoring script is running =====")
    
    heartbeat_count = 0
    while True:
        schedule.run_pending()
        time.sleep(60)
        heartbeat_count += 1
        if heartbeat_count % 5 == 0:  # 每5分钟输出一次心跳信息
            logger.info(f"Heartbeat: Script is still running. Uptime: {heartbeat_count} minutes")

if __name__ == "__main__":
    main()