-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
567 lines (524 loc) · 15.6 KB
/
main.py
File metadata and controls
567 lines (524 loc) · 15.6 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import os
import sys
import subprocess
import time
import threading
import psutil
import platform
import socket
import logging
import json
from flask import Flask, render_template_string, redirect, url_for, jsonify
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
BOTS_DIR = './bot'
STATUS = {}
PROCESSES = {}
DISABLED = set()
ERROR_HISTORY = {}
STATE_FILE = './bot_state.json'
def load_state():
global DISABLED
if os.path.exists(STATE_FILE):
try:
with open(STATE_FILE, 'r') as f:
data = json.load(f)
DISABLED = set(data.get('disabled', []))
except Exception:
pass
def save_state():
try:
with open(STATE_FILE, 'w') as f:
json.dump({'disabled': list(DISABLED)}, f)
except Exception:
pass
def add_error(botname, message):
if botname not in ERROR_HISTORY:
ERROR_HISTORY[botname] = []
ERROR_HISTORY[botname].append(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - {message}")
if len(ERROR_HISTORY[botname]) > 5:
ERROR_HISTORY[botname] = ERROR_HISTORY[botname][-5:]
def bot_worker(name, path):
while True:
if name in DISABLED:
STATUS[name] = 'OFFLINE'
time.sleep(5)
continue
try:
STATUS[name] = 'STARTING'
abs_path = os.path.abspath(path)
venv_python = os.path.join(abs_path, 'venv', 'bin', 'python3')
main_py = os.path.join(abs_path, 'main.py')
if not os.path.exists(venv_python):
add_error(name, f"Python not found: {venv_python}")
STATUS[name] = 'ERROR'
time.sleep(5)
continue
if not os.path.exists(main_py):
add_error(name, f"main.py not found")
STATUS[name] = 'ERROR'
time.sleep(5)
continue
PROCESSES[name] = subprocess.Popen(
[venv_python, main_py],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=abs_path,
env=os.environ.copy()
)
STATUS[name] = 'ON'
stdout, stderr = PROCESSES[name].communicate()
if PROCESSES[name].returncode != 0:
err_msg = stderr.decode('utf-8', errors='ignore').strip()
add_error(name, err_msg or f"Exit code {PROCESSES[name].returncode}")
STATUS[name] = 'DOWN'
except Exception as e:
add_error(name, str(e))
STATUS[name] = 'ERROR'
time.sleep(5)
def start_all_bots():
load_state()
for name in os.listdir(BOTS_DIR):
path = os.path.join(BOTS_DIR, name)
if os.path.isdir(path) and os.path.isfile(os.path.join(path, 'main.py')):
if name in DISABLED:
STATUS[name] = 'OFFLINE'
else:
STATUS[name] = 'STARTING'
t = threading.Thread(target=bot_worker, args=(name, path), daemon=True)
t.start()
def get_bot_cpu_usage():
usage = {}
for name, proc in PROCESSES.items():
try:
if proc.poll() is None:
p = psutil.Process(proc.pid)
cpu_percent = p.cpu_percent(interval=None)
mem_info = p.memory_info()
mem_percent = p.memory_percent(memtype='rss')
create_time = p.create_time()
uptime_sec = time.time() - create_time
if cpu_percent == 0.0:
time.sleep(0.1)
cpu_percent = p.cpu_percent(interval=None)
usage[name] = {
'cpu': cpu_percent,
'mem': mem_percent,
'uptime': uptime_sec,
}
else:
usage[name] = {'cpu': 0.0, 'mem': 0.0, 'uptime': 0}
except Exception:
usage[name] = {'cpu': 0.0, 'mem': 0.0, 'uptime': 0}
return usage
def format_uptime(seconds):
m, s = divmod(int(seconds), 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
parts = []
if d > 0:
parts.append(f"{d}d")
if h > 0:
parts.append(f"{h}h")
if m > 0:
parts.append(f"{m}m")
parts.append(f"{s}s")
return ' '.join(parts)
def get_system_info():
cpu = psutil.cpu_percent(interval=0.5)
ram = psutil.virtual_memory()
hostname = socket.gethostname()
os_info = platform.system() + " " + platform.release()
uptime_seconds = time.time() - psutil.boot_time()
uptime_str = format_uptime(uptime_seconds)
python_version = platform.python_version()
cpu_arch = platform.machine()
return {
'cpu': cpu,
'ram': ram.percent,
'ram_used_gb': ram.used / 1024**3,
'ram_total_gb': ram.total / 1024**3,
'hostname': hostname,
'os_info': os_info,
'uptime': uptime_str,
'python_version': python_version,
'cpu_arch': cpu_arch,
}
@app.route('/')
def index():
bots = STATUS
return render_template_string('''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BotCommander</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #1e1e1e;
color: #d4d4d4;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 13px;
margin: 0;
padding: 40px;
min-height: 100vh;
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header-icon {
font-size: 32px;
margin-bottom: 10px;
}
.header h1 {
font-size: 24px;
font-weight: 300;
margin: 0;
color: #ffffff;
letter-spacing: 2px;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
}
.system-info {
margin-bottom: 20px;
font-size: 12px;
color: #808080;
font-family: monospace;
background: #252526;
border: 1px solid #333;
border-radius: 4px;
padding: 16px 20px;
line-height: 1.6;
text-align: center;
}
.bots-container {
background: #252526;
border: 1px solid #333;
border-radius: 4px;
overflow: hidden;
}
.bots-header {
display: flex;
align-items: center;
padding: 12px 16px;
background: #2d2d30;
border-bottom: 1px solid #333;
gap: 16px;
}
.col-header {
font-size: 11px;
color: #808080;
text-transform: uppercase;
letter-spacing: 0.5px;
font-weight: 500;
cursor: pointer;
user-select: none;
transition: color 0.2s;
display: flex;
align-items: center;
gap: 6px;
}
.col-header:hover {
color: #d4d4d4;
}
.col-header.sort-active {
color: #4a9eff;
}
.sort-arrow {
font-size: 10px;
opacity: 0.5;
}
.col-header.sort-active .sort-arrow {
opacity: 1;
}
.h-name { width: 140px; }
.h-status { width: 100px; }
.h-cpu { width: 80px; text-align: center; }
.h-mem { width: 80px; text-align: center; }
.h-uptime { width: 80px; text-align: center; }
.h-actions { margin-left: auto; }
.bot-row {
display: flex;
align-items: center;
padding: 12px 16px;
border-bottom: 1px solid #2a2a2a;
gap: 16px;
}
.bot-row:last-child {
border-bottom: none;
}
.bot-row:hover {
background: #2a2d2e;
}
.bot-row.offline {
opacity: 0.6;
}
.bot-row.starting {
background: rgba(220, 220, 170, 0.05);
}
.col-name {
width: 140px;
font-weight: 500;
color: #ffffff;
flex-shrink: 0;
}
.col-status {
width: 100px;
flex-shrink: 0;
}
.col-metric {
width: 80px;
text-align: center;
font-family: monospace;
flex-shrink: 0;
}
.col-actions {
margin-left: auto;
display: flex;
gap: 8px;
flex-shrink: 0;
}
.btn {
display: inline-block;
padding: 6px 14px;
background: #1e1e1e;
border: 1px solid #404040;
border-radius: 3px;
color: #d4d4d4;
text-decoration: none;
font-size: 11px;
font-weight: 500;
transition: all 0.15s;
min-width: 60px;
text-align: center;
}
.btn:hover {
background: #2a2d2e;
border-color: #4a9eff;
color: #4a9eff;
}
.btn-primary {
background: #1e3a5f;
border-color: #4a9eff;
color: #4a9eff;
}
.btn-primary:hover {
background: #4a9eff;
color: #fff;
}
.btn-danger {
background: #3f1e1e;
border-color: #f48771;
color: #f48771;
}
.btn-danger:hover {
background: #f48771;
color: #fff;
}
.status-ON {
color: #4ec9b0;
font-weight: 500;
}
.status-DOWN, .status-ERROR {
color: #f48771;
font-weight: 500;
}
.status-STARTING {
color: #dcdcaa;
font-weight: 500;
}
.status-OFFLINE {
color: #6e6e6e;
}
.error-badge {
display: inline-block;
width: 6px;
height: 6px;
background: #f48771;
border-radius: 50%;
margin-left: 6px;
vertical-align: middle;
}
</style>
<script>
let sortColumn = 'cpu';
let sortDirection = 'desc';
function fetchStatus() {
fetch('/status').then(response => response.json()).then(data => {
document.getElementById('system-info').textContent = data.system_info;
renderBots(data.bots);
});
}
function renderBots(botsData) {
const container = document.getElementById('bots-rows');
container.innerHTML = '';
let botsArray = Object.entries(botsData).map(([name, info]) => ({
name, ...info,
cpuNum: parseFloat(info.cpu) || 0,
memNum: parseFloat(info.mem) || 0
}));
botsArray.sort((a, b) => {
let valA, valB;
if (sortColumn === 'name') { valA = a.name.toLowerCase(); valB = b.name.toLowerCase(); }
else if (sortColumn === 'status') { valA = a.status; valB = b.status; }
else if (sortColumn === 'cpu') { valA = a.cpuNum; valB = b.cpuNum; }
else if (sortColumn === 'mem') { valA = a.memNum; valB = b.memNum; }
else if (sortColumn === 'uptime') { valA = a.uptime; valB = b.uptime; }
if (valA < valB) return sortDirection === 'asc' ? -1 : 1;
if (valA > valB) return sortDirection === 'asc' ? 1 : -1;
return 0;
});
for (const bot of botsArray) {
const row = document.createElement('div');
row.className = 'bot-row';
if (bot.status === 'OFFLINE') row.classList.add('offline');
else if (bot.status.startsWith('STARTING')) row.classList.add('starting');
const statusClass = 'status-' + bot.status.split(' ')[0];
const hasErrors = bot.errors && bot.errors.length > 0;
let actionsHtml = '';
if (bot.status === 'OFFLINE') {
actionsHtml = `<a href="/enable/${bot.name}" class="btn btn-primary">Start</a>`;
} else {
actionsHtml = `
<a href="/restart/${bot.name}" class="btn">Restart</a>
<a href="/stop/${bot.name}" class="btn btn-danger">Stop</a>
<a href="/disable/${bot.name}" class="btn">Disable</a>
`;
}
row.innerHTML = `
<div class="col-name">${bot.name}${hasErrors ? '<span class="error-badge" title="' + bot.errors.join('\\n') + '"></span>' : ''}</div>
<div class="col-status ${statusClass}">${bot.status}</div>
<div class="col-metric">${bot.cpu.toFixed(1)}%</div>
<div class="col-metric">${bot.mem.toFixed(1)}%</div>
<div class="col-metric">${bot.uptime || '-'}</div>
<div class="col-actions">${actionsHtml}</div>
`;
container.appendChild(row);
}
updateHeaderArrows();
}
function setSort(column) {
if (sortColumn === column) {
sortDirection = sortDirection === 'asc' ? 'desc' : 'asc';
} else {
sortColumn = column;
sortDirection = 'desc';
}
fetchStatus();
}
function updateHeaderArrows() {
document.querySelectorAll('.col-header').forEach(el => {
el.classList.remove('sort-active');
const arrow = el.querySelector('.sort-arrow');
if (arrow) arrow.textContent = '⇅';
});
const activeHeader = document.querySelector(`.col-header[data-sort="${sortColumn}"]`);
if (activeHeader) {
activeHeader.classList.add('sort-active');
const arrow = activeHeader.querySelector('.sort-arrow');
if (arrow) arrow.textContent = sortDirection === 'asc' ? '▲' : '▼';
}
}
setInterval(fetchStatus, 1000);
window.onload = fetchStatus;
</script>
</head>
<body>
<div class="header">
<div class="header-icon">⌘</div>
<h1>BotCommander</h1>
</div>
<div id="system-info" class="system-info">Loading...</div>
<div class="bots-container">
<div class="bots-header">
<div class="col-header h-name" data-sort="name" onclick="setSort('name')">Name <span class="sort-arrow">⇅</span></div>
<div class="col-header h-status" data-sort="status" onclick="setSort('status')">Status <span class="sort-arrow">⇅</span></div>
<div class="col-header h-cpu sort-active" data-sort="cpu" onclick="setSort('cpu')">CPU <span class="sort-arrow">▼</span></div>
<div class="col-header h-mem" data-sort="mem" onclick="setSort('mem')">RAM <span class="sort-arrow">⇅</span></div>
<div class="col-header h-uptime" data-sort="uptime" onclick="setSort('uptime')">Uptime <span class="sort-arrow">⇅</span></div>
<div class="col-header h-actions">Actions</div>
</div>
<div id="bots-rows"></div>
</div>
</body>
</html>
''', bots=bots)
@app.route('/status')
def status():
system = get_system_info()
bot_stats = get_bot_cpu_usage()
bots_data = {}
for name, status in STATUS.items():
errors = ERROR_HISTORY.get(name, [])
usage = bot_stats.get(name, {'cpu': 0.0, 'mem': 0.0, 'uptime': 0})
bots_data[name] = {
'status': status,
'cpu': usage['cpu'],
'mem': usage['mem'],
'uptime': format_uptime(usage['uptime']) if usage['uptime'] > 0 else '-',
'errors': errors,
}
system_info_text = (
f"{system['hostname']} | {system['os_info']} | "
f"CPU: {system['cpu']:.1f}% | RAM: {system['ram']:.1f}% ({system['ram_used_gb']:.1f}/{system['ram_total_gb']:.1f} GB) | "
f"Uptime: {system['uptime']} | Python: {system['python_version']} | {system['cpu_arch']}"
)
return jsonify({
'system_info': system_info_text,
'bots': bots_data,
})
@app.route('/restart/<botname>')
def restart_bot(botname):
proc = PROCESSES.get(botname)
if proc and proc.poll() is None:
proc.terminate()
STATUS[botname] = 'STARTING'
return redirect(url_for('index'))
@app.route('/stop/<botname>')
def stop_bot(botname):
proc = PROCESSES.get(botname)
if proc and proc.poll() is None:
proc.terminate()
STATUS[botname] = 'OFFLINE'
return redirect(url_for('index'))
@app.route('/disable/<botname>')
def disable_bot(botname):
proc = PROCESSES.get(botname)
if proc and proc.poll() is None:
proc.terminate()
DISABLED.add(botname)
STATUS[botname] = 'OFFLINE'
save_state()
return redirect(url_for('index'))
@app.route('/enable/<botname>')
def enable_bot(botname):
if botname in DISABLED:
DISABLED.remove(botname)
save_state()
STATUS[botname] = 'STARTING'
path = os.path.join(BOTS_DIR, botname)
t = threading.Thread(target=bot_worker, args=(botname, path), daemon=True)
t.start()
return redirect(url_for('index'))
if __name__ == '__main__':
start_all_bots()
local_ip = socket.gethostbyname(socket.gethostname())
print(f" ⌘ BotCommander http://{local_ip}:9999")
try:
app.run(host='0.0.0.0', port=9999)
except OSError as e:
if e.errno == 98 or "Address already in use" in str(e):
print(f"[err] Port 9999 in use")
else:
print(f"[err] {e}")
sys.exit(1)
app.run(host='0.0.0.0', port=9999)