-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathportmap.php
More file actions
192 lines (172 loc) · 6.28 KB
/
portmap.php
File metadata and controls
192 lines (172 loc) · 6.28 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
<?php
require_once 'config.php';
$switch_ip = $_GET['switch_ip'] ?? '';
if (!filter_var($switch_ip, FILTER_VALIDATE_IP)) {
die("Invalid IP address provided");
}
try {
$pdo = new PDO("pgsql:host=$db_host;port=$db_port;dbname=$db_name", $db_user, $db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query for switch and its endpoints
$stmt = $pdo->prepare("
WITH port_nodes AS (
SELECT DISTINCT ON (dp.port)
dp.port,
dp.mac as portmac,
n.mac,
n.time_last as last_seen,
ni.ip as ip,
COALESCE(replace(nn.nbuser,'@','\@') || ' ' || nn.nbname, ni.dns, 'Unknown') as name,
o.company as vendor,
n.vlan,
dp.speed,
CASE
WHEN lower(trim(dp.speed)) LIKE '%gbps' THEN
(regexp_replace(trim(dp.speed), '[^0-9.]', '', 'g'))::numeric * 1000
WHEN lower(trim(dp.speed)) LIKE '%mbps' THEN
(regexp_replace(trim(dp.speed), '[^0-9.]', '', 'g'))::numeric
ELSE
1
END::bigint AS speed_bits,
CASE WHEN (dpp.power IS NULL AND dpp.status = 'deliveringPower') THEN -1 ELSE dpp.power END as power
FROM device_port dp
JOIN node n ON dp.ip = n.switch AND dp.port = n.port AND dp.vlan = n.vlan AND n.active = 't'
FULL OUTER JOIN node_ip ni ON n.mac = ni.mac and ni.active = 't'
FULL OUTER JOIN node_nbt nn ON n.mac = nn.mac and nn.active = 't'
FULL OUTER JOIN device_port_power dpp on dp.ip = dpp.ip and dp.port = dpp.port
FULL OUTER JOIN oui o ON n.oui = o.oui
WHERE
dp.up='up'
AND dp.ip = :ip
AND coalesce(dp.remote_ip,'0.0.0.0') not in (select ip from device_ip)
ORDER BY dp.port, n.time_last DESC
)
SELECT
d.name as switch_name,
d.model as switch_model,
pn.*,
power,
speed,
speed_bits
FROM device d
LEFT JOIN port_nodes pn ON true
WHERE d.ip = :ip
ORDER BY portmac desc
");
$stmt->execute([':ip' => $switch_ip]);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$data) die("No data found for switch $switch_ip");
// Start building Mermaid diagram
$mermaid = "%% Netdisco Switch Port Diagram\n";
$mermaid .= "graph TD\n";
$mermaid .= " classDef switch fill:#d4e3fc,stroke:#333,stroke-width:2px;\n";
$mermaid .= " classDef port fill:#f0f0f0,stroke:#666;\n";
$mermaid .= " classDef endpoint fill:#e6ffe6,stroke:#2d572c;\n\n";
$switchName = $data[0]['switch_name'] ?: $switch_ip;
$switchModel = $data[0]['switch_model'] ?: 'Unknown Model';
$mermaid .= " switch[\"<div style='padding:10px'><h3>$switchName</h3>$switchModel<br/><small><a href='$netdisco_base_url$switch_ip'>$switch_ip</a></small></div>\"]:::switch\n";
$linkStyle = "";
$linkindex = 0;
foreach ($data as $row) {
if (!$row['port']) continue;
$portId = 'port_' . preg_replace('/[^a-z0-9]/i', '_', $row['port']);
$endpointId = 'endpoint_' . preg_replace('/[^a-z0-9]/i', '_', $row['mac']);
$portLabel = $row['port'];
if ($row['vendor']) $portLabel .= "<br/><small>{$row['vendor']}</small>";
$endpointInfo = [];
if ($row['name'] && $row['name'] !== 'Unknown') $endpointInfo[] = $row['name'];
$endpointInfo[] = $row['mac'];
if ($row['ip'] && $row['ip'] !== 'No IP') $endpointInfo[] = $row['ip'];
if ($row['vlan'] && $row['vlan'] !== 'No VLAN') $endpointInfo[] = $row['vlan'];
if ($row['power'] && $row['power'] > '0') $endpointInfo[] = round($row['power']/1000,1) . " Watts⚡";
if ($row['power'] && $row['power'] == '-1') $endpointInfo[] = "PoE on (Unknown Watts⚡)";
$endpointLabel = implode("<br/>", $endpointInfo);
$mermaid .= " switch -->| | $portId:::port\n";
$mermaid .= " $portId\[\"$portLabel\"] --> | {$row['speed']} | $endpointId\[\"$endpointLabel\"]:::endpoint\n";
$speed = $row['speed_bits'];
$lineStyle = 'black';
$linewidth = 1;
switch(true) {
case ($speed >= 100 && $speed < 1000):
$linewidth = 2;
$lineStyle = 'purple';
break;
case ($speed >= 1000 && $speed < 2000):
$linewidth = 4;
$lineStyle = 'green';
break;
case ($speed >= 2000 && $speed < 10000):
$linewidth = 6;
$lineStyle = 'grey';
break;
case ($speed >= 10000 && $speed < 40000):
$linewidth = 8;
$lineStyle = 'gold';
break;
case ($speed >= 40000 && $speed < 80000):
$linewidth = 10;
$lineStyle = 'cyan';
break;
case ($speed >= 80000):
$linewidth = 12;
$lineStyle = 'silver';
break;
}
// $linkStyle .= " linkStyle $linkindex stroke:$lineStyle, stroke-width:$linewidth" . "px\n";
$linkindex++;
$linkStyle .= " linkStyle $linkindex stroke:$lineStyle, stroke-width:$linewidth" . "px\n";
$linkindex++;
}
$mermaid .= $linkStyle;
$htmlMermaid = str_replace('\[','[',$mermaid);
// Generate HTML output
echo <<<HTML
<!DOCTYPE html>
<html>
<title>Switch Port Diagram - $switch_ip</title>
<script src="mermaid.min.js"></script>
<style>
.mermaid {
font-family: sans-serif;
width: 100vw;
height: 100vh;
background: white;
}
.node rect {
rx: 5px;
ry: 5px;
}
.switch rect {
fill: #d4e3fc !important;
}
.port rect {
fill: #f0f0f0 !important;
}
.endpoint rect {
fill: #e6ffe6 !important;
}
</style>
</head>
<body>
<div class="mermaid">
$htmlMermaid
</div>
<script>
mermaid.initialize({
startOnLoad: true,
flowchart: {
useMaxWidth: false,
htmlLabels: true,
nodeSpacing: 50,
rankSpacing: 100
},
securityLevel: 'loose'
});
</script>
</body>
</html>
HTML;
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>