jablonka.czprosek.czf

netmap

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

 

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 
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) {
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)));
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)));
169 }
170 }
171 
172 }
173 
174 // ------------------------------------------------------------------------
175 
176 $image_filename = $argv[1];
177 
178 // Basic definitions
179 $calstats_version = "0.1.1";
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 
200 fclose($file_in);
201 // --- Load config file - end ---------------------------------------------
202 
203 // --- Load computer data - begin -----------------------------------------
204 $file_in = fopen($image_filename . ".comp", "r");
205 if (!$file_in) { exit; }
206 
207 $comp_num = 0;
208 while (!feof($file_in)) {
209 $fin_line=rtrim(fgets($file_in, 1024));
210 $a_comp[$comp_num][0] = strtok($fin_line, ";"); // name
211 $a_comp[$comp_num][1] = strtok(";"); // ip
212 $a_comp[$comp_num][2] = strtok(";"); // type
213 $a_comp[$comp_num][3] = strtok(";"); // x1
214 $a_comp[$comp_num][4] = strtok(";"); // y1
215 $comp_num++;
216 }
217 $comp_num--;
218 
219 fclose($file_in);
220 // --- Load computer data - end -------------------------------------------
221 
222 // --- Load links data - begin --------------------------------------------
223 $link_show = 1;
224 if (file_exists($image_filename . ".link")) {
225 $file_in = fopen($image_filename . ".link", "r");
226 if (!$file_in) { exit; }
227 
228 $link_num = 0;
229 while (!feof($file_in)) {
230 $fin_line=rtrim(fgets($file_in, 1024));
231 $link_tmp_type = strtok($fin_line, ";"); // link_type
232 $a_link[$link_num][0] = $link_tmp_type;
233 
234 $link_tmp = strtok(";");
235 while ($link_tmp != "") {
236 $a_link[$link_num][0] = $link_tmp_type;
237 $a_link[$link_num][3] = 0; // was it drawed on the screen
238 $a_link[$link_num][1] = $link_tmp; // comp
239 $link_tmp = strtok(";");
240 $a_link[$link_num][2] = $link_tmp; // comp
241 if ($link_tmp != "") { $link_num++; }
242 }
243 }
244 
245 fclose($file_in);
246 }
247 else {
248 $link_show=0;
249 }
250 // --- Load links data - end ----------------------------------------------
251 
252 // --- Load legend data - begin -------------------------------------------
253 $legend_show = 1;
254 if (file_exists($image_filename . ".legend")) {
255 $file_in = fopen($image_filename . ".legend", "r");
256 if (!$file_in) { exit; }
257 
258 $legend_num = 0;
259 while (!feof($file_in)) {
260 $fin_line=rtrim(fgets($file_in, 1024));
261 $a_legend[$legend_num][0] = strtok($fin_line, ";"); // name
262 $a_legend[$legend_num][1] = strtok(";"); // ip
263 $legend_num++;
264 }
265 $legend_num--;
266 
267 fclose($file_in);
268 }
269 else {
270 $legend_show = 0;
271 }
272 // --- Load legend data - end ---------------------------------------------
273 
274 // --- Load links state - begin -------------------------------------------
275 $file_in = fopen($image_filename . ".state", "r");
276 if (!$file_in) { exit; }
277 
278 $state_num = 0;
279 while (!feof($file_in)) {
280 $fin_line=rtrim(fgets($file_in, 1024));
281 $a_state[$state_num][0] = strtok($fin_line, ";"); // ip
282 $a_state[$state_num][1] = strtok(";"); // available
283 $a_state[$state_num][2] = strtok(";"); // packet loss
284 $state_num++;
285 }
286 $state_num--;
287 
288 fclose($file_in);
289 // --- Load links state - end ---------------------------------------------
290 
291 
292 Header("Content-type: image/png");
293 $im = ImageCreate($image_xsize, $image_ysize);
294 
295 $im_white = ImageColorAllocate($im, 255, 255, 255);
296 $im_black = ImageColorAllocate($im, 0, 0, 0);
297 
298 $im_gray1 = ImageColorAllocate($im, 140, 140, 140);
299 $im_gray2 = ImageColorAllocate($im, 180, 180, 180);
300 $im_gray3 = ImageColorAllocate($im, 200, 200, 200);
301 $im_gray4 = ImageColorAllocate($im, 220, 220, 220);
302 $im_gray5 = ImageColorAllocate($im, 240, 240, 240);
303 $im_red = ImageColorAllocate($im, 255, 0, 0);
304 $im_green = ImageColorAllocate($im, 0, 255, 0);
305 $im_blue = ImageColorAllocate($im, 0, 0, 255);
306 $im_red2 = ImageColorAllocate($im, 180, 0, 0);
307 $im_green2 = ImageColorAllocate($im, 0, 180, 0);
308 $im_blue2 = ImageColorAllocate($im, 0, 0, 180);
309 $im_magenta2 = ImageColorAllocate($im, 180, 0, 180);
310 $im_yellow2 = ImageColorAllocate($im, 180, 180, 0);
311 $im_wblue2 = ImageColorAllocate($im, 0, 180, 180);
312 $im_red3 = ImageColorAllocate($im, 255, 140, 140);
313 $im_green3 = ImageColorAllocate($im, 140, 255, 140);
314 $im_blue3 = ImageColorAllocate($im, 140, 140, 255);
315 
316 ImageRectangle($im, 0, 0, ($image_xsize-1), ($image_ysize-1), $im_black);
317 ImageFilledRectangle($im, 1, 1, ($image_xsize-2), 15, $im_black);
318 ImageString($im, 3, 5, 1, $image_title, $im_white);
319 ImageString($im, 2, ($image_xsize-ImageFontWidth(2)*strlen($image_date)-5), 1, $image_date, $im_gray4);
320 ImageStringUp($im, 1, ($image_xsize-10), ($image_ysize-5), $calstats_id . " " . $calstats_version, $im_gray2);
321 
322 // Place links
323 if ($link_show == 1) {
324 for ($i1 = 0; ($i1 < $link_num); $i1++) {
325 if ($a_link[$i1][3] == 0) {
326 $l_pat = return_link_str1($a_link[$i1][0]);
327 $a_link[$i1][3] = 1;
328 
329 $i_l1 = return_ap_index($a_comp, $comp_num, $a_link[$i1][1]);
330 $i_l2 = return_ap_index($a_comp, $comp_num, $a_link[$i1][2]);
331 
332 for ($i2 = ($i1 + 1); ($i2 < $link_num); $i2++) {
333 $i_l3_tmp = return_ap_index($a_comp, $comp_num, $a_link[$i2][1]);
334 $i_l4_tmp = return_ap_index($a_comp, $comp_num, $a_link[$i2][2]);
335 if ( (($i_l1 == $i_l3_tmp) && ($i_l2 == $i_l4_tmp))
336 || (($i_l1 == $i_l4_tmp) && ($i_l2 == $i_l3_tmp)) ) {
337 $l_pat = $l_pat . "----" . return_link_str1($a_link[$i2][0]);
338 $a_link[$i2][3] = 1;
339 }
340 }
341 
342 placeLN($im, $a_comp[$i_l1][3], $a_comp[$i_l1][4], $a_comp[$i_l2][3], $a_comp[$i_l2][4], $l_pat);
343 }
344 }
345 }
346 
347 // Place computers
348 for ($i1=0; ($i1 < $comp_num); $i1++) {
349 $comp_tmp1 = return_ip_state($a_state, $state_num, 0, $a_comp[$i1][1]);
350 $comp_tmp2 = return_ip_state($a_state, $state_num, 1, $a_comp[$i1][1]);
351 if ($a_comp[$i1][1] == "127.0.0.1") {
352 $comp_tmp1 = 2;
353 $comp_tmp2 = 0;
354 }
355 
356 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);
357 }
358 
359 // Draw legend
360 if ( ($legend_show == 1) && ($image_legend != 0) ) {
361 $l_num_string = 0;
362 for ($i1 = 0; ($i1 < $legend_num); $i1++) {
363 if (strlen($a_legend[$i1][1]) > $l_num_string) {
364 $l_num_string = strlen($a_legend[$i1][1]);
365 }
366 }
367 
368 if ( ($image_legend == 1) || ($image_legend == 3) ) { $l_xloc = 12; } else { $l_xloc = ($image_xsize-37-($l_num_string*ImageFontWidth(2))); }
369 if ( ($image_legend == 1) || ($image_legend == 2) ) { $l_yloc = 27; } else { $l_yloc = ($image_ysize-12-($legend_num*15)); }
370 
371 ImageRectangle($im, $l_xloc, ($l_yloc-2), ($l_xloc+25+($l_num_string*ImageFontWidth(2))), ($l_yloc + ($legend_num*15) + 1), $im_gray3);
372 ImageFilledRectangle($im, ($l_xloc+1), ($l_yloc-1), ($l_xloc+24+($l_num_string*ImageFontWidth(2))), ($l_yloc + ($legend_num*15)), $im_gray5);
373 
374 for ($i1 = 0; ($i1 < $legend_num); $i1++) {
375 placeLN($im, ($l_xloc+5), ($l_yloc+7), ($l_xloc+15), ($l_yloc+7), return_link_str1($a_legend[$i1][0]), 2);
376 ImageString($im, 2, ($l_xloc+20), ($l_yloc), $a_legend[$i1][1], $im_black);
377 $l_yloc+=15;
378 }
379 }
380 
381 ImagePng ($im);
382 ImageDestroy($im);
383 
384?>

Powered by WebSVN 2.2.1