jablonka.czprosek.czf

netmap

Subversion Repositories:
[/] [calstats.php] - Blame information for rev 21

 

Line No. Rev Author Line
11simandl<?php
2 
3 /* ****************************************** *
4 * *
5 * (c)2003 by Jan Krupa (krupaj@mobilnews.cz) *
6 * http://www.mobilnews.cz/honza/ *
7 * *
8 * ****************************************** */
9 
10 function return_ap_index($comp_ar, $comp_ar_num, $ap_name) {
11 // Return computer index in array
12 
13 for ($ix1 = 0; ($ix1 < $comp_ar_num); $ix1++) {
14 $comp_ar_tmp0 = $comp_ar[$ix1][0];
15 $comp_ar_tmp1 = $comp_ar[$ix1][1];
16 
17 if ( (substr($comp_ar_tmp0, 0, 1) == "!")
18 || (substr($comp_ar_tmp0, 0, 1) == "$")
19 || (substr($comp_ar_tmp0, 0, 1) == "%") ) { $comp_ar_tmp0 = substr($comp_ar_tmp0, 1, (strlen($comp_ar_tmp0)-1)); }
20 
21 if ( ($comp_ar_tmp0 == $ap_name) // name
22 || ($comp_ar_tmp1 == $ap_name) ) { // ip
23 return $ix1;
24 }
25 }
26 
27 return (-1);
28 
29 }
30 
31 function return_ip_state($state_ar, $state_ar_num, $state_type, $state_ip) {
32 // Return ip address state
33 // $state_type - 0 = availability, 1 = packet loss
34 
35 for ($ix1 = 0; ($ix1 < $state_ar_num); $ix1++) {
36 if ($state_ar[$ix1][0] == $state_ip) {
37 return $state_ar[$ix1][(1+$state_type)];
38 }
39 }
40 
41 return (-1);
42 
43 }
44 
45 function return_link_str1($in_s1) {
46 $l_tmp_pat = "";
47 $l_tmp_chr = '0'; if (strlen($in_s1) > 1) { $l_tmp_chr = substr($in_s1, (strlen($in_s1)-2), 1); }
48 for ($j1 = 0; ($j1 < (substr($in_s1, (strlen($in_s1)-1), 1) + 1)); $j1++) {
49 $l_tmp_pat = $l_tmp_pat . $l_tmp_chr;
50 }
51 
52 return $l_tmp_pat;
53 }
54 
55 function assign_color($im, $in_col) {
56 // Assign defined color to line
57 
58 $ln_color=ImageColorExact($im, 180, 180, 180);
59 switch ($in_col) {
60 case '-' : $ln_color = ImageColorExact($im, 255, 255, 255); break;
61 case 1 : $ln_color = ImageColorExact($im, 0, 0, 0); break;
62 case 2 : $ln_color = ImageColorExact($im, 180, 0, 0); break;
63 case 3 : $ln_color = ImageColorExact($im, 0, 180, 0); break;
64 case 4 : $ln_color = ImageColorExact($im, 0, 0, 180); break;
65 case 5 : $ln_color = ImageColorExact($im, 180, 0, 180); break; // magenta
66 case 6 : $ln_color = ImageColorExact($im, 180, 180, 0); break; // yellow
67 case 7 : $ln_color = ImageColorExact($im, 0, 180, 180); break; // white blue
68 case 8 : $ln_color = ImageColorExact($im, 255, 0, 0); break;
69 case 9 : $ln_color = ImageColorExact($im, 0, 255, 0); break;
70 }
71 
72 return $ln_color;
73 
74 }
75 
76 function placeAP ($im, $x1, $y1, $ap_name, $ap_ip, $ap_type, $ap_state, $ap_lat) {
77 // Place computer on the map
78 
79 $ap_showip = 1;
80 if ($ap_ip == "127.0.0.1") { $ap_showip = 0; }
81 
82 // ! ... hide ip and packet latency information
83 // $ ... hide ip
84 // % ... hide packet latency information
85 
86 $ap_name_show = $ap_name;
87 if ( (substr($ap_name, 0, 1) == "!") || (substr($ap_name, 0, 1) == "$") ) {
88 $ap_showip = 0;
89 $ap_name_show = substr($ap_name, 1, (strlen($ap_name)-1));
90 }
91 
92 $ap_lat_show = $ap_lat;
93 if ( (substr($ap_name, 0, 1) == "!") || (substr($ap_name, 0, 1) == "%") ) {
94 $ap_lat_show = 0;
95 $ap_name_show = substr($ap_name, 1, (strlen($ap_name)-1)); // not needed if ! (already done in previous step)
96 }
97 
9810simandl $ap_xsize = 70;
991simandl $ap_ysize = 35;
100 if ($ap_showip == 0) { $ap_ysize=17; }
101 $ap_name_size = ImageFontWidth(3)*strlen($ap_name_show);
102 $ap_ip_size = ImageFontWidth(2)*strlen($ap_ip);
103 $ap_lat_size = ImageFontWidth(2)*strlen(round($ap_lat_show))+6;
104 if ($ap_lat_show == 0) { $ap_lat_size = 1; }
105 
106 if (($ap_name_size + $ap_lat_size + 10) > ($ap_xsize)) {
107 $ap_xsize = $ap_name_size + $ap_lat_size+10;
108 /*
109 if (($ap_ip_size + 10) > $ap_xsize) {
110 $ap_xsize = $ap_ip_size + 10;
111 }
112 */
113 }
114 
115 switch ($ap_state) {
116 case 0 : $ap_color = ImageColorExact($im, 255, 0, 0); $ap_color2=ImageColorExact($im, 255, 140, 140); break;
117 case 1 : $ap_color = ImageColorExact($im, 0, 255, 0); $ap_color2=ImageColorExact($im, 140, 255, 140); break;
118 case 2 : $ap_color = ImageColorExact($im, 180, 180, 180); $ap_color2=ImageColorExact($im, 220, 220, 220); break;
119 }
120 
121 ImageRectangle($im, ($x1-($ap_xsize/2)), ($y1-($ap_ysize/2)), ($x1+($ap_xsize/2)), ($y1+($ap_ysize/2)), ImageColorExact($im, 0, 0, 0));
122 ImageFilledRectangle($im, ($x1-($ap_xsize/2-1)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-$ap_lat_size)), ($y1-($ap_ysize/2-16)), $ap_color);
123 if ($ap_showip == 1) {
124 ImageLine($im, ($x1-($ap_xsize/2-1)), ($y1-($ap_ysize/2-17)), ($x1+($ap_xsize/2-1)), ($y1-($ap_ysize/2-17)), ImageColorExact($im, 140, 140, 140));
125 }
126 
127 if ($ap_lat_show !=0 ) {
12821simandl if ($ap_lat_show < 15) {
129 ImageFilledRectangle($im, ($x1+($ap_xsize/2-$ap_lat_size+2)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-1)), ($y1-($ap_ysize/2-16)), $ap_color2);
130 } else {
131 ImageFilledRectangle($im, ($x1+($ap_xsize/2-$ap_lat_size+2)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-1)), ($y1-($ap_ysize/2-16)), ImageColorExact($im, 255, 140, 140));
132 }
1331simandl ImageLine($im, ($x1+($ap_xsize/2-$ap_lat_size+1)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-$ap_lat_size+1)), ($y1-($ap_ysize/2-16)),ImageColorExact($im, 140, 140, 140));
134 ImageString($im, 2, ($x1+($ap_xsize/2)-$ap_lat_size+4), $y1-($ap_ysize/2-2), round($ap_lat_show), ImageColorExact($im, 0, 0, 0));
135 }
136 
137 ImageFilledRectangle($im, ($x1-($ap_xsize/2-1)), ($y1-($ap_ysize/2-18)), ($x1+($ap_xsize/2-1)), ($y1+($ap_ysize/2-1)), ImageColorExact($im, 220, 220, 220));
138 
139 for ($i=1; $i<=$ap_type; $i++) {
140 ImageRectangle($im, ($x1-($ap_xsize/2)-$i), ($y1-($ap_ysize/2)-$i), ($x1+($ap_xsize/2)+$i), ($y1+($ap_ysize/2)+$i), ImageColorExact($im, 0, 0, 0));
141 }
142 
143 ImageString($im, 3, ($x1-($ap_name_size/2)-($ap_lat_size/2)+2), $y1-($ap_ysize/2-2), $ap_name_show, ImageColorExact($im, 0, 0, 0));
144 if ($ap_showip == 1) {
145 ImageString($im, 2, ($x1-strlen($ap_ip)*3), ($y1+1), $ap_ip, ImageColorExact($im, 0, 0, 0));
146 }
147 
148 }
149 
150 function placeLN($im, $x1, $y1, $x2, $y2, $lnd) {
151 // Place link onto map
152 
153 // Find out how to draw lines
154 $ln_dir = 0; // 0 = horizontal, 1 = verical
155 $tmp_x = abs($x1 - $x2);
156 $tmp_y = abs($y1 - $y2);
157 if ($tmp_x > $tmp_y) {
158 $ln_dir = 1;
159 }
160 
161 if ($ln_dir == 0) {
162 // Horizontal
163 $tmp_s1 = $x1 - round(strlen($lnd) / 2);
164 for ($ix1 = $tmp_s1; $ix1 < ($tmp_s1 + strlen($lnd)); $ix1++) {
165 ImageLine($im, $ix1, $y1, ($x2 - $x1 + $ix1), $y2, assign_color($im, substr($lnd, ($ix1 - $tmp_s1), 1)));
166 }
167 }
168 else {
169 // Vertical
170 $tmp_s1 = $y1 - round(strlen($lnd) / 2);
171 for ($ix1 = $tmp_s1; $ix1 < ($tmp_s1 + strlen($lnd)); $ix1++) {
172 ImageLine($im, $x1, $ix1, $x2, ($y2 - $y1 + $ix1), assign_color($im, substr($lnd, ($ix1 - $tmp_s1), 1)));
173 }
174 }
175 
176 }
177 
178 // ------------------------------------------------------------------------
179 
180 $image_filename = $argv[1];
181 
182 // Basic definitions
183 $calstats_version = "0.1.1";
184 $calstats_id = "CaLStats";
185 $image_date_lu = "Last update ";
186 
187 // Date
188 $a_date = getdate();
189 $a_date_minutes = $a_date['minutes']; if ($a_date_minutes < 10) { $a_date_minutes = "0" . $a_date_minutes; }
190 $a_date_seconds = $a_date['seconds']; if ($a_date_seconds < 10) { $a_date_seconds = "0" . $a_date_seconds; }
191 $image_date = $image_date_lu . $a_date['mday'] . " " . $a_date['month'] . " " . $a_date['year'] . " "
192 . $a_date['hours'] . ":" . $a_date_minutes . ":" . $a_date_seconds;
193 
194 // --- Load config file - begin -------------------------------------------
195 $file_in = fopen($image_filename . ".config", "r");
196 if (!$file_in) { exit; }
197 
198 $fin_line=rtrim(fgets($file_in, 1024));
199 $image_title = strtok($fin_line, ";");
200 $image_xsize = strtok(";");
201 $image_ysize = strtok(";");
202 $image_legend = strtok(";"); // legend location
203 
204 fclose($file_in);
205 // --- Load config file - end ---------------------------------------------
206 
207 // --- Load computer data - begin -----------------------------------------
208 $file_in = fopen($image_filename . ".comp", "r");
209 if (!$file_in) { exit; }
210 
211 $comp_num = 0;
212 while (!feof($file_in)) {
213 $fin_line=rtrim(fgets($file_in, 1024));
214 $a_comp[$comp_num][0] = strtok($fin_line, ";"); // name
215 $a_comp[$comp_num][1] = strtok(";"); // ip
216 $a_comp[$comp_num][2] = strtok(";"); // type
217 $a_comp[$comp_num][3] = strtok(";"); // x1
218 $a_comp[$comp_num][4] = strtok(";"); // y1
219 $comp_num++;
220 }
221 $comp_num--;
222 
223 fclose($file_in);
224 // --- Load computer data - end -------------------------------------------
225 
226 // --- Load links data - begin --------------------------------------------
227 $link_show = 1;
228 if (file_exists($image_filename . ".link")) {
229 $file_in = fopen($image_filename . ".link", "r");
230 if (!$file_in) { exit; }
231 
232 $link_num = 0;
233 while (!feof($file_in)) {
234 $fin_line=rtrim(fgets($file_in, 1024));
235 $link_tmp_type = strtok($fin_line, ";"); // link_type
236 $a_link[$link_num][0] = $link_tmp_type;
237 
238 $link_tmp = strtok(";");
239 while ($link_tmp != "") {
240 $a_link[$link_num][0] = $link_tmp_type;
241 $a_link[$link_num][3] = 0; // was it drawed on the screen
242 $a_link[$link_num][1] = $link_tmp; // comp
243 $link_tmp = strtok(";");
244 $a_link[$link_num][2] = $link_tmp; // comp
245 if ($link_tmp != "") { $link_num++; }
246 }
247 }
248 
249 fclose($file_in);
250 }
251 else {
252 $link_show=0;
253 }
254 // --- Load links data - end ----------------------------------------------
255 
256 // --- Load legend data - begin -------------------------------------------
257 $legend_show = 1;
258 if (file_exists($image_filename . ".legend")) {
259 $file_in = fopen($image_filename . ".legend", "r");
260 if (!$file_in) { exit; }
261 
262 $legend_num = 0;
263 while (!feof($file_in)) {
264 $fin_line=rtrim(fgets($file_in, 1024));
265 $a_legend[$legend_num][0] = strtok($fin_line, ";"); // name
266 $a_legend[$legend_num][1] = strtok(";"); // ip
267 $legend_num++;
268 }
269 $legend_num--;
270 
271 fclose($file_in);
272 }
273 else {
274 $legend_show = 0;
275 }
276 // --- Load legend data - end ---------------------------------------------
277 
278 // --- Load links state - begin -------------------------------------------
279 $file_in = fopen($image_filename . ".state", "r");
280 if (!$file_in) { exit; }
281 
282 $state_num = 0;
283 while (!feof($file_in)) {
284 $fin_line=rtrim(fgets($file_in, 1024));
285 $a_state[$state_num][0] = strtok($fin_line, ";"); // ip
286 $a_state[$state_num][1] = strtok(";"); // available
287 $a_state[$state_num][2] = strtok(";"); // packet loss
288 $state_num++;
289 }
290 $state_num--;
291 
292 fclose($file_in);
293 // --- Load links state - end ---------------------------------------------
294 
295 
296 Header("Content-type: image/png");
297 $im = ImageCreate($image_xsize, $image_ysize);
298 
299 $im_white = ImageColorAllocate($im, 255, 255, 255);
300 $im_black = ImageColorAllocate($im, 0, 0, 0);
301 
302 $im_gray1 = ImageColorAllocate($im, 140, 140, 140);
303 $im_gray2 = ImageColorAllocate($im, 180, 180, 180);
304 $im_gray3 = ImageColorAllocate($im, 200, 200, 200);
305 $im_gray4 = ImageColorAllocate($im, 220, 220, 220);
306 $im_gray5 = ImageColorAllocate($im, 240, 240, 240);
307 $im_red = ImageColorAllocate($im, 255, 0, 0);
308 $im_green = ImageColorAllocate($im, 0, 255, 0);
309 $im_blue = ImageColorAllocate($im, 0, 0, 255);
310 $im_red2 = ImageColorAllocate($im, 180, 0, 0);
311 $im_green2 = ImageColorAllocate($im, 0, 180, 0);
312 $im_blue2 = ImageColorAllocate($im, 0, 0, 180);
313 $im_magenta2 = ImageColorAllocate($im, 180, 0, 180);
314 $im_yellow2 = ImageColorAllocate($im, 180, 180, 0);
315 $im_wblue2 = ImageColorAllocate($im, 0, 180, 180);
316 $im_red3 = ImageColorAllocate($im, 255, 140, 140);
317 $im_green3 = ImageColorAllocate($im, 140, 255, 140);
318 $im_blue3 = ImageColorAllocate($im, 140, 140, 255);
319 
320 ImageRectangle($im, 0, 0, ($image_xsize-1), ($image_ysize-1), $im_black);
321 ImageFilledRectangle($im, 1, 1, ($image_xsize-2), 15, $im_black);
322 ImageString($im, 3, 5, 1, $image_title, $im_white);
323 ImageString($im, 2, ($image_xsize-ImageFontWidth(2)*strlen($image_date)-5), 1, $image_date, $im_gray4);
324 ImageStringUp($im, 1, ($image_xsize-10), ($image_ysize-5), $calstats_id . " " . $calstats_version, $im_gray2);
325 
326 // Place links
327 if ($link_show == 1) {
328 for ($i1 = 0; ($i1 < $link_num); $i1++) {
329 if ($a_link[$i1][3] == 0) {
330 $l_pat = return_link_str1($a_link[$i1][0]);
331 $a_link[$i1][3] = 1;
332 
333 $i_l1 = return_ap_index($a_comp, $comp_num, $a_link[$i1][1]);
334 $i_l2 = return_ap_index($a_comp, $comp_num, $a_link[$i1][2]);
335 
336 for ($i2 = ($i1 + 1); ($i2 < $link_num); $i2++) {
337 $i_l3_tmp = return_ap_index($a_comp, $comp_num, $a_link[$i2][1]);
338 $i_l4_tmp = return_ap_index($a_comp, $comp_num, $a_link[$i2][2]);
339 if ( (($i_l1 == $i_l3_tmp) && ($i_l2 == $i_l4_tmp))
340 || (($i_l1 == $i_l4_tmp) && ($i_l2 == $i_l3_tmp)) ) {
341 $l_pat = $l_pat . "----" . return_link_str1($a_link[$i2][0]);
342 $a_link[$i2][3] = 1;
343 }
344 }
345 
346 placeLN($im, $a_comp[$i_l1][3], $a_comp[$i_l1][4], $a_comp[$i_l2][3], $a_comp[$i_l2][4], $l_pat);
347 }
348 }
349 }
350 
351 // Place computers
352 for ($i1=0; ($i1 < $comp_num); $i1++) {
353 $comp_tmp1 = return_ip_state($a_state, $state_num, 0, $a_comp[$i1][1]);
354 $comp_tmp2 = return_ip_state($a_state, $state_num, 1, $a_comp[$i1][1]);
355 if ($a_comp[$i1][1] == "127.0.0.1") {
356 $comp_tmp1 = 2;
357 $comp_tmp2 = 0;
358 }
359 
36021simandl placeAP($im, $a_comp[$i1][3], $a_comp[$i1][4], $a_comp[$i1][0], $a_comp[$i1][1], $a_comp[$i1][2], $comp_tmp1, $comp_tmp2);
3611simandl }
362 
363 // Draw legend
364 if ( ($legend_show == 1) && ($image_legend != 0) ) {
365 $l_num_string = 0;
366 for ($i1 = 0; ($i1 < $legend_num); $i1++) {
367 if (strlen($a_legend[$i1][1]) > $l_num_string) {
368 $l_num_string = strlen($a_legend[$i1][1]);
369 }
370 }
371 
372 if ( ($image_legend == 1) || ($image_legend == 3) ) { $l_xloc = 12; } else { $l_xloc = ($image_xsize-37-($l_num_string*ImageFontWidth(2))); }
373 if ( ($image_legend == 1) || ($image_legend == 2) ) { $l_yloc = 27; } else { $l_yloc = ($image_ysize-12-($legend_num*15)); }
374 
375 ImageRectangle($im, $l_xloc, ($l_yloc-2), ($l_xloc+25+($l_num_string*ImageFontWidth(2))), ($l_yloc + ($legend_num*15) + 1), $im_gray3);
376 ImageFilledRectangle($im, ($l_xloc+1), ($l_yloc-1), ($l_xloc+24+($l_num_string*ImageFontWidth(2))), ($l_yloc + ($legend_num*15)), $im_gray5);
377 
378 for ($i1 = 0; ($i1 < $legend_num); $i1++) {
379 placeLN($im, ($l_xloc+5), ($l_yloc+7), ($l_xloc+15), ($l_yloc+7), return_link_str1($a_legend[$i1][0]), 2);
380 ImageString($im, 2, ($l_xloc+20), ($l_yloc), $a_legend[$i1][1], $im_black);
381 $l_yloc+=15;
382 }
383 }
384 
385 ImagePng ($im);
386 ImageDestroy($im);
387 
388?>

Powered by WebSVN 2.2.1