jablonka.czprosek.czf

netmap

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

 

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;
6546simandl case 5 : $ln_color = ImageColorExact($im, 255, 0, 255); break; // magenta
6647simandl case 6 : $ln_color = ImageColorExact($im, 180, 180, 0); break; // yellow
671simandl 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 
9844simandl $ap_xsize = 40;
99 $ap_ysize = 20;
100 $ap_rowh = $ap_ysize/2;
101 $ap_name_font = 1;
102 $ap_lat_font = 1;
103 $ap_ip_font = 1;
104 
105 if ($ap_showip == 0) { $ap_ysize=$ap_ysize/2; }
106 $ap_name_size = ImageFontWidth($ap_name_font)*strlen($ap_name_show);
107 $ap_ip_size = ImageFontWidth($ap_ip_font)*strlen($ap_ip);
108 $ap_lat_size = ImageFontWidth($ap_lat_font)*strlen(round($ap_lat_show))+3;
1091simandl if ($ap_lat_show == 0) { $ap_lat_size = 1; }
110 
11144simandl if (($ap_name_size + $ap_lat_size + 4) > ($ap_xsize)) {
112 $ap_xsize = $ap_name_size + $ap_lat_size + 4;
1131simandl /*
114 if (($ap_ip_size + 10) > $ap_xsize) {
115 $ap_xsize = $ap_ip_size + 10;
116 }
117 */
118 }
119 
12044simandl if (($ap_ip_size + 5) > ($ap_xsize)) {
121 $ap_xsize = $ap_ip_size + 5;
122 }
123 
1241simandl switch ($ap_state) {
125 case 0 : $ap_color = ImageColorExact($im, 255, 0, 0); $ap_color2=ImageColorExact($im, 255, 140, 140); break;
126 case 1 : $ap_color = ImageColorExact($im, 0, 255, 0); $ap_color2=ImageColorExact($im, 140, 255, 140); break;
127 case 2 : $ap_color = ImageColorExact($im, 180, 180, 180); $ap_color2=ImageColorExact($im, 220, 220, 220); break;
128 }
129 
130 ImageRectangle($im, ($x1-($ap_xsize/2)), ($y1-($ap_ysize/2)), ($x1+($ap_xsize/2)), ($y1+($ap_ysize/2)), ImageColorExact($im, 0, 0, 0));
13144simandl ImageFilledRectangle($im, ($x1-($ap_xsize/2-1)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-$ap_lat_size)), ($y1-($ap_ysize/2-$ap_rowh)), $ap_color);
1321simandl if ($ap_showip == 1) {
13344simandl ImageLine($im, ($x1-($ap_xsize/2-1)), ($y1-($ap_ysize/2-1-$ap_rowh)), ($x1+($ap_xsize/2-1)), ($y1-($ap_ysize/2-1-$ap_rowh)), ImageColorExact($im, 140, 140, 140));
1341simandl }
135 
136 if ($ap_lat_show !=0 ) {
13721simandl if ($ap_lat_show < 15) {
13844simandl ImageFilledRectangle($im, ($x1+($ap_xsize/2-$ap_lat_size+2)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-1)), ($y1-($ap_ysize/2-$ap_rowh)), $ap_color2);
13921simandl } else {
14044simandl ImageFilledRectangle($im, ($x1+($ap_xsize/2-$ap_lat_size+2)), ($y1-($ap_ysize/2-1)), ($x1+($ap_xsize/2-1)), ($y1-($ap_ysize/2-$ap_rowh)), ImageColorExact($im, 255, 140, 140));
14121simandl }
14244simandl 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-$ap_rowh)),ImageColorExact($im, 140, 140, 140));
143 ImageString($im, $ap_lat_font, ($x1+($ap_xsize/2)-$ap_lat_size+3), $y1-($ap_ysize/2-1), round($ap_lat_show), ImageColorExact($im, 0, 0, 0));
1441simandl }
145 
14644simandl if ($ap_showip == 1) {
147 ImageFilledRectangle($im, ($x1-($ap_xsize/2-1)), ($y1-($ap_ysize/2-2-$ap_rowh)), ($x1+($ap_xsize/2-1)), ($y1+($ap_ysize/2-1)), ImageColorExact($im, 220, 220, 220));
148 }
1491simandl 
150 for ($i=1; $i<=$ap_type; $i++) {
151 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));
152 }
153 
15444simandl ImageString($im, $ap_name_font, ($x1-($ap_name_size/2)-($ap_lat_size/2)+2), $y1-($ap_ysize/2-2), $ap_name_show, ImageColorExact($im, 0, 0, 0));
1551simandl if ($ap_showip == 1) {
15644simandl ImageString($im, $ap_ip_font, ($x1-$ap_ip_size/2), ($y1+2), $ap_ip, ImageColorExact($im, 0, 0, 0));
1571simandl }
158 
159 }
160 
161 function placeLN($im, $x1, $y1, $x2, $y2, $lnd) {
162 // Place link onto map
163 
164 // Find out how to draw lines
165 $ln_dir = 0; // 0 = horizontal, 1 = verical
166 $tmp_x = abs($x1 - $x2);
167 $tmp_y = abs($y1 - $y2);
168 if ($tmp_x > $tmp_y) {
169 $ln_dir = 1;
170 }
171 
172 if ($ln_dir == 0) {
173 // Horizontal
174 $tmp_s1 = $x1 - round(strlen($lnd) / 2);
175 for ($ix1 = $tmp_s1; $ix1 < ($tmp_s1 + strlen($lnd)); $ix1++) {
176 ImageLine($im, $ix1, $y1, ($x2 - $x1 + $ix1), $y2, assign_color($im, substr($lnd, ($ix1 - $tmp_s1), 1)));
177 }
178 }
179 else {
180 // Vertical
181 $tmp_s1 = $y1 - round(strlen($lnd) / 2);
182 for ($ix1 = $tmp_s1; $ix1 < ($tmp_s1 + strlen($lnd)); $ix1++) {
183 ImageLine($im, $x1, $ix1, $x2, ($y2 - $y1 + $ix1), assign_color($im, substr($lnd, ($ix1 - $tmp_s1), 1)));
184 }
185 }
186 
187 }
188 
189 // ------------------------------------------------------------------------
190 
191 $image_filename = $argv[1];
192 
193 // Basic definitions
194 $calstats_version = "0.1.1";
195 $calstats_id = "CaLStats";
196 $image_date_lu = "Last update ";
197 
198 // Date
199 $a_date = getdate();
200 $a_date_minutes = $a_date['minutes']; if ($a_date_minutes < 10) { $a_date_minutes = "0" . $a_date_minutes; }
201 $a_date_seconds = $a_date['seconds']; if ($a_date_seconds < 10) { $a_date_seconds = "0" . $a_date_seconds; }
202 $image_date = $image_date_lu . $a_date['mday'] . " " . $a_date['month'] . " " . $a_date['year'] . " "
203 . $a_date['hours'] . ":" . $a_date_minutes . ":" . $a_date_seconds;
204 
205 // --- Load config file - begin -------------------------------------------
206 $file_in = fopen($image_filename . ".config", "r");
207 if (!$file_in) { exit; }
208 
209 $fin_line=rtrim(fgets($file_in, 1024));
210 $image_title = strtok($fin_line, ";");
211 $image_xsize = strtok(";");
212 $image_ysize = strtok(";");
213 $image_legend = strtok(";"); // legend location
214 
215 fclose($file_in);
216 // --- Load config file - end ---------------------------------------------
217 
218 // --- Load computer data - begin -----------------------------------------
219 $file_in = fopen($image_filename . ".comp", "r");
220 if (!$file_in) { exit; }
221 
222 $comp_num = 0;
223 while (!feof($file_in)) {
224 $fin_line=rtrim(fgets($file_in, 1024));
225 $a_comp[$comp_num][0] = strtok($fin_line, ";"); // name
226 $a_comp[$comp_num][1] = strtok(";"); // ip
227 $a_comp[$comp_num][2] = strtok(";"); // type
228 $a_comp[$comp_num][3] = strtok(";"); // x1
229 $a_comp[$comp_num][4] = strtok(";"); // y1
230 $comp_num++;
231 }
232 $comp_num--;
233 
234 fclose($file_in);
235 // --- Load computer data - end -------------------------------------------
236 
237 // --- Load links data - begin --------------------------------------------
238 $link_show = 1;
239 if (file_exists($image_filename . ".link")) {
240 $file_in = fopen($image_filename . ".link", "r");
241 if (!$file_in) { exit; }
242 
243 $link_num = 0;
244 while (!feof($file_in)) {
245 $fin_line=rtrim(fgets($file_in, 1024));
246 $link_tmp_type = strtok($fin_line, ";"); // link_type
247 $a_link[$link_num][0] = $link_tmp_type;
248 
249 $link_tmp = strtok(";");
250 while ($link_tmp != "") {
251 $a_link[$link_num][0] = $link_tmp_type;
252 $a_link[$link_num][3] = 0; // was it drawed on the screen
253 $a_link[$link_num][1] = $link_tmp; // comp
254 $link_tmp = strtok(";");
255 $a_link[$link_num][2] = $link_tmp; // comp
256 if ($link_tmp != "") { $link_num++; }
257 }
258 }
259 
260 fclose($file_in);
261 }
262 else {
263 $link_show=0;
264 }
265 // --- Load links data - end ----------------------------------------------
266 
267 // --- Load legend data - begin -------------------------------------------
268 $legend_show = 1;
269 if (file_exists($image_filename . ".legend")) {
270 $file_in = fopen($image_filename . ".legend", "r");
271 if (!$file_in) { exit; }
272 
273 $legend_num = 0;
274 while (!feof($file_in)) {
275 $fin_line=rtrim(fgets($file_in, 1024));
276 $a_legend[$legend_num][0] = strtok($fin_line, ";"); // name
277 $a_legend[$legend_num][1] = strtok(";"); // ip
278 $legend_num++;
279 }
280 $legend_num--;
281 
282 fclose($file_in);
283 }
284 else {
285 $legend_show = 0;
286 }
287 // --- Load legend data - end ---------------------------------------------
288 
289 // --- Load links state - begin -------------------------------------------
290 $file_in = fopen($image_filename . ".state", "r");
291 if (!$file_in) { exit; }
292 
293 $state_num = 0;
294 while (!feof($file_in)) {
295 $fin_line=rtrim(fgets($file_in, 1024));
296 $a_state[$state_num][0] = strtok($fin_line, ";"); // ip
297 $a_state[$state_num][1] = strtok(";"); // available
298 $a_state[$state_num][2] = strtok(";"); // packet loss
299 $state_num++;
300 }
301 $state_num--;
302 
303 fclose($file_in);
304 // --- Load links state - end ---------------------------------------------
305 
306 
307 Header("Content-type: image/png");
308 $im = ImageCreate($image_xsize, $image_ysize);
309 
310 $im_white = ImageColorAllocate($im, 255, 255, 255);
311 $im_black = ImageColorAllocate($im, 0, 0, 0);
312 
313 $im_gray1 = ImageColorAllocate($im, 140, 140, 140);
314 $im_gray2 = ImageColorAllocate($im, 180, 180, 180);
315 $im_gray3 = ImageColorAllocate($im, 200, 200, 200);
316 $im_gray4 = ImageColorAllocate($im, 220, 220, 220);
317 $im_gray5 = ImageColorAllocate($im, 240, 240, 240);
318 $im_red = ImageColorAllocate($im, 255, 0, 0);
319 $im_green = ImageColorAllocate($im, 0, 255, 0);
320 $im_blue = ImageColorAllocate($im, 0, 0, 255);
321 $im_red2 = ImageColorAllocate($im, 180, 0, 0);
322 $im_green2 = ImageColorAllocate($im, 0, 180, 0);
323 $im_blue2 = ImageColorAllocate($im, 0, 0, 180);
324 $im_magenta2 = ImageColorAllocate($im, 180, 0, 180);
325 $im_yellow2 = ImageColorAllocate($im, 180, 180, 0);
326 $im_wblue2 = ImageColorAllocate($im, 0, 180, 180);
327 $im_red3 = ImageColorAllocate($im, 255, 140, 140);
328 $im_green3 = ImageColorAllocate($im, 140, 255, 140);
329 $im_blue3 = ImageColorAllocate($im, 140, 140, 255);
330 
331 ImageRectangle($im, 0, 0, ($image_xsize-1), ($image_ysize-1), $im_black);
332 ImageFilledRectangle($im, 1, 1, ($image_xsize-2), 15, $im_black);
333 ImageString($im, 3, 5, 1, $image_title, $im_white);
334 ImageString($im, 2, ($image_xsize-ImageFontWidth(2)*strlen($image_date)-5), 1, $image_date, $im_gray4);
335 ImageStringUp($im, 1, ($image_xsize-10), ($image_ysize-5), $calstats_id . " " . $calstats_version, $im_gray2);
336 
337 // Place links
338 if ($link_show == 1) {
339 for ($i1 = 0; ($i1 < $link_num); $i1++) {
340 if ($a_link[$i1][3] == 0) {
341 $l_pat = return_link_str1($a_link[$i1][0]);
342 $a_link[$i1][3] = 1;
343 
344 $i_l1 = return_ap_index($a_comp, $comp_num, $a_link[$i1][1]);
345 $i_l2 = return_ap_index($a_comp, $comp_num, $a_link[$i1][2]);
346 
347 for ($i2 = ($i1 + 1); ($i2 < $link_num); $i2++) {
348 $i_l3_tmp = return_ap_index($a_comp, $comp_num, $a_link[$i2][1]);
349 $i_l4_tmp = return_ap_index($a_comp, $comp_num, $a_link[$i2][2]);
350 if ( (($i_l1 == $i_l3_tmp) && ($i_l2 == $i_l4_tmp))
351 || (($i_l1 == $i_l4_tmp) && ($i_l2 == $i_l3_tmp)) ) {
352 $l_pat = $l_pat . "----" . return_link_str1($a_link[$i2][0]);
353 $a_link[$i2][3] = 1;
354 }
355 }
356 
357 placeLN($im, $a_comp[$i_l1][3], $a_comp[$i_l1][4], $a_comp[$i_l2][3], $a_comp[$i_l2][4], $l_pat);
358 }
359 }
360 }
361 
362 // Place computers
363 for ($i1=0; ($i1 < $comp_num); $i1++) {
364 $comp_tmp1 = return_ip_state($a_state, $state_num, 0, $a_comp[$i1][1]);
365 $comp_tmp2 = return_ip_state($a_state, $state_num, 1, $a_comp[$i1][1]);
366 if ($a_comp[$i1][1] == "127.0.0.1") {
367 $comp_tmp1 = 2;
368 $comp_tmp2 = 0;
369 }
370 
37121simandl 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);
3721simandl }
373 
374 // Draw legend
375 if ( ($legend_show == 1) && ($image_legend != 0) ) {
376 $l_num_string = 0;
377 for ($i1 = 0; ($i1 < $legend_num); $i1++) {
378 if (strlen($a_legend[$i1][1]) > $l_num_string) {
379 $l_num_string = strlen($a_legend[$i1][1]);
380 }
381 }
382 
383 if ( ($image_legend == 1) || ($image_legend == 3) ) { $l_xloc = 12; } else { $l_xloc = ($image_xsize-37-($l_num_string*ImageFontWidth(2))); }
384 if ( ($image_legend == 1) || ($image_legend == 2) ) { $l_yloc = 27; } else { $l_yloc = ($image_ysize-12-($legend_num*15)); }
385 
386 ImageRectangle($im, $l_xloc, ($l_yloc-2), ($l_xloc+25+($l_num_string*ImageFontWidth(2))), ($l_yloc + ($legend_num*15) + 1), $im_gray3);
387 ImageFilledRectangle($im, ($l_xloc+1), ($l_yloc-1), ($l_xloc+24+($l_num_string*ImageFontWidth(2))), ($l_yloc + ($legend_num*15)), $im_gray5);
388 
389 for ($i1 = 0; ($i1 < $legend_num); $i1++) {
390 placeLN($im, ($l_xloc+5), ($l_yloc+7), ($l_xloc+15), ($l_yloc+7), return_link_str1($a_legend[$i1][0]), 2);
391 ImageString($im, 2, ($l_xloc+20), ($l_yloc), $a_legend[$i1][1], $im_black);
392 $l_yloc+=15;
393 }
394 }
395 
396 ImagePng ($im);
397 ImageDestroy($im);
398 
399?>

Powered by WebSVN 2.2.1