-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull-discovery.mjs
More file actions
146 lines (115 loc) · 4.46 KB
/
full-discovery.mjs
File metadata and controls
146 lines (115 loc) · 4.46 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
import NetMapSNMP from './libsnmp.js';
import Database from 'better-sqlite3';
const db = new Database('./netmap.db');
const snmp = new NetMapSNMP();
console.log('=== FULL NETWORK DISCOVERY ===\n');
// 1. Get all existing devices
const devices = db.prepare('SELECT * FROM devices WHERE ip IS NOT NULL').all();
console.log(`Found ${devices.length} existing devices\n`);
// 2. Scan each device for LLDP neighbors
const allNeighbors = new Map();
let totalNeighbors = 0;
for (const device of devices) {
console.log(`Scanning ${device.sysname} (${device.ip})...`);
try {
const result = await snmp.discoverProtocols(device.ip, 15000);
if (result.lldp.length > 0) {
console.log(` Found ${result.lldp.length} LLDP neighbors`);
for (const n of result.lldp) {
const key = n.chassisId || n.sysName || n.ip;
if (key && !allNeighbors.has(key)) {
allNeighbors.set(key, {
...n,
discoveredFrom: device.sysname,
discoveredFromIp: device.ip
});
totalNeighbors++;
}
}
}
} catch (err) {
console.log(` Error: ${err.message}`);
}
}
console.log(`\n=== DISCOVERED ${totalNeighbors} UNIQUE NEIGHBORS ===\n`);
// 3. Identify Access Points
const accessPoints = [];
const switches = [];
const unknown = [];
for (const [key, n] of allNeighbors) {
const name = (n.sysName || '').toLowerCase();
const portId = (n.portId || '').toLowerCase();
if (name.includes('ap') || name.includes('pdv') || portId.includes('mgt')) {
accessPoints.push(n);
} else if (name.includes('l2') || name.includes('l3') || name.includes('rack')) {
switches.push(n);
} else {
unknown.push(n);
}
}
console.log(`Access Points: ${accessPoints.length}`);
console.log(`Switches: ${switches.length}`);
console.log(`Unknown: ${unknown.length}`);
// 4. Show Access Points
console.log('\n=== ACCESS POINTS ===');
accessPoints.forEach((ap, i) => {
console.log(`${i+1}. ${ap.sysName || 'N/A'}`);
console.log(` ChassisId: ${ap.chassisId}`);
console.log(` PortId: ${ap.portId}`);
console.log(` IP: ${ap.ip || 'N/A'}`);
console.log(` Discovered from: ${ap.discoveredFrom}`);
console.log('');
});
// 5. Add Access Points as neighbor devices
console.log('\n=== ADDING ACCESS POINTS TO DATABASE ===');
let added = 0;
for (const ap of accessPoints) {
const sysName = ap.sysName || `AP-${ap.chassisId}`;
const chassisId = ap.chassisId;
// Check if already exists
const existing = db.prepare('SELECT id FROM devices WHERE sysname = ? OR ip = ?').get(sysName, ap.ip);
if (!existing) {
// Create neighbor device
const nodeId = `ap-${chassisId?.replace(/:/g, '')}`;
db.prepare(`
INSERT INTO devices (ip, sysname, type, first_seen, last_seen)
VALUES (?, ?, 'neighbor', datetime('now'), datetime('now'))
`).run(ap.ip || nodeId, sysName);
console.log(`Added: ${sysName}`);
added++;
}
}
console.log(`\nAdded ${added} new Access Points`);
// 6. Update links for Access Points
console.log('\n=== UPDATING LINKS ===');
let linksAdded = 0;
for (const ap of accessPoints) {
const sysName = ap.sysName || `AP-${ap.chassisId}`;
// Find source device
const sourceDevice = db.prepare('SELECT id FROM devices WHERE sysname = ?').get(ap.discoveredFrom);
const targetDevice = db.prepare('SELECT id FROM devices WHERE sysname = ?').get(sysName);
if (sourceDevice && targetDevice) {
// Check if link exists
const existingLink = db.prepare(`
SELECT id FROM links
WHERE (device_id = ? AND remote_device_id = ?)
OR (device_id = ? AND remote_device_id = ?)
`).get(sourceDevice.id, targetDevice.id, targetDevice.id, sourceDevice.id);
if (!existingLink) {
db.prepare(`
INSERT INTO links (device_id, local_ifindex, local_ifname, remote_device_id, remote_sysname, remote_chassisid, remote_portid, protocol)
VALUES (?, ?, ?, ?, ?, ?, ?, 'LLDP')
`).run(sourceDevice.id, ap.localPort, `Port${ap.localPort}`, targetDevice.id, sysName, ap.chassisId, ap.portId);
linksAdded++;
}
}
}
console.log(`Added ${linksAdded} new links`);
// Final stats
const finalDevices = db.prepare('SELECT COUNT(*) as count FROM devices').get();
const finalLinks = db.prepare('SELECT COUNT(*) as count FROM links').get();
console.log(`\n=== FINAL STATS ===`);
console.log(`Total Devices: ${finalDevices.count}`);
console.log(`Total Links: ${finalLinks.count}`);
db.close();
console.log('\n✅ Discovery completed!');