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