1 | <?php |
---|
2 | /* |
---|
3 | * |
---|
4 | * This file is part of Jobmonarch |
---|
5 | * |
---|
6 | * Copyright (C) 2006 Ramon Bastiaans |
---|
7 | * |
---|
8 | * Jobmonarch is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * Jobmonarch is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
21 | * |
---|
22 | * SVN $Id: libtoga.php 303 2007-04-16 14:28:06Z bastiaans $ |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | |
---|
27 | class HTTPVariables { |
---|
28 | |
---|
29 | var $clustername, $metricname; |
---|
30 | var $restvars, $httpvars; |
---|
31 | |
---|
32 | function HTTPVariables( $httpvars, $getvars ) { |
---|
33 | |
---|
34 | $this->restvars = array(); |
---|
35 | |
---|
36 | $this->clustername = $httpvars["c"] ? $httpvars["c"] : $getvars["c"]; |
---|
37 | $this->metricname = $httpvars["m"] ? $httpvars["m"] : $getvars["m"]; |
---|
38 | |
---|
39 | foreach( $httpvars as $httpvar => $httpval ) { |
---|
40 | |
---|
41 | if( $httpval ) { |
---|
42 | $this->restvars[$httpvar] = $httpval; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | foreach( $getvars as $getvar => $getval ) { |
---|
47 | |
---|
48 | if( $getval ) { |
---|
49 | $this->restvars[$getvar] = $getval; |
---|
50 | } |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | function getClusterName() { |
---|
55 | return $this->clustername; |
---|
56 | } |
---|
57 | |
---|
58 | function getMetricName() { |
---|
59 | return $this->metricname; |
---|
60 | } |
---|
61 | |
---|
62 | function getHttpVar( $var ) { |
---|
63 | if( isset( $this->restvars[$var] ) ) |
---|
64 | return $this->restvars[$var]; |
---|
65 | else |
---|
66 | return null; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | // Toga's conf |
---|
71 | // |
---|
72 | include_once "./conf.php"; |
---|
73 | include_once "./version.php"; |
---|
74 | |
---|
75 | global $GANGLIA_PATH; |
---|
76 | global $RRDTOOL; |
---|
77 | global $JOB_ARCHIVE_DIR; |
---|
78 | global $JOB_ARCHIVE_DBASE; |
---|
79 | |
---|
80 | $my_dir = getcwd(); |
---|
81 | |
---|
82 | // Load Ganglia's PHP |
---|
83 | chdir( $GANGLIA_PATH ); |
---|
84 | |
---|
85 | include_once "./conf.php"; |
---|
86 | include_once "./functions.php"; |
---|
87 | include_once "./ganglia.php"; |
---|
88 | include_once "./get_context.php"; |
---|
89 | unset( $start ); |
---|
90 | $context = 'cluster'; |
---|
91 | include_once "./get_ganglia.php"; |
---|
92 | |
---|
93 | // Back to our PHP |
---|
94 | chdir( $my_dir ); |
---|
95 | |
---|
96 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET; |
---|
97 | $httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET ); |
---|
98 | |
---|
99 | // Set cluster context so that Ganglia will |
---|
100 | // provide us with the correct metrics array |
---|
101 | // |
---|
102 | global $context, $clustername, $reports; |
---|
103 | |
---|
104 | //$clustername = $httpvars->getClusterName(); |
---|
105 | |
---|
106 | global $default_metric; |
---|
107 | |
---|
108 | // Ganglia's array of host metrics |
---|
109 | // |
---|
110 | global $metrics, $hosts_up; |
---|
111 | |
---|
112 | global $DATETIME_FORMAT; |
---|
113 | |
---|
114 | function makeDate( $time ) { |
---|
115 | global $DATETIME_FORMAT; |
---|
116 | return strftime( $DATETIME_FORMAT, $time ); |
---|
117 | } |
---|
118 | |
---|
119 | class TarchDbase { |
---|
120 | |
---|
121 | var $ip, $dbase, $conn; |
---|
122 | |
---|
123 | function TarchDbase( $ip = null, $dbase = null ) { |
---|
124 | |
---|
125 | global $JOB_ARCHIVE_DBASE; |
---|
126 | |
---|
127 | $db_fields = explode( '/', $JOB_ARCHIVE_DBASE ); |
---|
128 | |
---|
129 | $this->ip = $db_fields[0]; |
---|
130 | $this->dbase = $db_fields[1]; |
---|
131 | $this->conn = null; |
---|
132 | } |
---|
133 | |
---|
134 | function connect() { |
---|
135 | |
---|
136 | if( $this->ip == null ) |
---|
137 | $this->conn = pg_connect( "dbname=".$this->dbase ); |
---|
138 | else |
---|
139 | $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase ); |
---|
140 | } |
---|
141 | |
---|
142 | 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 ) { |
---|
143 | |
---|
144 | global $SEARCH_RESULT_LIMIT; |
---|
145 | |
---|
146 | if( $id ) { |
---|
147 | $select_query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'"; |
---|
148 | $this->resultcount = 1; |
---|
149 | } else { |
---|
150 | $query_args = array(); |
---|
151 | |
---|
152 | if( $queue ) |
---|
153 | $query_args[] = "job_queue ='$queue'"; |
---|
154 | if( $user ) |
---|
155 | $query_args[] = "job_owner ='$user'"; |
---|
156 | if( $name ) |
---|
157 | $query_args[] = "job_name = '$name'"; |
---|
158 | if( $start_from_time ) |
---|
159 | $query_args[] = "job_start_timestamp >= $start_from_time"; |
---|
160 | if( $start_to_time ) |
---|
161 | $query_args[] = "job_start_timestamp <= $start_to_time"; |
---|
162 | if( $end_from_time ) |
---|
163 | $query_args[] = "job_stop_timestamp >= $end_from_time"; |
---|
164 | if( $end_to_time ) |
---|
165 | $query_args[] = "job_stop_timestamp <= $end_to_time"; |
---|
166 | |
---|
167 | $query = "FROM jobs WHERE job_status = 'F' AND "; |
---|
168 | $extra_query_args = ''; |
---|
169 | |
---|
170 | foreach( $query_args as $myquery ) { |
---|
171 | |
---|
172 | if( $extra_query_args == '' ) |
---|
173 | $extra_query_args = $myquery; |
---|
174 | else |
---|
175 | $extra_query_args .= " AND ".$myquery; |
---|
176 | } |
---|
177 | $query .= $extra_query_args; |
---|
178 | |
---|
179 | $count_result_idname = "COUNT(job_id)"; |
---|
180 | $select_result_idname = "job_id"; |
---|
181 | |
---|
182 | $count_query = "SELECT " . $count_result_idname . " " . $query; |
---|
183 | |
---|
184 | $count_result = $this->queryDbase( $count_query ); |
---|
185 | $this->resultcount = (int) $count_result[0][count]; |
---|
186 | |
---|
187 | $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id LIMIT " . $SEARCH_RESULT_LIMIT; |
---|
188 | } |
---|
189 | |
---|
190 | $ids = $this->queryDbase( $select_query ); |
---|
191 | |
---|
192 | $ret = array(); |
---|
193 | |
---|
194 | foreach( $ids as $crow) |
---|
195 | $ret[] = $crow[job_id]; |
---|
196 | |
---|
197 | return $ret; |
---|
198 | } |
---|
199 | |
---|
200 | function getNodesForJob( $jobid ) { |
---|
201 | |
---|
202 | $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" ); |
---|
203 | |
---|
204 | $nodes = array(); |
---|
205 | |
---|
206 | foreach( $result as $result_row ) |
---|
207 | |
---|
208 | $nodes[] = $this->getNodeArray( $result_row[node_id] ); |
---|
209 | |
---|
210 | return $nodes; |
---|
211 | } |
---|
212 | |
---|
213 | function getJobsForNode( $nodeid ) { |
---|
214 | |
---|
215 | $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" ); |
---|
216 | |
---|
217 | $jobs = array(); |
---|
218 | |
---|
219 | foreach( $result as $result_row ) |
---|
220 | |
---|
221 | $jobs[] = $this->getJobArray( $result_row[job_id] ); |
---|
222 | |
---|
223 | return $jobs; |
---|
224 | } |
---|
225 | |
---|
226 | function getJobArray( $id ) { |
---|
227 | $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" ); |
---|
228 | |
---|
229 | return ( $this->makeArray( $result[0] ) ); |
---|
230 | } |
---|
231 | |
---|
232 | function getNodeArray( $id ) { |
---|
233 | |
---|
234 | $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" ); |
---|
235 | |
---|
236 | return ( $this->makeArray( $result[0] ) ); |
---|
237 | } |
---|
238 | |
---|
239 | function makeArray( $result_row ) { |
---|
240 | |
---|
241 | $myar = array(); |
---|
242 | |
---|
243 | foreach( $result_row as $mykey => $myval ) { |
---|
244 | |
---|
245 | $map_key = explode( '_', $mykey ); |
---|
246 | |
---|
247 | $rmap_key = array_reverse( $map_key ); |
---|
248 | array_pop( $rmap_key ); |
---|
249 | $map_key = array_reverse( $rmap_key ); |
---|
250 | |
---|
251 | $newkey = implode( '_', $map_key ); |
---|
252 | |
---|
253 | $myar[$newkey] = $result_row[$mykey]; |
---|
254 | } |
---|
255 | |
---|
256 | return $myar; |
---|
257 | } |
---|
258 | |
---|
259 | function queryDbase( $query ) { |
---|
260 | |
---|
261 | $result_rows = array(); |
---|
262 | |
---|
263 | if( !$this->conn ) |
---|
264 | $this->connect(); |
---|
265 | |
---|
266 | //printf( "query = [%s]\n", $query ); |
---|
267 | $result = pg_query( $this->conn, $query ); |
---|
268 | |
---|
269 | while ($row = pg_fetch_assoc($result)) |
---|
270 | $result_rows[] = $row; |
---|
271 | |
---|
272 | return $result_rows; |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | class TarchRrdGraph { |
---|
277 | var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics; |
---|
278 | |
---|
279 | function TarchRrdGraph( $clustername, $hostname ) { |
---|
280 | |
---|
281 | global $RRDTOOL; |
---|
282 | global $JOB_ARCHIVE_DIR; |
---|
283 | |
---|
284 | $this->rrdbin = $RRDTOOL; |
---|
285 | $this->rrdvalues = array(); |
---|
286 | $this->tarchdir = $JOB_ARCHIVE_DIR; |
---|
287 | $this->clustername = $clustername; |
---|
288 | $this->hostname = $hostname; |
---|
289 | } |
---|
290 | |
---|
291 | function doCmd( $command ) { |
---|
292 | |
---|
293 | printf( "command = %s\n", $command ); |
---|
294 | $pipe = popen( $command . ' 2>&1', 'r' ); |
---|
295 | |
---|
296 | if (!$pipe) { |
---|
297 | print "pipe failed."; |
---|
298 | return ""; |
---|
299 | } |
---|
300 | |
---|
301 | $output = ''; |
---|
302 | while(!feof($pipe)) |
---|
303 | $output .= fread($pipe, 1024); |
---|
304 | |
---|
305 | pclose($pipe); |
---|
306 | |
---|
307 | $output = explode( "\n", $output ); |
---|
308 | //print_r( $output ); |
---|
309 | return $output; |
---|
310 | } |
---|
311 | |
---|
312 | function dirList( $dir ) { |
---|
313 | |
---|
314 | $dirlist = array(); |
---|
315 | |
---|
316 | if ($handle = opendir( $dir )) { |
---|
317 | while (false !== ($file = readdir($handle))) { |
---|
318 | if ($file != "." && $file != "..") { |
---|
319 | $dirlist[] = $file; |
---|
320 | } |
---|
321 | } |
---|
322 | closedir($handle); |
---|
323 | } |
---|
324 | |
---|
325 | return $dirlist; |
---|
326 | } |
---|
327 | |
---|
328 | function getTimePeriods( $start, $end ) { |
---|
329 | |
---|
330 | //printf("start = %s end = %s\n", $start, $end ); |
---|
331 | $times = array(); |
---|
332 | $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname ); |
---|
333 | |
---|
334 | //print_r( $dirlist ); |
---|
335 | |
---|
336 | $first = 0; |
---|
337 | $last = 9999999999999; |
---|
338 | |
---|
339 | foreach( $dirlist as $dir ) { |
---|
340 | |
---|
341 | if( $dir > $first and $dir <= $start ) |
---|
342 | $first = $dir; |
---|
343 | if( $dir < $last and $dir >= $end ) |
---|
344 | $last = $dir; |
---|
345 | } |
---|
346 | |
---|
347 | //printf( "first = %s last = %s\n", $first, $last ); |
---|
348 | |
---|
349 | foreach( $dirlist as $dir ) { |
---|
350 | |
---|
351 | //printf( "dir %s ", $dir ); |
---|
352 | |
---|
353 | if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) { |
---|
354 | |
---|
355 | $times[] = $dir; |
---|
356 | //printf("newtime %s ", $dir ); |
---|
357 | |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | //print_r( $times ); |
---|
362 | |
---|
363 | sort( $times ); |
---|
364 | |
---|
365 | //print_r( $times ); |
---|
366 | |
---|
367 | return $times; |
---|
368 | } |
---|
369 | |
---|
370 | function getRrdDirs( $start, $stop ) { |
---|
371 | |
---|
372 | //printf( "tarchdir = %s\n", $this->tarchdir ); |
---|
373 | $timess = $this->getTimePeriods( $start, $stop ); |
---|
374 | //print_r( $timess ); |
---|
375 | |
---|
376 | $rrd_files = array(); |
---|
377 | |
---|
378 | foreach( $timess as $time ) { |
---|
379 | |
---|
380 | $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time; |
---|
381 | } |
---|
382 | |
---|
383 | return $rrd_files; |
---|
384 | } |
---|
385 | |
---|
386 | function getRrdFiles( $metric, $start, $stop ) { |
---|
387 | |
---|
388 | $times = $this->getTimePeriods( $start, $stop ); |
---|
389 | |
---|
390 | $rrd_files = array(); |
---|
391 | |
---|
392 | foreach( $times as $time ) { |
---|
393 | |
---|
394 | $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd'; |
---|
395 | } |
---|
396 | |
---|
397 | return $rrd_files; |
---|
398 | } |
---|
399 | |
---|
400 | function graph( $descr ) { |
---|
401 | // 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 |
---|
402 | // 380x461 |
---|
403 | // monitor2:/data/toga/rrds/LISA Cluster/gb-r15n11.irc.sara.nl# |
---|
404 | //$command = $this->rrdbin . " graph - --start $start --end $end ". |
---|
405 | "--width $width --height $height $upper_limit $lower_limit ". |
---|
406 | "--title '$title' $vertical_label $extras $background ". $series; |
---|
407 | |
---|
408 | //$graph = $this->doCmd( $command ); |
---|
409 | |
---|
410 | //return $graph; |
---|
411 | return 0; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | class DataSource { |
---|
416 | |
---|
417 | var $data, $ip, $port; |
---|
418 | |
---|
419 | function DataSource( $ip = '127.0.0.1', $port = 8649 ) { |
---|
420 | $this->ip = $ip; |
---|
421 | $this->port = $port; |
---|
422 | } |
---|
423 | |
---|
424 | function getData() { |
---|
425 | |
---|
426 | $errstr; |
---|
427 | $errno = 0; |
---|
428 | $timeout = 3; |
---|
429 | |
---|
430 | $fp = fsockopen( $this->ip, $this->port, &$errno, &$errstr, $timeout ); |
---|
431 | |
---|
432 | if( !$fp ) { |
---|
433 | echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port ); |
---|
434 | return; |
---|
435 | } |
---|
436 | |
---|
437 | stream_set_timeout( $fp, 30 ); |
---|
438 | |
---|
439 | while ( !feof( $fp ) ) { |
---|
440 | |
---|
441 | $data .= fread( $fp, 16384 ); |
---|
442 | } |
---|
443 | |
---|
444 | fclose( $fp ); |
---|
445 | |
---|
446 | return $data; |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|
450 | class DataGatherer { |
---|
451 | |
---|
452 | var $xmlhandler, $data, $httpvars; |
---|
453 | |
---|
454 | function DataGatherer( $cluster ) { |
---|
455 | |
---|
456 | global $DATA_SOURCE; |
---|
457 | |
---|
458 | //printf("dg cluster = %s\n", $cluster ); |
---|
459 | $ds_fields = explode( ':', $DATA_SOURCE ); |
---|
460 | $ds_ip = $ds_fields[0]; |
---|
461 | $ds_port = $ds_fields[1]; |
---|
462 | |
---|
463 | $this->source = new DataSource( $ds_ip, $ds_port ); |
---|
464 | |
---|
465 | $this->parser = xml_parser_create(); |
---|
466 | $this->httpvars = $httpvars; |
---|
467 | $this->xmlhandler = new TorqueXMLHandler( $cluster ); |
---|
468 | xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) ); |
---|
469 | } |
---|
470 | |
---|
471 | function parseXML() { |
---|
472 | |
---|
473 | $src = &$this->source; |
---|
474 | $this->data = $src->getData(); |
---|
475 | |
---|
476 | if ( !xml_parse( &$this->parser, $this->data ) ) |
---|
477 | $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( &$this->parser ) ), xml_get_current_line_number( &$this->parser ) ); |
---|
478 | } |
---|
479 | |
---|
480 | function printInfo() { |
---|
481 | $handler = $this->xmlhandler; |
---|
482 | $handler->printInfo(); |
---|
483 | } |
---|
484 | |
---|
485 | function getNodes() { |
---|
486 | $handler = $this->xmlhandler; |
---|
487 | return $handler->getNodes(); |
---|
488 | } |
---|
489 | |
---|
490 | function getNode( $node ) { |
---|
491 | $handler = $this->xmlhandler; |
---|
492 | return $handler->getNode( $node ); |
---|
493 | } |
---|
494 | |
---|
495 | function getCpus() { |
---|
496 | $handler = $this->xmlhandler; |
---|
497 | return $handler->getCpus(); |
---|
498 | } |
---|
499 | |
---|
500 | function getJobs() { |
---|
501 | $handler = $this->xmlhandler; |
---|
502 | return $handler->getJobs(); |
---|
503 | } |
---|
504 | |
---|
505 | function getJob( $job ) { |
---|
506 | $handler = $this->xmlhandler; |
---|
507 | return $handler->getJob( $job ); |
---|
508 | } |
---|
509 | |
---|
510 | function getHeartbeat() { |
---|
511 | $handler = $this->xmlhandler; |
---|
512 | return $handler->getHeartbeat(); |
---|
513 | } |
---|
514 | |
---|
515 | function isJobmonRunning() { |
---|
516 | $handler = $this->xmlhandler; |
---|
517 | return $handler->isJobmonRunning(); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | class TorqueXMLHandler { |
---|
522 | |
---|
523 | var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster; |
---|
524 | |
---|
525 | function TorqueXMLHandler( $clustername ) { |
---|
526 | $jobs = array(); |
---|
527 | $clusters = array(); |
---|
528 | $this->nodes = array(); |
---|
529 | $heartbeat = array(); |
---|
530 | $this->clustername = $clustername; |
---|
531 | //printf(" cluster set to %s \n", $this->clustername ); |
---|
532 | } |
---|
533 | |
---|
534 | function getCpus() { |
---|
535 | |
---|
536 | $cpus = 0; |
---|
537 | |
---|
538 | if( isset( $this->jobs ) && count( $this->jobs ) > 0 ) { |
---|
539 | |
---|
540 | foreach( $this->jobs as $jobid=>$jobattrs ) { |
---|
541 | |
---|
542 | $nodes = count( $jobattrs[nodes] ); |
---|
543 | $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1; |
---|
544 | $mycpus = $nodes * $ppn; |
---|
545 | |
---|
546 | $cpus = $cpus + $mycpus; |
---|
547 | } |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | function isJobmonRunning() { |
---|
552 | |
---|
553 | if (isset( $this->heartbeat['time'] )) |
---|
554 | return 1; |
---|
555 | else |
---|
556 | return 0; |
---|
557 | } |
---|
558 | |
---|
559 | function startElement( $parser, $name, $attrs ) { |
---|
560 | |
---|
561 | $jobs = $this->jobs; |
---|
562 | $nodes = $this->nodes; |
---|
563 | |
---|
564 | if ( $attrs[TN] ) { |
---|
565 | |
---|
566 | // Ignore dead metrics. Detect and mask failures. |
---|
567 | if ( $attrs[TN] > $attrs[TMAX] * 4 ) |
---|
568 | return; |
---|
569 | } |
---|
570 | |
---|
571 | $jobid = null; |
---|
572 | |
---|
573 | //printf( '%s=%s', $attrs[NAME], $attrs[VAL] ); |
---|
574 | |
---|
575 | //printf( "clustername = %s proc_cluster = %s\n", $this->clustername, $this->proc_cluster ); |
---|
576 | |
---|
577 | if( $name == 'CLUSTER' ) { |
---|
578 | |
---|
579 | $this->proc_cluster = $attrs[NAME]; |
---|
580 | //printf( "Found cluster %s\n", $attrs[NAME] ); |
---|
581 | //print_r( $attrs ); |
---|
582 | |
---|
583 | //if( !isset( $clusters[$clustername] ) ) |
---|
584 | // $clusters[$clustername] = array(); |
---|
585 | |
---|
586 | } else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) { |
---|
587 | |
---|
588 | $hostname = $attrs[NAME]; |
---|
589 | $location = $attrs[LOCATION]; |
---|
590 | //printf( "Found node %s\n", $hostname ); |
---|
591 | |
---|
592 | if( !isset( $nodes[$hostname] ) ) |
---|
593 | $nodes[$hostname] = new NodeImage( $hostname ); |
---|
594 | |
---|
595 | } else if( $name == 'METRIC' and strstr( $attrs[NAME], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) { |
---|
596 | |
---|
597 | if( strstr( $attrs[NAME], 'MONARCH-HEARTBEAT' ) ) { |
---|
598 | |
---|
599 | $this->heartbeat['time'] = $attrs[VAL]; |
---|
600 | //printf( "heartbeat %s\n", $heartbeat['time'] ); |
---|
601 | |
---|
602 | } else if( strstr( $attrs[NAME], 'MONARCH-JOB' ) ) { |
---|
603 | |
---|
604 | sscanf( $attrs[NAME], 'MONARCH-JOB-%d-%d', $jobid, $monincr ); |
---|
605 | |
---|
606 | //printf( "jobid %s\n", $jobid ); |
---|
607 | |
---|
608 | if( !isset( $jobs[$jobid] ) ) |
---|
609 | $jobs[$jobid] = array(); |
---|
610 | |
---|
611 | $fields = explode( ' ', $attrs[VAL] ); |
---|
612 | |
---|
613 | foreach( $fields as $f ) { |
---|
614 | $togavalues = explode( '=', $f ); |
---|
615 | |
---|
616 | $toganame = $togavalues[0]; |
---|
617 | $togavalue = $togavalues[1]; |
---|
618 | |
---|
619 | //printf( "\t%s\t= %s\n", $toganame, $togavalue ); |
---|
620 | |
---|
621 | if( $toganame == 'nodes' ) { |
---|
622 | |
---|
623 | if( $jobs[$jobid][status] == 'R' ) { |
---|
624 | |
---|
625 | if( !isset( $jobs[$jobid][$toganame] ) ) |
---|
626 | $jobs[$jobid][$toganame] = array(); |
---|
627 | |
---|
628 | $mynodes = explode( ';', $togavalue ); |
---|
629 | |
---|
630 | //print_r($mynodes); |
---|
631 | |
---|
632 | foreach( $mynodes as $node ) { |
---|
633 | |
---|
634 | if( !in_array( $node, $jobs[$jobid][$toganame] ) ) { |
---|
635 | $jobs[$jobid][$toganame][] = $node; |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | } else if( $jobs[$jobid][status] == 'Q' ) { |
---|
640 | |
---|
641 | $jobs[$jobid][$toganame] = $togavalue; |
---|
642 | } |
---|
643 | |
---|
644 | } else { |
---|
645 | |
---|
646 | $jobs[$jobid][$toganame] = $togavalue; |
---|
647 | } |
---|
648 | } |
---|
649 | |
---|
650 | if( isset( $jobs[$jobid][domain] ) and isset( $jobs[$jobid][nodes] ) ) { |
---|
651 | |
---|
652 | $nr_nodes = count( $jobs[$jobid][nodes] ); |
---|
653 | |
---|
654 | if( $jobs[$jobid][status] == 'R' ) { |
---|
655 | |
---|
656 | foreach( $jobs[$jobid][nodes] as $node ) { |
---|
657 | |
---|
658 | $domain = $jobs[$jobid][domain]; |
---|
659 | $domain_len = 0 - strlen( $domain ); |
---|
660 | |
---|
661 | if( substr( $node, $domain_len ) != $domain ) { |
---|
662 | $host = $node. '.'.$domain; |
---|
663 | } else { |
---|
664 | $host = $node; |
---|
665 | } |
---|
666 | |
---|
667 | //$host = $node.'.'.$jobs[$jobid][domain]; |
---|
668 | |
---|
669 | if( !isset( $nodes[$host] ) ) |
---|
670 | $my_node = new NodeImage( $host ); |
---|
671 | else |
---|
672 | $my_node = $nodes[$host]; |
---|
673 | |
---|
674 | if( !$my_node->hasJob( $jobid ) ) |
---|
675 | |
---|
676 | if( isset( $jobs[$jobid][ppn] ) ) |
---|
677 | $my_node->addJob( $jobid, ((int) $jobs[$jobid][ppn]) ); |
---|
678 | else |
---|
679 | $my_node->addJob( $jobid, 1 ); |
---|
680 | |
---|
681 | $nodes[$host] = $my_node; |
---|
682 | } |
---|
683 | } |
---|
684 | } |
---|
685 | } |
---|
686 | } |
---|
687 | $this->jobs = $jobs; |
---|
688 | //print_r( $nodes ); |
---|
689 | $this->nodes = $nodes; |
---|
690 | //print_r( $this->nodes ); |
---|
691 | } |
---|
692 | |
---|
693 | function stopElement( $parser, $name ) { |
---|
694 | } |
---|
695 | |
---|
696 | function printInfo() { |
---|
697 | |
---|
698 | $jobs = &$this->jobs; |
---|
699 | |
---|
700 | printf( "---jobs---\n" ); |
---|
701 | |
---|
702 | foreach( $jobs as $jobid => $job ) { |
---|
703 | |
---|
704 | printf( "job %s\n", $jobid ); |
---|
705 | |
---|
706 | if( isset( $job[nodes] ) ) { |
---|
707 | |
---|
708 | foreach( $job[nodes] as $node ) { |
---|
709 | |
---|
710 | $mynode = $this->nodes[$node]; |
---|
711 | $hostname = $mynode->getHostname(); |
---|
712 | $location = $mynode->getLocation(); |
---|
713 | |
---|
714 | printf( "\t- node %s\tlocation %s\n", $hostname, $location ); |
---|
715 | //$this->nodes[$hostname]->setLocation( "hier draait job ".$jobid ); |
---|
716 | } |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | printf( "---nodes---\n" ); |
---|
721 | |
---|
722 | $nodes = &$this->nodes; |
---|
723 | |
---|
724 | foreach( $nodes as $node ) { |
---|
725 | |
---|
726 | $hostname = $node->getHostname(); |
---|
727 | $location = $node->getLocation(); |
---|
728 | $jobs = implode( ' ', $node->getJobs() ); |
---|
729 | printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs ); |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | function getNodes() { |
---|
734 | //print_r( $this->nodes ); |
---|
735 | return $this->nodes; |
---|
736 | } |
---|
737 | |
---|
738 | function getNode( $node ) { |
---|
739 | |
---|
740 | $nodes = &$this->nodes; |
---|
741 | if( isset( $nodes[$node] ) ) |
---|
742 | return $nodes[$node]; |
---|
743 | else |
---|
744 | return NULL; |
---|
745 | } |
---|
746 | |
---|
747 | function getJobs() { |
---|
748 | return $this->jobs; |
---|
749 | } |
---|
750 | |
---|
751 | function getJob( $job ) { |
---|
752 | |
---|
753 | $jobs = &$this->jobs; |
---|
754 | if( isset( $jobs[$job] ) ) |
---|
755 | return $jobs[$job]; |
---|
756 | else |
---|
757 | return NULL; |
---|
758 | } |
---|
759 | |
---|
760 | function getHeartbeat() { |
---|
761 | return $this->heartbeat['time']; |
---|
762 | } |
---|
763 | } |
---|
764 | |
---|
765 | class NodeImage { |
---|
766 | |
---|
767 | var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo; |
---|
768 | |
---|
769 | function NodeImage( $hostname ) { |
---|
770 | |
---|
771 | $this->jobs = array(); |
---|
772 | //$this->image = $image; |
---|
773 | //$this->x = $x; |
---|
774 | //$this->y = $y; |
---|
775 | $this->tasks = 0; |
---|
776 | $this->hostname = $hostname; |
---|
777 | $this->cpus = $this->determineCpus(); |
---|
778 | $this->showinfo = 1; |
---|
779 | } |
---|
780 | |
---|
781 | function addJob( $jobid, $cpus ) { |
---|
782 | $jobs = &$this->jobs; |
---|
783 | |
---|
784 | $jobs[] = $jobid; |
---|
785 | $this->jobs = $jobs; |
---|
786 | |
---|
787 | $this->addTask( $cpus ); |
---|
788 | } |
---|
789 | |
---|
790 | function hasJob( $jobid ) { |
---|
791 | |
---|
792 | $jobfound = 0; |
---|
793 | |
---|
794 | if( count( $this->jobs ) > 0 ) |
---|
795 | foreach( $this->jobs as $job ) |
---|
796 | |
---|
797 | if( $job == $jobid ) |
---|
798 | $jobfound = 1; |
---|
799 | |
---|
800 | return $jobfound; |
---|
801 | } |
---|
802 | |
---|
803 | function addTask( $cpus ) { |
---|
804 | |
---|
805 | $this->tasks = $this->tasks + $cpus; |
---|
806 | } |
---|
807 | |
---|
808 | function setImage( $image ) { |
---|
809 | |
---|
810 | $this->image = $image; |
---|
811 | } |
---|
812 | |
---|
813 | function setCoords( $x, $y ) { |
---|
814 | |
---|
815 | $this->x = $x; |
---|
816 | $this->y = $y; |
---|
817 | } |
---|
818 | |
---|
819 | function colorHex( $color ) { |
---|
820 | |
---|
821 | $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) ); |
---|
822 | |
---|
823 | return $my_color; |
---|
824 | } |
---|
825 | |
---|
826 | function setLoad( $load ) { |
---|
827 | $this->load = $load; |
---|
828 | } |
---|
829 | |
---|
830 | function setHostname( $hostname ) { |
---|
831 | $this->hostname = $hostname; |
---|
832 | } |
---|
833 | |
---|
834 | function getHostname() { |
---|
835 | return $this->hostname; |
---|
836 | } |
---|
837 | |
---|
838 | function getJobs() { |
---|
839 | return $this->jobs; |
---|
840 | } |
---|
841 | |
---|
842 | function setShowinfo( $showinfo ) { |
---|
843 | $this->showinfo = $showinfo; |
---|
844 | } |
---|
845 | |
---|
846 | function draw() { |
---|
847 | |
---|
848 | $this->drawSmall(); |
---|
849 | } |
---|
850 | |
---|
851 | function drawBig() { |
---|
852 | |
---|
853 | } |
---|
854 | |
---|
855 | function drawSmall() { |
---|
856 | |
---|
857 | global $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
858 | global $JOB_NODE_MARKING; |
---|
859 | |
---|
860 | $black_color = imageColorAllocate( $this->image, 0, 0, 0 ); |
---|
861 | $size = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
862 | |
---|
863 | imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color ); |
---|
864 | |
---|
865 | if( $this->showinfo) { |
---|
866 | |
---|
867 | $this->load = $this->determineLoad(); |
---|
868 | |
---|
869 | if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) { |
---|
870 | printf( "aborting\n" ); |
---|
871 | printf( "x %d y %d load %f\n", $this->x, $this->y, $load ); |
---|
872 | return; |
---|
873 | } |
---|
874 | |
---|
875 | |
---|
876 | // Convert Ganglias Hexadecimal load color to a Decimal one |
---|
877 | // |
---|
878 | $load = $this->determineLoad(); |
---|
879 | $usecolor = $this->colorHex( load_color($load) ); |
---|
880 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor ); |
---|
881 | if( count( $this->jobs ) > 0 ) |
---|
882 | imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color ); |
---|
883 | |
---|
884 | } else { |
---|
885 | |
---|
886 | // White |
---|
887 | $usecolor = imageColorAllocate( $this->image, 255, 255, 255 ); |
---|
888 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor ); |
---|
889 | } |
---|
890 | |
---|
891 | |
---|
892 | } |
---|
893 | |
---|
894 | function determineCpus() { |
---|
895 | |
---|
896 | global $metrics; |
---|
897 | |
---|
898 | $cpus = $metrics[$this->hostname][cpu_num][VAL]; |
---|
899 | if (!$cpus) $cpus=1; |
---|
900 | |
---|
901 | return $cpus; |
---|
902 | } |
---|
903 | |
---|
904 | function determineLoad() { |
---|
905 | |
---|
906 | global $metrics; |
---|
907 | |
---|
908 | $load_one = $metrics[$this->hostname][load_one][VAL]; |
---|
909 | $load = ((float) $load_one)/$this->cpus; |
---|
910 | |
---|
911 | return $load; |
---|
912 | } |
---|
913 | } |
---|
914 | |
---|
915 | class ClusterImage { |
---|
916 | |
---|
917 | var $dataget, $image, $clustername; |
---|
918 | var $filtername, $filters; |
---|
919 | |
---|
920 | function ClusterImage( $clustername ) { |
---|
921 | |
---|
922 | //printf( "image cluster = %s\n", $clustername ); |
---|
923 | $this->dataget = new DataGatherer( $clustername ); |
---|
924 | $this->clustername = $clustername; |
---|
925 | $this->filters = array(); |
---|
926 | } |
---|
927 | |
---|
928 | function setFilter( $filtername, $filtervalue ) { |
---|
929 | |
---|
930 | //printf("filter %s = %s\n", $filtername, $filtervalue ); |
---|
931 | //printf( "filter set to %s = %s\n", $filtername, $filtervalue ); |
---|
932 | $this->filters[$filtername] = $filtervalue; |
---|
933 | //print_r($this->filters); |
---|
934 | } |
---|
935 | |
---|
936 | function filterNodes( $jobs, $nodes ) { |
---|
937 | |
---|
938 | $filtered_nodes = array(); |
---|
939 | |
---|
940 | foreach( $nodes as $node ) { |
---|
941 | |
---|
942 | $hostname = $node->getHostname(); |
---|
943 | |
---|
944 | $addhost = 1; |
---|
945 | |
---|
946 | if( count( $this->filters ) > 0 ) { |
---|
947 | |
---|
948 | $mynjobs = $node->getJobs(); |
---|
949 | |
---|
950 | if( count( $mynjobs ) > 0 ) { |
---|
951 | |
---|
952 | foreach( $mynjobs as $myjob ) { |
---|
953 | |
---|
954 | foreach( $this->filters as $filtername => $filtervalue ) { |
---|
955 | |
---|
956 | //printf("filter bla %s = %s\n", $filtername,$filtervalue ); |
---|
957 | |
---|
958 | if( $filtername!=null && $filtername!='' ) { |
---|
959 | |
---|
960 | if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) { |
---|
961 | $addhost = 0; |
---|
962 | //printf("host %s has no job %s\n", $hostname, $filtervalue); |
---|
963 | } else if( $filtername != 'jobid' ) { |
---|
964 | //printf("myjob is %s\n", $myjob ); |
---|
965 | if( $jobs[$myjob][$filtername] != $filtervalue ) { |
---|
966 | //printf("host %s has no job with %s=%s\n", $hostname, $filtername, $filtervalue); |
---|
967 | $addhost = 0; |
---|
968 | } |
---|
969 | } |
---|
970 | } |
---|
971 | } |
---|
972 | } |
---|
973 | } else |
---|
974 | $addhost = 0; |
---|
975 | } |
---|
976 | |
---|
977 | if( $addhost ) |
---|
978 | $filtered_nodes[] = $hostname; |
---|
979 | } |
---|
980 | |
---|
981 | return $filtered_nodes; |
---|
982 | } |
---|
983 | |
---|
984 | function draw() { |
---|
985 | |
---|
986 | //printf("stopt met uitvoer"); |
---|
987 | //return; |
---|
988 | |
---|
989 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
990 | |
---|
991 | $mydatag = $this->dataget; |
---|
992 | $mydatag->parseXML(); |
---|
993 | |
---|
994 | //$max_width = 250; |
---|
995 | //$node_width = 11; |
---|
996 | |
---|
997 | $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH; |
---|
998 | $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
999 | |
---|
1000 | //printf( "cmaxw %s nmaxw %s", $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH ); |
---|
1001 | |
---|
1002 | $nodes = $mydatag->getNodes(); |
---|
1003 | $nodes_hosts = array_keys( $nodes ); |
---|
1004 | |
---|
1005 | $nodes_nr = count( $nodes ); |
---|
1006 | |
---|
1007 | $nodes_size = $nodes_nr*$node_width; |
---|
1008 | $node_rows = 0; |
---|
1009 | |
---|
1010 | if( $nodes_size > $max_width ) { |
---|
1011 | $nodes_per_row = ( (int) ($max_width/$node_width) ); |
---|
1012 | } else { |
---|
1013 | $nodes_per_row = $nodes_size; |
---|
1014 | $node_rows = 1; |
---|
1015 | } |
---|
1016 | |
---|
1017 | if( $nodes_per_row < $nodes_nr ) { |
---|
1018 | $node_rows = ( (int) ($nodes_nr/$nodes_per_row) ); |
---|
1019 | $node_rest = fmod( $nodes_nr, $nodes_per_row ); |
---|
1020 | //printf( "nodesnr %d noderest %f\n", $nodes_nr, $node_rest ); |
---|
1021 | if( $node_rest > 0 ) { |
---|
1022 | $node_rows++; |
---|
1023 | //printf( "noderows %d\n", $node_rows ); |
---|
1024 | } |
---|
1025 | } |
---|
1026 | |
---|
1027 | //printf( "imagecreate: %dx%d", ($nodes_per_row*$node_width), ($node_rows*$node_width) ); |
---|
1028 | //$image = imageCreateTrueColor( ($nodes_per_row*$node_width)+1, ($node_rows*$node_width)+1 ); |
---|
1029 | $image = imageCreateTrueColor( $max_width, ($node_rows*$node_width)+1 ); |
---|
1030 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
1031 | imageFill( $image, 0, 0, $colorwhite ); |
---|
1032 | |
---|
1033 | $jobs = $mydatag->getJobs(); |
---|
1034 | //printf("filtername = %s\n", $filtername ); |
---|
1035 | $filtered_nodes = $this->filterNodes( $jobs, $nodes ); |
---|
1036 | |
---|
1037 | //print_r($filtered_nodes); |
---|
1038 | |
---|
1039 | for( $n = 0; $n < $node_rows; $n++ ) { |
---|
1040 | |
---|
1041 | for( $m = 0; $m < $nodes_per_row; $m++ ) { |
---|
1042 | |
---|
1043 | $x = ($m * $node_width); |
---|
1044 | $y = ($n * $node_width); |
---|
1045 | |
---|
1046 | $cur_node = ($n * $nodes_per_row) + ($m); |
---|
1047 | $host = $nodes_hosts[$cur_node]; |
---|
1048 | |
---|
1049 | |
---|
1050 | if( isset( $nodes[$host] ) ) { |
---|
1051 | |
---|
1052 | $nodes[$host]->setCoords( $x, $y ); |
---|
1053 | $nodes[$host]->setImage( $image ); |
---|
1054 | |
---|
1055 | if( !in_array( $host, $filtered_nodes ) ) |
---|
1056 | $nodes[$host]->setShowinfo( 0 ); |
---|
1057 | |
---|
1058 | $nodes[$host]->draw(); |
---|
1059 | } |
---|
1060 | } |
---|
1061 | } |
---|
1062 | |
---|
1063 | header( 'Content-type: image/png' ); |
---|
1064 | imagePNG( $image ); |
---|
1065 | imageDestroy( $image ); |
---|
1066 | } |
---|
1067 | } |
---|
1068 | |
---|
1069 | class EmptyImage { |
---|
1070 | |
---|
1071 | function draw() { |
---|
1072 | $image = imageCreateTrueColor( 1, 1 ); |
---|
1073 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
1074 | imageFill( $image, 0, 0, $colorwhite ); |
---|
1075 | |
---|
1076 | header( 'Content-type: image/png' ); |
---|
1077 | imagePNG( $image ); |
---|
1078 | imageDestroy( $image ); |
---|
1079 | } |
---|
1080 | } |
---|
1081 | |
---|
1082 | class HostImage { |
---|
1083 | |
---|
1084 | var $data_gather, $cluster, $host, $node, $image; |
---|
1085 | var $headerstrlen; |
---|
1086 | |
---|
1087 | function HostImage( $data_gather, $cluster, $host ) { |
---|
1088 | |
---|
1089 | $this->data_gather = $data_gather; |
---|
1090 | $this->cluster = $cluster; |
---|
1091 | $this->host = $host; |
---|
1092 | $this->y_offset = 0; |
---|
1093 | $this->font = 2; |
---|
1094 | $this->fontspaceing = 2; |
---|
1095 | $this->headerstrlen = array(); |
---|
1096 | |
---|
1097 | $this->fontheight = ImageFontHeight( $this->font ); |
---|
1098 | $this->fontwidth = ImageFontWidth( $this->font ); |
---|
1099 | |
---|
1100 | $dg = &$this->data_gather; |
---|
1101 | $this->node = &$dg->getNode( $this->host ); |
---|
1102 | $n = &$this->node; |
---|
1103 | $this->njobs = $n->getJobs(); |
---|
1104 | } |
---|
1105 | |
---|
1106 | function drawJobs() { |
---|
1107 | |
---|
1108 | $dg = &$this->data_gather; |
---|
1109 | $colorblack = imageColorAllocate( $this->image, 0, 0, 0 ); |
---|
1110 | |
---|
1111 | for( $n = 0; $n < count( $this->njobs ); $n++ ) { |
---|
1112 | |
---|
1113 | $jobid = $this->njobs[$n]; |
---|
1114 | $jobinfo = $dg->getJob( $jobid ); |
---|
1115 | |
---|
1116 | $xoffset = 5; |
---|
1117 | imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack ); |
---|
1118 | |
---|
1119 | foreach( $this->headerstrlen as $headername => $headerlen ) { |
---|
1120 | |
---|
1121 | if( $headername == 'nodes' ) { |
---|
1122 | $attrval = strval( count( $jobinfo[nodes] ) ); |
---|
1123 | } else if( $headername == 'cpus' ) { |
---|
1124 | |
---|
1125 | if( !isset( $jobinfo[ppn] ) ) |
---|
1126 | $jobinfo[ppn] = 1; |
---|
1127 | |
---|
1128 | $attrval = strval( count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ) ); |
---|
1129 | |
---|
1130 | } else if( $headername == 'runningtime' ) { |
---|
1131 | $attrval = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) ); |
---|
1132 | } else { |
---|
1133 | $attrval = strval( $jobinfo[$headername] ); |
---|
1134 | } |
---|
1135 | |
---|
1136 | imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack ); |
---|
1137 | |
---|
1138 | $xoffset = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) ); |
---|
1139 | |
---|
1140 | } |
---|
1141 | |
---|
1142 | $this->newLineOffset(); |
---|
1143 | } |
---|
1144 | } |
---|
1145 | |
---|
1146 | function drawHeader() { |
---|
1147 | |
---|
1148 | $dg = &$this->data_gather; |
---|
1149 | |
---|
1150 | for( $n = 0; $n < count( $this->njobs ); $n++ ) { |
---|
1151 | |
---|
1152 | $jobid = $this->njobs[$n]; |
---|
1153 | $jobinfo = $dg->getJob( $jobid ); |
---|
1154 | |
---|
1155 | if( !isset( $this->headerstrlen[id] ) ) |
---|
1156 | $this->headerstrlen[id] = strlen( strval( $jobid ) ); |
---|
1157 | else |
---|
1158 | if( strlen( strval( $jobid ) ) > $this->headerstrlen[id] ) |
---|
1159 | $this->headerstrlen[id] = strlen( strval( $jobid ) ); |
---|
1160 | |
---|
1161 | if( !isset( $this->headerstrlen[owner] ) ) |
---|
1162 | $this->headerstrlen[owner] = strlen( strval( $jobinfo[owner] ) ); |
---|
1163 | else |
---|
1164 | if( strlen( strval( $jobinfo[owner] ) ) > $this->headerstrlen[owner] ) |
---|
1165 | $this->headerstrlen[owner] = strlen( strval( $jobinfo[owner] ) ); |
---|
1166 | |
---|
1167 | if( !isset( $this->headerstrlen[queue] ) ) |
---|
1168 | $this->headerstrlen[queue] = strlen( strval( $jobinfo[queue] ) ); |
---|
1169 | else |
---|
1170 | if( strlen( strval( $jobinfo[queue] ) ) > $this->headerstrlen[queue] ) |
---|
1171 | $this->headerstrlen[queue] = strlen( strval( $jobinfo[queue] ) ); |
---|
1172 | |
---|
1173 | if( !isset( $jobinfo[ppn] ) ) |
---|
1174 | $jobinfo[ppn] = 1; |
---|
1175 | |
---|
1176 | $cpus = count( $jobinfo[nodes] ) * intval( $jobinfo[ppn] ); |
---|
1177 | |
---|
1178 | if( !isset( $this->headerstrlen[cpus] ) ) |
---|
1179 | $this->headerstrlen[cpus] = strlen( strval( $cpus ) ); |
---|
1180 | else |
---|
1181 | if( strlen( strval( $cpus ) ) > $this->headerstrlen[cpus] ) |
---|
1182 | $this->headerstrlen[cpus] = strlen( strval( $cpus ) ); |
---|
1183 | |
---|
1184 | $nodes = count( $jobinfo[nodes] ); |
---|
1185 | |
---|
1186 | if( !isset( $this->headerstrlen[nodes] ) ) |
---|
1187 | $this->headerstrlen[nodes] = strlen( strval( $nodes ) ); |
---|
1188 | else |
---|
1189 | if( strlen( strval( $nodes) ) > $this->headerstrlen[nodes] ) |
---|
1190 | $this->headerstrlen[nodes] = strlen( strval( $nodes ) ); |
---|
1191 | |
---|
1192 | $runningtime = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo[start_timestamp] ) ); |
---|
1193 | |
---|
1194 | if( !isset( $this->headerstrlen[runningtime] ) ) |
---|
1195 | $this->headerstrlen[runningtime] = strlen( strval( $runningtime) ); |
---|
1196 | else |
---|
1197 | if( strlen( strval( $runningtime) ) > $this->headerstrlen[runningtime] ) |
---|
1198 | $this->headerstrlen[runningtime] = strlen( strval( $runningtime) ); |
---|
1199 | |
---|
1200 | if( !isset( $this->headerstrlen[name] ) ) |
---|
1201 | $this->headerstrlen[name] = strlen( strval( $jobinfo[name] ) ); |
---|
1202 | else |
---|
1203 | if( strlen( strval( $jobinfo[name] ) ) > $this->headerstrlen[name] ) |
---|
1204 | $this->headerstrlen[name] = strlen( strval( $jobinfo[name] ) ); |
---|
1205 | |
---|
1206 | } |
---|
1207 | |
---|
1208 | $xoffset = 5; |
---|
1209 | |
---|
1210 | foreach( $this->headerstrlen as $headername => &$headerlen ) { |
---|
1211 | |
---|
1212 | $colorgreen = imageColorAllocate( $this->image, 0, 200, 0 ); |
---|
1213 | |
---|
1214 | imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen ); |
---|
1215 | |
---|
1216 | if( $headerlen < strlen( $headername ) ) |
---|
1217 | $headerlen = strlen( $headername ); |
---|
1218 | |
---|
1219 | $xoffset = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) ); |
---|
1220 | |
---|
1221 | } |
---|
1222 | $this->newLineOffset(); |
---|
1223 | } |
---|
1224 | |
---|
1225 | function newLineOffset() { |
---|
1226 | |
---|
1227 | $this->y_offset = $this->y_offset + $this->fontheight + $this->fontspaceing; |
---|
1228 | } |
---|
1229 | |
---|
1230 | function draw() { |
---|
1231 | |
---|
1232 | $xlen = 450; |
---|
1233 | $ylen = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight); |
---|
1234 | |
---|
1235 | $this->image = imageCreateTrueColor( $xlen, $ylen ); |
---|
1236 | $colorwhite = imageColorAllocate( $this->image, 255, 255, 255 ); |
---|
1237 | imageFill( $this->image, 0, 0, $colorwhite ); |
---|
1238 | |
---|
1239 | $colorblue = imageColorAllocate( $this->image, 0, 0, 255 ); |
---|
1240 | |
---|
1241 | imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue ); |
---|
1242 | $this->newLineOffset(); |
---|
1243 | |
---|
1244 | $this->drawHeader(); |
---|
1245 | $this->drawJobs(); |
---|
1246 | |
---|
1247 | header( 'Content-type: image/png' ); |
---|
1248 | imagePNG( $this->image ); |
---|
1249 | imageDestroy( $this->image ); |
---|
1250 | } |
---|
1251 | } |
---|
1252 | |
---|
1253 | function makeTime( $time ) { |
---|
1254 | |
---|
1255 | $days = intval( $time / 86400 ); |
---|
1256 | $time = ($days>0) ? $time % ($days * 86400) : $time; |
---|
1257 | |
---|
1258 | //printf( "time = %s, days = %s\n", $time, $days ); |
---|
1259 | |
---|
1260 | $date_str = ''; |
---|
1261 | $day_str = ''; |
---|
1262 | |
---|
1263 | if( $days > 0 ) { |
---|
1264 | if( $days > 1 ) |
---|
1265 | $day_str .= $days . ' days'; |
---|
1266 | else |
---|
1267 | $day_str .= $days . ' day'; |
---|
1268 | } |
---|
1269 | |
---|
1270 | $hours = intval( $time / 3600 ); |
---|
1271 | $time = $hours ? $time % ($hours * 3600) : $time; |
---|
1272 | |
---|
1273 | //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours ); |
---|
1274 | if( $hours > 0 ) { |
---|
1275 | $date_str .= $hours . ':'; |
---|
1276 | $date_unit = 'hours'; |
---|
1277 | } |
---|
1278 | |
---|
1279 | $minutes = intval( $time / 60 ); |
---|
1280 | $seconds = $minutes ? $time % ($minutes * 60) : $time; |
---|
1281 | |
---|
1282 | if( $minutes > 0 ) { |
---|
1283 | |
---|
1284 | if( $minutes >= 10 ) |
---|
1285 | $date_str .= $minutes . ':'; |
---|
1286 | else |
---|
1287 | $date_str .= '0' . $minutes . ':'; |
---|
1288 | |
---|
1289 | $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit; |
---|
1290 | } else { |
---|
1291 | if($hours > 0 ) { |
---|
1292 | $date_str .= '00:'; |
---|
1293 | $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit; |
---|
1294 | } |
---|
1295 | } |
---|
1296 | |
---|
1297 | |
---|
1298 | $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit; |
---|
1299 | |
---|
1300 | if( $seconds > 0 ) { |
---|
1301 | |
---|
1302 | if( $seconds >= 10 ) |
---|
1303 | $date_str .= $seconds . ' ' . $date_unit; |
---|
1304 | else |
---|
1305 | $date_str .= '0' . $seconds . ' ' . $date_unit; |
---|
1306 | |
---|
1307 | } else if ( $hours > 0 or $minutes > 0 ) |
---|
1308 | |
---|
1309 | $date_str .= '00 ' . $date_unit; |
---|
1310 | |
---|
1311 | if( $days > 0) { |
---|
1312 | |
---|
1313 | if( $hours > 0 or $minutes > 0 or $seconds > 0 ) |
---|
1314 | $date_str = $day_str . ' - ' . $date_str; |
---|
1315 | else |
---|
1316 | $date_str = $day_str; |
---|
1317 | } |
---|
1318 | |
---|
1319 | return $date_str; |
---|
1320 | } |
---|
1321 | ?> |
---|