check for InnoDB support consistently
[racktables] / wwwroot / inc / interface-reports.php
CommitLineData
069689de
DO
1<?php
2
004c2fd2
DO
3# This file is a part of RackTables, a datacenter and server room management
4# framework. See accompanying file "COPYING" for the full copyright and
5# licensing information.
6
069689de
DO
7function renderSystemReports ()
8{
9 $tmp = array
10 (
11 array
12 (
13 'title' => 'Dictionary/objects',
14 'type' => 'counters',
15 'func' => 'getDictStats'
16 ),
17 array
18 (
19 'title' => 'Rackspace',
20 'type' => 'counters',
21 'func' => 'getRackspaceStats'
22 ),
23 array
24 (
25 'title' => 'Files',
26 'type' => 'counters',
27 'func' => 'getFileStats'
28 ),
29 array
30 (
31 'title' => 'Tags top list',
32 'type' => 'custom',
33 'func' => 'renderTagStats'
34 ),
35 );
36 renderReports ($tmp);
37}
38
39function renderLocalReports ()
40{
41 global $localreports;
42 renderReports ($localreports);
43}
44
45function renderRackCodeReports ()
46{
47 $tmp = array
48 (
49 array
50 (
51 'title' => 'Stats',
52 'type' => 'counters',
53 'func' => 'getRackCodeStats',
069689de
DO
54 ),
55 array
56 (
57 'title' => 'Warnings',
58 'type' => 'messages',
59 'func' => 'getRackCodeWarnings',
069689de
DO
60 ),
61 );
62 renderReports ($tmp);
63}
64
65function renderIPv4Reports ()
66{
67 $tmp = array
68 (
69 array
70 (
71 'title' => 'Stats',
72 'type' => 'counters',
73 'func' => 'getIPv4Stats'
74 ),
75 );
76 renderReports ($tmp);
77}
78
79function renderIPv6Reports ()
80{
81 $tmp = array
82 (
83 array
84 (
85 'title' => 'Stats',
86 'type' => 'counters',
87 'func' => 'getIPv6Stats'
88 ),
89 );
90 renderReports ($tmp);
91}
92
93function renderPortsReport ()
94{
95 $tmp = array();
96 foreach (getPortIIFOptions() as $iif_id => $iif_name)
97 if (count (getPortIIFStats ($iif_id)))
98 $tmp[] = array
99 (
100 'title' => $iif_name,
101 'type' => 'meters',
102 'func' => 'getPortIIFStats',
103 'args' => $iif_id,
104 );
105 renderReports ($tmp);
106}
107
108function render8021QReport ()
109{
110 if (!count ($domains = getVLANDomainOptions()))
111 {
112 echo '<center><h3>(no VLAN configuration exists)</h3></center>';
113 return;
114 }
115 $vlanstats = array();
116 for ($i = VLAN_MIN_ID; $i <= VLAN_MAX_ID; $i++)
117 $vlanstats[$i] = array();
118 $header = '<tr><th>&nbsp;</th>';
119 foreach ($domains as $domain_id => $domain_name)
120 {
121 foreach (getDomainVLANList ($domain_id) as $vlan_id => $vlan_info)
122 $vlanstats[$vlan_id][$domain_id] = $vlan_info;
123 $header .= '<th>' . mkA ($domain_name, 'vlandomain', $domain_id) . '</th>';
124 }
125 $header .= '</tr>';
126 $output = $available = array();
127 for ($i = VLAN_MIN_ID; $i <= VLAN_MAX_ID; $i++)
128 if (!count ($vlanstats[$i]))
129 $available[] = $i;
130 else
131 $output[$i] = FALSE;
132 foreach (listToRanges ($available) as $span)
133 {
134 if ($span['to'] - $span['from'] < 4)
135 for ($i = $span['from']; $i <= $span['to']; $i++)
136 $output[$i] = FALSE;
137 else
138 {
139 $output[$span['from']] = TRUE;
140 $output[$span['to']] = FALSE;
141 }
142 }
143 ksort ($output, SORT_NUMERIC);
144 $header_delay = 0;
145 startPortlet ('VLAN existence per domain');
146 echo '<table border=1 cellspacing=0 cellpadding=5 align=center class=rackspace>';
147 foreach ($output as $vlan_id => $tbc)
148 {
149 if (--$header_delay <= 0)
150 {
151 echo $header;
152 $header_delay = 25;
153 }
154 echo '<tr class="state_' . (count ($vlanstats[$vlan_id]) ? 'T' : 'F');
155 echo '"><th class=tdright>' . $vlan_id . '</th>';
156 foreach (array_keys ($domains) as $domain_id)
157 {
158 echo '<td class=tdcenter>';
159 if (array_key_exists ($domain_id, $vlanstats[$vlan_id]))
160 echo mkA ('&exist;', 'vlan', "${domain_id}-${vlan_id}");
161 else
162 echo '&nbsp;';
163 echo '</td>';
164 }
165 echo '</tr>';
166 if ($tbc)
167 echo '<tr class="state_A"><th>...</th><td colspan=' . count ($domains) . '>&nbsp;</td></tr>';
168 }
169 echo '</table>';
170 finishPortlet();
171}
172
173function renderReports ($what)
174{
175 if (!count ($what))
176 return;
177 echo "<table align=center>\n";
178 foreach ($what as $item)
179 {
069689de
DO
180 echo "<tr><th colspan=2><h3>${item['title']}</h3></th></tr>\n";
181 switch ($item['type'])
182 {
183 case 'counters':
184 if (array_key_exists ('args', $item))
185 $data = $item['func'] ($item['args']);
186 else
187 $data = $item['func'] ();
188 foreach ($data as $header => $data)
189 echo "<tr><td class=tdright>${header}:</td><td class=tdleft>${data}</td></tr>\n";
190 break;
191 case 'messages':
192 if (array_key_exists ('args', $item))
193 $data = $item['func'] ($item['args']);
194 else
195 $data = $item['func'] ();
196 foreach ($data as $msg)
197 echo "<tr class='msg_${msg['class']}'><td class=tdright>${msg['header']}:</td><td class=tdleft>${msg['text']}</td></tr>\n";
198 break;
199 case 'meters':
200 if (array_key_exists ('args', $item))
201 $data = $item['func'] ($item['args']);
202 else
203 $data = $item['func'] ();
204 foreach ($data as $meter)
205 {
206 echo "<tr><td class=tdright>${meter['title']}:</td><td class=tdcenter>";
207 renderProgressBar ($meter['max'] ? $meter['current'] / $meter['max'] : 0);
208 echo '<br><small>' . ($meter['max'] ? $meter['current'] . '/' . $meter['max'] : '0') . '</small></td></tr>';
209 }
210 break;
211 case 'custom':
212 echo "<tr><td colspan=2>";
213 $item['func'] ();
214 echo "</td></tr>\n";
215 break;
216 default:
217 throw new InvalidArgException ('type', $item['type']);
218 }
219 echo "<tr><td colspan=2><hr></td></tr>\n";
220 }
221 echo "</table>\n";
222}
223
224function renderTagStats ()
225{
226 global $taglist;
227 echo '<table border=1><tr><th>tag</th><th>total</th><th>objects</th><th>IPv4 nets</th><th>IPv6 nets</th>';
228 echo '<th>racks</th><th>IPv4 VS</th><th>IPv4 RS pools</th><th>users</th><th>files</th></tr>';
229 $pagebyrealm = array
230 (
231 'file' => 'files&tab=default',
232 'ipv4net' => 'ipv4space&tab=default',
233 'ipv6net' => 'ipv6space&tab=default',
234 'ipv4vs' => 'ipv4slb&tab=default',
235 'ipv4rspool' => 'ipv4slb&tab=rspools',
236 'object' => 'depot&tab=default',
237 'rack' => 'rackspace&tab=default',
238 'user' => 'userlist&tab=default'
239 );
240 foreach (getTagChart (getConfigVar ('TAGS_TOPLIST_SIZE')) as $taginfo)
241 {
242 echo "<tr><td>${taginfo['tag']}</td><td>" . $taginfo['refcnt']['total'] . "</td>";
243 foreach (array ('object', 'ipv4net', 'ipv6net', 'rack', 'ipv4vs', 'ipv4rspool', 'user', 'file') as $realm)
244 {
245 echo '<td>';
246 if (!isset ($taginfo['refcnt'][$realm]))
247 echo '&nbsp;';
248 else
249 {
250 echo "<a href='index.php?page=" . $pagebyrealm[$realm] . "&cft[]=${taginfo['id']}'>";
251 echo $taginfo['refcnt'][$realm] . '</a>';
252 }
253 echo '</td>';
254 }
255 echo '</tr>';
256 }
257 echo '</table>';
258}
259
330f20d8
DO
260function renderExpirations ()
261{
262 global $nextorder, $expirations;
263 $attrmap = getAttrMap();
264 foreach ($expirations as $attr_id => $sections)
265 {
266 startPortlet ($attrmap[$attr_id]['name']);
267 foreach ($sections as $section)
268 {
269 $count = 1;
270 $order = 'odd';
271 $result = scanAttrRelativeDays ($attr_id, $section['from'], $section['to']);
272
273 echo '<table align=center width=60% border=0 cellpadding=5 cellspacing=0 align=center class=cooltable>';
274 echo "<caption>${section['title']}</caption>\n";
275
276 if (! count ($result))
277 {
278 echo "<tr><td colspan=4>(none)</td></tr></table><br>\n";
279 continue;
280 }
281 echo '<tr valign=top><th align=center>Count</th><th align=center>Name</th>';
282 echo "<th align=center>Asset Tag</th><th align=center>OEM S/N 1</th><th align=center>Date Warranty <br> Expires</th></tr>\n";
283 foreach ($result as $row)
284 {
285 $date_value = datetimestrFromTimestamp ($row['uint_value']);
286
287 $object = spotEntity ('object', $row['object_id']);
288 $attributes = getAttrValues ($object['id']);
289 $oem_sn_1 = array_key_exists (1, $attributes) ? $attributes[1]['a_value'] : '&nbsp;';
290 echo '<tr class=' . $section['class'] . $order . ' valign=top>';
ef08756a
DO
291 echo "<td class=tdright>${count}</td>";
292 echo '<td class=tdleft>' . mkCellA ($object) . '</td>';
293 echo "<td class=tdleft>${object['asset_no']}</td>";
294 echo "<td class=tdleft>${oem_sn_1}</td>";
330f20d8
DO
295 echo "<td>${date_value}</td>";
296 echo "</tr>\n";
297 $order = $nextorder[$order];
298 $count++;
299 }
300 echo "</table><br>\n";
301 }
302 finishPortlet ();
303 }
304}
305
306// The validity of some data cannot be guaranteed using foreign keys.
307// Display any invalid rows that have crept in.
308// Possible enhancements:
309// - check for IP addresses whose subnet does not exist in IPvXNetwork (X = 4 or 6)
310// - IPvXAddress, IPvXAllocation, IPvXLog, IPvXRS, IPvXVS
311// - provide links/buttons to delete invalid rows
312// - verify that the current DDL is correct for each DB element
313// - columns, indexes, character sets
314function renderDataIntegrityReport ()
315{
330f20d8
DO
316 $violations = FALSE;
317
318 // check 1: EntityLink rows referencing not-existent relatives
319 // check 1.1: children
320 $realms = array
321 (
322 'location' => 'Location',
323 'object' => 'RackObject',
324 'rack' => 'Rack',
325 'row' => 'Row'
326 );
327 $orphans = array ();
328 foreach ($realms as $realm => $table)
329 {
330 $result = usePreparedSelectBlade
331 (
0c40b71e
DO
332 'SELECT EL.parent_entity_type, EL.parent_entity_id, ' .
333 'EL.child_entity_type, EL.child_entity_id FROM EntityLink EL ' .
330f20d8
DO
334 "LEFT JOIN ${table} ON EL.child_entity_id = ${table}.id " .
335 "WHERE EL.child_entity_type = ? AND ${table}.id IS NULL",
336 array ($realm)
337 );
338 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
339 unset ($result);
340 $orphans = array_merge ($orphans, $rows);
341 }
342 if (count ($orphans))
343 {
344 $violations = TRUE;
345 startPortlet ('EntityLink: Missing Children (' . count ($orphans) . ')');
3b15479f 346 echo "<table cellpadding=5 cellspacing=0 align=center class='cooltable zebra'>\n";
ee660c28 347 echo "<tr><th>Parent</th><th>Child Type</th><th class=tdright>Child ID</th></tr>\n";
330f20d8
DO
348 foreach ($orphans as $orphan)
349 {
350 $realm_name = formatRealmName ($orphan['parent_entity_type']);
351 try
352 {
353 $parent = spotEntity ($orphan['parent_entity_type'], $orphan['parent_entity_id']);
354 $parent_name = $parent['name'];
355 }
356 catch (EntityNotFoundException $e)
357 {
358 $parent_name = 'missing from DB';
359 }
3b15479f 360 echo '<tr>';
330f20d8
DO
361 echo "<td>${realm_name}: ${parent_name}</td>";
362 echo "<td>${orphan['child_entity_type']}</td>";
ee660c28 363 echo "<td class=tdright>${orphan['child_entity_id']}</td>";
330f20d8 364 echo "</tr>\n";
330f20d8
DO
365 }
366 echo "</table>\n";
367 finishPortLet ();
368 }
369
370 // check 1.2: parents
371 $orphans = array ();
372 foreach ($realms as $realm => $table)
373 {
374 $result = usePreparedSelectBlade
375 (
0c40b71e
DO
376 'SELECT EL.parent_entity_type, EL.parent_entity_id, ' .
377 'EL.child_entity_type, EL.child_entity_id FROM EntityLink EL ' .
330f20d8
DO
378 "LEFT JOIN ${table} ON EL.parent_entity_id = ${table}.id " .
379 "WHERE EL.parent_entity_type = ? AND ${table}.id IS NULL",
380 array ($realm)
381 );
382 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
383 unset ($result);
384 $orphans = array_merge ($orphans, $rows);
385 }
386 if (count ($orphans))
387 {
388 $violations = TRUE;
389 startPortlet ('EntityLink: Missing Parents (' . count ($orphans) . ')');
3b15479f 390 echo "<table cellpadding=5 cellspacing=0 align=center class='cooltable zebra'>\n";
ee660c28 391 echo "<tr><th>Child</th><th>Parent Type</th><th class=tdright>Parent ID</th></tr>\n";
330f20d8
DO
392 foreach ($orphans as $orphan)
393 {
394 $realm_name = formatRealmName ($orphan['child_entity_type']);
395 try
396 {
397 $child = spotEntity ($orphan['child_entity_type'], $orphan['child_entity_id']);
398 $child_name = $child['name'];
399 }
400 catch (EntityNotFoundException $e)
401 {
402 $child_name = 'missing from DB';
403 }
3b15479f 404 echo '<tr>';
330f20d8
DO
405 echo "<td>${realm_name}: ${child_name}</td>";
406 echo "<td>${orphan['parent_entity_type']}</td>";
ee660c28 407 echo "<td class=tdright>${orphan['parent_entity_id']}</td>";
330f20d8 408 echo "</tr>\n";
330f20d8
DO
409 }
410 echo "</table>\n";
411 finishPortLet ();
412 }
413
414 // check 3: multiple tables referencing non-existent dictionary entries
415 // check 3.1: AttributeMap
416 $orphans = array ();
417 $result = usePreparedSelectBlade
418 (
dff67d81 419 'SELECT AM.objtype_id, A.name AS attr_name, C.name AS chapter_name ' .
330f20d8
DO
420 'FROM AttributeMap AM ' .
421 'LEFT JOIN Attribute A ON AM.attr_id = A.id ' .
422 'LEFT JOIN Chapter C ON AM.chapter_id = C.id ' .
423 'LEFT JOIN Dictionary D ON AM.objtype_id = D.dict_key ' .
424 'WHERE D.dict_key IS NULL'
425 );
426 $orphans = $result->fetchAll (PDO::FETCH_ASSOC);
427 unset ($result);
428 if (count ($orphans))
429 {
430 $violations = TRUE;
431 startPortlet ('AttributeMap: Invalid Mappings (' . count ($orphans) . ')');
66eece6d
DO
432 $columns = array
433 (
434 array ('th_text' => 'Attribute', 'row_key' => 'attr_name'),
435 array ('th_text' => 'Chapter', 'row_key' => 'chapter_name'),
436 array ('th_text' => 'Object TypeID', 'row_key' => 'objtype_id', 'td_class' => 'tdright'),
437 );
438 renderTableViewer ($columns, $orphans);
330f20d8
DO
439 finishPortLet ();
440 }
441
442 // check 3.2: Object
443 $orphans = array ();
444 $result = usePreparedSelectBlade
445 (
dff67d81 446 'SELECT O.id, O.name, O.objtype_id FROM Object O ' .
330f20d8
DO
447 'LEFT JOIN Dictionary D ON O.objtype_id = D.dict_key ' .
448 'WHERE D.dict_key IS NULL'
449 );
450 $orphans = $result->fetchAll (PDO::FETCH_ASSOC);
451 unset ($result);
452 if (count ($orphans))
453 {
454 $violations = TRUE;
455 startPortlet ('Object: Invalid Types (' . count ($orphans) . ')');
66eece6d
DO
456 $columns = array
457 (
458 array ('th_text' => 'ID', 'row_key' => 'id', 'td_class' => 'tdright'),
459 array ('th_text' => 'Name', 'row_key' => 'name'),
460 array ('th_text' => 'Type ID', 'row_key' => 'objtype_id', 'td_class' => 'tdright'),
461 );
462 renderTableViewer ($columns, $orphans);
330f20d8
DO
463 finishPortLet ();
464 }
465
466 // check 3.3: ObjectHistory
467 $orphans = array ();
468 $result = usePreparedSelectBlade
469 (
dff67d81 470 'SELECT OH.id, OH.name, OH.objtype_id FROM ObjectHistory OH ' .
330f20d8
DO
471 'LEFT JOIN Dictionary D ON OH.objtype_id = D.dict_key ' .
472 'WHERE D.dict_key IS NULL'
473 );
474 $orphans = $result->fetchAll (PDO::FETCH_ASSOC);
475 unset ($result);
476 if (count ($orphans))
477 {
478 $violations = TRUE;
479 startPortlet ('ObjectHistory: Invalid Types (' . count ($orphans) . ')');
66eece6d
DO
480 $columns = array
481 (
482 array ('th_text' => 'ID', 'row_key' => 'id', 'td_class' => 'tdright'),
483 array ('th_text' => 'Name', 'row_key' => 'name'),
484 array ('th_text' => 'Type ID', 'row_key' => 'objtype_id', 'td_class' => 'tdright'),
485 );
486 renderTableViewer ($columns, $orphans);
330f20d8
DO
487 finishPortLet ();
488 }
489
490 // check 3.4: ObjectParentCompat
491 $orphans = array ();
492 $result = usePreparedSelectBlade
493 (
0c40b71e 494 'SELECT OPC.parent_objtype_id, OPC.child_objtype_id, PD.dict_value AS parent_name, CD.dict_value AS child_name '.
330f20d8
DO
495 'FROM ObjectParentCompat OPC ' .
496 'LEFT JOIN Dictionary PD ON OPC.parent_objtype_id = PD.dict_key ' .
497 'LEFT JOIN Dictionary CD ON OPC.child_objtype_id = CD.dict_key ' .
498 'WHERE PD.dict_key IS NULL OR CD.dict_key IS NULL'
499 );
500 $orphans = $result->fetchAll (PDO::FETCH_ASSOC);
501 unset ($result);
502 if (count ($orphans))
503 {
504 $violations = TRUE;
505 startPortlet ('Object Container Compatibility rules: Invalid Parent or Child Type (' . count ($orphans) . ')');
66eece6d
DO
506 $columns = array
507 (
508 array ('th_text' => 'Parent', 'row_key' => 'parent_name'),
509 array ('th_text' => 'Parent Type ID', 'row_key' => 'parent_objtype_id', 'td_class' => 'tdright'),
510 array ('th_text' => 'Child', 'row_key' => 'child_name'),
511 array ('th_text' => 'Child Type ID', 'row_key' => 'child_objtype_id', 'td_class' => 'tdright'),
512 );
513 renderTableViewer ($columns, $orphans);
330f20d8
DO
514 finishPortLet ();
515 }
516
517 // check 4: relationships that violate ObjectParentCompat Rules
518 $invalids = array ();
519 $result = usePreparedSelectBlade
520 (
521 'SELECT CO.id AS child_id, CO.objtype_id AS child_type_id, CD.dict_value AS child_type, CO.name AS child_name, ' .
522 'PO.id AS parent_id, PO.objtype_id AS parent_type_id, PD.dict_value AS parent_type, PO.name AS parent_name ' .
523 'FROM Object CO ' .
524 'LEFT JOIN EntityLink EL ON CO.id = EL.child_entity_id ' .
525 'LEFT JOIN Object PO ON EL.parent_entity_id = PO.id ' .
526 'LEFT JOIN ObjectParentCompat OPC ON PO.objtype_id = OPC.parent_objtype_id ' .
527 'LEFT JOIN Dictionary PD ON PO.objtype_id = PD.dict_key ' .
528 'LEFT JOIN Dictionary CD ON CO.objtype_id = CD.dict_key ' .
529 "WHERE EL.parent_entity_type = 'object' AND EL.child_entity_type = 'object' " .
530 'AND OPC.parent_objtype_id IS NULL'
531 );
532 $invalids = $result->fetchAll (PDO::FETCH_ASSOC);
533 unset ($result);
534 if (count ($invalids))
535 {
536 $violations = TRUE;
537 startPortlet ('Objects: Violate Object Container Compatibility rules (' . count ($invalids) . ')');
66eece6d
DO
538 $columns = array
539 (
540 array ('th_text' => 'Contained Obj Name', 'row_key' => 'child_name'),
541 array ('th_text' => 'Contained Obj Type', 'row_key' => 'child_type'),
542 array ('th_text' => 'Container Obj Name', 'row_key' => 'parent_name'),
543 array ('th_text' => 'Container Obj Type', 'row_key' => 'parent_type'),
544 );
545 renderTableViewer ($columns, $invalids);
330f20d8
DO
546 finishPortLet ();
547 }
548
549 // check 5: Links that violate PortCompat Rules
550 $invalids = array ();
551 $result = usePreparedSelectBlade
552 (
553 'SELECT OA.id AS obja_id, OA.name AS obja_name, L.porta AS porta_id, PA.name AS porta_name, POIA.oif_name AS porta_type, ' .
554 'OB.id AS objb_id, OB.name AS objb_name, L.portb AS portb_id, PB.name AS portb_name, POIB.oif_name AS portb_type ' .
555 'FROM Link L ' .
556 'LEFT JOIN Port PA ON L.porta = PA.id ' .
557 'LEFT JOIN Object OA ON PA.object_id = OA.id ' .
558 'LEFT JOIN PortOuterInterface POIA ON PA.type = POIA.id ' .
559 'LEFT JOIN Port PB ON L.portb = PB.id ' .
560 'LEFT JOIN Object OB ON PB.object_id = OB.id ' .
561 'LEFT JOIN PortOuterInterface POIB ON PB.type = POIB.id ' .
562 'LEFT JOIN PortCompat PC on PA.type = PC.type1 AND PB.type = PC.type2 ' .
563 'WHERE PC.type1 IS NULL OR PC.type2 IS NULL'
564 );
565 $invalids = $result->fetchAll (PDO::FETCH_ASSOC);
566 unset ($result);
567 if (count ($invalids))
568 {
569 $violations = TRUE;
570 startPortlet ('Port Links: Violate Port Compatibility Rules (' . count ($invalids) . ')');
66eece6d
DO
571 $columns = array
572 (
573 array ('th_text' => 'Object A', 'row_key' => 'obja_name'),
574 array ('th_text' => 'Port A Name', 'row_key' => 'porta_name'),
575 array ('th_text' => 'Port A Type', 'row_key' => 'porta_type'),
576 array ('th_text' => 'Object B', 'row_key' => 'objb_name'),
577 array ('th_text' => 'Port B Name', 'row_key' => 'portb_name'),
578 array ('th_text' => 'Port B Type', 'row_key' => 'portb_type'),
579 );
580 renderTableViewer ($columns, $invalids);
330f20d8
DO
581 finishPortLet ();
582 }
583
584 // check 6: TagStorage rows referencing non-existent parents
585 $realms = array
586 (
587 'file' => array ('table' => 'File', 'column' => 'id'),
588 'ipv4net' => array ('table' => 'IPv4Network', 'column' => 'id'),
589 'ipv4rspool' => array ('table' => 'IPv4RSPool', 'column' => 'id'),
590 'ipv4vs' => array ('table' => 'IPv4VS', 'column' => 'id'),
591 'ipv6net' => array ('table' => 'IPv6Network', 'column' => 'id'),
592 'ipvs' => array ('table' => 'VS', 'column' => 'id'),
593 'location' => array ('table' => 'Location', 'column' => 'id'),
594 'object' => array ('table' => 'RackObject', 'column' => 'id'),
595 'rack' => array ('table' => 'Rack', 'column' => 'id'),
596 'user' => array ('table' => 'UserAccount', 'column' => 'user_id'),
597 'vst' => array ('table' => 'VLANSwitchTemplate', 'column' => 'id'),
598 );
599 $orphans = array ();
600 foreach ($realms as $realm => $details)
601 {
602 $result = usePreparedSelectBlade
603 (
dff67d81 604 'SELECT TS.entity_realm, TS.entity_id, TT.tag FROM TagStorage TS ' .
330f20d8
DO
605 'LEFT JOIN TagTree TT ON TS.tag_id = TT.id ' .
606 "LEFT JOIN ${details['table']} ON TS.entity_id = ${details['table']}.${details['column']} " .
607 "WHERE TS.entity_realm = ? AND ${details['table']}.${details['column']} IS NULL",
608 array ($realm)
609 );
610 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
611 unset ($result);
612 $orphans = array_merge ($orphans, $rows);
613 }
614 if (count ($orphans))
615 {
616 $violations = TRUE;
617 startPortlet ('TagStorage: Missing Parents (' . count ($orphans) . ')');
66eece6d
DO
618 foreach (array_keys ($orphans) as $key)
619 $orphans[$key]['realm_name'] = formatRealmName ($orphans[$key]['entity_realm']);
620 $columns = array
621 (
622 array ('th_text' => 'Tag', 'row_key' => 'tag'),
623 array ('th_text' => 'Parent Type', 'row_key' => 'realm_name'),
624 array ('th_text' => 'Parent ID', 'row_key' => 'entity_id', 'td_class' => 'tdright'),
625 );
626 renderTableViewer ($columns, $orphans);
330f20d8
DO
627 finishPortLet ();
628 }
629
630 // check 7: FileLink rows referencing non-existent parents
631 // re-use the realms list from the TagStorage check, with a few mods
632 unset ($realms['file'], $realms['vst']);
633 $realms['row'] = array ('table' => 'Row', 'column' => 'id');
634 $orphans = array ();
635 foreach ($realms as $realm => $details)
636 {
637 $result = usePreparedSelectBlade
638 (
66eece6d 639 'SELECT FL.entity_type, FL.entity_id, F.id FROM FileLink FL ' .
330f20d8
DO
640 'LEFT JOIN File F ON FL.file_id = F.id ' .
641 "LEFT JOIN ${details['table']} ON FL.entity_id = ${details['table']}.${details['column']} " .
642 "WHERE FL.entity_type = ? AND ${details['table']}.${details['column']} IS NULL",
643 array ($realm)
644 );
645 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
646 unset ($result);
647 $orphans = array_merge ($orphans, $rows);
648 }
649 if (count ($orphans))
650 {
651 $violations = TRUE;
652 startPortlet ('FileLink: Missing Parents (' . count ($orphans) . ')');
66eece6d 653 foreach (array_keys ($orphans) as $key)
330f20d8 654 {
66eece6d
DO
655 $orphans[$key]['name'] = mkCellA (spotEntity ('file', $orphans[$key]['id']));
656 $orphans[$key]['realm_name'] = formatRealmName ($orphans[$key]['entity_type']);
330f20d8 657 }
66eece6d
DO
658 $columns = array
659 (
660 array ('th_text' => 'File', 'row_key' => 'name', 'td_escape' => FALSE),
661 array ('th_text' => 'Parent Type', 'row_key' => 'realm_name'),
662 array ('th_text' => 'Parent ID', 'row_key' => 'entity_id', 'td_class' => 'tdright'),
663 );
664 renderTableViewer ($columns, $orphans);
330f20d8
DO
665 finishPortLet ();
666 }
667
f24df831
DO
668 // check 8: triggers
669 $known_triggers= array
330f20d8 670 (
f24df831
DO
671 array ('trigger_name' => 'Link-before-insert', 'table_name' => 'Link'),
672 array ('trigger_name' => 'Link-before-update', 'table_name' => 'Link'),
673 array ('trigger_name' => 'EntityLink-before-insert', 'table_name' => 'EntityLink'),
674 array ('trigger_name' => 'EntityLink-before-update', 'table_name' => 'EntityLink'),
330f20d8 675 );
f24df831
DO
676 $known_triggers = reindexById ($known_triggers, 'trigger_name');
677
330f20d8
DO
678 $result = usePreparedSelectBlade
679 (
f24df831 680 'SELECT TRIGGER_NAME AS trigger_name, EVENT_OBJECT_TABLE AS table_name ' .
330f20d8
DO
681 'FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA = SCHEMA()'
682 );
f24df831 683 $existing_triggers = reindexById ($result->fetchAll (PDO::FETCH_ASSOC), 'trigger_name');
330f20d8 684 unset ($result);
f24df831
DO
685
686 $missing_triggers = array_diff_key ($known_triggers, $existing_triggers);
687 $unknown_triggers = array_diff_key ($existing_triggers, $known_triggers);
688 $columns = array
689 (
690 array ('th_text' => 'Table', 'row_key' => 'table_name'),
691 array ('th_text' => 'Trigger', 'row_key' => 'trigger_name'),
692 );
693
330f20d8
DO
694 if (count ($missing_triggers))
695 {
696 $violations = TRUE;
697 startPortlet ('Missing Triggers (' . count ($missing_triggers) . ')');
f24df831
DO
698 renderTableViewer ($columns, $missing_triggers);
699 finishPortLet ();
700 }
701
702 if (count ($unknown_triggers))
703 {
704 $violations = TRUE;
705 startPortlet ('Unknown Triggers (' . count ($unknown_triggers) . ')');
706 renderTableViewer ($columns, $unknown_triggers);
330f20d8
DO
707 finishPortLet ();
708 }
709
f24df831
DO
710 // check 9: foreign keys
711 $known_fkeys = array
330f20d8 712 (
f24df831
DO
713 array ('fkey_name' => 'Atom-FK-molecule_id', 'table_name' => 'Atom'),
714 array ('fkey_name' => 'Atom-FK-rack_id', 'table_name' => 'Atom'),
715 array ('fkey_name' => 'AttributeMap-FK-chapter_id', 'table_name' => 'AttributeMap'),
716 array ('fkey_name' => 'AttributeMap-FK-attr_id', 'table_name' => 'AttributeMap'),
717 array ('fkey_name' => 'AttributeValue-FK-map', 'table_name' => 'AttributeValue'),
718 array ('fkey_name' => 'AttributeValue-FK-object', 'table_name' => 'AttributeValue'),
719 array ('fkey_name' => 'CachedPAV-FK-object-port', 'table_name' => 'CachedPAV'),
720 array ('fkey_name' => 'CachedPAV-FK-vlan_id', 'table_name' => 'CachedPAV'),
721 array ('fkey_name' => 'CachedPNV-FK-compound', 'table_name' => 'CachedPNV'),
722 array ('fkey_name' => 'CachedPVM-FK-object_id', 'table_name' => 'CachedPVM'),
723 array ('fkey_name' => 'Dictionary-FK-chapter_id', 'table_name' => 'Dictionary'),
724 array ('fkey_name' => 'FileLink-File_fkey', 'table_name' => 'FileLink'),
725 array ('fkey_name' => 'IPv4Allocation-FK-object_id', 'table_name' => 'IPv4Allocation'),
726 array ('fkey_name' => 'IPv4LB-FK-vs_id', 'table_name' => 'IPv4LB'),
727 array ('fkey_name' => 'IPv4LB-FK-object_id', 'table_name' => 'IPv4LB'),
728 array ('fkey_name' => 'IPv4LB-FK-rspool_id', 'table_name' => 'IPv4LB'),
729 array ('fkey_name' => 'IPv4NAT-FK-object_id', 'table_name' => 'IPv4NAT'),
730 array ('fkey_name' => 'IPv4RS-FK', 'table_name' => 'IPv4RS'),
731 array ('fkey_name' => 'IPv6Allocation-FK-object_id', 'table_name' => 'IPv6Allocation'),
732 array ('fkey_name' => 'Link-FK-a', 'table_name' => 'Link'),
733 array ('fkey_name' => 'Link-FK-b', 'table_name' => 'Link'),
734 array ('fkey_name' => 'MountOperation-FK-object_id', 'table_name' => 'MountOperation'),
735 array ('fkey_name' => 'MountOperation-FK-old_molecule_id', 'table_name' => 'MountOperation'),
736 array ('fkey_name' => 'MountOperation-FK-new_molecule_id', 'table_name' => 'MountOperation'),
737 array ('fkey_name' => 'ObjectHistory-FK-object_id', 'table_name' => 'ObjectHistory'),
738 array ('fkey_name' => 'ObjectLog-FK-object_id', 'table_name' => 'ObjectLog'),
739 array ('fkey_name' => 'PatchCableConnectorCompat-FK-connector_id', 'table_name' => 'PatchCableConnectorCompat'),
740 array ('fkey_name' => 'PatchCableConnectorCompat-FK-pctype_id', 'table_name' => 'PatchCableConnectorCompat'),
741 array ('fkey_name' => 'PatchCableHeap-FK-compat1', 'table_name' => 'PatchCableHeap'),
742 array ('fkey_name' => 'PatchCableHeap-FK-compat2', 'table_name' => 'PatchCableHeap'),
743 array ('fkey_name' => 'PatchCableHeapLog-FK-heap_id', 'table_name' => 'PatchCableHeapLog'),
744 array ('fkey_name' => 'PatchCableOIFCompat-FK-oif_id', 'table_name' => 'PatchCableOIFCompat'),
745 array ('fkey_name' => 'PatchCableOIFCompat-FK-pctype_id', 'table_name' => 'PatchCableOIFCompat'),
746 array ('fkey_name' => 'Port-FK-iif-oif', 'table_name' => 'Port'),
747 array ('fkey_name' => 'Port-FK-object_id', 'table_name' => 'Port'),
748 array ('fkey_name' => 'PortAllowedVLAN-FK-object-port', 'table_name' => 'PortAllowedVLAN'),
749 array ('fkey_name' => 'PortAllowedVLAN-FK-vlan_id', 'table_name' => 'PortAllowedVLAN'),
750 array ('fkey_name' => 'PortCompat-FK-oif_id1', 'table_name' => 'PortCompat'),
751 array ('fkey_name' => 'PortCompat-FK-oif_id2', 'table_name' => 'PortCompat'),
752 array ('fkey_name' => 'PortInterfaceCompat-FK-iif_id', 'table_name' => 'PortInterfaceCompat'),
753 array ('fkey_name' => 'PortInterfaceCompat-FK-oif_id', 'table_name' => 'PortInterfaceCompat'),
754 array ('fkey_name' => 'PortLog_ibfk_1', 'table_name' => 'PortLog'),
755 array ('fkey_name' => 'PortNativeVLAN-FK-compound', 'table_name' => 'PortNativeVLAN'),
756 array ('fkey_name' => 'PortVLANMode-FK-object-port', 'table_name' => 'PortVLANMode'),
757 array ('fkey_name' => 'RackSpace-FK-rack_id', 'table_name' => 'RackSpace'),
758 array ('fkey_name' => 'RackSpace-FK-object_id', 'table_name' => 'RackSpace'),
759 array ('fkey_name' => 'RackThumbnail-FK-rack_id', 'table_name' => 'RackThumbnail'),
760 array ('fkey_name' => 'TagStorage-FK-TagTree', 'table_name' => 'TagStorage'),
761 array ('fkey_name' => 'TagTree-K-parent_id', 'table_name' => 'TagTree'),
762 array ('fkey_name' => 'UserConfig-FK-varname', 'table_name' => 'UserConfig'),
763 array ('fkey_name' => 'VLANDescription-FK-domain_id', 'table_name' => 'VLANDescription'),
764 array ('fkey_name' => 'VLANDescription-FK-vlan_id', 'table_name' => 'VLANDescription'),
765 array ('fkey_name' => 'VLANDomain-FK-group_id', 'table_name' => 'VLANDomain'),
766 array ('fkey_name' => 'VLANIPv4-FK-compound', 'table_name' => 'VLANIPv4'),
767 array ('fkey_name' => 'VLANIPv4-FK-ipv4net_id', 'table_name' => 'VLANIPv4'),
768 array ('fkey_name' => 'VLANIPv6-FK-compound', 'table_name' => 'VLANIPv6'),
769 array ('fkey_name' => 'VLANIPv6-FK-ipv6net_id', 'table_name' => 'VLANIPv6'),
770 array ('fkey_name' => 'VLANSTRule-FK-vst_id', 'table_name' => 'VLANSTRule'),
771 array ('fkey_name' => 'VLANSwitch-FK-domain_id', 'table_name' => 'VLANSwitch'),
772 array ('fkey_name' => 'VLANSwitch-FK-object_id', 'table_name' => 'VLANSwitch'),
773 array ('fkey_name' => 'VLANSwitch-FK-template_id', 'table_name' => 'VLANSwitch'),
774 array ('fkey_name' => 'VSEnabledIPs-FK-object_id', 'table_name' => 'VSEnabledIPs'),
775 array ('fkey_name' => 'VSEnabledIPs-FK-rspool_id', 'table_name' => 'VSEnabledIPs'),
776 array ('fkey_name' => 'VSEnabledIPs-FK-vs_id-vip', 'table_name' => 'VSEnabledIPs'),
777 array ('fkey_name' => 'VSEnabledPorts-FK-object_id', 'table_name' => 'VSEnabledPorts'),
778 array ('fkey_name' => 'VSEnabledPorts-FK-rspool_id', 'table_name' => 'VSEnabledPorts'),
779 array ('fkey_name' => 'VSEnabledPorts-FK-vs_id-proto-vport', 'table_name' => 'VSEnabledPorts'),
780 array ('fkey_name' => 'VSIPs-vs_id', 'table_name' => 'VSIPs'),
781 array ('fkey_name' => 'VS-vs_id', 'table_name' => 'VSPorts'),
330f20d8 782 );
011df355
AD
783
784 $plugins = getPlugins ('enabled');
785 foreach (array_keys ($plugins) as $plugin)
786 {
787 global ${"plugin_${plugin}_fkeys"};
788 if (isset (${"plugin_${plugin}_fkeys"}))
f24df831 789 $known_fkeys = array_merge ($known_fkeys, ${"plugin_${plugin}_fkeys"});
011df355 790 }
f24df831
DO
791 $known_fkeys = reindexById ($known_fkeys, 'fkey_name');
792 ksort ($known_fkeys);
011df355 793
330f20d8
DO
794 $result = usePreparedSelectBlade
795 (
f24df831 796 'SELECT CONSTRAINT_NAME as fkey_name, TABLE_NAME AS table_name ' .
330f20d8
DO
797 'FROM information_schema.TABLE_CONSTRAINTS ' .
798 "WHERE CONSTRAINT_SCHEMA = SCHEMA() AND CONSTRAINT_TYPE = 'FOREIGN KEY'"
799 );
f24df831 800 $existing_fkeys = reindexById ($result->fetchAll (PDO::FETCH_ASSOC), 'fkey_name');
330f20d8 801 unset ($result);
f24df831
DO
802 $missing_fkeys = array_diff_key ($known_fkeys, $existing_fkeys);
803 $unknown_fkeys = array_diff_key ($existing_fkeys, $known_fkeys);
804 $columns = array
805 (
806 array ('th_text' => 'Table', 'row_key' => 'table_name'),
807 array ('th_text' => 'Key', 'row_key' => 'fkey_name'),
808 );
809
330f20d8
DO
810 if (count ($missing_fkeys))
811 {
812 $violations = TRUE;
813 startPortlet ('Missing Foreign Keys (' . count ($missing_fkeys) . ')');
f24df831
DO
814 renderTableViewer ($columns, $missing_fkeys);
815 finishPortLet ();
816 }
817
818 if (count ($unknown_fkeys))
819 {
820 $violations = TRUE;
821 startPortlet ('Unknown Foreign Keys (' . count ($unknown_fkeys) . ')');
822 renderTableViewer ($columns, $unknown_fkeys);
330f20d8
DO
823 finishPortLet ();
824 }
825
826 // check 10: circular references
827 // - all affected members of the tree are displayed
828 // - it would be beneficial to only display the offending records
829 // check 10.1: locations
830 $invalids = array ();
831 $locations = listCells ('location');
832 foreach ($locations as $location)
833 {
834 try
835 {
836 $children = getLocationChildrenList ($location['id']);
837 }
838 catch (RackTablesError $e)
839 {
840 $invalids[] = $location;
841 }
842 }
843 if (count ($invalids))
844 {
845 $violations = TRUE;
846 startPortlet ('Locations: Tree Contains Circular References (' . count ($invalids) . ')');
3b15479f 847 echo "<table cellpadding=5 cellspacing=0 align=center class='cooltable zebra'>\n";
ee660c28 848 echo "<tr><th class=tdright>Child ID</th><th>Child Location</th><th class=tdright>Parent ID</th><th>Parent Location</th></tr>\n";
330f20d8
DO
849 foreach ($invalids as $invalid)
850 {
3b15479f 851 echo '<tr>';
ee660c28 852 echo "<td class=tdright>${invalid['id']}</td>";
330f20d8 853 echo "<td>${invalid['name']}</td>";
ee660c28 854 echo "<td class=tdright>${invalid['parent_id']}</td>";
330f20d8
DO
855 echo "<td>${invalid['parent_name']}</td>";
856 echo "</tr>\n";
330f20d8
DO
857 }
858 echo "</table>\n";
859 finishPortLet ();
860 }
861
862 // check 10.2: objects
863 $invalids = array ();
864 $objects = listCells ('object');
865 foreach ($objects as $object)
866 {
867 try
868 {
869 $children = getObjectContentsList ($object['id']);
870 }
871 catch (RackTablesError $e)
872 {
873 $invalids[] = $object;
874 }
875 }
876 if (count ($invalids))
877 {
878 $violations = TRUE;
879 startPortlet ('Objects: Tree Contains Circular References (' . count ($invalids) . ')');
66eece6d
DO
880 foreach (array_keys ($invalids) as $key)
881 $invalids[$key]['object'] = mkCellA ($invalids[$key]);
882 $columns = array
883 (
884 array ('th_text' => 'Contained ID', 'row_key' => 'id', 'td_class' => 'tdright'),
885 array ('th_text' => 'Contained Object', 'row_key' => 'object', 'td_escape' => FALSE),
886 array ('th_text' => 'Container ID', 'row_key' => 'container_id', 'td_class' => 'tdright'),
887 array ('th_text' => 'Container Object', 'row_key' => 'container_name'),
888 );
889 renderTableViewer ($columns, $invalids);
330f20d8
DO
890 finishPortLet ();
891 }
892
893 // check 10.3: tags
a04b65ce 894 global $taglist;
b46bd2c6 895 $invalids = getInvalidNodes ($taglist);
330f20d8
DO
896 if (count ($invalids))
897 {
898 $violations = TRUE;
899 startPortlet ('Tags: Tree Contains Circular References (' . count ($invalids) . ')');
3b15479f 900 echo "<table cellpadding=5 cellspacing=0 align=center class='cooltable zebra'>\n";
ee660c28 901 echo "<tr><th class=tdright>Child ID</th><th>Child Tag</th><th class=tdright>Parent ID</th><th>Parent Tag</th></tr>\n";
330f20d8
DO
902 foreach ($invalids as $invalid)
903 {
3b15479f 904 echo '<tr>';
ee660c28 905 echo "<td class=tdright>${invalid['id']}</td>";
330f20d8 906 echo "<td>${invalid['tag']}</td>";
ee660c28 907 echo "<td class=tdright>${invalid['parent_id']}</td>";
a04b65ce 908 printf('<td>%s</td>', $taglist[$invalid['parent_id']]['tag']);
330f20d8 909 echo "</tr>\n";
330f20d8
DO
910 }
911 echo "</table>\n";
912 finishPortLet ();
913 }
914
11b1059e 915 // L2 addresses
53f8e018
DO
916 $columns = array
917 (
918 array ('th_text' => 'L2 address', 'row_key' => 'l2address', 'td_class' => 'l2address'),
919 array ('th_text' => 'Object', 'row_key' => 'object', 'td_escape' => FALSE),
920 array ('th_text' => 'Port', 'row_key' => 'name'),
921 );
922
11b1059e
DO
923 // The section below is only required so long as Port.l2address is a char column,
924 // switching to a binary type should eliminate the need for this check.
925 $result = usePreparedSelectBlade
926 (
927 'SELECT l2address, object_id, name FROM Port ' .
928 'WHERE l2address IS NOT NULL AND l2address NOT REGEXP("^[0-9A-F]+$")'
929 );
930 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
931 unset ($result);
932 if (count ($rows))
933 {
934 $violations = TRUE;
935 foreach (array_keys ($rows) as $key)
936 $rows[$key]['object'] = mkCellA (spotEntity ('object', $rows[$key]['object_id']));
53f8e018 937 startPortlet ('L2 address invalid characters');
11b1059e
DO
938 renderTableViewer ($columns, $rows);
939 finishPortlet();
940 }
53f8e018 941
11b1059e
DO
942 // The section below will be relevant as long as the L2 address constraint remains
943 // implemented at PHP level.
944 $result = usePreparedSelectBlade
945 (
946 'SELECT l2address, object_id, name, ' .
947 '(SELECT COUNT(*) FROM Port AS P2 WHERE P2.l2address = P1.l2address AND P2.object_id != P1.object_id) AS ocnt ' .
948 'FROM Port AS P1 WHERE P1.l2address IS NOT NULL HAVING ocnt > 0 ORDER BY l2address, object_id'
949 );
950 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
951 unset ($result);
952 if (count ($rows))
953 {
954 $violations = TRUE;
955 foreach (array_keys ($rows) as $key)
66eece6d 956 {
11b1059e 957 $rows[$key]['object'] = mkCellA (spotEntity ('object', $rows[$key]['object_id']));
66eece6d
DO
958 $rows[$key]['l2address'] = l2addressFromDatabase ($rows[$key]['l2address']);
959 }
11b1059e
DO
960 startPortlet ('L2 address unique constraint errors');
961 renderTableViewer ($columns, $rows);
962 finishPortlet();
963 }
964
53f8e018
DO
965 $result = usePreparedSelectBlade
966 (
967 'SELECT l2address, object_id, name ' .
968 'FROM Port WHERE LENGTH(l2address) NOT IN(12, 16, 40)'
969 );
970 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
971 unset ($result);
972 if (count ($rows))
973 {
974 $violations = TRUE;
975 // Do not try to call l2addressFromDatabase() as it will normally throw an exception.
976 foreach (array_keys ($rows) as $key)
977 $rows[$key]['object'] = mkCellA (spotEntity ('object', $rows[$key]['object_id']));
978 startPortlet ('L2 address invalid length');
979 renderTableViewer ($columns, $rows);
980 finishPortlet();
981 }
982
330f20d8 983 if (! $violations)
7941b2e1 984 echo '<h2 class=centered>No integrity violations found</h2>';
330f20d8 985}