add the roadmap .dia file for 2019Q2
[racktables-contribs] / rtv.php
1 <?php
2
3 /* This script is "RackTables visualised", it was contributed
4 * by Colin Coe to racktables-users mailing list in 2009.
5 */
6
7 // Our purpose is to produce a graphic
8 header ("Content-type: image/png");
9
10 function GetMySQLData() {
11 global $devices, $links, $sites, $port_count, $ports, $racks;
12
13 $sql_query = "
14 select distinct RO.id as ROid1, RO2.id as ROid2, RO.name as ROname1, RO2.name as ROname2, P.name as pname1, P2.name as pname2, L.porta, RR.name as site, R.name as rackname, R2.name as rackname2, P.type as linktype, P.label as label1, P2.label as label2
15 from RackObject RO, RackObject RO2, Port P, Link L, Link L2, Port P2, RackRow RR, Rack R, Rack R2, RackSpace RS, RackSpace RS2
16 where RO.name != RO2.name and
17 RR.id = R.row_id and
18 (R.id = RS.rack_id and
19 RS.object_id = RO.id and
20 RO.id = P.object_id) and
21 (R2.id = RS2.rack_id and
22 RS2.object_id = RO2.id and
23 RO2.id = P2.object_id) and
24 P.id = L.porta and
25 RO2.id = P2.object_id and
26 (P2.id = L2.porta or P2.id = L2.portb) and
27 (L2.porta = L.porta and L2.portb = L.portb)
28 order by L.porta;";
29
30 include "/var/www/racktables/inc/secret.php";
31 $dblink = mysql_connect("localhost", $db_username, $db_password) or die(mysql_error());
32 mysql_select_db("racktables", $dblink) or die(mysql_error());
33 $result = mysql_query($sql_query, $dblink) or die(mysql_error());
34 while($row = mysql_fetch_assoc($result)) {
35 $devices[$row['ROname1']] = $row['rackname'];
36 $devices[$row['ROname2']] = $row['rackname2'];
37
38 $links[$row['porta']] = array(
39 "Site" => $row['site'],
40 "Device1" => $row['ROname1'],
41 "Port1" => $row['pname1'],
42 "Label1" => $row['label1'],
43 "Rack1" => $row['rackname'],
44 "Device2" => $row['ROname2'],
45 "Port2" => $row['pname2'],
46 "Label2" => $row['label2'],
47 "Rack2" => $row['rackname2'],
48 "Type" => $row['linktype']);
49
50 $sites[$row['ROname1']] = $row['site'];
51 $sites[$row['ROname2']] = $row['site'];
52 $racks[$row['rackname']] = $row['site'];
53 $racks[$row['rackname2']] = $row['site'];
54
55 $presult = mysql_query("SELECT name FROM Port where object_id = ".$row['ROid1'], $dblink);
56 $port_count[$row['ROname1']] = mysql_num_rows($presult);
57 $i = 0;
58 while($prow = mysql_fetch_assoc($presult)) {
59 $ports[$row['ROname1']][$i] = $prow['name'];
60 $i++;
61 }
62 unset($i);
63
64 $i = 0;
65 $presult = mysql_query("SELECT * FROM Port where object_id = ".$row['ROid2'], $dblink);
66 $port_count[$row['ROname2']] = mysql_num_rows($presult);
67 while($prow = mysql_fetch_assoc($presult)) {
68 $ports[$row['ROname2']][$i] = $prow['name'];
69 $i++;
70 }
71 unset($i);
72 }
73 // asort($ports);
74 }
75
76 function SwapLink(&$link) {
77 $items = array('Device', 'Port', 'Label', 'Rack');
78 foreach ($items as $item) {
79 $tmp = $link[sprintf("%s1", $item)];
80 $link[sprintf("%s1", $item)] = $link[sprintf("%s2", $item)];
81 $link[sprintf("%s2", $item)] = $tmp;
82 unset($tmp);
83 }
84 unset($items);
85 }
86
87 function CountRacksInSite($site) {
88 global $racks;
89
90 $rack_count = 0;
91 foreach($racks as $rackname => $sitename) {
92 if ($site == $sitename) {
93 $rack_count++;
94 }
95 }
96
97 return $rack_count;
98 }
99
100 function CountDevicesInRack($site) {
101 // How many devices in the most populated rack?
102 global $devices, $racks;
103
104 $rack = array();
105 $max = 0;
106
107 foreach($devices as $device_name => $installed_rack) {
108 if ($racks[$installed_rack] == $site) {
109 if (isset($rack[$installed_rack])) {
110 $rack[$installed_rack]++;
111 }
112 else {
113 $rack[$installed_rack]=1;
114 }
115 if ($rack[$installed_rack] > $max) {
116 $max = $rack[$installed_rack];
117 }
118 }
119 }
120
121 return $max;
122 }
123
124 function CountLinks($site) {
125 global $links;
126
127 $tmp_array = array();
128 $max = 0;
129
130 foreach($links as $link) {
131 if ($link['Site'] == $site) {
132 if ($link['Rack1'] > $link['Rack2']) {
133 // For simplicity, make sure all links go from left to right
134 SwapLink($link);
135 }
136 for($i=1;$i<=2;$i++) {
137 if (isset($tmp_array[$link['Rack'.$i]])) {
138 $tmp_array[$link['Rack'.$i]]++;
139 }
140 else {
141 $tmp_array[$link['Rack'.$i]] = 1;
142 }
143 if ($tmp_array[$link['Rack'.$i]] > $max) {
144 $max = $tmp_array[$link['Rack'.$i]];
145 }
146 }
147 }
148 }
149
150 return $max;
151 }
152
153
154 function Draw($site) {
155 global $image, $racks, $devices, $links, $port_count, $ports;
156
157 $rack_count = CountRacksInSite($site);
158
159 // Define the modifiers
160 $modifier = 3;
161 $top_modifier = 0;
162 $dev_modifier = array();
163 $side_modifier = array();
164
165 $rack_buffer = (CountLinks($site) + 2) * $modifier ; // Space between racks
166 $rack_margin = $rack_buffer; // Space between the racks and edge of page
167 $y_top_margin = 100 + (CountLinks($site) * 1.25);
168 $y_bot_margin = 75;
169
170 $page_size = 7;
171 $page_counter = 0;
172 $x_offset = 25;
173 $y_offset = 50;
174 $height = 100;
175 $rack_width = 300;
176 $rack_height = CountDevicesInRack($site) * $height;
177 $width = $rack_width;
178 $x_buffer = $width + intval($width * 0.5);
179 $y_buffer = 50;
180
181 ksort($racks);
182 ksort($devices);
183
184 $x_max = ($rack_count * $rack_width) + ($rack_count* $rack_buffer) + ($rack_margin * 2);
185 $y_max = $rack_height + $y_top_margin + intval(ImageFontHeight(2) * (CountLinks($site) * 1.25) + 0.5);
186
187 $image = ImageCreate($x_max, $y_max) or die ("Cannot Create image");
188 $background_color = imagecolorallocate($image, 255, 255, 255);
189
190 $pink = ImageColorAllocate($image, 255, 105, 180);
191 $white = ImageColorAllocate($image, 255, 255, 255);
192 $black = ImageColorAllocate($image, 0, 0, 0);
193 $red = ImageColorAllocate($image, 255, 0, 0);
194 $green = ImageColorAllocate($image, 89, 200, 180);
195 $blue = ImageColorAllocate($image, 34,68,228);
196 $grey = ImageColorAllocate($image, 225, 225, 225);
197
198 $title = "RackTables Visualised ($site)";
199 $title_font = 5;
200 $title_font_width = ImageFontWidth($title_font);
201 ImageString($image, $title_font, ($x_max / 2) - (($title_font_width * strlen($title)) / 2),
202 $title_font_width * 2, $title, $black);
203
204 // Draw racks
205 $rc = 1;
206 ksort($racks, SORT_STRING);
207
208 foreach($racks as $rackname => $sitename) {
209 if ($site != $sitename) {
210 continue;
211 }
212 $y = $y_top_margin;
213 $x = $rack_margin + (($rc - 1) * $rack_width) + ($rc * $rack_buffer);
214 $x = (($rc - 1) * $rack_width) + ($rc * $rack_buffer);
215 ImageRectangle($image, $x, $y, $x + $rack_width, $y + $rack_height, $black);
216 $rack_name_width = ImageFontWidth($rackname);
217 $rack_name_font = 2;
218
219 ImageString($image, $rack_name_font, $x + ($rack_width - ($rack_name_width * strlen($rackname))) / 2,
220 $y - intval(ImageFontHeight($rack_name_font) * 1.25), $rackname, $black);
221
222 // Draw devices
223 foreach($devices as $device_name => $installed_rack) {
224 if ($installed_rack != $rackname) {
225 continue;
226 }
227 ImageRectangle($image, $x, $y, $x + $width, $y + $height, $black);
228 ImageString($image, 2, $x + 5, $y + 3, strtoupper($device_name), $black);
229 $unit = ($width / (($port_count[$device_name]) + 1));
230 for($i=0;$i<$port_count[$device_name];$i++) {
231 $cx = $x + ($unit * ($i + 1));
232
233 $coord[$device_name][$ports[$device_name][$i]][0] = $cx;
234 $coord[$device_name][$ports[$device_name][$i]][1] = intval($y + $height / 2);
235
236 ImageFilledEllipse($image, $cx, $coord[$device_name][$ports[$device_name][$i]][1], 3, 3, $black);
237
238 ImageStringUp($image, 1, $cx - intval(ImageFontWidth(1) / 2), $coord[$device_name][$ports[$device_name][$i]][1] - 5, $ports[$device_name][$i], $black);
239 }
240 $y = $y + $height;
241 }
242 $rc++;
243 }
244 unset($rc);
245
246 $line_tracker = array();
247 $index = 0;
248
249 $label = "Device Inter-Connections";
250 ImageString($image, 3, $rack_margin, $y_top_margin + $rack_height + 20, $label, $black);
251 ImageLine($image, $rack_margin, $y_top_margin + $rack_height + 20 + ImageFontHeight(3) + 1, $rack_margin + (ImageFontWidth(3) * strlen($label)), $y_top_margin + $rack_height + 20 + ImageFontHeight(3) + 1, $black);
252 unset($label);
253
254 $y = $y_top_margin - intval(ImageFontHeight(2) * 1.25);
255
256 $text_x_coord['data'] = $rack_margin;
257 $text_x_coord['power'] = $rack_margin + 320;
258 $text_x_coord['kvm'] = $rack_margin + 640;
259
260 // Iterate through the links
261 foreach($links as $link) {
262
263 switch($link['Type']) {
264 case 16: // Power
265 $colour = $red;
266 $type = 'power';
267 break;
268 case 19: // Fast Ethernet
269 $colour = $blue;
270 $type = 'data';
271 break;
272 case 24: // GB Ethernet
273 $colour = $blue;
274 $type = 'data';
275 break;
276 case 1077: // SFF
277 $colour = $blue;
278 $type = 'data';
279 break;
280 case 33: // KVM
281 $colour = $green;
282 $type = 'kvm';
283 break;
284 case 446: // KVM
285 $colour = $green;
286 $type = 'kvm';
287 break;
288 }
289
290 // Filter out devices with un-linked ports
291 if ((isset($coord[$link['Device1']][$link['Port1']][0]) and
292 isset($coord[$link['Device1']][$link['Port1']][1]) and
293 isset($coord[$link['Device2']][$link['Port2']][0]) and
294 isset($coord[$link['Device2']][$link['Port2']][1]))) {
295 $d1x = $coord[$link['Device1']][$link['Port1']][0];
296 $d1y = $coord[$link['Device1']][$link['Port1']][1];
297 $d2x = $coord[$link['Device2']][$link['Port2']][0];
298 $d2y = $coord[$link['Device2']][$link['Port2']][1];
299
300 // $port_list = $port_list + intval(ImageFontHeight(2) * 1.25);
301 if (isset($text_y_coord[$type])) {
302 $text_y_coord[$type] += intval(ImageFontHeight(2) * 1.25);
303 }
304 else {
305 $text_y_coord[$type] = $y_top_margin + $rack_height + 20 + intval(ImageFontHeight(2) * 1.25);
306 }
307
308 // Print the link information and darw a simple table
309 ImageString($image, 2, $text_x_coord[$type], $text_y_coord[$type], $link['Device1'].":".$link['Port1'], $colour);
310 ImageString($image, 2, $text_x_coord[$type] + 150, $text_y_coord[$type], $link['Device2'].":".$link['Port2'], $colour);
311 ImageLine($image, $text_x_coord[$type] - 10, $text_y_coord[$type], $text_x_coord[$type] + 300, $text_y_coord[$type], $grey);
312 if ($text_y_coord[$type] != $y_top_margin + $rack_height + 20 + intval(ImageFontHeight(2) * 1.25)) {
313 ImageLine($image, $text_x_coord[$type] - 10, $text_y_coord[$type] + intval(ImageFontHeight(2) * 1.25), $text_x_coord[$type] + 300, $text_y_coord[$type] + intval(ImageFontHeight(2) * 1.25), $grey);
314 }
315 ImageLine($image, $text_x_coord[$type] - 10, $text_y_coord[$type], $text_x_coord[$type] - 10, $text_y_coord[$type] + intval(ImageFontHeight(2) * 1.25), $grey);
316 ImageLine($image, $text_x_coord[$type] + 150 - 10, $text_y_coord[$type], $text_x_coord[$type] + 150 - 10, $text_y_coord[$type] + intval(ImageFontHeight(2) * 1.25), $grey);
317 ImageLine($image, $text_x_coord[$type] + 300, $text_y_coord[$type], $text_x_coord[$type] + 300, $text_y_coord[$type] + intval(ImageFontHeight(2) * 1.25), $grey);
318
319 if (isset($side_modifier[$link['Rack1']])) {
320 $side_modifier[$link['Rack1']] += $modifier;
321 }
322 else {
323 $side_modifier[$link['Rack1']] = $modifier;
324 }
325
326 if (isset($side_modifier[$link['Rack2']])) {
327 $side_modifier[$link['Rack2']] += $modifier;
328 }
329 else {
330 $side_modifier[$link['Rack2']] = $modifier;
331 }
332
333 if (isset($dev_modifier[$link['Device1']])) {
334 $dev_modifier[$link['Device1']] += $modifier;
335 }
336 else {
337 $dev_modifier[$link['Device1']] = $modifier;
338 }
339
340 if (isset($dev_modifier[$link['Device2']])) {
341 $dev_modifier[$link['Device2']] += $modifier;
342 }
343 else {
344 $dev_modifier[$link['Device2']] = $modifier;
345 }
346
347 $r1x = $d1x - $rack_margin;
348 $i = 0;
349 while ($r1x > 0) {
350 $r1x = $r1x - $rack_width - $rack_buffer;
351 $i++;
352 }
353 $r1x = $rack_margin + ($rack_width * $i) + ($rack_buffer * $i);
354 $r1x = ($rack_width * $i) + ($rack_buffer * $i);
355
356 $r2x = $d2x - $rack_margin;
357 $j = 0;
358 while ($r2x > 0) {
359 $r2x = $r2x - $rack_width - $rack_buffer;
360 $j++;
361 }
362 $r2x = $rack_margin + ($rack_width * ($j - 1)) + ($rack_buffer * $j);
363 $r2x = ($rack_width * ($j - 1)) + ($rack_buffer * $j);
364
365 ImageLine($image, $d1x, $d1y, $d1x, $d1y + $dev_modifier[$link['Device1']], $colour);
366 ImageLine($image, $d1x, $d1y + $dev_modifier[$link['Device1']], $r1x + $side_modifier[$link['Rack1']], $d1y + $dev_modifier[$link['Device1']], $colour);
367 if ($link['Rack1'] == $link['Rack2']) {
368 ImageLine($image, $r1x + $side_modifier[$link['Rack1']], $d1y + $dev_modifier[$link['Device1']], $r1x + $side_modifier[$link['Rack1']], $d2y + $dev_modifier[$link['Device2']], $colour);
369 ImageLine($image, $r1x + $side_modifier[$link['Rack1']], $d2y + $dev_modifier[$link['Device2']], $d2x, $d2y + $dev_modifier[$link['Device2']], $colour);
370 // ImageLine($image, $d2x, $d2y + $dev_modifier[$link['Device2']], $d2x, $d2y, $colour);
371 }
372 else {
373 $top_modifier += $modifier;
374 // ImageLine($image, $d1x, $d1y, $d1x, $d1y + $dev_modifier[$link['Device1']], $colour);
375 // ImageLine($image, $d1x, $d1y + $dev_modifier[$link['Device1']], $r1x + $side_modifier[$link['Rack1']], $d1y + $dev_modifier[$link['Device1']], $colour);
376 ImageLine($image, $r1x + $side_modifier[$link['Rack1']], $d1y + $dev_modifier[$link['Device1']], $r1x + $side_modifier[$link['Rack1']], $y - $top_modifier, $colour);
377 ImageLine($image, $r1x + $side_modifier[$link['Rack1']], $y - $top_modifier, $d2x - ($d2x - $r2x) + $rack_width + $side_modifier[$link['Rack2']], $y - $top_modifier, $colour);
378 ImageLine($image, $d2x - ($d2x - $r2x) + $rack_width + $side_modifier[$link['Rack2']], $y - $top_modifier, $d2x - ($d2x - $r2x) + $rack_width + $side_modifier[$link['Rack2']], $d2y + $dev_modifier[$link['Device2']], $colour);
379 ImageLine($image, $d2x - ($d2x - $r2x) + $rack_width + $side_modifier[$link['Rack2']], $d2y + $dev_modifier[$link['Device2']], $d2x, $d2y + $dev_modifier[$link['Device2']], $colour);
380 }
381 ImageLine($image, $d2x, $d2y + $dev_modifier[$link['Device2']], $d2x, $d2y, $colour);
382
383
384 unset($i ,$j, $r1x, $r2x, $ry);
385 }
386 }
387
388 ob_start();
389 ImagePng($image);
390 $contents = ob_get_contents();
391 ob_end_clean();
392
393 $new_site = str_replace(' ', '_', $site);
394
395 $fh = fopen("/tmp/$new_site.png", "w");
396 fwrite($fh, $contents);
397 fclose($fh);
398 }
399
400 $devices = array();
401 $links = array();
402 $sites = array();
403 $ports = array();
404 $racks = array();
405 $port_count = array();
406
407 GetMySQLData();
408
409 foreach($sites as $site) {
410 $image = "";
411 CountRacksInSite($site);
412 CountLinks($site);
413 Draw($site);
414 }