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