jablonka.czprosek.czf

netmap

Subversion Repositories:
[/] [work/] [calstats-0.1.2/] [calstats/] [calstats.php] - Blame information for rev 128

 

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

Powered by WebSVN 2.2.1