source: www/is/topologie-img.php@ 1

Last change on this file since 1 was 1, checked in by george, 17 years ago

Prvotní import všeho

  • Property svn:executable set to *
File size: 8.3 KB
Line 
1<? // Skript pro generování grafu stromové struktury sítì do PNG obrázku
2include('../db.php');
3DB_Init('localhost','root','','is');
4
5
6if(array_key_exists('debug', $_GET)) $debug = $_GET['debug'];
7else $debug = 0;
8$TopHostName = 'NIX-ROUTER';
9// $debug = 0;
10
11// === Zpìtné vyvá¾ení stromu do hloubky =======================================
12function balance($id, $level, &$vlast, &$vleft, &$vpred, &$vfirst, &$vnext, &$tbound, &$width, $limit) {
13 global $debug, $bbound;
14 if ($i=@$vfirst[$id]) {
15 if ($debug==2) echo @$id.':'.@$i.','.@$vpred[$i].'-'.@$vleft[@$vpred[$i]]."\n";
16 if ((@$vlast[$id]>0)&&(@$vleft[$id]>@$vleft[$vlast[$id]])) {
17 $diff=$vleft[$id]-$vleft[$vlast[$id]];
18 $i=$vfirst[$id];
19 if ($vleft[$id]>=@$tbound[$level]) {
20 $tbound[$level]=$vleft[$id]+2;
21 if ($vleft[$id]>$width) $width=$vleft[$id];
22 }
23 } else {
24 $diff=0;
25 if ($vpred[$i]&&($vleft[$i]<=$vleft[$vpred[$i]])) {
26 $diff=$vleft[$i]-$vleft[$vpred[$i]]+2;
27 } else $i = 0;
28 }
29 while ($i>0) {
30 $vleft[$i]+=$diff;
31 $limit = balance($i,$level+1, $vlast,$vleft,$vpred, $vfirst,$vnext,$tbound, $width, $limit) + 2;
32 $i=@$vnext[$i];
33 }
34 }
35}
36
37// === Generování rovinné stromové struktury ===================================
38function gentree($mode) { // depth-first algorithm
39global $debug, $TopHostName;
40 // --- Inicializace ----------------------------------------------------------
41 $tbound=array(); // Hranice pozic jednotlivých úrovní
42 $tranger=array(); // Hranicni prvek
43 $position=array(); // Pozice aktuálního prvku na dané úrovni
44 $vfirst=array(); // První potomek prvku
45 $vlast=array(); // Poslední potomek prvku
46 $vnext=array(); // Následující sourozenec
47 $vleft=array(); // Pozice prvku zleva
48 $vtop=array(); // Pozice prvku shora
49 $vpred=array(); // Vedlejsi prvek na øádku
50
51 $index = 0; // Index aktuálního prvku
52 $curr = 0; // Aktuální prvek
53 $level = 0; // Aktuální úroveò hloubky ve stromu
54 $width = 0; // ©íøka stromu
55 $height = 0; // Hloubka stromu
56
57 $parent[$level]=0; // Rodiè dané úrovnì
58 $position[$level]=0; // Aktuální pozice prvku na dané úrovni
59 $count[$level]=0; // Poèet prvkù na dané úrovni
60
61 // --- Hlavní cyklus ---------------------------------------------------------
62 do {
63 // --- Proveï databázový dotaz -----------------------------------------------
64 $query = 'SELECT * FROM hosts WHERE used=1 AND ';
65 if ($level==0) { $query .= 'name = "'.$TopHostName.'" ORDER BY id'; } else {
66 $query .= ' parent = '.$parent[$level].' ORDER BY id';
67 }
68 if ($mode) $query.=' DESC';
69 $query .= ' LIMIT '.$position[$level].',1';
70 DB_Query($query);
71 $item = DB_Row();
72 if ($item) {
73 // --- Zpracování polo¾ky z DB -----------------------------------------------
74 if ($position[$level]>0) {
75 $vnext[$curr]=$item['id']; // Neprvní polo¾ka, nastav pøedchozí
76 }
77 $curr = $item['id'];
78 if ($curr>@$maxindex) $maxindex=$curr;
79 if ($position[$level]==0) $vfirst[$parent[$level]]=$curr; // První polo¾ka, nastav první
80 $vlast[$parent[$level]]=$curr;
81 $vtop[$curr] = $level;
82 $vleft[$curr] = @$tbound[$level];
83 $vpred[$curr] = @$tranger[$level];
84 $tranger[$level] = $curr;
85 if (($debug==3)&&($level==8)) echo $curr.',';
86 $position[$level]++;
87 $count[$level]++;
88 // --- Zjisti existenci potomkù ----------------------------------------------
89 DB_Query("SELECT COUNT(*) FROM hosts WHERE used=1 AND parent = ".$curr);
90 $childcnt = DB_Row();
91 if ($childcnt[0]>0) { // Uzelový vrchol
92 if (@$tbound[$level+1]>$vleft[$curr]) $vleft[$curr]=@$tbound[$level+1];
93 }
94 $tbound[$level]=$vleft[$curr]+2;
95 if ($vleft[$curr]>$width) $width=$vleft[$curr];
96 if ($childcnt[0]>0) {
97 $level++;
98 if ($level>$height) $height = $level;
99 $parent[$level]=$curr;
100 $position[$level]=0;
101 $count[$level] = 0;
102 } else $index++; // Listový vrchol
103 } else {
104 // --- Zarovnávání prvkù kvùli vzhledu
105 if (@$vleft[$vfirst[$parent[$level]]]>@$vleft[$parent[$level]]) {
106 $vleft[$parent[$level]] = $vleft[$vfirst[$parent[$level]]];
107 if ($vleft[$parent[$level]]+2>$tbound[$level-1]) $tbound[$level-1] = $vleft[$parent[$level]]+2;
108 }
109 balance(@$parent[$level],$level, $vlast,$vleft,$vpred,$vfirst,$vnext,$tbound, $width, 0);
110 if (@$position[$level]==1) {
111 $vleft[$vfirst[$parent[$level]]]=@$vleft[$parent[$level]];
112 }
113 $level--;
114 $curr=@$vlast[$parent[$level]];
115 if (@$tbound[$level]>@$tbound[$level+1]) $tbound[$level+1]=$tbound[$level];
116 }
117 } while ($level>=0);
118 $data = compact('tbound','count','tbound','vfirst','vlast','vtop','vleft','height','width','index','maxindex');
119 return $data;
120};
121
122// === Vytvoø stromy a spoj je =================================================
123extract(gentree(0));
124// exit();
125$data = gentree(1);
126$datawidth = @$data['width'];
127for ($i=0; $i<=$maxindex; $i++) $vleft[$i]=.2+(@$vleft[$i]+($datawidth-@$data['vleft'][$i]))/2;
128
129$spacex=32;
130$spacey=68;
131$halfx=$spacex/2;
132$halfy=$spacey/2;
133// === Naètení pomocných obrázkù ===============================================
134$im_comp = @imagecreatefrompng('images/comp.png');
135$im_dev = @imagecreatefrompng('images/device.png');
136// === Generování obrázku ======================================================
137$im = @imagecreate(($width+1.6)*$spacex, ($height+1)*$spacey);
138$background_color = imagecolorallocate($im, 255, 255, 255);
139$black = imagecolorallocate($im, 0,0,0);
140$red = imagecolorallocate($im, 255,0,0);
141$green = imagecolorallocate($im, 0,128,0);
142$blue = imagecolorallocate($im, 100,100,255);
143$gray = imagecolorallocate($im, 160,160,160);
144
145function xpos($id) {
146 global $vleft, $spacex;
147 return @$vleft[$id]*$spacex;
148}
149
150DB_Query("SELECT * FROM hosts WHERE used=1");
151while ($item = DB_Row()) {
152 $id = $item['id'];
153 if ((@$vtop[$id]>0)||($item['name']==$TopHostName)) {
154 if ($vtop[$id]>0) imageline($im,xpos($id)+$halfx,@$vtop[$id]*$spacey,xpos($id)+$halfx,@$vtop[$id]*$spacey+8,$black);
155 if (@$vfirst[$id]>0) {
156 imageline($im,xpos($vfirst[$id])+$halfx,$vtop[$id]*$spacey+$spacey,xpos($vlast[$id])+$halfx,$vtop[$id]*$spacey+$spacey,$black);
157 imageline($im,xpos($id)+$halfx,($vtop[$id]+1)*$spacey-10,xpos($id)+$halfx,($vtop[$id]+1)*$spacey,$black);
158 }
159 $ip=explode('.',$item['IP']);
160 if (@$ip[3]<100) { $image=$im_comp; } else $image=$im_dev;
161 if(($ip[0] != 192) and ($ip[0] != 168)) {
162 $image = $im_dev;
163 }
164 if($item['IP'] == '') {
165 $color = $gray;
166 $image = $im_dev;
167 } else {
168 if ((($ip[0] != 192) and ($ip[0] != 168)) or ($ip[3]>=100) or ($ip[3]==1)) {
169 if($item['online'] == 1) { $color = $green; } else $color = $red;
170 } else {
171 if($item['online'] == 1) { $color = $green; } else $color = $black;
172 }
173 }
174// $text='IP: '.$ip[0];
175// imagestring($im,2,xpos($id)+($spacex-strlen($text)*imagefontwidth(2))/2,$vtop[$id]*$spacey+24+imagefontheight(2),$text,$black);
176 imagecopy($im, $image, xpos($id)+$halfx-15,$vtop[$id]*$spacey+12, 0, 0, 30, 30);
177// imagerectangle($im,xpos($id)+$halfx-6,$vtop[$id]*$spacey+16,xpos($id)+$halfx+6,$vtop[$id]*$spacey+28,$color);
178 if ($debug) {
179 imagestring($im,2,xpos($id)+($spacex-strlen($item['id'])*imagefontwidth(2))/2,$vtop[$id]*$spacey+31+imagefontheight(2),$item['id'],$color);
180 } else imagestring($im,2,xpos($id)+($spacex-strlen($item['name'])*imagefontwidth(2))/2,$vtop[$id]*$spacey+31+imagefontheight(2),$item['name'],$color);
181 }
182}
183
184// === Sestavení výsledného souboru ============================================
185if(!($debug>1))
186{
187 header("Content-type: image/png");
188 header("Cache-Control: no-cache"); // Dynamický graf, neke¹ovat
189 imagepng($im);
190 imagedestroy($im);
191 imagedestroy($im_comp);
192 imagedestroy($im_dev);
193}
194?>
Note: See TracBrowser for help on using the repository browser.