1 | <?php |
---|
2 | // If php is compiled without globals |
---|
3 | // |
---|
4 | if ( !empty( $_GET ) ) { |
---|
5 | extract( $_GET ); |
---|
6 | } |
---|
7 | |
---|
8 | class HTTPVariables { |
---|
9 | |
---|
10 | var $clustername, $metricname; |
---|
11 | var $restvars, $httpvars; |
---|
12 | |
---|
13 | function HTTPVariables( $httpvars, $getvars ) { |
---|
14 | |
---|
15 | $this->restvars = array(); |
---|
16 | |
---|
17 | $this->clustername = $httpvars["c"] ? $httpvars["c"] : null; |
---|
18 | $this->metricname = $httpvars["m"] ? $httpvars["m"] : null; |
---|
19 | |
---|
20 | foreach( $httpvars as $httpvar => $httpval ) { |
---|
21 | |
---|
22 | if( $httpval ) { |
---|
23 | $this->restvars[$httpvar] = $httpval; |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | foreach( $getvars as $getvar => $getval ) { |
---|
28 | |
---|
29 | if( $getval ) { |
---|
30 | $this->restvars[$getvar] = $getval; |
---|
31 | } |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | function getClusterName() { |
---|
36 | return $this->clustername; |
---|
37 | } |
---|
38 | |
---|
39 | function getMetricName() { |
---|
40 | return $this->metricname; |
---|
41 | } |
---|
42 | |
---|
43 | function getHttpVar( $var ) { |
---|
44 | if( isset( $this->restvars[$var] ) ) |
---|
45 | return $this->restvars[$var]; |
---|
46 | else |
---|
47 | return null; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | // Toga's conf |
---|
52 | // |
---|
53 | include_once "./conf.php"; |
---|
54 | |
---|
55 | global $GANGLIA_PATH; |
---|
56 | |
---|
57 | $my_dir = getcwd(); |
---|
58 | |
---|
59 | // Load Ganglia's PHP |
---|
60 | chdir( $GANGLIA_PATH ); |
---|
61 | |
---|
62 | $context = 'cluster'; |
---|
63 | |
---|
64 | include_once "./conf.php"; |
---|
65 | include_once "./functions.php"; |
---|
66 | include_once "./ganglia.php"; |
---|
67 | //include_once "./get_context.php"; |
---|
68 | include_once "./get_ganglia.php"; |
---|
69 | |
---|
70 | // Back to our PHP |
---|
71 | chdir( $my_dir ); |
---|
72 | |
---|
73 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET; |
---|
74 | $httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET ); |
---|
75 | |
---|
76 | // Set cluster context so that Ganglia will |
---|
77 | // provide us with the correct metrics array |
---|
78 | // |
---|
79 | global $context, $clustername, $reports; |
---|
80 | |
---|
81 | //$clustername = $httpvars->getClusterName(); |
---|
82 | |
---|
83 | global $default_metric; |
---|
84 | |
---|
85 | // Ganglia's array of host metrics |
---|
86 | // |
---|
87 | global $metrics, $hosts_up; |
---|
88 | |
---|
89 | |
---|
90 | class TarchDbase { |
---|
91 | |
---|
92 | var $ip, $dbase; |
---|
93 | |
---|
94 | function TarchDbase( $ip = null, $dbase = 'toga' ) { |
---|
95 | $this->ip = $ip; |
---|
96 | $this->dbase = $dbase; |
---|
97 | } |
---|
98 | |
---|
99 | function connect() { |
---|
100 | |
---|
101 | if( $this->ip == null and $this->dbase == 'toga' ) |
---|
102 | $this->conn = pg_connect( "dbase=".$this->dbase ); |
---|
103 | else |
---|
104 | $this->conn = pg_connect( "host=".$this->ip." dbase=".$this->dbase ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | class TarchRrd { |
---|
109 | var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics; |
---|
110 | |
---|
111 | function TarchRrd( $rrdbin = '/usr/bin/rrdtool', $tarchdir = '/data/toga/rrds' ) { |
---|
112 | $this->rrdbin = $rrdbin; |
---|
113 | $this->rrdvalues = array(); |
---|
114 | $this->tarchdir = $tarchdir; |
---|
115 | $this->tempdir = '/tmp/toga-web-temp'; |
---|
116 | $this->metrics = array(); |
---|
117 | } |
---|
118 | |
---|
119 | function doCmd( $command ) { |
---|
120 | |
---|
121 | printf( "command = %s\n", $command ); |
---|
122 | $pipe = popen( $command . ' 2>&1', 'r' ); |
---|
123 | |
---|
124 | if (!$pipe) { |
---|
125 | print "pipe failed."; |
---|
126 | return ""; |
---|
127 | } |
---|
128 | |
---|
129 | $output = ''; |
---|
130 | while(!feof($pipe)) |
---|
131 | $output .= fread($pipe, 1024); |
---|
132 | |
---|
133 | pclose($pipe); |
---|
134 | |
---|
135 | $output = explode( "\n", $output ); |
---|
136 | //print_r( $output ); |
---|
137 | return $output; |
---|
138 | } |
---|
139 | |
---|
140 | function dirList( $dir ) { |
---|
141 | |
---|
142 | $dirlist = array(); |
---|
143 | |
---|
144 | if ($handle = opendir( $dir )) { |
---|
145 | while (false !== ($file = readdir($handle))) { |
---|
146 | if ($file != "." && $file != "..") { |
---|
147 | $dirlist[] = $file; |
---|
148 | } |
---|
149 | } |
---|
150 | closedir($handle); |
---|
151 | } |
---|
152 | |
---|
153 | return $dirlist; |
---|
154 | } |
---|
155 | |
---|
156 | function getTimePeriods( $start, $end ) { |
---|
157 | |
---|
158 | $times = array(); |
---|
159 | $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname ); |
---|
160 | //print_r( $dirlist ); |
---|
161 | |
---|
162 | $first = 0; |
---|
163 | $last = 9999999999999; |
---|
164 | |
---|
165 | foreach( $dirlist as $dir ) { |
---|
166 | |
---|
167 | //printf("dir = %s\n", $dir ); |
---|
168 | |
---|
169 | if( $dir > $first and $dir <= $start ) |
---|
170 | $first = $dir; |
---|
171 | if( $dir < $last and $dir >= $end ) |
---|
172 | $last = $dir; |
---|
173 | } |
---|
174 | |
---|
175 | //if( $first != 0 and !array_key_exists( $first, $times ) ) |
---|
176 | // $times[] = $first; |
---|
177 | |
---|
178 | foreach( $dirlist as $dir ) { |
---|
179 | |
---|
180 | if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) |
---|
181 | $times[] = $dir; |
---|
182 | } |
---|
183 | |
---|
184 | //if( $last != 9999999999 and !array_key_exists( $last, $times ) ) |
---|
185 | // $times[] = $last; |
---|
186 | |
---|
187 | sort( $times ); |
---|
188 | |
---|
189 | //print_r( $times ); |
---|
190 | |
---|
191 | return $times; |
---|
192 | } |
---|
193 | |
---|
194 | function getIntervalStep( $file ) { |
---|
195 | |
---|
196 | $ret = $this->doCmd( $this->rrdbin .' info '. $file ); |
---|
197 | |
---|
198 | foreach( $ret as $r ) { |
---|
199 | |
---|
200 | $fields = explode( ' = ', $r ); |
---|
201 | |
---|
202 | if( $fields[0] == 'step' ) |
---|
203 | return $fields[1]; |
---|
204 | } |
---|
205 | |
---|
206 | return null; |
---|
207 | } |
---|
208 | |
---|
209 | function makeJobRrd( $clustername, $hostname, $metric, $descr, $start, $end) { |
---|
210 | $this->clustername = $clustername; |
---|
211 | $this->hostname = $hostname; |
---|
212 | |
---|
213 | $myvalues = array(); |
---|
214 | |
---|
215 | $times = $this->getTimePeriods( $start, $end ); |
---|
216 | |
---|
217 | if( count( $times ) > 0 ) { |
---|
218 | |
---|
219 | $time_size = count( $times ); |
---|
220 | $curtime = 1; |
---|
221 | //print_r( $this->metrics ); |
---|
222 | |
---|
223 | $firstold = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname .'/'. $times[0]. '/'.$metric; |
---|
224 | |
---|
225 | if( !file_exists( $firstold ) ) |
---|
226 | return 0; |
---|
227 | |
---|
228 | $hostdir = $this->tempdir .'/'. $hostname; |
---|
229 | $newdir = $hostdir .'/'.$descr; |
---|
230 | $newfile = $newdir .'/'.$metric; |
---|
231 | |
---|
232 | //if( file_exists( $newfile ) ) |
---|
233 | // return 0; |
---|
234 | |
---|
235 | if( !file_exists( $hostdir ) ) |
---|
236 | mkdir( $hostdir ); |
---|
237 | |
---|
238 | if( !file_exists( $newdir ) ) |
---|
239 | mkdir( $newdir ); |
---|
240 | |
---|
241 | //$this->metrics = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname .'/'. $times[0] ); |
---|
242 | $intv = $this->getIntervalStep( '"'.$firstold.'"' ); |
---|
243 | |
---|
244 | foreach( $times as $timep ) { |
---|
245 | |
---|
246 | $r_start = null; |
---|
247 | $r_end = null; |
---|
248 | |
---|
249 | if( $curtime == 1 ) |
---|
250 | $r_start = $start; |
---|
251 | |
---|
252 | if( $curtime == $time_size ) |
---|
253 | $r_end = $end; |
---|
254 | |
---|
255 | $file = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname .'/'. $timep .'/'. $metric; |
---|
256 | |
---|
257 | $r_values = $this->getValues( $file, $r_start, $r_end ); |
---|
258 | //print_r($r_values); |
---|
259 | |
---|
260 | $myvalues = $myvalues + $r_values; |
---|
261 | |
---|
262 | $curtime++; |
---|
263 | } |
---|
264 | //printf( "----myvalues----\n" ); |
---|
265 | //print_r($myvalues); |
---|
266 | //printf( "----myvalues----\n" ); |
---|
267 | |
---|
268 | $heartbeat = intval( 8 * $intv ); |
---|
269 | $ret = $this->doCmd( $this->rrdbin .' create "'.$newfile.'" --step '. $intv .' --start '. $start .' DS:sum:GAUGE:'.$heartbeat.':U:U RRA:AVERAGE:0.5:1:'. count( $myvalues ) ); |
---|
270 | |
---|
271 | $update_args = array(); |
---|
272 | $arglist_nr = 0; |
---|
273 | |
---|
274 | ksort( $myvalues ); |
---|
275 | reset( $myvalues ); |
---|
276 | $myvalues = array_unique( $myvalues ); |
---|
277 | reset( $myvalues ); |
---|
278 | |
---|
279 | foreach( $myvalues as $mytime=>$myvalue ) { |
---|
280 | $myupdateval = ' '.trim($mytime).':'.trim($myvalue); |
---|
281 | |
---|
282 | if( !isset($update_args[$arglist_nr]) ) |
---|
283 | $update_args[$arglist_nr] = ''; |
---|
284 | |
---|
285 | // Max_Args for Linux kernel is normally about 130k |
---|
286 | // |
---|
287 | if( intval( strlen($update_args[$arglist_nr]) + strlen($myupdateval) ) > 100000 ) |
---|
288 | $arglist_nr++; |
---|
289 | |
---|
290 | $update_args[$arglist_nr] .= $myupdateval; |
---|
291 | } |
---|
292 | |
---|
293 | //printf( "grootte args = %s\n", strlen( $update_args ) ); |
---|
294 | |
---|
295 | foreach( $update_args as $update_arg ) |
---|
296 | $ret = $this->doCmd( $this->rrdbin .' update "'. $newfile . '"'.$update_arg ); |
---|
297 | |
---|
298 | printf( "generated %s\n", $newfile ); |
---|
299 | } else |
---|
300 | return 0; |
---|
301 | } |
---|
302 | |
---|
303 | function getValues( $file, $start = null, $end = null ) { |
---|
304 | |
---|
305 | $rrdargs = 'AVERAGE -r 15'; |
---|
306 | |
---|
307 | if( $start ) |
---|
308 | $rrdargs .= ' -s '. $start; |
---|
309 | if( $end ) |
---|
310 | $rrdargs .= ' -e '. $end; |
---|
311 | |
---|
312 | $values = $this->doCmd( $this->rrdbin .' fetch "'.$file.'" '. $rrdargs ); |
---|
313 | |
---|
314 | //print_r( $values ); |
---|
315 | $arvalues = array(); |
---|
316 | |
---|
317 | foreach( $values as $value ) { |
---|
318 | //printf( "value = %s\n", $value ); |
---|
319 | |
---|
320 | $fields = explode( ':', $value ); |
---|
321 | |
---|
322 | if( count( $fields ) == 2 ) { |
---|
323 | |
---|
324 | $timestamp = trim($fields[0]); |
---|
325 | $keepval = 1; |
---|
326 | |
---|
327 | if( $start ) { |
---|
328 | |
---|
329 | if( intval($timestamp) >= intval($start) ) |
---|
330 | $keepval = 1; |
---|
331 | else |
---|
332 | $keepval = 0; |
---|
333 | } else if( $end ) { |
---|
334 | |
---|
335 | if( intval($timestamp) <= intval($end) ) |
---|
336 | $keepval = 1; |
---|
337 | else |
---|
338 | $keepval = 0; |
---|
339 | } |
---|
340 | |
---|
341 | $value = $fields[1]; |
---|
342 | //printf("timestamp = %s, value = %s\n", $timestamp, $value ); |
---|
343 | |
---|
344 | if( $keepval and !isset($arvalues[$timestamp] ) ) |
---|
345 | $arvalues[$timestamp] = $value; |
---|
346 | } |
---|
347 | } |
---|
348 | //printf( "----arvalues----\n" ); |
---|
349 | //print_r( $arvalues); |
---|
350 | //printf( "----arvalues----\n" ); |
---|
351 | |
---|
352 | //ksort( $arvalues ); |
---|
353 | //printf( "----arsortvalues----\n" ); |
---|
354 | //print_r( $arvalues); |
---|
355 | //printf( "----arsortvalues----\n" ); |
---|
356 | |
---|
357 | return $arvalues; |
---|
358 | } |
---|
359 | |
---|
360 | function graph( $descr ) { |
---|
361 | //$command = $this->rrdbin . " graph - --start $start --end $end ". |
---|
362 | "--width $width --height $height $upper_limit $lower_limit ". |
---|
363 | "--title '$title' $vertical_label $extras $background ". $series; |
---|
364 | |
---|
365 | //$graph = $this->doCmd( $command ); |
---|
366 | |
---|
367 | //return $graph; |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | class DataSource { |
---|
372 | |
---|
373 | var $data, $ip, $port; |
---|
374 | |
---|
375 | function DataSource( $ip = '127.0.0.1', $port = 8649 ) { |
---|
376 | $this->ip = $ip; |
---|
377 | $this->port = $port; |
---|
378 | } |
---|
379 | |
---|
380 | function getData() { |
---|
381 | |
---|
382 | $errstr; |
---|
383 | $errno = 0; |
---|
384 | $timeout = 3; |
---|
385 | |
---|
386 | $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout ); |
---|
387 | |
---|
388 | if( !$fp ) { |
---|
389 | echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port ); |
---|
390 | return; |
---|
391 | } |
---|
392 | |
---|
393 | while ( !feof( $fp ) ) { |
---|
394 | |
---|
395 | $data .= fread( $fp, 16384 ); |
---|
396 | } |
---|
397 | |
---|
398 | fclose( $fp ); |
---|
399 | |
---|
400 | return $data; |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | class DataGatherer { |
---|
405 | |
---|
406 | var $xmlhandler, $data, $httpvars; |
---|
407 | |
---|
408 | function DataGatherer() { |
---|
409 | |
---|
410 | global $DATA_SOURCE; |
---|
411 | |
---|
412 | $ds_fields = explode( ':', $DATA_SOURCE ); |
---|
413 | $ds_ip = $ds_fields[0]; |
---|
414 | $ds_port = $ds_fields[1]; |
---|
415 | |
---|
416 | $this->source = new DataSource( $ds_ip, $ds_port ); |
---|
417 | |
---|
418 | $this->parser = xml_parser_create(); |
---|
419 | $this->httpvars = $httpvars; |
---|
420 | $this->xmlhandler = new TorqueXMLHandler(); |
---|
421 | xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) ); |
---|
422 | } |
---|
423 | |
---|
424 | function parseXML() { |
---|
425 | |
---|
426 | $src = &$this->source; |
---|
427 | $this->data = $src->getData(); |
---|
428 | |
---|
429 | if ( !xml_parse( &$this->parser, $this->data ) ) |
---|
430 | $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) ); |
---|
431 | } |
---|
432 | |
---|
433 | function printInfo() { |
---|
434 | $handler = $this->xmlhandler; |
---|
435 | $handler->printInfo(); |
---|
436 | } |
---|
437 | |
---|
438 | function getNodes() { |
---|
439 | $handler = $this->xmlhandler; |
---|
440 | return $handler->getNodes(); |
---|
441 | } |
---|
442 | |
---|
443 | function getCpus() { |
---|
444 | $handler = $this->xmlhandler; |
---|
445 | return $handler->getCpus(); |
---|
446 | } |
---|
447 | |
---|
448 | function getJobs() { |
---|
449 | $handler = $this->xmlhandler; |
---|
450 | return $handler->getJobs(); |
---|
451 | } |
---|
452 | |
---|
453 | function getHeartbeat() { |
---|
454 | $handler = $this->xmlhandler; |
---|
455 | return $handler->getHeartbeat(); |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | class TorqueXMLHandler { |
---|
460 | |
---|
461 | var $clusters, $heartbeat, $nodes, $jobs; |
---|
462 | |
---|
463 | function TorqueXMLHandler() { |
---|
464 | $jobs = array(); |
---|
465 | $clusters = array(); |
---|
466 | $nodes = array(); |
---|
467 | $heartbeat = array(); |
---|
468 | } |
---|
469 | |
---|
470 | function getCpus() { |
---|
471 | |
---|
472 | $cpus = 0; |
---|
473 | |
---|
474 | foreach( $this->jobs as $jobid=>$jobattrs ) { |
---|
475 | |
---|
476 | $nodes = count( $jobattrs[nodes] ); |
---|
477 | $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1; |
---|
478 | $mycpus = $nodes * $ppn; |
---|
479 | |
---|
480 | $cpus = $cpus + $mycpus; |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | function startElement( $parser, $name, $attrs ) { |
---|
485 | |
---|
486 | $jobs = &$this->jobs; |
---|
487 | $nodes = &$this->nodes; |
---|
488 | |
---|
489 | if ( $attrs[TN] ) { |
---|
490 | |
---|
491 | // Ignore dead metrics. Detect and mask failures. |
---|
492 | if ( $attrs[TN] > $attrs[TMAX] * 4 ) |
---|
493 | return; |
---|
494 | } |
---|
495 | |
---|
496 | $jobid = null; |
---|
497 | |
---|
498 | // printf( '%s=%s', $attrs[NAME], $attrs[VAL] ); |
---|
499 | |
---|
500 | if( $name == 'CLUSTER' ) { |
---|
501 | |
---|
502 | $clustername = $attrs[VAL]; |
---|
503 | |
---|
504 | if( !isset( $clusters[$clustername] ) ) |
---|
505 | $clusters[$clustername] = array(); |
---|
506 | |
---|
507 | } else if( $name == 'HOST' ) { |
---|
508 | |
---|
509 | $hostname = $attrs[NAME]; |
---|
510 | $location = $attrs[LOCATION]; |
---|
511 | |
---|
512 | if( !isset( $this->nodes[$hostname] ) ) |
---|
513 | $this->nodes[$hostname] = new NodeImage( $hostname ); |
---|
514 | |
---|
515 | } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'TOGA' ) ) { |
---|
516 | |
---|
517 | if( strstr( $attrs[NAME], 'TOGA-HEARTBEAT' ) ) { |
---|
518 | |
---|
519 | $this->heartbeat['time'] = $attrs[VAL]; |
---|
520 | //printf( "heartbeat %s\n", $heartbeat['time'] ); |
---|
521 | |
---|
522 | } else if( strstr( $attrs[NAME], 'TOGA-JOB' ) ) { |
---|
523 | |
---|
524 | sscanf( $attrs[NAME], 'TOGA-JOB-%d', $jobid ); |
---|
525 | |
---|
526 | //printf( "jobid %s\n", $jobid ); |
---|
527 | |
---|
528 | if( !isset( $jobs[$jobid] ) ) |
---|
529 | $jobs[$jobid] = array(); |
---|
530 | |
---|
531 | $fields = explode( ' ', $attrs[VAL] ); |
---|
532 | |
---|
533 | foreach( $fields as $f ) { |
---|
534 | $togavalues = explode( '=', $f ); |
---|
535 | |
---|
536 | $toganame = $togavalues[0]; |
---|
537 | $togavalue = $togavalues[1]; |
---|
538 | |
---|
539 | //printf( "\t%s\t= %s\n", $toganame, $togavalue ); |
---|
540 | |
---|
541 | if( $toganame == 'nodes' ) { |
---|
542 | |
---|
543 | if( $jobs[$jobid][status] == 'R' ) { |
---|
544 | |
---|
545 | if( !isset( $jobs[$jobid][$toganame] ) ) |
---|
546 | $jobs[$jobid][$toganame] = array(); |
---|
547 | |
---|
548 | $mynodes = explode( ';', $togavalue ); |
---|
549 | |
---|
550 | foreach( $mynodes as $node ) |
---|
551 | |
---|
552 | $jobs[$jobid][$toganame][] = $node; |
---|
553 | |
---|
554 | } else if( $jobs[$jobid][status] == 'Q' ) { |
---|
555 | |
---|
556 | $jobs[$jobid][$toganame] = $togavalue; |
---|
557 | } |
---|
558 | |
---|
559 | } else { |
---|
560 | |
---|
561 | $jobs[$jobid][$toganame] = $togavalue; |
---|
562 | } |
---|
563 | } |
---|
564 | |
---|
565 | if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) { |
---|
566 | |
---|
567 | $nr_nodes = count( $jobs[$jobid][nodes] ); |
---|
568 | |
---|
569 | foreach( $jobs[$jobid][nodes] as $node ) { |
---|
570 | |
---|
571 | $host = $node.'.'.$jobs[$jobid][domain]; |
---|
572 | |
---|
573 | if( !isset( $this->nodes[$host] ) ) |
---|
574 | $my_node = new NodeImage( $host ); |
---|
575 | else |
---|
576 | $my_node = $this->nodes[$host]; |
---|
577 | |
---|
578 | if( !$my_node->hasJob( $jobid ) ) |
---|
579 | |
---|
580 | if( isset( $jobs[$jobid][ppn] ) ) |
---|
581 | $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) ); |
---|
582 | else |
---|
583 | $my_node->addJob( $jobid, 1 ); |
---|
584 | |
---|
585 | $this->nodes[$host] = $my_node; |
---|
586 | } |
---|
587 | } |
---|
588 | } |
---|
589 | } |
---|
590 | $this->jobs = $jobs; |
---|
591 | } |
---|
592 | |
---|
593 | function stopElement( $parser, $name ) { |
---|
594 | } |
---|
595 | |
---|
596 | function printInfo() { |
---|
597 | |
---|
598 | $jobs = &$this->jobs; |
---|
599 | |
---|
600 | printf( "---jobs---\n" ); |
---|
601 | |
---|
602 | foreach( $jobs as $jobid => $job ) { |
---|
603 | |
---|
604 | printf( "job %s\n", $jobid ); |
---|
605 | |
---|
606 | if( isset( $job[nodes] ) ) { |
---|
607 | |
---|
608 | foreach( $job[nodes] as $node ) { |
---|
609 | |
---|
610 | $mynode = $this->nodes[$node]; |
---|
611 | $hostname = $mynode->getHostname(); |
---|
612 | $location = $mynode->getLocation(); |
---|
613 | |
---|
614 | printf( "\t- node %s\tlocation %s\n", $hostname, $location ); |
---|
615 | //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid ); |
---|
616 | } |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | printf( "---nodes---\n" ); |
---|
621 | |
---|
622 | $nodes = &$this->nodes; |
---|
623 | |
---|
624 | foreach( $nodes as $node ) { |
---|
625 | |
---|
626 | $hostname = $node->getHostname(); |
---|
627 | $location = $node->getLocation(); |
---|
628 | $jobs = implode( ' ', $node->getJobs() ); |
---|
629 | printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs ); |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | function getNodes() { |
---|
634 | return $this->nodes; |
---|
635 | } |
---|
636 | |
---|
637 | function getJobs() { |
---|
638 | return $this->jobs; |
---|
639 | } |
---|
640 | |
---|
641 | function getHeartbeat() { |
---|
642 | return $this->heartbeat['time']; |
---|
643 | } |
---|
644 | } |
---|
645 | |
---|
646 | class NodeImage { |
---|
647 | |
---|
648 | var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo; |
---|
649 | |
---|
650 | function NodeImage( $hostname ) { |
---|
651 | |
---|
652 | $this->jobs = array(); |
---|
653 | //$this->image = $image; |
---|
654 | //$this->x = $x; |
---|
655 | //$this->y = $y; |
---|
656 | $this->tasks = 0; |
---|
657 | $this->hostname = $hostname; |
---|
658 | $this->cpus = $this->determineCpus(); |
---|
659 | $this->showinfo = 1; |
---|
660 | } |
---|
661 | |
---|
662 | function addJob( $jobid, $cpus ) { |
---|
663 | $jobs = &$this->jobs; |
---|
664 | |
---|
665 | $jobs[] = $jobid; |
---|
666 | $this->jobs = $jobs; |
---|
667 | |
---|
668 | $this->addTask( $cpus ); |
---|
669 | } |
---|
670 | |
---|
671 | function hasJob( $jobid ) { |
---|
672 | |
---|
673 | $jobfound = 0; |
---|
674 | |
---|
675 | if( count( $this->jobs ) > 0 ) |
---|
676 | foreach( $this->jobs as $job ) |
---|
677 | |
---|
678 | if( $job == $jobid ) |
---|
679 | $jobfound = 1; |
---|
680 | |
---|
681 | return $jobfound; |
---|
682 | } |
---|
683 | |
---|
684 | function addTask( $cpus ) { |
---|
685 | |
---|
686 | $this->tasks = $this->tasks + $cpus; |
---|
687 | } |
---|
688 | |
---|
689 | function setImage( $image ) { |
---|
690 | |
---|
691 | $this->image = $image; |
---|
692 | } |
---|
693 | |
---|
694 | function setCoords( $x, $y ) { |
---|
695 | |
---|
696 | $this->x = $x; |
---|
697 | $this->y = $y; |
---|
698 | } |
---|
699 | |
---|
700 | function colorHex( $color ) { |
---|
701 | |
---|
702 | $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) ); |
---|
703 | |
---|
704 | return $my_color; |
---|
705 | } |
---|
706 | |
---|
707 | function setLoad( $load ) { |
---|
708 | $this->load = $load; |
---|
709 | } |
---|
710 | |
---|
711 | function setHostname( $hostname ) { |
---|
712 | $this->hostname = $hostname; |
---|
713 | } |
---|
714 | |
---|
715 | function getHostname() { |
---|
716 | return $this->hostname; |
---|
717 | } |
---|
718 | |
---|
719 | function getJobs() { |
---|
720 | return $this->jobs; |
---|
721 | } |
---|
722 | |
---|
723 | function setShowinfo( $showinfo ) { |
---|
724 | $this->showinfo = $showinfo; |
---|
725 | } |
---|
726 | |
---|
727 | function draw() { |
---|
728 | |
---|
729 | $this->drawSmall(); |
---|
730 | } |
---|
731 | |
---|
732 | function drawBig() { |
---|
733 | |
---|
734 | } |
---|
735 | |
---|
736 | function drawSmall() { |
---|
737 | |
---|
738 | global $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
739 | global $JOB_NODE_MARKING; |
---|
740 | |
---|
741 | $black_color = imageColorAllocate( $this->image, 0, 0, 0 ); |
---|
742 | $size = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
743 | |
---|
744 | imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color ); |
---|
745 | |
---|
746 | if( $this->showinfo) { |
---|
747 | |
---|
748 | $this->load = $this->determineLoad(); |
---|
749 | |
---|
750 | if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) { |
---|
751 | printf( "aborting\n" ); |
---|
752 | printf( "x %d y %d load %f\n", $this->x, $this->y, $load ); |
---|
753 | return; |
---|
754 | } |
---|
755 | |
---|
756 | |
---|
757 | // Convert Ganglias Hexadecimal load color to a Decimal one |
---|
758 | // |
---|
759 | $load = $this->determineLoad(); |
---|
760 | $usecolor = $this->colorHex( load_color($load) ); |
---|
761 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor ); |
---|
762 | if( count( $this->jobs ) > 0 ) |
---|
763 | imageString( $this->image, 1, $this->x+(($size/2)-2), $this->y+(($size/2)-3), $JOB_NODE_MARKING, $black_color ); |
---|
764 | |
---|
765 | } else { |
---|
766 | |
---|
767 | // White |
---|
768 | $usecolor = imageColorAllocate( $this->image, 255, 255, 255 ); |
---|
769 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor ); |
---|
770 | } |
---|
771 | |
---|
772 | |
---|
773 | } |
---|
774 | |
---|
775 | function determineCpus() { |
---|
776 | |
---|
777 | global $metrics; |
---|
778 | |
---|
779 | $cpus = $metrics[$this->hostname][cpu_num][VAL]; |
---|
780 | if (!$cpus) $cpus=1; |
---|
781 | |
---|
782 | return $cpus; |
---|
783 | } |
---|
784 | |
---|
785 | function determineLoad() { |
---|
786 | |
---|
787 | global $metrics; |
---|
788 | |
---|
789 | $load_one = $metrics[$this->hostname][load_one][VAL]; |
---|
790 | $load = ((float) $load_one)/$this->cpus; |
---|
791 | |
---|
792 | return $load; |
---|
793 | } |
---|
794 | } |
---|
795 | |
---|
796 | class ClusterImage { |
---|
797 | |
---|
798 | var $dataget, $image, $clustername; |
---|
799 | var $filtername, $filters; |
---|
800 | |
---|
801 | function ClusterImage( $clustername ) { |
---|
802 | |
---|
803 | $this->dataget = new DataGatherer(); |
---|
804 | $this->clustername = $clustername; |
---|
805 | $this->filters = array(); |
---|
806 | } |
---|
807 | |
---|
808 | function setFilter( $filtername, $filtervalue ) { |
---|
809 | |
---|
810 | //printf("filter %s = %s\n", $filtername, $filtervalue ); |
---|
811 | //printf( "filter set to %s = %s\n", $filtername, $filtervalue ); |
---|
812 | $this->filters[$filtername] = $filtervalue; |
---|
813 | //print_r($this->filters); |
---|
814 | } |
---|
815 | |
---|
816 | function filterNodes( $jobs, $nodes ) { |
---|
817 | |
---|
818 | $filtered_nodes = array(); |
---|
819 | |
---|
820 | foreach( $nodes as $node ) { |
---|
821 | |
---|
822 | $hostname = $node->getHostname(); |
---|
823 | |
---|
824 | $addhost = 1; |
---|
825 | |
---|
826 | if( count( $this->filters ) > 0 ) { |
---|
827 | |
---|
828 | $mynjobs = $node->getJobs(); |
---|
829 | |
---|
830 | if( count( $mynjobs ) > 0 ) { |
---|
831 | |
---|
832 | foreach( $mynjobs as $myjob ) { |
---|
833 | |
---|
834 | foreach( $this->filters as $filtername => $filtervalue ) { |
---|
835 | |
---|
836 | //printf("filter bla %s = %s\n", $filtername,$filtervalue ); |
---|
837 | |
---|
838 | if( $filtername!=null && $filtername!='' ) { |
---|
839 | |
---|
840 | if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) { |
---|
841 | $addhost = 0; |
---|
842 | //printf("host %s has no job %s\n", $hostname, $filtervalue); |
---|
843 | } else if( $filtername != 'jobid' ) { |
---|
844 | //printf("myjob is %s\n", $myjob ); |
---|
845 | if( $jobs[$myjob][$filtername] != $filtervalue ) { |
---|
846 | //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue); |
---|
847 | $addhost = 0; |
---|
848 | } |
---|
849 | } |
---|
850 | } |
---|
851 | } |
---|
852 | } |
---|
853 | } else |
---|
854 | $addhost = 0; |
---|
855 | } |
---|
856 | |
---|
857 | if( $addhost ) |
---|
858 | $filtered_nodes[] = $hostname; |
---|
859 | } |
---|
860 | |
---|
861 | return $filtered_nodes; |
---|
862 | } |
---|
863 | |
---|
864 | function draw() { |
---|
865 | |
---|
866 | //printf("stopt met uitvoer"); |
---|
867 | //return; |
---|
868 | |
---|
869 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
870 | |
---|
871 | $mydatag = $this->dataget; |
---|
872 | $mydatag->parseXML(); |
---|
873 | |
---|
874 | //$max_width = 250; |
---|
875 | //$node_width = 11; |
---|
876 | |
---|
877 | $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH; |
---|
878 | $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
879 | |
---|
880 | //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH ); |
---|
881 | |
---|
882 | $nodes = $mydatag->getNodes(); |
---|
883 | $nodes_hosts = array_keys( $nodes ); |
---|
884 | |
---|
885 | $nodes_nr = count( $nodes ); |
---|
886 | |
---|
887 | $nodes_size = $nodes_nr*$node_width; |
---|
888 | $node_rows = 0; |
---|
889 | |
---|
890 | if( $nodes_size > $max_width ) { |
---|
891 | $nodes_per_row = ( (int) ($max_width/$node_width) ); |
---|
892 | } else { |
---|
893 | $nodes_per_row = $nodes_size; |
---|
894 | $node_rows = 1; |
---|
895 | } |
---|
896 | |
---|
897 | if( $nodes_per_row < $nodes_nr ) { |
---|
898 | $node_rows = ( (int) ($nodes_nr/$nodes_per_row) ); |
---|
899 | $node_rest = fmod( $nodes_nr, $nodes_per_row ); |
---|
900 | //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest ); |
---|
901 | if( $node_rest > 0 ) { |
---|
902 | $node_rows++; |
---|
903 | //printf( "noderows %d\n", $node_rows ); |
---|
904 | } |
---|
905 | } |
---|
906 | |
---|
907 | //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) ); |
---|
908 | $image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 ); |
---|
909 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
910 | imageFill( $image, 0, 0, $colorwhite ); |
---|
911 | |
---|
912 | $jobs = $mydatag->getJobs(); |
---|
913 | //printf("filtername = %s\n", $filtername ); |
---|
914 | $filtered_nodes = $this->filterNodes( $jobs, $nodes ); |
---|
915 | |
---|
916 | //print_r($filtered_nodes); |
---|
917 | |
---|
918 | for( $n = 0; $n < $node_rows; $n++ ) { |
---|
919 | |
---|
920 | for( $m = 0; $m < $nodes_per_row; $m++ ) { |
---|
921 | |
---|
922 | $x = ($m * $node_width); |
---|
923 | $y = ($n * $node_width); |
---|
924 | |
---|
925 | $cur_node = ($n * $nodes_per_row) + ($m); |
---|
926 | $host = $nodes_hosts[$cur_node]; |
---|
927 | |
---|
928 | |
---|
929 | if( isset( $nodes[$host] ) ) { |
---|
930 | |
---|
931 | $nodes[$host]->setCoords( $x, $y ); |
---|
932 | $nodes[$host]->setImage( $image ); |
---|
933 | |
---|
934 | if( !in_array( $host, $filtered_nodes ) ) |
---|
935 | $nodes[$host]->setShowinfo( 0 ); |
---|
936 | |
---|
937 | $nodes[$host]->draw(); |
---|
938 | } |
---|
939 | } |
---|
940 | } |
---|
941 | |
---|
942 | header( 'Content-type: image/png' ); |
---|
943 | imagePNG( $image ); |
---|
944 | imageDestroy( $image ); |
---|
945 | } |
---|
946 | } |
---|
947 | |
---|
948 | //$my_data = new DataGatherer(); |
---|
949 | //$my_data->parseXML(); |
---|
950 | //$my_data->printInfo(); |
---|
951 | |
---|
952 | //$ic = new ClusterImage( "LISA Cluster" ); |
---|
953 | //$ic->draw(); |
---|
954 | ?> |
---|