-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.php
More file actions
248 lines (231 loc) · 13.5 KB
/
Copy pathinventory.php
File metadata and controls
248 lines (231 loc) · 13.5 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
<?php
// ============================================================
// NetDTL v3.0 — Inventaire des machines
// ============================================================
require_once __DIR__ . '/db.php';
requireAuth();
session_start();
initDB();
$pdo = getDB();
$message = ''; $msgType = '';
// ─── IP locale du serveur ───────────────────────────────────
$serverIP = $_SERVER['SERVER_ADDR'] ?? gethostbyname(gethostname());
// ─── Ajout manuel ───────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_machine'])) {
$hostname = trim($_POST['hostname'] ?? '');
$ip = trim($_POST['ip'] ?? '');
$os = normalizeOsLabel(trim($_POST['os'] ?? '')) ?? '';
$comment = trim($_POST['comment'] ?? '');
if (!$hostname || !filter_var($ip, FILTER_VALIDATE_IP)) {
$message = 'Hostname et IP valide requis.'; $msgType = 'error';
} else {
try {
$switch_port = trim($_POST['switch_port'] ?? '');
$patch_port = trim($_POST['patch_port'] ?? '');
$stmt = $pdo->prepare("INSERT INTO machines (hostname, ip, os, switch_port, patch_port, comment) VALUES (?,?,?,?,?,?)");
$stmt->execute([$hostname, $ip, $os ?: null, $switch_port ?: null, $patch_port ?: null, $comment ?: null]);
$message = "Machine $hostname ($ip) ajoutée."; $msgType = 'success';
} catch (PDOException $e) {
$message = 'Erreur : IP déjà existante ou problème BDD.'; $msgType = 'error';
}
}
}
// ─── Suppression ────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
$id = (int)$_POST['delete_id'];
$pdo->prepare("DELETE FROM machines WHERE id=?")->execute([$id]);
$message = 'Machine supprimée.'; $msgType = 'success';
}
// ─── Ping rapide d'une machine ───────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ping_id'])) {
$id = (int)$_POST['ping_id'];
$machine = $pdo->prepare("SELECT ip FROM machines WHERE id=?");
$machine->execute([$id]);
$m = $machine->fetch();
if ($m) {
$lines = runPing($m['ip']);
$stats = parsePingStats($lines);
$status = $stats['recv'] > 0 ? 'up' : 'down';
$pdo->prepare("UPDATE machines SET status=?, last_ping_ms=?, last_seen=NOW() WHERE id=?")
->execute([$status, $stats['avg_ms'], $id]);
$message = "Ping {$m['ip']} : $status" . ($stats['avg_ms'] ? " ({$stats['avg_ms']} ms)" : '');
$msgType = $status === 'up' ? 'success' : 'error';
}
}
// ─── Ping de tout l'inventaire ───────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ping_all'])) {
$machines = $pdo->query("SELECT id, ip FROM machines")->fetchAll();
$up = 0; $down = 0;
foreach ($machines as $m) {
$lines = runPing($m['ip']);
$stats = parsePingStats($lines);
$status = $stats['recv'] > 0 ? 'up' : 'down';
$pdo->prepare("UPDATE machines SET status=?, last_ping_ms=?, last_seen=NOW() WHERE id=?")
->execute([$status, $stats['avg_ms'], $m['id']]);
$status === 'up' ? $up++ : $down++;
}
$message = "Ping global terminé : $up en ligne, $down hors ligne.";
$msgType = 'success';
}
// ─── Export CSV ──────────────────────────────────────────────
if (isset($_GET['export']) && $_GET['export'] === 'csv') {
$machines = $pdo->query("SELECT hostname,ip,mac,vendor,switch_port,patch_port,os,status,open_ports,last_ping_ms,last_seen,comment FROM machines ORDER BY INET_ATON(ip)")->fetchAll();
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="inventaire_' . date('Ymd_His') . '.csv"');
echo "\xEF\xBB\xBF";
echo '"Hostname","IP","MAC","Fabricant","Port switch","Brassage","OS","Statut","Ports ouverts","Ping (ms)","Dernière vue","Commentaire"' . "\r\n";
foreach ($machines as $m) {
$m['os'] = normalizeOsLabel($m['os']) ?? '';
echo implode(',', array_map(fn($v) => '"' . str_replace('"','""',$v??'') . '"', $m)) . "\r\n";
}
exit;
}
// ─── Filtres & recherche ────────────────────────────────────
$search = trim($_GET['q'] ?? '');
$filter = $_GET['status'] ?? '';
$where = []; $params = [];
if ($search) { $where[] = "(hostname LIKE ? OR ip LIKE ? OR os LIKE ? OR comment LIKE ?)"; $p = "%$search%"; $params = array_merge($params,[$p,$p,$p,$p]); }
if (in_array($filter,['up','down','unknown'])) { $where[] = "status=?"; $params[] = $filter; }
$sql = "SELECT * FROM machines" . ($where ? ' WHERE '.implode(' AND ',$where) : '') . " ORDER BY INET_ATON(ip)";
$stmt = $pdo->prepare($sql); $stmt->execute($params);
$machines = $stmt->fetchAll();
$total = (int)$pdo->query("SELECT COUNT(*) FROM machines")->fetchColumn();
$upCount = (int)$pdo->query("SELECT COUNT(*) FROM machines WHERE status='up'")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>NetDTL — Inventaire</title>
<?php include __DIR__ . '/style.php'; ?>
<style>
.filter-bar { display:flex; gap:8px; align-items:center; flex-wrap:wrap; }
.filter-btn { padding:4px 12px; border-radius:20px; border:1px solid var(--border2); background:none; color:var(--txt2); font-size:11px; font-family:var(--sans); cursor:pointer; text-decoration:none; transition:background .15s; }
.filter-btn:hover,.filter-btn.active { background:rgba(88,166,255,.15); color:var(--accent2); border-color:var(--accent2); }
.modal-overlay { display:none; position:fixed; inset:0; background:rgba(0,0,0,.6); z-index:100; align-items:center; justify-content:center; }
.modal-overlay.open { display:flex; }
.modal { background:var(--bg2); border:1px solid var(--border2); border-radius:10px; padding:24px; width:420px; display:flex; flex-direction:column; gap:14px; }
.modal-title { font-size:14px; font-weight:600; color:var(--txt); }
.modal-actions { display:flex; gap:8px; justify-content:flex-end; margin-top:4px; }
</style>
</head>
<body>
<?php include __DIR__ . '/topbar.php'; ?>
<div class="layout">
<?php include __DIR__ . '/sidebar.php'; ?>
<main class="main">
<!-- Toolbar -->
<div class="toolbar">
<form method="get" style="display:contents">
<input type="text" name="q" placeholder="Recherche hostname, IP, OS…" value="<?= htmlspecialchars($search) ?>">
</form>
<form method="post" style="display:contents">
<button type="submit" name="ping_all" value="1" class="export-btn">◎ Ping tout</button>
</form>
<button class="run-btn" onclick="document.getElementById('add-modal').classList.add('open')">+ Ajouter</button>
<a href="?export=csv" class="export-btn">↓ CSV</a>
</div>
<div class="content">
<div class="page-title">Inventaire des machines</div>
<?php if ($message): ?>
<div class="<?= $msgType==='error'?'error-box':'success-box' ?>"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>
<!-- Stats + filtres -->
<div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px">
<div class="stats-grid" style="flex:1;min-width:300px">
<div class="stat-card"><div class="stat-label">Total</div><div class="stat-val"><?= $total ?></div></div>
<div class="stat-card"><div class="stat-label">En ligne</div><div class="stat-val green"><?= $upCount ?></div></div>
<div class="stat-card"><div class="stat-label">Résultats</div><div class="stat-val amber"><?= count($machines) ?></div></div>
</div>
<div class="filter-bar">
<a href="inventory.php" class="filter-btn <?= !$filter?'active':'' ?>">Tous</a>
<a href="?status=up<?= $search?"&q=".urlencode($search):'' ?>" class="filter-btn <?= $filter==='up'?'active':'' ?>">● Up</a>
<a href="?status=down<?= $search?"&q=".urlencode($search):'' ?>" class="filter-btn <?= $filter==='down'?'active':'' ?>">● Down</a>
<a href="?status=unknown<?= $search?"&q=".urlencode($search):'' ?>" class="filter-btn <?= $filter==='unknown'?'active':'' ?>">? Inconnu</a>
</div>
</div>
<!-- Table inventaire -->
<div class="panel">
<table class="data-table">
<thead><tr>
<th>Statut</th><th>Hostname</th><th>IP</th><th>MAC</th><th>Fabricant</th><th>Port switch</th><th>Brassage</th><th>OS</th><th>Commentaire</th><th>Actions</th>
</tr></thead>
<tbody>
<?php if (!$machines): ?>
<tr><td colspan="10" class="empty-cell">Aucune machine trouvée. Lancez une découverte réseau ou ajoutez une machine manuellement.</td></tr>
<?php endif; ?>
<?php foreach ($machines as $m): ?>
<tr>
<td><?php
$cls = $m['status']==='up'?'badge-up':($m['status']==='down'?'badge-down':'badge-unknown');
$lbl = $m['status']==='up'?'up':($m['status']==='down'?'down':'?');
echo "<span class='badge $cls'>$lbl</span>";
?></td>
<td><a href="machine.php?id=<?= $m['id'] ?>" class="link"><?= htmlspecialchars($m['hostname']) ?></a></td>
<td class="mono"><?= htmlspecialchars($m['ip']) ?></td>
<td class="mono muted small"><?php
if ($m['ip'] === $serverIP) {
echo '<span style="color:var(--accent2);font-style:italic">ce PC</span>';
} else {
echo htmlspecialchars($m['mac'] ?? '—');
}
?></td>
<td class="muted small"><?= htmlspecialchars($m['vendor'] ?? '—') ?></td>
<td class="mono small" style="color:var(--accent2)"><?= htmlspecialchars($m['switch_port'] ?? '—') ?></td>
<td class="mono small" style="color:var(--purple)"><?= htmlspecialchars($m['patch_port'] ?? '—') ?></td>
<td class="muted small"><?= htmlspecialchars(normalizeOsLabel($m['os'] ?? '') ?? '—') ?></td>
<td class="muted small"><?= htmlspecialchars($m['comment'] ?? '—') ?></td>
<td>
<div style="display:flex;gap:6px">
<form method="post" style="display:inline">
<input type="hidden" name="ping_id" value="<?= $m['id'] ?>">
<button type="submit" class="export-btn" style="padding:3px 8px;font-size:10px">◎</button>
</form>
<a href="machine.php?id=<?= $m['id'] ?>" class="export-btn" style="padding:3px 8px;font-size:10px">⊞</a>
<form method="post" style="display:inline" onsubmit="return confirm('Supprimer cette machine ?')">
<input type="hidden" name="delete_id" value="<?= $m['id'] ?>">
<button type="submit" class="danger-btn" style="padding:3px 8px;font-size:10px">✕</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</main>
</div>
<!-- Modal ajout machine -->
<div class="modal-overlay" id="add-modal" onclick="if(event.target===this)this.classList.remove('open')">
<div class="modal">
<div class="modal-title">⊞ Ajouter une machine</div>
<form method="post" style="display:flex;flex-direction:column;gap:12px">
<div class="form-grid">
<div class="form-group">
<label class="form-label">Hostname *</label>
<input type="text" name="hostname" class="form-input" placeholder="PC-BUREAU-01" required>
</div>
<div class="form-group">
<label class="form-label">Adresse IP *</label>
<input type="text" name="ip" class="form-input" placeholder="172.17.7.10" required>
</div>
</div>
<div class="form-group">
<label class="form-label">Système d'exploitation</label>
<input type="text" name="os" class="form-input" placeholder="Windows 11 Pro">
</div>
<div class="form-group">
<label class="form-label">Commentaire</label>
<textarea name="comment" class="form-textarea" placeholder="PC direction, salle serveur…"></textarea>
</div>
<div class="modal-actions">
<button type="button" class="export-btn" onclick="document.getElementById('add-modal').classList.remove('open')">Annuler</button>
<button type="submit" name="add_machine" value="1" class="run-btn">Ajouter</button>
</div>
</form>
</div>
</div>
</body>
</html>