-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggressive-cleanup.mjs
More file actions
74 lines (59 loc) · 2.53 KB
/
aggressive-cleanup.mjs
File metadata and controls
74 lines (59 loc) · 2.53 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
import Database from 'better-sqlite3';
const db = new Database('./netmap.db');
console.log('=== PULIZIA AGGRESSIVA - SOLO LINK ESSENZIALI ===\n');
const before = db.prepare('SELECT COUNT(*) as count FROM links').get();
console.log('Link prima:', before.count);
// 1. Trova i 12 device che NON hanno link LLDP (né come source né come dest)
const isolatedDevices = db.prepare(`
SELECT d.id, d.ip, d.sysname
FROM devices d
WHERE d.status = 'active'
AND d.id NOT IN (
SELECT DISTINCT device_id FROM links WHERE protocol IN ('LLDP', 'CDP')
)
AND d.id NOT IN (
SELECT DISTINCT remote_device_id FROM links WHERE protocol IN ('LLDP', 'CDP') AND remote_device_id IS NOT NULL
)
`).all();
console.log(`Device isolati (senza LLDP): ${isolatedDevices.length}`);
isolatedDevices.forEach(d => console.log(` - ${d.sysname}`));
// 2. Rimuovi TUTTI i link FDB
const removedAll = db.prepare("DELETE FROM links WHERE protocol = 'FDB'").run();
console.log(`\nRimossi TUTTI i link FDB: ${removedAll.changes}`);
// 3. Ricrea UN SOLO link FDB per ogni device isolato (verso lo switch core S6730)
const coreSwitch = db.prepare("SELECT id, ip, sysname FROM devices WHERE sysname LIKE '%S6730%'").get();
console.log(`\nCore switch: ${coreSwitch?.sysname || 'NON TROVATO'}`);
if (coreSwitch) {
for (const device of isolatedDevices) {
// Crea link dal device isolato verso il core
db.prepare(`
INSERT INTO links (device_id, remote_device_id, remote_ip, remote_sysname, protocol)
VALUES (?, ?, ?, ?, 'FDB-essential')
`).run(device.id, coreSwitch.id, coreSwitch.ip, coreSwitch.sysname);
console.log(` + ${device.sysname} -> ${coreSwitch.sysname}`);
}
}
// Statistiche finali
const after = db.prepare('SELECT COUNT(*) as count FROM links').get();
const byProtocol = db.prepare(`
SELECT protocol, COUNT(*) as count
FROM links
GROUP BY protocol
`).all();
console.log('\n=== RISULTATO ===');
console.log(`Link totali: ${before.count} → ${after.count}`);
console.log('Per protocollo:');
byProtocol.forEach(p => console.log(` - ${p.protocol}: ${p.count}`));
// Verifica copertura
const coverage = db.prepare(`
SELECT COUNT(DISTINCT d.id) as withLinks
FROM devices d
WHERE d.status = 'active' AND d.id IN (
SELECT DISTINCT device_id FROM links
UNION
SELECT DISTINCT remote_device_id FROM links WHERE remote_device_id IS NOT NULL
)
`).get();
const total = db.prepare("SELECT COUNT(*) as count FROM devices WHERE status = 'active'").get();
console.log(`\nDevice con link: ${coverage.withLinks}/${total.count}`);
db.close();