-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
161 lines (140 loc) · 6.4 KB
/
api.php
File metadata and controls
161 lines (140 loc) · 6.4 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
<?php
header('Content-Type: application/json; charset=utf-8');
require __DIR__ . '/db.php';
$action = $_GET['action'] ?? '';
try {
switch ($action) {
/* ── Dashboard summary (latest scan) ── */
case 'dashboard':
$scan = $pdo->query("SELECT * FROM scan_runs ORDER BY id DESC LIMIT 1")->fetch();
if (!$scan) {
echo json_encode(['success' => true, 'data' => null]);
exit;
}
$sid = $scan['id'];
$hosts = $pdo->prepare("SELECT * FROM proxmox_hosts WHERE scan_id = ?");
$hosts->execute([$sid]);
$vms = $pdo->prepare("SELECT * FROM virtual_machines WHERE scan_id = ? ORDER BY host_ip, vmid");
$vms->execute([$sid]);
$lxcs = $pdo->prepare("SELECT * FROM lxc_containers WHERE scan_id = ? ORDER BY host_ip, vmid");
$lxcs->execute([$sid]);
$docker = $pdo->prepare("SELECT * FROM docker_containers WHERE scan_id = ? ORDER BY host_ip, name");
$docker->execute([$sid]);
$storage = $pdo->prepare("SELECT * FROM storage_info WHERE scan_id = ? ORDER BY host_ip, storage_name");
$storage->execute([$sid]);
$network = $pdo->prepare("SELECT * FROM network_info WHERE scan_id = ? ORDER BY host_ip, interface_name");
$network->execute([$sid]);
$errors = $pdo->prepare("SELECT * FROM scan_errors WHERE scan_id = ? ORDER BY occurred_at DESC");
$errors->execute([$sid]);
echo json_encode([
'success' => true,
'data' => [
'scan' => $scan,
'hosts' => $hosts->fetchAll(),
'vms' => $vms->fetchAll(),
'lxcs' => $lxcs->fetchAll(),
'docker' => $docker->fetchAll(),
'storage' => $storage->fetchAll(),
'network' => $network->fetchAll(),
'errors' => $errors->fetchAll(),
]
]);
break;
/* ── Scan history ── */
case 'scans':
$scans = $pdo->query("SELECT * FROM scan_runs ORDER BY id DESC LIMIT 50")->fetchAll();
echo json_encode(['success' => true, 'data' => $scans]);
break;
/* ── Get specific scan ── */
case 'scan':
$id = (int)($_GET['id'] ?? 0);
if (!$id) {
echo json_encode(['error' => 'Missing scan ID']);
exit;
}
$scan = $pdo->prepare("SELECT * FROM scan_runs WHERE id = ?");
$scan->execute([$id]);
$scan = $scan->fetch();
if (!$scan) {
echo json_encode(['error' => 'Scan not found']);
exit;
}
$hosts = $pdo->prepare("SELECT * FROM proxmox_hosts WHERE scan_id = ?");
$hosts->execute([$id]);
$vms = $pdo->prepare("SELECT * FROM virtual_machines WHERE scan_id = ? ORDER BY host_ip, vmid");
$vms->execute([$id]);
$lxcs = $pdo->prepare("SELECT * FROM lxc_containers WHERE scan_id = ? ORDER BY host_ip, vmid");
$lxcs->execute([$id]);
$docker = $pdo->prepare("SELECT * FROM docker_containers WHERE scan_id = ? ORDER BY host_ip, name");
$docker->execute([$id]);
$storage = $pdo->prepare("SELECT * FROM storage_info WHERE scan_id = ? ORDER BY host_ip, storage_name");
$storage->execute([$id]);
$network = $pdo->prepare("SELECT * FROM network_info WHERE scan_id = ? ORDER BY host_ip, interface_name");
$network->execute([$id]);
$errors = $pdo->prepare("SELECT * FROM scan_errors WHERE scan_id = ? ORDER BY occurred_at DESC");
$errors->execute([$id]);
echo json_encode([
'success' => true,
'data' => [
'scan' => $scan,
'hosts' => $hosts->fetchAll(),
'vms' => $vms->fetchAll(),
'lxcs' => $lxcs->fetchAll(),
'docker' => $docker->fetchAll(),
'storage' => $storage->fetchAll(),
'network' => $network->fetchAll(),
'errors' => $errors->fetchAll(),
]
]);
break;
/* ── Trigger new scan ── */
case 'trigger_scan':
$scannerPath = __DIR__ . '/scanner/venv/bin/python';
$scriptPath = __DIR__ . '/scanner/scan.py';
$logPath = __DIR__ . '/scanner/last_scan.log';
$cmd = "$scannerPath $scriptPath > $logPath 2>&1 &";
exec($cmd);
echo json_encode(['success' => true, 'message' => 'Scan gestartet']);
break;
/* ── Get scan log ── */
case 'scan_log':
$logPath = __DIR__ . '/scanner/last_scan.log';
$log = file_exists($logPath) ? file_get_contents($logPath) : 'Kein Log vorhanden';
echo json_encode(['success' => true, 'data' => $log]);
break;
/* ── Comparison of two scans ── */
case 'compare':
$id1 = (int)($_GET['id1'] ?? 0);
$id2 = (int)($_GET['id2'] ?? 0);
if (!$id1 || !$id2) {
echo json_encode(['error' => 'Two scan IDs required']);
exit;
}
$getVms = function($id) use ($pdo) {
$st = $pdo->prepare("SELECT vmid, name, status, host_ip FROM virtual_machines WHERE scan_id = ?");
$st->execute([$id]);
return $st->fetchAll();
};
$getLxcs = function($id) use ($pdo) {
$st = $pdo->prepare("SELECT vmid, name, status, host_ip FROM lxc_containers WHERE scan_id = ?");
$st->execute([$id]);
return $st->fetchAll();
};
echo json_encode([
'success' => true,
'data' => [
'scan1_vms' => $getVms($id1),
'scan2_vms' => $getVms($id2),
'scan1_lxcs' => $getLxcs($id1),
'scan2_lxcs' => $getLxcs($id2),
]
]);
break;
default:
echo json_encode(['error' => 'Unknown action: ' . $action]);
break;
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}