-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDudeParser.php
More file actions
executable file
·211 lines (202 loc) · 9.53 KB
/
Copy pathDudeParser.php
File metadata and controls
executable file
·211 lines (202 loc) · 9.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
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
<?php
/**
* DudeParser – číta pôvodnú Dude databázu (dude.db = SQLite) a rozparsuje
* binárny "obj" formát. Overené na reálnej DB.
*
* Formát objektu: opakovane [1B dĺžka názvu][názov][1B tag][1B dĺžka][hodnota].
* Kľúč k mapám: prvok mapy má sys-type == elementsID svojej mapy.
* Typy objektov: 10=mapa, 15=zariadenie, 14=typ zariadenia, 17=služba,
* 5=súbor(ikona), 41=sonda/funkcia, 31=linka(dáta).
*/
class DudeParser
{
private PDO $src;
public function __construct(string $dudeDbPath)
{
if (!is_file($dudeDbPath)) throw new RuntimeException("Nenašiel som $dudeDbPath");
$this->src = new PDO('sqlite:' . $dudeDbPath);
$this->src->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function kv(string $b): array
{
$d = []; $i = 0; $n = strlen($b);
while ($i < $n) {
$nl = ord($b[$i]); $i++;
if ($nl === 0 || $i + $nl > $n) break;
$name = substr($b, $i, $nl); $i += $nl;
if ($i + 2 > $n) break;
$len = ord($b[$i + 1]); $i += 2;
$d[$name] = substr($b, $i, $len); $i += $len;
}
return $d;
}
public static function i32(?string $v): ?int
{
if ($v === null || strlen($v) !== 4) return null;
$u = unpack('V', $v)[1];
return ($u > 0x7fffffff) ? $u - 0x100000000 : $u;
}
public static function str(?string $v): string
{
if ($v === null) return '';
$out = '';
for ($i = 0, $n = strlen($v); $i < $n; $i++) {
$c = ord($v[$i]);
if ($c >= 32 || $c === 9) $out .= $v[$i];
}
return trim($out);
}
public static function ip(?string $v): ?string
{
if ($v === null || strlen($v) < 4) return null;
for ($k = 0; $k + 4 <= strlen($v); $k += 4) {
$o = array_values(unpack('C4', substr($v, $k, 4)));
$sum = $o[0] + $o[1] + $o[2] + $o[3];
if ($sum === 0 || $sum === 1020) continue;
return "$o[0].$o[1].$o[2].$o[3]";
}
return null;
}
public function extract(): array
{
$rows = $this->src->query('SELECT obj FROM objs')->fetchAll(PDO::FETCH_COLUMN);
$parsed = [];
foreach ($rows as $b) $parsed[] = self::kv($b);
// index podľa sys-id
$bySid = [];
foreach ($parsed as $o) {
$s = self::i32($o['sys-id'] ?? null);
if ($s !== null && !isset($bySid[$s])) $bySid[$s] = $o;
}
$maps = []; $canvas2eid = []; $devices = []; $types = [];
$nodes = []; $links = []; $services = [];
// mapy (10)
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 10) {
$eid = self::i32($o['elementsID'] ?? null);
$cid = self::i32($o['sys-id'] ?? null);
$maps[$eid] = ['id'=>$eid, 'name'=>self::str($o['sys-name'] ?? ''),
'image_id'=>self::i32($o['imageID'] ?? null), 'canvas_id'=>$cid];
$canvas2eid[$cid] = $eid;
}
}
// typy zariadení (14) -> ikona zo súboru (5)
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 14) {
$img = self::i32($o['imageID'] ?? null);
$file = $bySid[$img] ?? null;
$icon = $file ? self::str($file['fileName'] ?? '') : '';
$types[self::i32($o['sys-id'])] = ['id'=>self::i32($o['sys-id']),
'name'=>self::str($o['sys-name'] ?? ''), 'icon'=>$icon];
}
}
// typy spojov (34): štýl + hrúbka čiary
$linkTypes = [];
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 34) {
$linkTypes[self::i32($o['sys-id'])] = [
'style'=>self::i32($o['style'] ?? null) ?: 0,
'thickness'=>self::i32($o['thickness'] ?? null) ?: 2,
'name'=>self::str($o['sys-name'] ?? '')];
}
}
// (typy spojov už máme v $linkTypes)
// SNMP profily (58)
$snmpProfiles = [];
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 58) {
$snmpProfiles[self::i32($o['sys-id'])] = [
'id'=>self::i32($o['sys-id']), 'name'=>self::str($o['sys-name'] ?? ''),
'community'=>self::str($o['community'] ?? ''),
'version'=>self::i32($o['version'] ?? null), // 0=v1, 1=v2c, 2=v3, -1=none
'port'=>self::i32($o['port'] ?? null) ?: 161,
'sec_name'=>self::str($o['v3Security'] ?? ''),
'auth_pass'=>self::str($o['v3AuthPassword'] ?? ''),
'priv_pass'=>self::str($o['v3CryptPassword'] ?? ''),
'auth_proto'=>self::i32($o['v3AuthMethod'] ?? null) === 1 ? 'SHA' : 'MD5',
'priv_proto'=>self::i32($o['v3CryptMethod'] ?? null) === 1 ? 'AES' : 'DES'];
}
}
// link-dáta (31): netMapElementID -> typeID + master device/interface/type
$linkMeta = [];
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 31) {
$linkMeta[self::i32($o['netMapElementID'] ?? null)] = [
'typeID'=>self::i32($o['typeID'] ?? null),
'md'=>self::i32($o['masterDevice'] ?? null),
'ifidx'=>self::i32($o['masterInterface'] ?? null),
'mtype'=>self::i32($o['masteringType'] ?? null)];
}
}
// zariadenia (15)
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 15) {
$sid = self::i32($o['sys-id']);
$devices[$sid] = ['id'=>$sid, 'name'=>self::str($o['sys-name'] ?? ''),
'ip'=>self::ip($o['addresses'] ?? null), 'dns'=>self::str($o['dnsNames'] ?? ''),
'snmp_profile'=>self::i32($o['snmpProfileID'] ?? null),
'username'=>self::str($o['user'] ?? ''), 'password'=>self::str($o['pwd'] ?? ''), 'type_id'=>self::i32($o['typeID'] ?? null)];
}
}
// sondy (13) – typ + port pre monitoring
$typeMap = [1=>'icmp',2=>'random',3=>'tcp',4=>'dns',5=>'snmp',6=>'tcp',8=>'function'];
$probes = [];
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 13) {
$pid = self::i32($o['sys-id']);
$tid = self::i32($o['typeID'] ?? null);
$probes[$pid] = ['id'=>$pid, 'name'=>self::str($o['sys-name'] ?? ''),
'type'=>$typeMap[$tid] ?? 'tcp', 'port'=>self::i32($o['defaultPort'] ?? null) ?: 0,
'dns_name'=>self::str($o['dnsName'] ?? '')];
}
}
// služby (17) – prepojené na sondu (probe_id + typ + port)
foreach ($parsed as $o) {
if (self::i32($o['sys-type'] ?? null) === 17) {
$pid = self::i32($o['probeID'] ?? null);
$pr = $probes[$pid] ?? null;
$pname = $pr ? $pr['name'] : (isset($bySid[$pid]) ? self::str($bySid[$pid]['sys-name'] ?? '') : '');
$services[] = ['id'=>self::i32($o['sys-id']), 'device_id'=>self::i32($o['deviceID'] ?? null),
'probe_id'=>$pid, 'name'=>$pname ?: self::str($o['sys-name'] ?? ''),
'enabled'=>self::i32($o['enabled'] ?? null) === 0 ? 0 : 1,
'down'=>self::i32($o['probesDown'] ?? null) ?: 0,
'acked'=>self::i32($o['acked'] ?? null) ? 1 : 0];
}
}
// prvky máp (sys-type == elementsID mapy)
foreach ($parsed as $o) {
$st = self::i32($o['sys-type'] ?? null);
if ($st === null || !isset($maps[$st])) continue;
$sid = self::i32($o['sys-id']);
$lf = self::i32($o['linkFrom'] ?? null);
if ($lf === null || $lf === -1) {
$iid = self::i32($o['itemID'] ?? null);
$isSub = isset($canvas2eid[$iid]);
$nodes[] = ['id'=>$sid, 'map_id'=>$st,
'kind'=>$isSub ? 'submap' : 'device',
'device_id'=>$isSub ? null : $iid,
'submap_id'=>$isSub ? $canvas2eid[$iid] : null,
'x'=>self::i32($o['itemX'] ?? null), 'y'=>self::i32($o['itemY'] ?? null),
'image'=>self::i32($o['itemImage'] ?? null),
'label'=>self::str($o['sys-name'] ?? '')];
} else {
$meta = $linkMeta[$sid] ?? null;
$tid = $meta['typeID'] ?? null;
$lt = ($tid !== null && isset($linkTypes[$tid])) ? $linkTypes[$tid] : null;
$links[] = ['id'=>$sid, 'map_id'=>$st, 'from_node'=>$lf,
'to_node'=>self::i32($o['linkTo'] ?? null),
'width'=>self::i32($o['linkWidth'] ?? null),
'style'=>$lt['style'] ?? 0,
'thickness'=>$lt['thickness'] ?? 2,
'ltype'=>$lt['name'] ?? '',
'snmp_device'=>$meta['md'] ?? null,
'snmp_ifindex'=>$meta['ifidx'] ?? null,
'snmp_type'=>$meta['mtype'] ?? null,
'label'=>self::str($o['sys-name'] ?? '')];
}
}
return ['maps'=>$maps,'devices'=>$devices,'types'=>$types,'probes'=>$probes,
'link_types'=>$linkTypes,'snmp_profiles'=>$snmpProfiles,
'nodes'=>$nodes,'links'=>$links,'services'=>$services];
}
}