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 686 2012-11-28 15:21:51Z ramonb $ |
---|
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 = isset( $httpvars["c"] ) ? $httpvars["c"] : $getvars["c"]; |
---|
37 | $this->metricname = isset( $httpvars["m"] ) ? $httpvars["m"] : $getvars["m"]; |
---|
38 | |
---|
39 | if( count( $httpvars ) > 0 ) |
---|
40 | { |
---|
41 | foreach( $httpvars as $httpvar => $httpval ) |
---|
42 | { |
---|
43 | if( $httpval ) |
---|
44 | { |
---|
45 | $this->restvars[$httpvar] = $httpval; |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | if( count( $getvars ) > 0 ) |
---|
51 | { |
---|
52 | foreach( $getvars as $getvar => $getval ) |
---|
53 | { |
---|
54 | if( $getval ) |
---|
55 | { |
---|
56 | $this->restvars[$getvar] = $getval; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | function getClusterName() |
---|
63 | { |
---|
64 | return $this->clustername; |
---|
65 | } |
---|
66 | |
---|
67 | function getMetricName() |
---|
68 | { |
---|
69 | return $this->metricname; |
---|
70 | } |
---|
71 | |
---|
72 | function getHttpVar( $var ) |
---|
73 | { |
---|
74 | if( isset( $this->restvars[$var] ) ) |
---|
75 | { |
---|
76 | return $this->restvars[$var]; |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | return null; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | $CLUSTER_CONFS = array(); |
---|
86 | |
---|
87 | ini_set("memory_limit","500M"); |
---|
88 | set_time_limit(0); |
---|
89 | |
---|
90 | // Monarch's conf |
---|
91 | // |
---|
92 | include_once "./conf.php"; |
---|
93 | include_once "./version.php"; |
---|
94 | |
---|
95 | global $GANGLIA_PATH; |
---|
96 | global $RRDTOOL; |
---|
97 | global $JOB_ARCHIVE_DIR; |
---|
98 | global $JOB_ARCHIVE_DBASE; |
---|
99 | global $skan_str; |
---|
100 | global $x_first, $y_first; |
---|
101 | global $CLUSTER_CONFS; |
---|
102 | |
---|
103 | $my_dir = getcwd(); |
---|
104 | |
---|
105 | // Load Ganglia's PHP |
---|
106 | chdir( $GANGLIA_PATH ); |
---|
107 | |
---|
108 | //include_once "./conf.php"; |
---|
109 | include_once "./eval_conf.php"; |
---|
110 | include_once "./functions.php"; |
---|
111 | include_once "./ganglia.php"; |
---|
112 | include_once "./get_context.php"; |
---|
113 | |
---|
114 | $context = 'cluster'; |
---|
115 | include_once "./get_ganglia.php"; |
---|
116 | |
---|
117 | // Back to our PHP |
---|
118 | chdir( $my_dir ); |
---|
119 | |
---|
120 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH, $DATA_SOURCE, $HTTP_GET_VARS, $_GET; |
---|
121 | $httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET ); |
---|
122 | |
---|
123 | // Set cluster context so that Ganglia will |
---|
124 | // provide us with the correct metrics array |
---|
125 | // |
---|
126 | global $context, $clustername, $reports; |
---|
127 | |
---|
128 | global $default_metric; |
---|
129 | |
---|
130 | // Ganglia's array of host metrics |
---|
131 | // |
---|
132 | global $metrics, $hosts_up; |
---|
133 | global $range, $start; |
---|
134 | |
---|
135 | global $DATETIME_FORMAT; |
---|
136 | |
---|
137 | function makeDate( $time ) |
---|
138 | { |
---|
139 | global $DATETIME_FORMAT; |
---|
140 | return strftime( $DATETIME_FORMAT, $time ); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | class TarchDbase |
---|
145 | { |
---|
146 | var $ip, $dbase, $conn; |
---|
147 | |
---|
148 | function TarchDbase( $ip = null, $dbase = null ) |
---|
149 | { |
---|
150 | global $CLUSTER_CONFS, $clustername; |
---|
151 | global $JOB_ARCHIVE_DBASE; |
---|
152 | |
---|
153 | // Import cluster specific settings |
---|
154 | // |
---|
155 | foreach( $CLUSTER_CONFS as $confcluster => $conffile ) |
---|
156 | { |
---|
157 | if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) ) |
---|
158 | { |
---|
159 | include_once $conffile; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | $db_fields = explode( '/', $JOB_ARCHIVE_DBASE ); |
---|
164 | |
---|
165 | $this->ip = $db_fields[0]; |
---|
166 | $this->dbase = $db_fields[1]; |
---|
167 | $this->conn = null; |
---|
168 | } |
---|
169 | |
---|
170 | function connect() |
---|
171 | { |
---|
172 | if( $this->ip == null ) |
---|
173 | $this->conn = pg_connect( "dbname=".$this->dbase ); |
---|
174 | else |
---|
175 | $this->conn = pg_connect( "host=".$this->ip." dbname=".$this->dbase ); |
---|
176 | } |
---|
177 | |
---|
178 | 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 ) |
---|
179 | { |
---|
180 | global $SEARCH_RESULT_LIMIT; |
---|
181 | |
---|
182 | if( $id ) |
---|
183 | { |
---|
184 | $select_query = "SELECT job_id FROM jobs WHERE job_id = '$id' AND job_status = 'F'"; |
---|
185 | $this->resultcount = 1; |
---|
186 | } |
---|
187 | else |
---|
188 | { |
---|
189 | $query_args = array(); |
---|
190 | |
---|
191 | if( $queue ) |
---|
192 | { |
---|
193 | $query_args[] = "job_queue ='$queue'"; |
---|
194 | } |
---|
195 | if( $user ) |
---|
196 | { |
---|
197 | $query_args[] = "job_owner ='$user'"; |
---|
198 | } |
---|
199 | if( $name ) |
---|
200 | { |
---|
201 | $query_args[] = "job_name = '$name'"; |
---|
202 | } |
---|
203 | if( $start_from_time ) |
---|
204 | { |
---|
205 | $query_args[] = "job_start_timestamp >= $start_from_time"; |
---|
206 | } |
---|
207 | if( $start_to_time ) |
---|
208 | { |
---|
209 | $query_args[] = "job_start_timestamp <= $start_to_time"; |
---|
210 | } |
---|
211 | if( $end_from_time ) |
---|
212 | { |
---|
213 | $query_args[] = "job_stop_timestamp >= $end_from_time"; |
---|
214 | } |
---|
215 | if( $end_to_time ) |
---|
216 | { |
---|
217 | $query_args[] = "job_stop_timestamp <= $end_to_time"; |
---|
218 | } |
---|
219 | |
---|
220 | $query = "FROM jobs WHERE job_status = 'F' AND "; |
---|
221 | $extra_query_args = ''; |
---|
222 | |
---|
223 | foreach( $query_args as $myquery ) |
---|
224 | { |
---|
225 | if( $extra_query_args == '' ) |
---|
226 | { |
---|
227 | $extra_query_args = $myquery; |
---|
228 | } |
---|
229 | else |
---|
230 | { |
---|
231 | $extra_query_args .= " AND ".$myquery; |
---|
232 | } |
---|
233 | } |
---|
234 | $query .= $extra_query_args; |
---|
235 | |
---|
236 | $count_result_idname = "COUNT(job_id)"; |
---|
237 | $select_result_idname = "job_id"; |
---|
238 | |
---|
239 | $count_query = "SELECT " . $count_result_idname . " " . $query; |
---|
240 | |
---|
241 | $count_result = $this->queryDbase( $count_query ); |
---|
242 | $this->resultcount = (int) $count_result[0]['count']; |
---|
243 | |
---|
244 | $select_query = "SELECT " . $select_result_idname . " " . $query . " ORDER BY job_id DESC LIMIT " . $SEARCH_RESULT_LIMIT; |
---|
245 | } |
---|
246 | |
---|
247 | $ids = $this->queryDbase( $select_query ); |
---|
248 | |
---|
249 | $ret = array(); |
---|
250 | |
---|
251 | foreach( $ids as $crow) |
---|
252 | { |
---|
253 | $ret[] = $crow['job_id']; |
---|
254 | } |
---|
255 | |
---|
256 | return $ret; |
---|
257 | } |
---|
258 | |
---|
259 | function getNodesForJob( $jobid ) |
---|
260 | { |
---|
261 | $result = $this->queryDbase( "SELECT node_id FROM job_nodes WHERE job_id = '$jobid'" ); |
---|
262 | |
---|
263 | $nodes = array(); |
---|
264 | |
---|
265 | foreach( $result as $result_row ) |
---|
266 | { |
---|
267 | $nodes[] = $this->getNodeArray( $result_row['node_id'] ); |
---|
268 | } |
---|
269 | |
---|
270 | return $nodes; |
---|
271 | } |
---|
272 | |
---|
273 | function getJobsForNode( $nodeid ) |
---|
274 | { |
---|
275 | $result = $this->queryDbase( "SELECT job_id FROM job_nodes WHERE node_id = '$nodeid'" ); |
---|
276 | |
---|
277 | $jobs = array(); |
---|
278 | |
---|
279 | foreach( $result as $result_row ) |
---|
280 | { |
---|
281 | $jobs[] = $this->getJobArray( $result_row['job_id'] ); |
---|
282 | } |
---|
283 | return $jobs; |
---|
284 | } |
---|
285 | |
---|
286 | function getJobArray( $id ) |
---|
287 | { |
---|
288 | $result = $this->queryDbase( "SELECT * FROM jobs WHERE job_id = '$id'" ); |
---|
289 | |
---|
290 | return ( $this->makeArray( $result[0] ) ); |
---|
291 | } |
---|
292 | |
---|
293 | function getNodeArray( $id ) |
---|
294 | { |
---|
295 | $result = $this->queryDbase( "SELECT * FROM nodes WHERE node_id = '$id'" ); |
---|
296 | |
---|
297 | return ( $this->makeArray( $result[0] ) ); |
---|
298 | } |
---|
299 | |
---|
300 | function makeArray( $result_row ) |
---|
301 | { |
---|
302 | $myar = array(); |
---|
303 | |
---|
304 | foreach( $result_row as $mykey => $myval ) |
---|
305 | { |
---|
306 | $map_key = explode( '_', $mykey ); |
---|
307 | |
---|
308 | $rmap_key = array_reverse( $map_key ); |
---|
309 | array_pop( $rmap_key ); |
---|
310 | $map_key = array_reverse( $rmap_key ); |
---|
311 | |
---|
312 | $newkey = implode( '_', $map_key ); |
---|
313 | |
---|
314 | $myar[$newkey] = $result_row[$mykey]; |
---|
315 | } |
---|
316 | |
---|
317 | return $myar; |
---|
318 | } |
---|
319 | |
---|
320 | function queryDbase( $query ) |
---|
321 | { |
---|
322 | $result_rows = array(); |
---|
323 | |
---|
324 | if( !$this->conn ) |
---|
325 | { |
---|
326 | $this->connect(); |
---|
327 | } |
---|
328 | |
---|
329 | $result = pg_query( $this->conn, $query ); |
---|
330 | |
---|
331 | while ($row = pg_fetch_assoc($result)) |
---|
332 | { |
---|
333 | $result_rows[] = $row; |
---|
334 | } |
---|
335 | |
---|
336 | return $result_rows; |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | class TarchRrdGraph |
---|
341 | { |
---|
342 | var $rrdbin, $rrdvalues, $clustername, $hostname, $tempdir, $tarchdir, $metrics; |
---|
343 | |
---|
344 | function TarchRrdGraph( $clustername, $hostname ) |
---|
345 | { |
---|
346 | global $RRDTOOL; |
---|
347 | global $JOB_ARCHIVE_DIR; |
---|
348 | |
---|
349 | $this->rrdbin = $RRDTOOL; |
---|
350 | $this->rrdvalues = array(); |
---|
351 | $this->tarchdir = $JOB_ARCHIVE_DIR; |
---|
352 | $this->clustername = $clustername; |
---|
353 | $this->hostname = $hostname; |
---|
354 | } |
---|
355 | |
---|
356 | function doCmd( $command ) |
---|
357 | { |
---|
358 | $pipe = popen( $command . ' 2>&1', 'r' ); |
---|
359 | |
---|
360 | if (!$pipe) |
---|
361 | { |
---|
362 | print "pipe failed."; |
---|
363 | return ""; |
---|
364 | } |
---|
365 | |
---|
366 | $output = ''; |
---|
367 | |
---|
368 | while(!feof($pipe)) |
---|
369 | { |
---|
370 | $output .= fread($pipe, 1024); |
---|
371 | } |
---|
372 | |
---|
373 | pclose($pipe); |
---|
374 | |
---|
375 | $output = explode( "\n", $output ); |
---|
376 | |
---|
377 | return $output; |
---|
378 | } |
---|
379 | |
---|
380 | function dirList( $dir ) |
---|
381 | { |
---|
382 | $dirlist = array(); |
---|
383 | |
---|
384 | if ($handle = opendir( $dir )) |
---|
385 | { |
---|
386 | while (false !== ($file = readdir($handle))) |
---|
387 | { |
---|
388 | if ($file != "." && $file != "..") |
---|
389 | { |
---|
390 | $dirlist[] = $file; |
---|
391 | } |
---|
392 | } |
---|
393 | closedir($handle); |
---|
394 | } |
---|
395 | |
---|
396 | return $dirlist; |
---|
397 | } |
---|
398 | |
---|
399 | function getTimePeriods( $start, $end ) |
---|
400 | { |
---|
401 | $times = array(); |
---|
402 | $dirlist = $this->dirList( $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname ); |
---|
403 | |
---|
404 | $first = 0; |
---|
405 | $last = 9999999999999; |
---|
406 | |
---|
407 | foreach( $dirlist as $dir ) |
---|
408 | { |
---|
409 | if( $dir > $first and $dir <= $start ) |
---|
410 | { |
---|
411 | $first = $dir; |
---|
412 | } |
---|
413 | if( $dir < $last and $dir >= $end ) |
---|
414 | { |
---|
415 | $last = $dir; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | foreach( $dirlist as $dir ) |
---|
420 | { |
---|
421 | if( $dir >= $first and $dir <= $last and !array_key_exists( $dir, $times ) ) |
---|
422 | { |
---|
423 | $times[] = $dir; |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | sort( $times ); |
---|
428 | |
---|
429 | return $times; |
---|
430 | } |
---|
431 | |
---|
432 | function getRrdDirs( $start, $stop ) |
---|
433 | { |
---|
434 | $timess = $this->getTimePeriods( $start, $stop ); |
---|
435 | |
---|
436 | $rrd_files = array(); |
---|
437 | |
---|
438 | foreach( $timess as $time ) |
---|
439 | { |
---|
440 | $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname. '/'.$time; |
---|
441 | } |
---|
442 | |
---|
443 | return $rrd_files; |
---|
444 | } |
---|
445 | |
---|
446 | function getRrdFiles( $metric, $start, $stop ) |
---|
447 | { |
---|
448 | $times = $this->getTimePeriods( $start, $stop ); |
---|
449 | |
---|
450 | $rrd_files = array(); |
---|
451 | |
---|
452 | foreach( $times as $time ) |
---|
453 | { |
---|
454 | $rrd_files[] = $this->tarchdir . '/' . $this->clustername . '/' . $this->hostname . '/' .$time. '/' . $metric. '.rrd'; |
---|
455 | } |
---|
456 | |
---|
457 | return $rrd_files; |
---|
458 | } |
---|
459 | } |
---|
460 | |
---|
461 | class DataSource |
---|
462 | { |
---|
463 | var $data, $ip, $port; |
---|
464 | |
---|
465 | function DataSource() |
---|
466 | { |
---|
467 | global $DATA_SOURCE; |
---|
468 | |
---|
469 | $ds_fields = explode( ':', $DATA_SOURCE ); |
---|
470 | |
---|
471 | $ds_ip = $ds_fields[0]; |
---|
472 | $ds_port = $ds_fields[1]; |
---|
473 | |
---|
474 | $this->ip = $ds_ip; |
---|
475 | $this->port = $ds_port; |
---|
476 | |
---|
477 | } |
---|
478 | |
---|
479 | function getData() |
---|
480 | { |
---|
481 | $errstr = ''; |
---|
482 | $errno = 0; |
---|
483 | $timeout = 3; |
---|
484 | |
---|
485 | $fp = fsockopen( $this->ip, $this->port, $errno, $errstr, $timeout ); |
---|
486 | |
---|
487 | if( !$fp ) |
---|
488 | { |
---|
489 | echo 'Unable to connect to '.$this->ip.':'.$this->port; // printf( 'Unable to connect to [%s:%.0f]', $this->ip, $this->port ); |
---|
490 | return; |
---|
491 | } |
---|
492 | |
---|
493 | stream_set_timeout( $fp, 30 ); |
---|
494 | |
---|
495 | while ( !feof( $fp ) ) |
---|
496 | { |
---|
497 | $data .= fread( $fp, 16384 ); |
---|
498 | } |
---|
499 | |
---|
500 | fclose( $fp ); |
---|
501 | |
---|
502 | return $data; |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | class DataGatherer |
---|
507 | { |
---|
508 | var $xmlhandler, $data, $httpvars; |
---|
509 | |
---|
510 | function DataGatherer( $cluster ) |
---|
511 | { |
---|
512 | $this->cluster = $cluster; |
---|
513 | $this->httpvars = $httpvars; |
---|
514 | } |
---|
515 | |
---|
516 | function parseXML( $data ) |
---|
517 | { |
---|
518 | $this->parser = xml_parser_create(); |
---|
519 | $this->xmlhandler = new TorqueXMLHandler( $this->cluster ); |
---|
520 | |
---|
521 | xml_set_element_handler( $this->parser, array( &$this->xmlhandler, 'startElement' ), array( &$this->xmlhandler, 'stopElement' ) ); |
---|
522 | |
---|
523 | if ( !xml_parse( $this->parser, $data ) ) |
---|
524 | { |
---|
525 | $error = sprintf( 'XML error: %s at %d', xml_error_string( xml_get_error_code( $this->parser ) ), xml_get_current_line_number( $this->parser ) ); |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | function printInfo() |
---|
530 | { |
---|
531 | $handler = $this->xmlhandler; |
---|
532 | $handler->printInfo(); |
---|
533 | } |
---|
534 | |
---|
535 | function getUsingFQDN() |
---|
536 | { |
---|
537 | $handler = $this->xmlhandler; |
---|
538 | return $handler->getUsingFQDN(); |
---|
539 | } |
---|
540 | |
---|
541 | function getNodes() |
---|
542 | { |
---|
543 | $handler = $this->xmlhandler; |
---|
544 | return $handler->getNodes(); |
---|
545 | } |
---|
546 | |
---|
547 | function getNode( $node ) |
---|
548 | { |
---|
549 | $handler = $this->xmlhandler; |
---|
550 | return $handler->getNode( $node ); |
---|
551 | } |
---|
552 | |
---|
553 | function getCpus() |
---|
554 | { |
---|
555 | $handler = $this->xmlhandler; |
---|
556 | return $handler->getCpus(); |
---|
557 | } |
---|
558 | |
---|
559 | function getJobs() |
---|
560 | { |
---|
561 | $handler = $this->xmlhandler; |
---|
562 | return $handler->getJobs(); |
---|
563 | } |
---|
564 | |
---|
565 | function getJob( $job ) |
---|
566 | { |
---|
567 | $handler = $this->xmlhandler; |
---|
568 | return $handler->getJob( $job ); |
---|
569 | } |
---|
570 | |
---|
571 | function getHeartbeat() |
---|
572 | { |
---|
573 | $handler = $this->xmlhandler; |
---|
574 | return $handler->getHeartbeat(); |
---|
575 | } |
---|
576 | |
---|
577 | function isJobmonRunning() |
---|
578 | { |
---|
579 | $handler = $this->xmlhandler; |
---|
580 | return $handler->isJobmonRunning(); |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | class TorqueXMLHandler |
---|
585 | { |
---|
586 | var $clusters, $heartbeat, $nodes, $jobs, $clustername, $proc_cluster; |
---|
587 | |
---|
588 | function TorqueXMLHandler( $clustername ) |
---|
589 | { |
---|
590 | $this->jobs = array(); |
---|
591 | $this->clusters = array(); |
---|
592 | $this->nodes = array(); |
---|
593 | $this->heartbeat = array(); |
---|
594 | $this->down_nodes = array(); |
---|
595 | $this->offline_nodes = array(); |
---|
596 | $this->clustername = $clustername; |
---|
597 | $this->fqdn = 1; |
---|
598 | } |
---|
599 | |
---|
600 | function getUsingFQDN() |
---|
601 | { |
---|
602 | return $this->fqdn; |
---|
603 | } |
---|
604 | |
---|
605 | function getCpus() |
---|
606 | { |
---|
607 | $cpus = 0; |
---|
608 | |
---|
609 | if( isset( $this->jobs ) && count( $this->jobs ) > 0 ) |
---|
610 | { |
---|
611 | foreach( $this->jobs as $jobid=>$jobattrs ) |
---|
612 | { |
---|
613 | $nodes = count( $jobattrs['nodes'] ); |
---|
614 | $ppn = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1; |
---|
615 | $mycpus = $nodes * $ppn; |
---|
616 | |
---|
617 | $cpus = $cpus + $mycpus; |
---|
618 | } |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | function isJobmonRunning() |
---|
623 | { |
---|
624 | if (isset( $this->heartbeat['time'] )) |
---|
625 | { |
---|
626 | return 1; |
---|
627 | } |
---|
628 | else |
---|
629 | { |
---|
630 | return 0; |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | function makeHostname( $thostname, $tdomain=null ) |
---|
635 | { |
---|
636 | // Should hostname be FQDN or short w/o domain |
---|
637 | // |
---|
638 | $nodes = &$this->nodes; |
---|
639 | |
---|
640 | $fqdn = 1; |
---|
641 | |
---|
642 | //$tdomain = explode( '.', $thostname ); |
---|
643 | // |
---|
644 | // TODO?: extract domain from hostname or something? |
---|
645 | |
---|
646 | if( $tdomain ) |
---|
647 | { |
---|
648 | $domain_len = 0 - strlen( $tdomain ); |
---|
649 | |
---|
650 | // Let's see if Ganglia use's FQDN or short hostnames |
---|
651 | // |
---|
652 | foreach( $nodes as $hostname => $nimage ) |
---|
653 | { |
---|
654 | if( substr( $hostname, $domain_len ) != $tdomain ) |
---|
655 | { |
---|
656 | $fqdn = 0; |
---|
657 | } |
---|
658 | } |
---|
659 | } |
---|
660 | else |
---|
661 | { |
---|
662 | $fqdn = 0; |
---|
663 | } |
---|
664 | |
---|
665 | if( $tdomain && $fqdn ) |
---|
666 | { |
---|
667 | if( substr( $thostname, $domain_len ) != $tdomain ) |
---|
668 | { |
---|
669 | $thostname = $thostname . '.'.$tdomain; |
---|
670 | } |
---|
671 | else |
---|
672 | { |
---|
673 | $thostname = $thostname; |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | return $thostname; |
---|
678 | } |
---|
679 | |
---|
680 | function startElement( $parser, $name, $attrs ) |
---|
681 | { |
---|
682 | $jobs = $this->jobs; |
---|
683 | $nodes = $this->nodes; |
---|
684 | |
---|
685 | if( isset( $attrs['TN'] ) ) |
---|
686 | { |
---|
687 | if ( $attrs['TN'] ) |
---|
688 | { |
---|
689 | // Ignore dead metrics. Detect and mask failures. |
---|
690 | if ( $attrs['TN'] > $attrs['TMAX'] * 4 ) |
---|
691 | { |
---|
692 | return; |
---|
693 | } |
---|
694 | } |
---|
695 | } |
---|
696 | |
---|
697 | $jobid = null; |
---|
698 | |
---|
699 | if( $name == 'CLUSTER' ) |
---|
700 | { |
---|
701 | $this->proc_cluster = $attrs['NAME']; |
---|
702 | } |
---|
703 | else if( $name == 'HOST' and $this->proc_cluster == $this->clustername) |
---|
704 | { |
---|
705 | $hostname = $attrs['NAME']; |
---|
706 | |
---|
707 | $location = $attrs['LOCATION']; |
---|
708 | |
---|
709 | if( !isset( $nodes[$hostname] ) ) |
---|
710 | { |
---|
711 | $nodes[$hostname] = new NodeImage( $this->proc_cluster, $hostname ); |
---|
712 | } |
---|
713 | } |
---|
714 | else if( $name == 'METRIC' and strstr( $attrs['NAME'], 'MONARCH' ) and $this->proc_cluster == $this->clustername ) |
---|
715 | { |
---|
716 | if( strstr( $attrs['NAME'], 'MONARCH-HEARTBEAT' ) ) |
---|
717 | { |
---|
718 | $this->heartbeat['time'] = $attrs['VAL']; |
---|
719 | } |
---|
720 | else if( strstr( $attrs['NAME'], 'MONARCH-DOWN' ) ) |
---|
721 | { |
---|
722 | $fields = explode( ' ', $attrs['VAL'] ); |
---|
723 | |
---|
724 | $nodes_down = array(); |
---|
725 | $down_domain = null; |
---|
726 | |
---|
727 | foreach( $fields as $f ) |
---|
728 | { |
---|
729 | $togavalues = explode( '=', $f ); |
---|
730 | |
---|
731 | $toganame = $togavalues[0]; |
---|
732 | $togavalue = $togavalues[1]; |
---|
733 | |
---|
734 | if( $toganame == 'nodes' ) |
---|
735 | { |
---|
736 | $mynodes = explode( ';', $togavalue ); |
---|
737 | |
---|
738 | foreach( $mynodes as $node ) |
---|
739 | { |
---|
740 | $nodes_down[] = $node; |
---|
741 | } |
---|
742 | } |
---|
743 | else if( $toganame == 'domain' ) |
---|
744 | { |
---|
745 | $down_domain = $togavalue; |
---|
746 | } |
---|
747 | else if( $toganame == 'reported' ) |
---|
748 | { |
---|
749 | if( !isset( $this->down_nodes['heartbeat'] ) ) |
---|
750 | { |
---|
751 | $this->down_nodes[$togavalue] = array( $nodes_down, $down_domain ); |
---|
752 | } |
---|
753 | } |
---|
754 | } |
---|
755 | } |
---|
756 | else if( strstr( $attrs['NAME'], 'MONARCH-OFFLINE' ) ) |
---|
757 | { |
---|
758 | $fields = explode( ' ', $attrs['VAL'] ); |
---|
759 | |
---|
760 | $nodes_offline = array(); |
---|
761 | $offline_domain = null; |
---|
762 | |
---|
763 | foreach( $fields as $f ) |
---|
764 | { |
---|
765 | $togavalues = explode( '=', $f ); |
---|
766 | |
---|
767 | $toganame = $togavalues[0]; |
---|
768 | $togavalue = $togavalues[1]; |
---|
769 | |
---|
770 | if( $toganame == 'nodes' ) |
---|
771 | { |
---|
772 | $mynodes = explode( ';', $togavalue ); |
---|
773 | |
---|
774 | foreach( $mynodes as $node ) |
---|
775 | { |
---|
776 | $nodes_offline[] = $node; |
---|
777 | } |
---|
778 | } |
---|
779 | else if( $toganame == 'domain' ) |
---|
780 | { |
---|
781 | $offline_domain = $togavalue; |
---|
782 | } |
---|
783 | else if( $toganame == 'reported' ) |
---|
784 | { |
---|
785 | if( !isset( $this->offline_nodes['heartbeat'] ) ) |
---|
786 | { |
---|
787 | $this->offline_nodes[$togavalue] = array( $nodes_offline, $offline_domain ); |
---|
788 | } |
---|
789 | } |
---|
790 | } |
---|
791 | } |
---|
792 | else if( strstr( $attrs['NAME'], 'MONARCH-JOB' ) ) |
---|
793 | { |
---|
794 | sscanf( $attrs['NAME'], 'MONARCH-JOB-%d-%d', $jobid, $monincr ); |
---|
795 | |
---|
796 | if( !isset( $jobs[$jobid] ) ) |
---|
797 | { |
---|
798 | $jobs[$jobid] = array(); |
---|
799 | } |
---|
800 | |
---|
801 | $fields = explode( ' ', $attrs['VAL'] ); |
---|
802 | |
---|
803 | foreach( $fields as $f ) |
---|
804 | { |
---|
805 | $togavalues = explode( '=', $f ); |
---|
806 | |
---|
807 | $toganame = $togavalues[0]; |
---|
808 | $togavalue = $togavalues[1]; |
---|
809 | |
---|
810 | if( $toganame == 'nodes' ) |
---|
811 | { |
---|
812 | if( $jobs[$jobid]['status'] == 'R' ) |
---|
813 | { |
---|
814 | if( !isset( $jobs[$jobid][$toganame] ) ) |
---|
815 | { |
---|
816 | $jobs[$jobid][$toganame] = array(); |
---|
817 | } |
---|
818 | |
---|
819 | $mynodes = explode( ';', $togavalue ); |
---|
820 | |
---|
821 | foreach( $mynodes as $node ) |
---|
822 | { |
---|
823 | if( !in_array( $node, $jobs[$jobid][$toganame] ) ) |
---|
824 | { |
---|
825 | array_push( $jobs[$jobid][$toganame], $node ); |
---|
826 | } |
---|
827 | } |
---|
828 | |
---|
829 | } |
---|
830 | else if( $jobs[$jobid]['status'] == 'Q' ) |
---|
831 | { |
---|
832 | $jobs[$jobid][$toganame] = $togavalue; |
---|
833 | } |
---|
834 | } |
---|
835 | else |
---|
836 | { |
---|
837 | $jobs[$jobid][$toganame] = $togavalue; |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | if( isset( $jobs[$jobid]['nodes'] ) ) |
---|
842 | { |
---|
843 | $nr_nodes = count( $jobs[$jobid]['nodes'] ); |
---|
844 | |
---|
845 | if( $jobs[$jobid]['status'] == 'R' ) |
---|
846 | { |
---|
847 | |
---|
848 | if( isset( $jobs[$jobid]['domain'] ) ) |
---|
849 | { |
---|
850 | $domain = $jobs[$jobid]['domain']; |
---|
851 | $domain_len = 0 - strlen( $domain ); |
---|
852 | |
---|
853 | // Let's see if Ganglia use's FQDN or short hostnames |
---|
854 | // |
---|
855 | foreach( $nodes as $hostname => $nimage ) |
---|
856 | { |
---|
857 | |
---|
858 | if( substr( $hostname, $domain_len ) != $domain ) |
---|
859 | { |
---|
860 | $this->fqdn = 0; |
---|
861 | } |
---|
862 | } |
---|
863 | } |
---|
864 | else |
---|
865 | { |
---|
866 | $this->fqdn = 0; |
---|
867 | } |
---|
868 | |
---|
869 | foreach( $jobs[$jobid]['nodes'] as $node ) |
---|
870 | { |
---|
871 | |
---|
872 | // Only add domain name to the hostname if Ganglia is doing that too |
---|
873 | // |
---|
874 | if( $this->fqdn && isset( $jobs[$jobid]['domain'] ) ) |
---|
875 | { |
---|
876 | if( substr( $node, $domain_len ) != $domain ) |
---|
877 | { |
---|
878 | $host = $node. '.'.$domain; |
---|
879 | } else |
---|
880 | { |
---|
881 | $host = $node; |
---|
882 | } |
---|
883 | } |
---|
884 | else |
---|
885 | { |
---|
886 | $host = $node; |
---|
887 | } |
---|
888 | |
---|
889 | if( !isset( $nodes[$host] ) ) |
---|
890 | { |
---|
891 | $my_node = new NodeImage( $this->proc_cluster, $host ); |
---|
892 | } |
---|
893 | else |
---|
894 | { |
---|
895 | $my_node = $nodes[$host]; |
---|
896 | } |
---|
897 | |
---|
898 | if( !$my_node->hasJob( $jobid ) ) |
---|
899 | { |
---|
900 | if( isset( $jobs[$jobid]['ppn'] ) ) |
---|
901 | { |
---|
902 | $my_node->addJob( $jobid, ((int) $jobs[$jobid]['ppn']) ); |
---|
903 | } |
---|
904 | else |
---|
905 | { |
---|
906 | $my_node->addJob( $jobid, 1 ); |
---|
907 | } |
---|
908 | } |
---|
909 | |
---|
910 | $nodes[$host] = $my_node; |
---|
911 | } |
---|
912 | } |
---|
913 | } |
---|
914 | } |
---|
915 | } |
---|
916 | $this->jobs = $jobs; |
---|
917 | $this->nodes = $nodes; |
---|
918 | } |
---|
919 | |
---|
920 | function stopElement( $parser, $name ) |
---|
921 | { |
---|
922 | $nodes = $this->nodes; |
---|
923 | |
---|
924 | if( $name == "GANGLIA_XML" ) |
---|
925 | { |
---|
926 | if( sizeof( $this->down_nodes ) > 0 ) |
---|
927 | { |
---|
928 | foreach( $this->down_nodes as $reported => $dnodes ) |
---|
929 | { |
---|
930 | if( $reported == $this->heartbeat['time'] ) |
---|
931 | { |
---|
932 | $domain = $dnodes[1]; |
---|
933 | |
---|
934 | foreach( $dnodes[0] as $downhost ) |
---|
935 | { |
---|
936 | $downhost = $this->makeHostname( $downhost, $domain ); |
---|
937 | |
---|
938 | if( isset( $nodes[$downhost] ) ) |
---|
939 | { |
---|
940 | // OMG PHP4 is fking stupid! |
---|
941 | // $nodes[$downhost]->setDown( 1 ) won't work here.. |
---|
942 | // |
---|
943 | $mynode = $nodes[$downhost]; |
---|
944 | $mynode->setDown( 1 ); |
---|
945 | $nodes[$downhost] = $mynode; |
---|
946 | } |
---|
947 | } |
---|
948 | } |
---|
949 | } |
---|
950 | } |
---|
951 | |
---|
952 | if( sizeof( $this->offline_nodes ) > 0 ) |
---|
953 | { |
---|
954 | foreach( $this->offline_nodes as $reported => $onodes ) |
---|
955 | { |
---|
956 | if( $reported == $this->heartbeat['time'] ) |
---|
957 | { |
---|
958 | $domain = $onodes[1]; |
---|
959 | |
---|
960 | foreach( $onodes[0] as $offlinehost ) |
---|
961 | { |
---|
962 | $offlinehost = $this->makeHostname( $offlinehost, $domain ); |
---|
963 | |
---|
964 | if( isset( $nodes[$offlinehost] ) ) |
---|
965 | { |
---|
966 | // OMG PHP4 is fking stupid! |
---|
967 | // $nodes[$offlinehost]->setDown( 1 ) won't work here.. |
---|
968 | // |
---|
969 | $mynode = $nodes[$offlinehost]; |
---|
970 | $mynode->setOffline( 1 ); |
---|
971 | $nodes[$offlinehost] = $mynode; |
---|
972 | } |
---|
973 | } |
---|
974 | } |
---|
975 | } |
---|
976 | } |
---|
977 | } |
---|
978 | |
---|
979 | $this->nodes = $nodes; |
---|
980 | } |
---|
981 | |
---|
982 | function printInfo() |
---|
983 | { |
---|
984 | $jobs = &$this->jobs; |
---|
985 | |
---|
986 | printf( "---jobs---\n" ); |
---|
987 | |
---|
988 | foreach( $jobs as $jobid => $job ) |
---|
989 | { |
---|
990 | printf( "job %s\n", $jobid ); |
---|
991 | |
---|
992 | if( isset( $job['nodes'] ) ) |
---|
993 | { |
---|
994 | foreach( $job['nodes'] as $node ) |
---|
995 | { |
---|
996 | $mynode = $this->nodes[$node]; |
---|
997 | $hostname = $mynode->getHostname(); |
---|
998 | $location = $mynode->getLocation(); |
---|
999 | |
---|
1000 | printf( "\t- node %s\tlocation %s\n", $hostname, $location ); |
---|
1001 | } |
---|
1002 | } |
---|
1003 | } |
---|
1004 | |
---|
1005 | printf( "---nodes---\n" ); |
---|
1006 | |
---|
1007 | $nodes = &$this->nodes; |
---|
1008 | |
---|
1009 | foreach( $nodes as $node ) |
---|
1010 | { |
---|
1011 | $hostname = $node->getHostname(); |
---|
1012 | $location = $node->getLocation(); |
---|
1013 | $jobs = implode( ' ', $node->getJobs() ); |
---|
1014 | printf( "* node %s\tlocation %s\tjobs %s\n", $hostname, $location, $jobs ); |
---|
1015 | } |
---|
1016 | } |
---|
1017 | |
---|
1018 | function getNodes() |
---|
1019 | { |
---|
1020 | return $this->nodes; |
---|
1021 | } |
---|
1022 | |
---|
1023 | function getNode( $node ) |
---|
1024 | { |
---|
1025 | $nodes = &$this->nodes; |
---|
1026 | |
---|
1027 | if( isset( $nodes[$node] ) ) |
---|
1028 | { |
---|
1029 | return $nodes[$node]; |
---|
1030 | } |
---|
1031 | else |
---|
1032 | { |
---|
1033 | return NULL; |
---|
1034 | } |
---|
1035 | } |
---|
1036 | |
---|
1037 | function getJobs() |
---|
1038 | { |
---|
1039 | return $this->jobs; |
---|
1040 | } |
---|
1041 | |
---|
1042 | function getJob( $job ) |
---|
1043 | { |
---|
1044 | $jobs = &$this->jobs; |
---|
1045 | |
---|
1046 | if( isset( $jobs[$job] ) ) |
---|
1047 | { |
---|
1048 | return $jobs[$job]; |
---|
1049 | } |
---|
1050 | else |
---|
1051 | { |
---|
1052 | return NULL; |
---|
1053 | } |
---|
1054 | } |
---|
1055 | |
---|
1056 | function getHeartbeat() |
---|
1057 | { |
---|
1058 | return $this->heartbeat['time']; |
---|
1059 | } |
---|
1060 | } |
---|
1061 | |
---|
1062 | class NodeImage |
---|
1063 | { |
---|
1064 | var $image, $x, $y, $hostname, $jobs, $tasks, $showinfo; |
---|
1065 | |
---|
1066 | function NodeImage( $cluster, $hostname ) |
---|
1067 | { |
---|
1068 | global $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
1069 | |
---|
1070 | $this->jobs = array(); |
---|
1071 | $this->tasks = 0; |
---|
1072 | $this->hostname = $hostname; |
---|
1073 | $this->cpus = $this->determineCpus(); |
---|
1074 | $this->clustername = $cluster; |
---|
1075 | $this->showinfo = 1; |
---|
1076 | $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
1077 | $this->down = 0; |
---|
1078 | $this->offline = 0; |
---|
1079 | } |
---|
1080 | |
---|
1081 | function addJob( $jobid, $cpus ) |
---|
1082 | { |
---|
1083 | $jobs = &$this->jobs; |
---|
1084 | $jobs[] = $jobid; |
---|
1085 | $this->jobs = $jobs; |
---|
1086 | |
---|
1087 | $this->addTask( $cpus ); |
---|
1088 | } |
---|
1089 | |
---|
1090 | function hasJob( $jobid ) |
---|
1091 | { |
---|
1092 | $jobfound = 0; |
---|
1093 | |
---|
1094 | if( count( $this->jobs ) > 0 ) |
---|
1095 | { |
---|
1096 | foreach( $this->jobs as $job ) |
---|
1097 | { |
---|
1098 | if( $job == $jobid ) |
---|
1099 | { |
---|
1100 | $jobfound = 1; |
---|
1101 | } |
---|
1102 | } |
---|
1103 | } |
---|
1104 | |
---|
1105 | return $jobfound; |
---|
1106 | } |
---|
1107 | |
---|
1108 | function addTask( $cpus ) |
---|
1109 | { |
---|
1110 | $this->tasks = $this->tasks + $cpus; |
---|
1111 | } |
---|
1112 | function setDown( $down ) |
---|
1113 | { |
---|
1114 | $this->down = $down; |
---|
1115 | } |
---|
1116 | function isDown() |
---|
1117 | { |
---|
1118 | return $this->down; |
---|
1119 | } |
---|
1120 | function setOffline( $offline ) |
---|
1121 | { |
---|
1122 | $this->offline = $offline; |
---|
1123 | } |
---|
1124 | function isOffline() |
---|
1125 | { |
---|
1126 | return $this->offline; |
---|
1127 | } |
---|
1128 | function setImage( $image ) |
---|
1129 | { |
---|
1130 | $this->image = $image; |
---|
1131 | } |
---|
1132 | function setCoords( $x, $y ) |
---|
1133 | { |
---|
1134 | $this->x = $x; |
---|
1135 | $this->y = $y; |
---|
1136 | } |
---|
1137 | function getX() |
---|
1138 | { |
---|
1139 | return $this->x; |
---|
1140 | } |
---|
1141 | function getY() |
---|
1142 | { |
---|
1143 | return $this->y; |
---|
1144 | } |
---|
1145 | |
---|
1146 | function getImagemapArea() |
---|
1147 | { |
---|
1148 | $area_topleft = $this->x . "," . $this->y; |
---|
1149 | $area_bottomright = ($this->x + $this->size) . "," . ($this->y + $this->size); |
---|
1150 | $area_coords = $area_topleft . "," . $area_bottomright; |
---|
1151 | |
---|
1152 | $area_href = "./?c=" . $this->clustername . "&h=" . $this->hostname; |
---|
1153 | |
---|
1154 | $area_tooltip = $this->hostname; |
---|
1155 | |
---|
1156 | if( $this->down) |
---|
1157 | { |
---|
1158 | $area_tooltip = $area_tooltip . ": DOWN"; |
---|
1159 | } |
---|
1160 | else if( $this->offline ) |
---|
1161 | { |
---|
1162 | $area_tooltip = $area_tooltip . ": OFFLINE"; |
---|
1163 | } |
---|
1164 | |
---|
1165 | $area_tooltip = $area_tooltip . ": " . implode( " ", $this->jobs ); |
---|
1166 | |
---|
1167 | $tag_href = "HREF=\"" . $area_href . "\""; |
---|
1168 | $tag_coords = "COORDS=\"" . $area_coords . "\""; |
---|
1169 | $tag_tooltip1 = "ALT=\"" . $area_tooltip . "\""; |
---|
1170 | $tag_tooltip2 = "TITLE=\"" . $area_tooltip . "\""; |
---|
1171 | |
---|
1172 | return ("<AREA SHAPE=\"RECT\" " . $tag_coords . " " . $tag_href . " " . $tag_tooltip1 . " " . $tag_tooltip2 . ">"); |
---|
1173 | } |
---|
1174 | |
---|
1175 | function colorHex( $color ) |
---|
1176 | { |
---|
1177 | $my_color = imageColorAllocate( $this->image, hexdec( substr( $color, 0, 2 )), hexdec( substr( $color, 2, 2 )), hexdec( substr( $color, 4, 2 )) ); |
---|
1178 | |
---|
1179 | return $my_color; |
---|
1180 | } |
---|
1181 | |
---|
1182 | function setLoad( $load ) |
---|
1183 | { |
---|
1184 | $this->load = $load; |
---|
1185 | } |
---|
1186 | |
---|
1187 | function setHostname( $hostname ) |
---|
1188 | { |
---|
1189 | $this->hostname = $hostname; |
---|
1190 | } |
---|
1191 | |
---|
1192 | function getHostname() |
---|
1193 | { |
---|
1194 | return $this->hostname; |
---|
1195 | } |
---|
1196 | |
---|
1197 | function getJobs() |
---|
1198 | { |
---|
1199 | return $this->jobs; |
---|
1200 | } |
---|
1201 | |
---|
1202 | function setShowinfo( $showinfo ) |
---|
1203 | { |
---|
1204 | $this->showinfo = $showinfo; |
---|
1205 | } |
---|
1206 | |
---|
1207 | function drawSmall() |
---|
1208 | { |
---|
1209 | global $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
1210 | |
---|
1211 | $this->size = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
1212 | |
---|
1213 | $this->draw(); |
---|
1214 | } |
---|
1215 | |
---|
1216 | function drawBig() |
---|
1217 | { |
---|
1218 | global $BIG_CLUSTERIMAGE_NODEWIDTH; |
---|
1219 | |
---|
1220 | $this->size = $BIG_CLUSTERIMAGE_NODEWIDTH; |
---|
1221 | |
---|
1222 | $this->draw(); |
---|
1223 | } |
---|
1224 | |
---|
1225 | function draw() |
---|
1226 | { |
---|
1227 | global $JOB_NODE_MARKING, $NODE_DOWN_MARKING, $NODE_OFFLINE_MARKING; |
---|
1228 | |
---|
1229 | $black_color = imageColorAllocate( $this->image, 0, 0, 0 ); |
---|
1230 | $size = $this->size; |
---|
1231 | |
---|
1232 | imageFilledRectangle( $this->image, $this->x, $this->y, $this->x+($size), $this->y+($size), $black_color ); |
---|
1233 | |
---|
1234 | if( $this->showinfo) |
---|
1235 | { |
---|
1236 | $this->load = $this->determineLoad(); |
---|
1237 | |
---|
1238 | if( !isset( $this->image ) or !isset( $this->x ) or !isset( $this->y ) ) |
---|
1239 | { |
---|
1240 | printf( "aborting\n" ); |
---|
1241 | printf( "x %d y %d load %f\n", $this->x, $this->y, $load ); |
---|
1242 | return; |
---|
1243 | } |
---|
1244 | |
---|
1245 | // Convert Ganglias Hexadecimal load color to a Decimal one |
---|
1246 | // |
---|
1247 | $load = $this->determineLoad(); |
---|
1248 | $usecolor = $this->colorHex( load_color($load) ); |
---|
1249 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor ); |
---|
1250 | if( $this->down ) |
---|
1251 | { |
---|
1252 | imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_DOWN_MARKING, $black_color ); |
---|
1253 | } |
---|
1254 | else if( $this->offline ) |
---|
1255 | { |
---|
1256 | imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $NODE_OFFLINE_MARKING, $black_color ); |
---|
1257 | } |
---|
1258 | else if( count( $this->jobs ) > 0 ) |
---|
1259 | { |
---|
1260 | imageString( $this->image, 1, $this->x+(($size/2)-1), $this->y+(($size/2)-4), $JOB_NODE_MARKING, $black_color ); |
---|
1261 | } |
---|
1262 | } |
---|
1263 | else |
---|
1264 | { |
---|
1265 | // White |
---|
1266 | $usecolor = imageColorAllocate( $this->image, 255, 255, 255 ); |
---|
1267 | imageFilledRectangle( $this->image, $this->x+1, $this->y+1, $this->x+($size-1), $this->y+($size-1), $usecolor ); |
---|
1268 | } |
---|
1269 | } |
---|
1270 | |
---|
1271 | function determineCpus() |
---|
1272 | { |
---|
1273 | global $metrics; |
---|
1274 | |
---|
1275 | $cpus = $metrics[$this->hostname]['cpu_num']['VAL']; |
---|
1276 | |
---|
1277 | if (!$cpus) |
---|
1278 | { |
---|
1279 | $cpus=1; |
---|
1280 | } |
---|
1281 | |
---|
1282 | return $cpus; |
---|
1283 | } |
---|
1284 | |
---|
1285 | function determineLoad() |
---|
1286 | { |
---|
1287 | global $metrics; |
---|
1288 | |
---|
1289 | $load_one = $metrics[$this->hostname]['load_one']['VAL']; |
---|
1290 | $load = ((float) $load_one)/$this->cpus; |
---|
1291 | |
---|
1292 | return $load; |
---|
1293 | } |
---|
1294 | } |
---|
1295 | |
---|
1296 | class ClusterImage |
---|
1297 | { |
---|
1298 | var $dataget, $image, $clustername; |
---|
1299 | var $filtername, $filters; |
---|
1300 | |
---|
1301 | function ClusterImage( $data, $clustername ) |
---|
1302 | { |
---|
1303 | $this->dataget = new DataGatherer( $clustername ); |
---|
1304 | $this->data = $data; |
---|
1305 | $this->clustername = $clustername; |
---|
1306 | $this->filters = array(); |
---|
1307 | $this->size = 's'; |
---|
1308 | $this->width = 0; |
---|
1309 | $this->height = 0; |
---|
1310 | $this->output = 1; |
---|
1311 | } |
---|
1312 | |
---|
1313 | function getWidth() |
---|
1314 | { |
---|
1315 | return $this->width; |
---|
1316 | } |
---|
1317 | function getHeight() |
---|
1318 | { |
---|
1319 | return $this->height; |
---|
1320 | } |
---|
1321 | function setSmall() |
---|
1322 | { |
---|
1323 | $this->size = 's'; |
---|
1324 | } |
---|
1325 | function setBig() |
---|
1326 | { |
---|
1327 | $this->size = 'b'; |
---|
1328 | } |
---|
1329 | function setNoimage() |
---|
1330 | { |
---|
1331 | $this->output = 0; |
---|
1332 | } |
---|
1333 | function isSmall() |
---|
1334 | { |
---|
1335 | return ($this->size == 's'); |
---|
1336 | } |
---|
1337 | function isBig() |
---|
1338 | { |
---|
1339 | return ($this->size == 'b'); |
---|
1340 | } |
---|
1341 | function setFilter( $filtername, $filtervalue ) |
---|
1342 | { |
---|
1343 | $this->filters[$filtername] = $filtervalue; |
---|
1344 | } |
---|
1345 | |
---|
1346 | function filterNodes( $jobs, $nodes ) |
---|
1347 | { |
---|
1348 | $filtered_nodes = array(); |
---|
1349 | |
---|
1350 | foreach( $nodes as $node ) |
---|
1351 | { |
---|
1352 | $hostname = $node->getHostname(); |
---|
1353 | |
---|
1354 | $addhost = 1; |
---|
1355 | |
---|
1356 | if( count( $this->filters ) > 0 ) |
---|
1357 | { |
---|
1358 | $mynjobs = $node->getJobs(); |
---|
1359 | |
---|
1360 | if( count( $mynjobs ) > 0 ) |
---|
1361 | { |
---|
1362 | foreach( $mynjobs as $myjob ) |
---|
1363 | { |
---|
1364 | foreach( $this->filters as $filtername => $filtervalue ) |
---|
1365 | { |
---|
1366 | if( $filtername!=null && $filtername!='' ) |
---|
1367 | { |
---|
1368 | if( $filtername == 'jobid' && !$node->hasJob( $filtervalue) ) |
---|
1369 | { |
---|
1370 | $addhost = 0; |
---|
1371 | } |
---|
1372 | else if( $filtername != 'jobid' ) |
---|
1373 | { |
---|
1374 | if( $jobs[$myjob][$filtername] != $filtervalue ) |
---|
1375 | { |
---|
1376 | $addhost = 0; |
---|
1377 | } |
---|
1378 | } |
---|
1379 | } |
---|
1380 | } |
---|
1381 | } |
---|
1382 | } |
---|
1383 | else |
---|
1384 | { |
---|
1385 | $addhost = 0; |
---|
1386 | } |
---|
1387 | } |
---|
1388 | |
---|
1389 | if( $addhost ) |
---|
1390 | { |
---|
1391 | $filtered_nodes[] = $hostname; |
---|
1392 | } |
---|
1393 | } |
---|
1394 | |
---|
1395 | return $filtered_nodes; |
---|
1396 | } |
---|
1397 | |
---|
1398 | function draw() |
---|
1399 | { |
---|
1400 | global $SMALL_CLUSTERIMAGE_MAXWIDTH, $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
1401 | global $BIG_CLUSTERIMAGE_MAXWIDTH, $BIG_CLUSTERIMAGE_NODEWIDTH; |
---|
1402 | global $CLUSTER_CONFS, $confcluster, $SHOW_EMPTY_COLUMN, $SHOW_EMPTY_ROW; |
---|
1403 | |
---|
1404 | global $SORTBY_HOSTNAME, $SORT_ORDER, $skan_str; |
---|
1405 | global $x_first, $y_first; |
---|
1406 | |
---|
1407 | foreach( $CLUSTER_CONFS as $confcluster => $conffile ) |
---|
1408 | { |
---|
1409 | if( strtolower( trim($this->clustername) ) == strtolower(trim($confcluster)) ) |
---|
1410 | { |
---|
1411 | include_once $conffile; |
---|
1412 | } |
---|
1413 | } |
---|
1414 | |
---|
1415 | $mydatag = $this->dataget; |
---|
1416 | $mydatag->parseXML( $this->data ); |
---|
1417 | |
---|
1418 | if( $this->isSmall() ) |
---|
1419 | { |
---|
1420 | $max_width = $SMALL_CLUSTERIMAGE_MAXWIDTH; |
---|
1421 | $node_width = $SMALL_CLUSTERIMAGE_NODEWIDTH; |
---|
1422 | } |
---|
1423 | else if( $this->isBig() ) |
---|
1424 | { |
---|
1425 | $max_width = $BIG_CLUSTERIMAGE_MAXWIDTH; |
---|
1426 | $node_width = $BIG_CLUSTERIMAGE_NODEWIDTH; |
---|
1427 | } |
---|
1428 | |
---|
1429 | $nodes = $mydatag->getNodes(); |
---|
1430 | $nodes_hosts = array_keys( $nodes ); |
---|
1431 | |
---|
1432 | $nodes_nr = count( $nodes ); |
---|
1433 | |
---|
1434 | $nodes_size = $nodes_nr*$node_width; |
---|
1435 | $node_rows = 0; |
---|
1436 | |
---|
1437 | if( $nodes_size > $max_width ) |
---|
1438 | { |
---|
1439 | $nodes_per_row = ( (int) ($max_width/$node_width) ); |
---|
1440 | } |
---|
1441 | else |
---|
1442 | { |
---|
1443 | $nodes_per_row = $nodes_size; |
---|
1444 | $node_rows = 1; |
---|
1445 | } |
---|
1446 | |
---|
1447 | if( $nodes_per_row < $nodes_nr ) |
---|
1448 | { |
---|
1449 | $node_rows = ( (int) ($nodes_nr/$nodes_per_row) ); |
---|
1450 | $node_rest = fmod( $nodes_nr, $nodes_per_row ); |
---|
1451 | |
---|
1452 | if( $node_rest > 0 ) |
---|
1453 | { |
---|
1454 | $node_rows++; |
---|
1455 | } |
---|
1456 | } |
---|
1457 | |
---|
1458 | $y_offset = 0; |
---|
1459 | $font = 2; |
---|
1460 | $fontwidth = ImageFontWidth( $font ); |
---|
1461 | $fontheight = ImageFontHeight( $font ); |
---|
1462 | $fontspaceing = 2; |
---|
1463 | $y_offset = $fontheight + (2 * $fontspaceing); |
---|
1464 | |
---|
1465 | $this->width = $max_width; |
---|
1466 | $this->height = ($y_offset + (($node_rows*$node_width)+1) ); |
---|
1467 | |
---|
1468 | $jobs = $mydatag->getJobs(); |
---|
1469 | $filtered_nodes = $this->filterNodes( $jobs, $nodes ); |
---|
1470 | |
---|
1471 | if( $SORTBY_HOSTNAME != "" ) |
---|
1472 | { |
---|
1473 | $sorted = array(); |
---|
1474 | |
---|
1475 | $x_first = 0; |
---|
1476 | $y_first = 0; |
---|
1477 | |
---|
1478 | $skan_str = $SORTBY_HOSTNAME; |
---|
1479 | |
---|
1480 | global $x_present, $y_present; |
---|
1481 | $x_present = false; |
---|
1482 | $y_present = false; |
---|
1483 | |
---|
1484 | // Should we scan by X, Y or both |
---|
1485 | // |
---|
1486 | if(strpos( $SORTBY_HOSTNAME, "{x}" ) != false ) |
---|
1487 | { |
---|
1488 | $x_str = "{x}"; |
---|
1489 | $x_present = true; |
---|
1490 | } |
---|
1491 | else if(strpos( $SORTBY_HOSTNAME, "{X}" ) != false ) |
---|
1492 | { |
---|
1493 | $x_str = "{X}"; |
---|
1494 | $x_present = true; |
---|
1495 | } |
---|
1496 | if(strpos( $SORTBY_HOSTNAME, "{y}" ) != false ) |
---|
1497 | { |
---|
1498 | $y_str = "{y}"; |
---|
1499 | $y_present = true; |
---|
1500 | } |
---|
1501 | else if(strpos( $SORTBY_HOSTNAME, "{Y}" ) != false ) |
---|
1502 | { |
---|
1503 | $y_str = "{Y}"; |
---|
1504 | $y_present = true; |
---|
1505 | } |
---|
1506 | |
---|
1507 | // If we should scan for both X and Y: see which one is first |
---|
1508 | // |
---|
1509 | if(( strpos( $SORTBY_HOSTNAME, $x_str ) < strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present )) |
---|
1510 | { |
---|
1511 | $x_first = 1; |
---|
1512 | } |
---|
1513 | else if(( strpos( $SORTBY_HOSTNAME, $x_str ) > strpos( $SORTBY_HOSTNAME, $y_str ) ) && ( $x_present && $y_present )) |
---|
1514 | { |
---|
1515 | $y_first = 1; |
---|
1516 | |
---|
1517 | } |
---|
1518 | else if( $x_present ) |
---|
1519 | { |
---|
1520 | $x_first = 1; |
---|
1521 | } |
---|
1522 | else if( $y_present ) |
---|
1523 | { |
---|
1524 | $y_first = 1; |
---|
1525 | } |
---|
1526 | |
---|
1527 | // Now replace our {x} and {y} with %d for sscanf parsing |
---|
1528 | // |
---|
1529 | if(( $x_first ) && ( $x_present && $y_present ) ) |
---|
1530 | { |
---|
1531 | $skan_str = str_replace( $x_str, "%d", $skan_str ); |
---|
1532 | $skan_str = str_replace( $y_str, "%d", $skan_str ); |
---|
1533 | } |
---|
1534 | else if( $x_present) |
---|
1535 | { |
---|
1536 | $skan_str = str_replace( $x_str, "%d", $skan_str ); |
---|
1537 | } |
---|
1538 | else if( $y_present) |
---|
1539 | { |
---|
1540 | $skan_str = str_replace( $y_str, "%d", $skan_str ); |
---|
1541 | } |
---|
1542 | |
---|
1543 | $x_min = null; |
---|
1544 | $x_max = null; |
---|
1545 | $y_min = null; |
---|
1546 | $y_max = null; |
---|
1547 | |
---|
1548 | $x_columns = array(); |
---|
1549 | $y_rows = array(); |
---|
1550 | |
---|
1551 | // Now let's walk through all our nodes and see which one are valid for our scan pattern |
---|
1552 | // |
---|
1553 | foreach( $nodes as $hostname => $node ) |
---|
1554 | { |
---|
1555 | $x = null; |
---|
1556 | $y = null; |
---|
1557 | |
---|
1558 | if( $x_present && $y_present ) |
---|
1559 | { |
---|
1560 | if( $x_first ) |
---|
1561 | { |
---|
1562 | $n = sscanf( $hostname, $skan_str, $x, $y ); |
---|
1563 | } |
---|
1564 | else if( $y_first ) |
---|
1565 | { |
---|
1566 | $n = sscanf( $hostname, $skan_str, $y, $x ); |
---|
1567 | } |
---|
1568 | |
---|
1569 | // Remove nodes that don't match |
---|
1570 | // |
---|
1571 | if( $n < 2 ) |
---|
1572 | { |
---|
1573 | // This node hostname has no match for: {x} and {y} |
---|
1574 | // |
---|
1575 | unset( $nodes[$hostname] ); |
---|
1576 | } |
---|
1577 | } |
---|
1578 | else if( $x_present && !$y_present ) |
---|
1579 | { |
---|
1580 | $n = sscanf( $hostname, $skan_str, $x ); |
---|
1581 | |
---|
1582 | // Remove nodes that don't match |
---|
1583 | // |
---|
1584 | if( $n < 1 ) |
---|
1585 | { |
---|
1586 | // This node hostname has no match for: {x} |
---|
1587 | // |
---|
1588 | unset( $nodes[$hostname] ); |
---|
1589 | } |
---|
1590 | $y = 1; |
---|
1591 | } |
---|
1592 | else if( $y_present && !$x_present ) |
---|
1593 | { |
---|
1594 | $n = sscanf( $hostname, $skan_str, $y ); |
---|
1595 | |
---|
1596 | // Remove nodes that don't match |
---|
1597 | // |
---|
1598 | if( $n < 1 ) |
---|
1599 | { |
---|
1600 | // This node hostname has no match for: {y} |
---|
1601 | // |
---|
1602 | unset( $nodes[$hostname] ); |
---|
1603 | } |
---|
1604 | $x = 1; |
---|
1605 | } |
---|
1606 | |
---|
1607 | // Determine the lowest value of {x} that exists in all node hostnames |
---|
1608 | // |
---|
1609 | if( !$x_min && $x != null ) |
---|
1610 | { |
---|
1611 | $x_min = $x; |
---|
1612 | } |
---|
1613 | else if( $x < $x_min && $x != null ) |
---|
1614 | { |
---|
1615 | $x_min = $x; |
---|
1616 | } |
---|
1617 | |
---|
1618 | // Determine the highest value of {x} that exists in all node hostnames |
---|
1619 | // |
---|
1620 | if( !$x_max && $x != null ) |
---|
1621 | { |
---|
1622 | $x_max = $x; |
---|
1623 | } |
---|
1624 | else if( $x > $x_max && $x != null ) |
---|
1625 | { |
---|
1626 | $x_max = $x; |
---|
1627 | } |
---|
1628 | |
---|
1629 | // Determine the lowest value of {y} that exists in all node hostnames |
---|
1630 | // |
---|
1631 | if( !$y_min && $y != null ) |
---|
1632 | { |
---|
1633 | $y_min = $y; |
---|
1634 | } |
---|
1635 | else if( $y < $y_min && $y != null ) |
---|
1636 | { |
---|
1637 | $y_min = $y; |
---|
1638 | } |
---|
1639 | |
---|
1640 | // Determine the highest value of {y} that exists in all node hostnames |
---|
1641 | // |
---|
1642 | if( !$y_max && $y != null ) |
---|
1643 | { |
---|
1644 | $y_max = $y; |
---|
1645 | } |
---|
1646 | else if( $y > $y_max && $y != null ) |
---|
1647 | { |
---|
1648 | $y_max = $y; |
---|
1649 | } |
---|
1650 | |
---|
1651 | // Store which non-empty columns and rows we found |
---|
1652 | // |
---|
1653 | if( !in_array( $x, $x_columns ) ) |
---|
1654 | { |
---|
1655 | $x_columns[] = $x; |
---|
1656 | } |
---|
1657 | if( !in_array( $y, $y_rows ) ) |
---|
1658 | { |
---|
1659 | $y_rows[] = $y; |
---|
1660 | } |
---|
1661 | } |
---|
1662 | |
---|
1663 | // Sort all the nodes (alpha and numerically) |
---|
1664 | // 1: gb-r1n1, 2: gb-r1n2, 3: gb-r2n1, etc |
---|
1665 | // |
---|
1666 | $sorted_nodes = usort( $nodes, "cmp" ); |
---|
1667 | |
---|
1668 | //print_r( $x_columns ) ; |
---|
1669 | |
---|
1670 | $cur_node = 0; |
---|
1671 | |
---|
1672 | $x_offset = 0; |
---|
1673 | $y_offset = 0; |
---|
1674 | $font = 2; |
---|
1675 | $fontwidth = ImageFontWidth( $font ); |
---|
1676 | $fontheight = ImageFontHeight( $font ); |
---|
1677 | $fontspaceing = 2; |
---|
1678 | |
---|
1679 | if( $this->isSmall() ) |
---|
1680 | { |
---|
1681 | $y_offset = $y_offset + (2 * $fontspaceing) + $fontheight; |
---|
1682 | } |
---|
1683 | |
---|
1684 | if( $this->isBig() ) |
---|
1685 | { |
---|
1686 | $y_offset = ($fontheight * (1 + strlen( $x_max) ) ) + ((2 + strlen( $x_max)) * $fontspaceing); |
---|
1687 | $x_offset = ($fontwidth * (1 + strlen( $y_max) ) ) + ((2 + strlen( $y_max)) * $fontspaceing); |
---|
1688 | } |
---|
1689 | |
---|
1690 | $image_width = $x_offset + ($node_width * ($x_max-$x_min+2)); |
---|
1691 | |
---|
1692 | if( $this->isSmall() ) |
---|
1693 | { |
---|
1694 | $image_width = $max_width; |
---|
1695 | } |
---|
1696 | else if( $this->isBig() ) |
---|
1697 | { |
---|
1698 | $image_width = ($image_width < $max_width) ? $image_width : $max_width; |
---|
1699 | } |
---|
1700 | $image_height = $y_offset + ($node_width * ($y_max-$y_min+2)); |
---|
1701 | |
---|
1702 | $this->width = $image_width; |
---|
1703 | $this->heigth = $image_heigth; |
---|
1704 | |
---|
1705 | $image = imageCreateTrueColor( $image_width, $image_height ); |
---|
1706 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
1707 | |
---|
1708 | imageFill( $image, 0, 0, $colorwhite ); |
---|
1709 | |
---|
1710 | if( $this->isSmall() ) |
---|
1711 | { |
---|
1712 | // Draw a fancy little header text to explain what it is |
---|
1713 | // |
---|
1714 | $colorblue = imageColorAllocate( $image, 0, 0, 255 ); |
---|
1715 | |
---|
1716 | imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue ); |
---|
1717 | } |
---|
1718 | |
---|
1719 | if( $this->isBig() && ( isset( $SORT_XLABEL ) || isset( $SORT_YLABEL ) ) ) |
---|
1720 | { |
---|
1721 | $colorblue = imageColorAllocate( $image, 0, 0, 255 ); |
---|
1722 | |
---|
1723 | if( isset( $SORT_XLABEL ) ) |
---|
1724 | { |
---|
1725 | // Print the {x} label: rack |
---|
1726 | // |
---|
1727 | imageString( $image, $font, $x_offset, $fontspaceing, $SORT_XLABEL, $colorblue ); |
---|
1728 | } |
---|
1729 | |
---|
1730 | if( isset( $SORT_YLABEL ) ) |
---|
1731 | { |
---|
1732 | // Stupid php without imageStringDown function... we'll make one ourself |
---|
1733 | // |
---|
1734 | |
---|
1735 | // Print the {y} label: node |
---|
1736 | // |
---|
1737 | imageStringDown( $image, $font, $fontspaceing, $y_offset, $SORT_YLABEL, $colorblue ); |
---|
1738 | } |
---|
1739 | } |
---|
1740 | |
---|
1741 | $previous_n = 0; |
---|
1742 | $previous_m = 0; |
---|
1743 | $x_empty_count = 0; |
---|
1744 | $y_empty_count = 0; |
---|
1745 | |
---|
1746 | // Let's start assigning x,y coordinates now |
---|
1747 | // |
---|
1748 | for( $n = $x_min; $n <= $x_max; $n++ ) |
---|
1749 | { |
---|
1750 | for( $m = $y_min; $m <= $y_max; $m++ ) |
---|
1751 | { |
---|
1752 | if( $x_min > 0 ) |
---|
1753 | { |
---|
1754 | $x = $x_offset + ( ($n-$x_min) * $node_width ) - ($x_empty_count * $node_width); |
---|
1755 | } |
---|
1756 | if( $y_min > 0 ) |
---|
1757 | { |
---|
1758 | $y = $y_offset + ( ($m-$y_min) * $node_width ) - ($y_empty_count * $node_width); |
---|
1759 | } |
---|
1760 | |
---|
1761 | // Don't show empty rows/columns if option enabled |
---|
1762 | // |
---|
1763 | if( !in_array( $n, $x_columns ) && !$SHOW_EMPTY_COLUMN ) |
---|
1764 | { |
---|
1765 | // Skip to next iteration: we don't want a empty column |
---|
1766 | // |
---|
1767 | if( $n > $previous_n ) |
---|
1768 | { |
---|
1769 | $previous_n = $n; |
---|
1770 | $x_empty_count++; |
---|
1771 | } |
---|
1772 | continue; |
---|
1773 | } |
---|
1774 | if( !in_array( $m, $y_rows ) && !$SHOW_EMPTY_ROW ) |
---|
1775 | |
---|
1776 | { |
---|
1777 | // Skip to next iteration: we don't want a empty column |
---|
1778 | // |
---|
1779 | if( $m > $previous_m ) |
---|
1780 | { |
---|
1781 | $previous_m = $m; |
---|
1782 | $y_empty_count++; |
---|
1783 | } |
---|
1784 | continue; |
---|
1785 | } |
---|
1786 | |
---|
1787 | if( $this->isBig() ) |
---|
1788 | { |
---|
1789 | // Draw y(node) column number header |
---|
1790 | // |
---|
1791 | if(( $n == $x_min ) && ( isset($SORT_YLABEL) ) ) |
---|
1792 | { |
---|
1793 | $mfontspacing = 1; |
---|
1794 | |
---|
1795 | $ylabel_x = $x - ( $fontwidth * strlen( $y_max ) ) - $mfontspacing; |
---|
1796 | $ylabel_y = $y; |
---|
1797 | |
---|
1798 | imageString( $image, $font, $ylabel_x, $ylabel_y, strval( $m ), $colorblue ); |
---|
1799 | |
---|
1800 | $xmin_hit[$n] = true; |
---|
1801 | } |
---|
1802 | |
---|
1803 | // Draw x(rack) column number header |
---|
1804 | // |
---|
1805 | if(( $m == $y_min ) && ( isset($SORT_XLABEL) ) ) |
---|
1806 | { |
---|
1807 | $mfontspacing = 2; |
---|
1808 | $xlabel_y = $y - ( $fontheight * strlen( $x_max ) ); |
---|
1809 | $xlabel_x = $x + $mfontspacing; |
---|
1810 | |
---|
1811 | imageStringDown( $image, $font, $xlabel_x, $xlabel_y, strval( $n ), $colorblue ); |
---|
1812 | } |
---|
1813 | } |
---|
1814 | |
---|
1815 | if( isset( $nodes[$cur_node] ) ) |
---|
1816 | { |
---|
1817 | $host = $nodes[$cur_node]->getHostname(); |
---|
1818 | |
---|
1819 | if( $x_present && $y_present ) |
---|
1820 | { |
---|
1821 | if( $x_first ) |
---|
1822 | { |
---|
1823 | $nn = sscanf( $host, $skan_str, $rx, $ry ); |
---|
1824 | } |
---|
1825 | else if( $y_first ) |
---|
1826 | { |
---|
1827 | $nn = sscanf( $host, $skan_str, $ry, $rx ); |
---|
1828 | } |
---|
1829 | if ( $nn < 2 ) |
---|
1830 | { |
---|
1831 | //printf( "skipping node %s - y present & x present + <2 x,y matchs\n", $host); |
---|
1832 | continue; |
---|
1833 | } |
---|
1834 | if( intval( $rx ) > $n ) |
---|
1835 | { |
---|
1836 | // If x(rack) is higher than current x, skip to next x(rack) |
---|
1837 | // |
---|
1838 | $m = $y_max + 1; |
---|
1839 | |
---|
1840 | continue; |
---|
1841 | } |
---|
1842 | if( intval( $ry ) > $m ) |
---|
1843 | { |
---|
1844 | // If y(node) is higher than current y, skip to next y(node) |
---|
1845 | // |
---|
1846 | continue; |
---|
1847 | } |
---|
1848 | } |
---|
1849 | else if( $x_present ) |
---|
1850 | { |
---|
1851 | $nn = sscanf( $host, $skan_str, $rx ); |
---|
1852 | } |
---|
1853 | else if( $y_present ) |
---|
1854 | { |
---|
1855 | $nn = sscanf( $host, $skan_str, $ry ); |
---|
1856 | } |
---|
1857 | |
---|
1858 | if( !in_array( $host, $filtered_nodes ) ) |
---|
1859 | { |
---|
1860 | // This node has been filtered out: we only want to see certain nodes |
---|
1861 | // |
---|
1862 | $nodes[$cur_node]->setShowinfo( 0 ); |
---|
1863 | } |
---|
1864 | |
---|
1865 | $nodes[$cur_node]->setCoords( $x, $y ); |
---|
1866 | $nodes[$cur_node]->setImage( $image ); |
---|
1867 | |
---|
1868 | if( $this->isSmall() ) |
---|
1869 | { |
---|
1870 | $nodes[$cur_node]->drawSmall(); |
---|
1871 | } |
---|
1872 | else if( $this->isBig() ) |
---|
1873 | { |
---|
1874 | $nodes[$cur_node]->drawBig(); |
---|
1875 | } |
---|
1876 | |
---|
1877 | $cur_node++; |
---|
1878 | } |
---|
1879 | } |
---|
1880 | } |
---|
1881 | |
---|
1882 | } |
---|
1883 | else |
---|
1884 | { |
---|
1885 | if( $this->isSmall() ) |
---|
1886 | { |
---|
1887 | $image = imageCreateTrueColor( $max_width, ($y_offset + (($node_rows*$node_width)+1) ) ); |
---|
1888 | } |
---|
1889 | else if( $this->isBig() ) |
---|
1890 | { |
---|
1891 | $image_width = ($node_width * $nodes_nr) + 2; |
---|
1892 | $image_width = ($image_width < $max_width) ? $image_width : $max_width; |
---|
1893 | $image = imageCreateTrueColor( $image_width, ($y_offset + (($node_rows*$node_width)+1) ) ); |
---|
1894 | } |
---|
1895 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
1896 | |
---|
1897 | imageFill( $image, 0, 0, $colorwhite ); |
---|
1898 | |
---|
1899 | if( $this->isSmall() ) |
---|
1900 | { |
---|
1901 | $colorblue = imageColorAllocate( $image, 0, 0, 255 ); |
---|
1902 | |
---|
1903 | imageString( $image, $font, 2, 2, "Monarch Joblist - cluster: ".$this->clustername, $colorblue ); |
---|
1904 | } |
---|
1905 | |
---|
1906 | for( $n = 0; $n < $node_rows; $n++ ) |
---|
1907 | { |
---|
1908 | for( $m = 0; $m < $nodes_per_row; $m++ ) |
---|
1909 | { |
---|
1910 | $x = ($m * $node_width); |
---|
1911 | $y = $y_offset + ($n * $node_width); |
---|
1912 | |
---|
1913 | $cur_node = ($n * $nodes_per_row) + ($m); |
---|
1914 | $host = isset( $nodes_hosts[$cur_node] ) ? $nodes_hosts[$cur_node] : ''; |
---|
1915 | |
---|
1916 | if( isset( $nodes[$host] ) ) |
---|
1917 | { |
---|
1918 | $nodes[$host]->setCoords( $x, $y ); |
---|
1919 | $nodes[$host]->setImage( $image ); |
---|
1920 | |
---|
1921 | if( !in_array( $host, $filtered_nodes ) ) |
---|
1922 | { |
---|
1923 | $nodes[$host]->setShowinfo( 0 ); |
---|
1924 | } |
---|
1925 | |
---|
1926 | if( $this->isSmall() ) |
---|
1927 | { |
---|
1928 | $nodes[$host]->drawSmall(); |
---|
1929 | } |
---|
1930 | else if( $this->isBig() ) |
---|
1931 | { |
---|
1932 | $nodes[$host]->drawBig(); |
---|
1933 | } |
---|
1934 | } |
---|
1935 | } |
---|
1936 | } |
---|
1937 | } |
---|
1938 | |
---|
1939 | $this->nodes = &$nodes; |
---|
1940 | |
---|
1941 | if ($this->output) |
---|
1942 | { |
---|
1943 | header( 'Content-type: image/png' ); |
---|
1944 | imagePNG( $image ); |
---|
1945 | imageDestroy( $image ); |
---|
1946 | } |
---|
1947 | } |
---|
1948 | |
---|
1949 | function getImagemapArea() |
---|
1950 | { |
---|
1951 | $clusterimage_map = ""; |
---|
1952 | |
---|
1953 | foreach( $this->nodes as $hostname => $node ) |
---|
1954 | { |
---|
1955 | $node_map = $node->getImagemapArea(); |
---|
1956 | $clusterimage_map .= $node_map; |
---|
1957 | } |
---|
1958 | |
---|
1959 | return $clusterimage_map; |
---|
1960 | } |
---|
1961 | } |
---|
1962 | |
---|
1963 | class EmptyImage |
---|
1964 | { |
---|
1965 | function draw() |
---|
1966 | { |
---|
1967 | $image = imageCreateTrueColor( 1, 1 ); |
---|
1968 | $colorwhite = imageColorAllocate( $image, 255, 255, 255 ); |
---|
1969 | imageFill( $image, 0, 0, $colorwhite ); |
---|
1970 | |
---|
1971 | header( 'Content-type: image/png' ); |
---|
1972 | imagePNG( $image ); |
---|
1973 | imageDestroy( $image ); |
---|
1974 | } |
---|
1975 | } |
---|
1976 | |
---|
1977 | class HostImage |
---|
1978 | { |
---|
1979 | var $data_gather, $cluster, $host, $node, $image; |
---|
1980 | var $headerstrlen; |
---|
1981 | |
---|
1982 | function HostImage( $data_gather, $cluster, $host ) |
---|
1983 | { |
---|
1984 | $this->data_gather = $data_gather; |
---|
1985 | $this->cluster = $cluster; |
---|
1986 | $this->host = $host; |
---|
1987 | $this->y_offset = 0; |
---|
1988 | $this->font = 2; |
---|
1989 | $this->fontspaceing = 2; |
---|
1990 | $this->headerstrlen = array(); |
---|
1991 | |
---|
1992 | $this->fontheight = ImageFontHeight( $this->font ); |
---|
1993 | $this->fontwidth = ImageFontWidth( $this->font ); |
---|
1994 | |
---|
1995 | $dg = &$this->data_gather; |
---|
1996 | $this->node = &$dg->getNode( $this->host ); |
---|
1997 | $n = &$this->node; |
---|
1998 | $this->njobs = $n->getJobs(); |
---|
1999 | } |
---|
2000 | |
---|
2001 | function drawJobs() |
---|
2002 | { |
---|
2003 | $dg = &$this->data_gather; |
---|
2004 | $colorblack = imageColorAllocate( $this->image, 0, 0, 0 ); |
---|
2005 | |
---|
2006 | for( $n = 0; $n < count( $this->njobs ); $n++ ) |
---|
2007 | { |
---|
2008 | $jobid = $this->njobs[$n]; |
---|
2009 | $jobinfo = $dg->getJob( $jobid ); |
---|
2010 | |
---|
2011 | $xoffset = 5; |
---|
2012 | imageString( $this->image, $this->font, $xoffset, $this->y_offset, strval( $jobid ), $colorblack ); |
---|
2013 | |
---|
2014 | foreach( $this->headerstrlen as $headername => $headerlen ) |
---|
2015 | { |
---|
2016 | if( $headername == 'nodes' ) |
---|
2017 | { |
---|
2018 | $attrval = strval( count( $jobinfo['nodes'] ) ); |
---|
2019 | } |
---|
2020 | else if( $headername == 'cpus' ) |
---|
2021 | { |
---|
2022 | if( !isset( $jobinfo['ppn'] ) ) |
---|
2023 | { |
---|
2024 | $jobinfo['ppn'] = 1; |
---|
2025 | } |
---|
2026 | |
---|
2027 | $attrval = strval( count( $jobinfo['nodes'] ) * intval( $jobinfo['ppn'] ) ); |
---|
2028 | } |
---|
2029 | else if( $headername == 'runningtime' ) |
---|
2030 | { |
---|
2031 | $attrval = makeTime( intval( $jobinfo['reported'] ) - intval( $jobinfo['start_timestamp'] ) ); |
---|
2032 | } |
---|
2033 | else |
---|
2034 | { |
---|
2035 | $attrval = strval( $jobinfo[$headername] ); |
---|
2036 | } |
---|
2037 | |
---|
2038 | imageString( $this->image, $this->font, $xoffset, $this->y_offset, $attrval, $colorblack ); |
---|
2039 | |
---|
2040 | $xoffset = $xoffset + ($this->fontwidth * ( $headerlen + 1 ) ); |
---|
2041 | } |
---|
2042 | |
---|
2043 | $this->newLineOffset(); |
---|
2044 | } |
---|
2045 | } |
---|
2046 | |
---|
2047 | function drawHeader() |
---|
2048 | { |
---|
2049 | $dg = &$this->data_gather; |
---|
2050 | |
---|
2051 | for( $n = 0; $n < count( $this->njobs ); $n++ ) |
---|
2052 | { |
---|
2053 | $jobid = $this->njobs[$n]; |
---|
2054 | $jobinfo = $dg->getJob( $jobid ); |
---|
2055 | |
---|
2056 | if( !isset( $this->headerstrlen['id'] ) ) |
---|
2057 | { |
---|
2058 | $this->headerstrlen['id'] = strlen( strval( $jobid ) ); |
---|
2059 | } |
---|
2060 | else if( strlen( strval( $jobid ) ) > $this->headerstrlen['id'] ) |
---|
2061 | { |
---|
2062 | $this->headerstrlen['id'] = strlen( strval( $jobid ) ); |
---|
2063 | } |
---|
2064 | |
---|
2065 | if( !isset( $this->headerstrlen['owner'] ) ) |
---|
2066 | { |
---|
2067 | $this->headerstrlen['owner'] = strlen( strval( $jobinfo['owner'] ) ); |
---|
2068 | } |
---|
2069 | else if( strlen( strval( $jobinfo['owner'] ) ) > $this->headerstrlen['owner'] ) |
---|
2070 | { |
---|
2071 | $this->headerstrlen['owner'] = strlen( strval( $jobinfo['owner'] ) ); |
---|
2072 | } |
---|
2073 | |
---|
2074 | if( !isset( $this->headerstrlen['queue'] ) ) |
---|
2075 | { |
---|
2076 | $this->headerstrlen['queue'] = strlen( strval( $jobinfo['queue'] ) ); |
---|
2077 | } |
---|
2078 | else if( strlen( strval( $jobinfo['queue'] ) ) > $this->headerstrlen['queue'] ) |
---|
2079 | { |
---|
2080 | $this->headerstrlen['queue'] = strlen( strval( $jobinfo['queue'] ) ); |
---|
2081 | } |
---|
2082 | |
---|
2083 | if( !isset( $jobinfo['ppn'] ) ) |
---|
2084 | { |
---|
2085 | $jobinfo['ppn'] = 1; |
---|
2086 | } |
---|
2087 | |
---|
2088 | $cpus = count( $jobinfo['nodes'] ) * intval( $jobinfo['ppn'] ); |
---|
2089 | |
---|
2090 | if( !isset( $this->headerstrlen['cpus'] ) ) |
---|
2091 | { |
---|
2092 | $this->headerstrlen['cpus'] = strlen( strval( $cpus ) ); |
---|
2093 | } |
---|
2094 | else if( strlen( strval( $cpus ) ) > $this->headerstrlen['cpus'] ) |
---|
2095 | { |
---|
2096 | $this->headerstrlen['cpus'] = strlen( strval( $cpus ) ); |
---|
2097 | } |
---|
2098 | |
---|
2099 | $nodes = count( $jobinfo['nodes'] ); |
---|
2100 | |
---|
2101 | if( !isset( $this->headerstrlen['nodes'] ) ) |
---|
2102 | { |
---|
2103 | $this->headerstrlen['nodes'] = strlen( strval( $nodes ) ); |
---|
2104 | } |
---|
2105 | else if( strlen( strval( $nodes) ) > $this->headerstrlen['nodes'] ) |
---|
2106 | { |
---|
2107 | $this->headerstrlen['nodes'] = strlen( strval( $nodes ) ); |
---|
2108 | } |
---|
2109 | |
---|
2110 | $runningtime = makeTime( intval( $jobinfo[reported] ) - intval( $jobinfo['start_timestamp'] ) ); |
---|
2111 | |
---|
2112 | if( !isset( $this->headerstrlen['runningtime'] ) ) |
---|
2113 | { |
---|
2114 | $this->headerstrlen['runningtime'] = strlen( strval( $runningtime) ); |
---|
2115 | } |
---|
2116 | else if( strlen( strval( $runningtime) ) > $this->headerstrlen['runningtime'] ) |
---|
2117 | { |
---|
2118 | $this->headerstrlen['runningtime'] = strlen( strval( $runningtime) ); |
---|
2119 | } |
---|
2120 | |
---|
2121 | if( !isset( $this->headerstrlen['name'] ) ) |
---|
2122 | { |
---|
2123 | $this->headerstrlen['name'] = strlen( strval( $jobinfo['name'] ) ); |
---|
2124 | } |
---|
2125 | else if( strlen( strval( $jobinfo['name'] ) ) > $this->headerstrlen['name'] ) |
---|
2126 | { |
---|
2127 | $this->headerstrlen['name'] = strlen( strval( $jobinfo['name'] ) ); |
---|
2128 | } |
---|
2129 | } |
---|
2130 | |
---|
2131 | $xoffset = 5; |
---|
2132 | |
---|
2133 | foreach( $this->headerstrlen as $headername => $headerlen ) |
---|
2134 | { |
---|
2135 | $colorgreen = imageColorAllocate( $this->image, 0, 200, 0 ); |
---|
2136 | |
---|
2137 | if( $headerlen < strlen( $headername ) ) |
---|
2138 | { |
---|
2139 | $this->headerstrlen[$headername] = strlen( $headername ); |
---|
2140 | } |
---|
2141 | |
---|
2142 | imageString( $this->image, $this->font, $xoffset, $this->y_offset, ucfirst( $headername ), $colorgreen ); |
---|
2143 | |
---|
2144 | $xoffset = $xoffset + ($this->fontwidth * ( $this->headerstrlen[$headername] + 1 ) ); |
---|
2145 | } |
---|
2146 | $this->newLineOffset(); |
---|
2147 | } |
---|
2148 | |
---|
2149 | function newLineOffset() |
---|
2150 | { |
---|
2151 | $this->y_offset = $this->y_offset + $this->fontheight + $this->fontspaceing; |
---|
2152 | } |
---|
2153 | |
---|
2154 | function draw() |
---|
2155 | { |
---|
2156 | $xlen = 450; |
---|
2157 | $ylen = ( count( $this->njobs ) * ( $this->fontheight + $this->fontspaceing ) ) + (3 * $this->fontheight); |
---|
2158 | |
---|
2159 | $this->image = imageCreateTrueColor( $xlen, $ylen ); |
---|
2160 | $colorwhite = imageColorAllocate( $this->image, 255, 255, 255 ); |
---|
2161 | imageFill( $this->image, 0, 0, $colorwhite ); |
---|
2162 | |
---|
2163 | $colorblue = imageColorAllocate( $this->image, 0, 0, 255 ); |
---|
2164 | |
---|
2165 | imageString( $this->image, $this->font, 1, $this->y_offset, "Monarch Joblist - host: ".$this->host, $colorblue ); |
---|
2166 | $this->newLineOffset(); |
---|
2167 | |
---|
2168 | $this->drawHeader(); |
---|
2169 | $this->drawJobs(); |
---|
2170 | |
---|
2171 | header( 'Content-type: image/png' ); |
---|
2172 | imagePNG( $this->image ); |
---|
2173 | imageDestroy( $this->image ); |
---|
2174 | } |
---|
2175 | } |
---|
2176 | |
---|
2177 | function imageStringDown( &$image, $font, $x, $y, &$s, &$col ) |
---|
2178 | { |
---|
2179 | $fw = imagefontwidth( $font); |
---|
2180 | $fh = imagefontheight( $font); |
---|
2181 | |
---|
2182 | $fontspacing = 0; |
---|
2183 | |
---|
2184 | $fx = $x; |
---|
2185 | $fy = $y; |
---|
2186 | |
---|
2187 | for( $n=0; $n<strlen( $s ); $n++ ) |
---|
2188 | { |
---|
2189 | $myc = $s{$n}; |
---|
2190 | |
---|
2191 | imagestring( $image, $font, $fx, $fy, $myc, $col ); |
---|
2192 | |
---|
2193 | $fy += ($fontspacing + $fh ); |
---|
2194 | } |
---|
2195 | } |
---|
2196 | |
---|
2197 | function array_rem( $val, &$arr ) |
---|
2198 | { |
---|
2199 | // Delete val from arr |
---|
2200 | // |
---|
2201 | $i = array_search( $val, $arr ); |
---|
2202 | |
---|
2203 | if( $i == false ) return false; |
---|
2204 | |
---|
2205 | $arr = array_merge( array_slice( $arr, 0, $i ), array_slice( $arr, $i+1, count( $arr ) ) ); |
---|
2206 | |
---|
2207 | return true; |
---|
2208 | } |
---|
2209 | |
---|
2210 | function cmp( $a, $b ) |
---|
2211 | { |
---|
2212 | global $SORT_ORDER; |
---|
2213 | global $skan_str; |
---|
2214 | global $x_first, $y_first; |
---|
2215 | global $x_present, $y_present; |
---|
2216 | |
---|
2217 | $a_node = $a; |
---|
2218 | $b_node = $b; |
---|
2219 | $a = $a_node->getHostname(); |
---|
2220 | $b = $b_node->getHostname(); |
---|
2221 | |
---|
2222 | if( $a == $b ) return 0; |
---|
2223 | |
---|
2224 | $a_x = 0; |
---|
2225 | $b_x = 0; |
---|
2226 | $a_y = 0; |
---|
2227 | $b_y = 0; |
---|
2228 | |
---|
2229 | if( $x_present && $y_present ) |
---|
2230 | { |
---|
2231 | if( $x_first ) |
---|
2232 | { |
---|
2233 | $n = sscanf( $a, $skan_str, $a_x, $a_y ); |
---|
2234 | $n = sscanf( $b, $skan_str, $b_x, $b_y ); |
---|
2235 | } |
---|
2236 | else if( $y_first ) |
---|
2237 | { |
---|
2238 | $n = sscanf( $a, $skan_str, $a_y, $a_x ); |
---|
2239 | $n = sscanf( $b, $skan_str, $b_y, $b_x ); |
---|
2240 | } |
---|
2241 | } |
---|
2242 | else if( $x_present && !$y_present ) |
---|
2243 | { |
---|
2244 | $n = sscanf( $a, $skan_str, $a_x ); |
---|
2245 | $n = sscanf( $b, $skan_str, $b_x ); |
---|
2246 | } |
---|
2247 | else if( $y_present && !$x_present ) |
---|
2248 | { |
---|
2249 | $n = sscanf( $a, $skan_str, $a_y ); |
---|
2250 | $n = sscanf( $b, $skan_str, $b_y ); |
---|
2251 | } |
---|
2252 | |
---|
2253 | if ( $SORT_ORDER=="desc" ) |
---|
2254 | { |
---|
2255 | |
---|
2256 | if( $x_present && $y_present ) |
---|
2257 | { |
---|
2258 | // 1 = a < b |
---|
2259 | // -1 = a > b |
---|
2260 | // |
---|
2261 | if ($a_x == $b_x) |
---|
2262 | { |
---|
2263 | if ($a_y < $b_y) |
---|
2264 | { |
---|
2265 | return 1; |
---|
2266 | } |
---|
2267 | else if ($a_y > $b_y) |
---|
2268 | { |
---|
2269 | return -1; |
---|
2270 | } |
---|
2271 | } |
---|
2272 | else if ($a_x < $b_x) |
---|
2273 | { |
---|
2274 | return 1; |
---|
2275 | } |
---|
2276 | else if ($a_x > $b_x) |
---|
2277 | { |
---|
2278 | return -1; |
---|
2279 | } |
---|
2280 | } |
---|
2281 | else if( $x_present && !$y_present ) |
---|
2282 | { |
---|
2283 | if ($a_x < $b_x) |
---|
2284 | { |
---|
2285 | return 1; |
---|
2286 | } |
---|
2287 | else if ($a_x > $b_x) |
---|
2288 | { |
---|
2289 | return -1; |
---|
2290 | } |
---|
2291 | } |
---|
2292 | else if( $y_present && !$x_present ) |
---|
2293 | { |
---|
2294 | if ($a_y < $b_y) |
---|
2295 | { |
---|
2296 | return 1; |
---|
2297 | } |
---|
2298 | else if ($a_y > $b_y) |
---|
2299 | { |
---|
2300 | return -1; |
---|
2301 | } |
---|
2302 | } |
---|
2303 | } |
---|
2304 | else if ( $SORT_ORDER == "asc" ) |
---|
2305 | { |
---|
2306 | |
---|
2307 | if( $x_present && $y_present ) |
---|
2308 | { |
---|
2309 | // 1 = a > b |
---|
2310 | // -1 = a < b |
---|
2311 | // |
---|
2312 | if ($a_x == $b_x) |
---|
2313 | { |
---|
2314 | if ($a_y > $b_y) |
---|
2315 | { |
---|
2316 | return 1; |
---|
2317 | } |
---|
2318 | else if ($a_y < $b_y) |
---|
2319 | { |
---|
2320 | return -1; |
---|
2321 | } |
---|
2322 | } |
---|
2323 | else if ($a_x > $b_x) |
---|
2324 | { |
---|
2325 | return 1; |
---|
2326 | } |
---|
2327 | else if ($a_x < $b_x) |
---|
2328 | { |
---|
2329 | return -1; |
---|
2330 | } |
---|
2331 | } |
---|
2332 | else if( $x_present && !$y_present ) |
---|
2333 | { |
---|
2334 | if ($a_x > $b_x) |
---|
2335 | { |
---|
2336 | return 1; |
---|
2337 | } |
---|
2338 | else if ($a_x < $b_x) |
---|
2339 | { |
---|
2340 | return -1; |
---|
2341 | } |
---|
2342 | } |
---|
2343 | else if( $y_present && !$x_present ) |
---|
2344 | { |
---|
2345 | if ($a_y > $b_y) |
---|
2346 | { |
---|
2347 | return 1; |
---|
2348 | } |
---|
2349 | else if ($a_y < $b_y) |
---|
2350 | { |
---|
2351 | return -1; |
---|
2352 | } |
---|
2353 | } |
---|
2354 | } |
---|
2355 | } |
---|
2356 | function makeTime( $time ) |
---|
2357 | { |
---|
2358 | $days = intval( $time / 86400 ); |
---|
2359 | $time = ($days>0) ? $time % ($days * 86400) : $time; |
---|
2360 | |
---|
2361 | $date_str = ''; |
---|
2362 | $day_str = ''; |
---|
2363 | |
---|
2364 | if( $days > 0 ) |
---|
2365 | { |
---|
2366 | if( $days > 1 ) |
---|
2367 | { |
---|
2368 | $day_str .= $days . ' days'; |
---|
2369 | } |
---|
2370 | else |
---|
2371 | { |
---|
2372 | $day_str .= $days . ' day'; |
---|
2373 | } |
---|
2374 | } |
---|
2375 | |
---|
2376 | $hours = intval( $time / 3600 ); |
---|
2377 | $time = $hours ? $time % ($hours * 3600) : $time; |
---|
2378 | |
---|
2379 | if( $hours > 0 ) |
---|
2380 | { |
---|
2381 | $date_str .= $hours . ':'; |
---|
2382 | $date_unit = 'hours'; |
---|
2383 | } |
---|
2384 | |
---|
2385 | $minutes = intval( $time / 60 ); |
---|
2386 | $seconds = $minutes ? $time % ($minutes * 60) : $time; |
---|
2387 | |
---|
2388 | if( $minutes > 0 ) |
---|
2389 | { |
---|
2390 | if( $minutes >= 10 ) |
---|
2391 | { |
---|
2392 | $date_str .= $minutes . ':'; |
---|
2393 | } |
---|
2394 | else |
---|
2395 | { |
---|
2396 | $date_str .= '0' . $minutes . ':'; |
---|
2397 | } |
---|
2398 | |
---|
2399 | $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit; |
---|
2400 | } |
---|
2401 | else |
---|
2402 | { |
---|
2403 | if($hours > 0 ) |
---|
2404 | { |
---|
2405 | $date_str .= '00:'; |
---|
2406 | $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit; |
---|
2407 | } |
---|
2408 | } |
---|
2409 | |
---|
2410 | $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit; |
---|
2411 | |
---|
2412 | if( $seconds > 0 ) |
---|
2413 | { |
---|
2414 | if( $seconds >= 10 ) |
---|
2415 | { |
---|
2416 | $date_str .= $seconds . ' ' . $date_unit; |
---|
2417 | } |
---|
2418 | else |
---|
2419 | { |
---|
2420 | $date_str .= '0' . $seconds . ' ' . $date_unit; |
---|
2421 | } |
---|
2422 | } |
---|
2423 | else if ( $hours > 0 or $minutes > 0 ) |
---|
2424 | { |
---|
2425 | $date_str .= '00 ' . $date_unit; |
---|
2426 | } |
---|
2427 | |
---|
2428 | if( $days > 0) |
---|
2429 | { |
---|
2430 | if( $hours > 0 or $minutes > 0 or $seconds > 0 ) |
---|
2431 | { |
---|
2432 | $date_str = $day_str . ' - ' . $date_str; |
---|
2433 | } |
---|
2434 | else |
---|
2435 | { |
---|
2436 | $date_str = $day_str; |
---|
2437 | } |
---|
2438 | } |
---|
2439 | return $date_str; |
---|
2440 | } |
---|
2441 | ?> |
---|