source: trunk/web/addons/toga/overview.php @ 129

Last change on this file since 129 was 129, checked in by bastiaans, 19 years ago

web/templates/toga/cluster_extra.tpl:

  • Renamed link to joblist

web/addons/toga/templates/header.tpl:

  • Inclusion to archivelink template

web/addons/toga/index.php:

  • Misc cleanup
  • Setup skeleton for search

web/addons/toga/overview.php:

  • Fixed showhosts bugs

web/addons/toga/conf.php:

  • Cleanup
  • Added TARCHD option for if a archive is present

web/addons/toga/libtoga.php:

  • Don't use Ganglia's get_context, it messes stuff up
File size: 15.6 KB
Line 
1<?php
2global $GANGLIA_PATH, $clustername, $tpl, $filter, $cluster, $get_metric_string, $cluster_url, $sh;
3global $hosts_up, $m, $start, $end;
4
5$data_gatherer = new DataGatherer();
6
7//$tpl->assign( "self", "./index.php" );
8$tpl->assign( "clustername", $clustername );
9
10if( $TARCHD )
11        $tpl->assign( "cluster_url", rawurlencode($clustername) );
12
13$data_gatherer->parseXML();
14
15$heartbeat = $data_gatherer->getHeartbeat();
16$jobs = $data_gatherer->getJobs();
17$gnodes = $data_gatherer->getNodes();
18$cpus = $data_gatherer->getCpus();
19
20$filter_image_url = "";
21
22foreach( $filter as $filtername => $filtervalue ) {
23        $tpl->assign( "f_".$filtername, $filtervalue );
24        $filter_image_url .= "&$filtername=$filtervalue";
25}
26
27$tpl->assign( "clusterimage", "./image.php?c=".rawurlencode($clustername)."&view=big-clusterimage".$filter_image_url );
28
29if( array_key_exists( "id", $filter ) )
30        $piefilter = 'id';
31else if( array_key_exists( "user", $filter ) )
32        $piefilter = 'user';
33else if( array_key_exists( "queue", $filter ) )
34        $piefilter = 'queue';
35
36$pie = drawPie();
37$tpl->assign("pie", $pie );
38
39//if( !array_key_exists( 'id', $filter ) ) {
40
41//      $graph_args = "c=$cluster_url&$get_metric_string&st=$cluster[LOCALTIME]";
42//      $tpl->newBlock( "average_graphs" );
43//      $tpl->assign( "graph_args", $graph_args );
44//}
45
46function timeToEpoch( $time ) {
47
48        $time_fields = explode( ':', $time );
49
50        if( count($time_fields) == 3 ) {
51
52                $hours = $time_fields[0];
53                $minutes = $time_fields[1];
54                $seconds = $time_fields[2];
55
56        } else if( count($time_fields) == 2 ) {
57
58                $hours = 0;
59                $minutes = $time_fields[0];
60                $seconds = $time_fields[1];
61
62        } else if( count($time_fields) == 1 ) {
63
64                $hours = 0;
65                $minutes = 0;
66                $seconds = $time_fields[0];
67        }
68
69        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
70
71        return $myepoch;
72}
73
74function makeTime( $time ) {
75
76        $days = intval( $time / 86400 );
77        $time = ($days>0) ? $time % ($days * 86400) : $time;
78
79        //printf( "time = %s, days = %s\n", $time, $days );
80
81        $date_str = '';
82        $day_str = '';
83
84        if( $days > 0 ) {
85                if( $days > 1 )
86                        $day_str .= $days . ' days';
87                else
88                        $day_str .= $days . ' day';
89        }
90
91        $hours = intval( $time / 3600 );
92        $time = $hours ? $time % ($hours * 3600) : $time;
93
94        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
95
96        if( $hours > 0 ) {
97                $date_str .= $hours . ':';
98                $date_unit = 'hours';
99        }
100               
101        $minutes = intval( $time / 60 );
102        $seconds = $minutes ? $time % ($minutes * 60) : $time;
103
104        if( $minutes > 0 ) {
105
106                if( $minutes >= 10 )
107                        $date_str .= $minutes . ':';
108                else
109                        $date_str .= '0' . $minutes . ':';
110
111                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
112        } else {
113                if($hours > 0 ) {
114                        $date_str .= '00:';
115                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
116                }
117        }
118
119
120        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
121
122        if( $seconds > 0 ) {
123
124                if( $seconds >= 10 )
125                        $date_str .= $seconds . ' ' . $date_unit;
126                else
127                        $date_str .= '0' . $seconds . ' ' . $date_unit;
128                       
129        } else if ( $hours > 0 or $minutes > 0 )
130
131                $date_str .= '00 ' . $date_unit;
132
133        if( $days > 0) {
134
135                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
136                        $date_str = $day_str . ' - ' . $date_str;
137                else
138                        $date_str = $day_str;
139        }
140
141        return $date_str;
142}
143
144function makeDate( $time ) {
145        return strftime( "%a %d %b %Y %H:%M:%S", $time );
146}
147
148function colorRed( $color ) {
149        return substr( $color, 0, 2 );
150}
151function colorGreen( $color ) {
152        return substr( $color, 2, 2 );
153}
154function colorBlue( $color ) {
155        return substr( $color, 4, 2 );
156}
157
158function colorDiffer( $first, $second ) {
159
160        // Make sure these two colors differ atleast 50 R/G/B
161        $min_diff = 50;
162
163        $c1r = decHex( colorRed( $first ) );
164        $c1g = decHex( colorGreen( $first ) );
165        $c1b = decHex( colorBlue( $first ) );
166
167        $c2r = decHex( colorRed( $second ) );
168        $c2g = decHex( colorGreen( $second ) );
169        $c2b = decHex( colorBlue( $second ) );
170
171        $rdiff = ($c1r >= $c2r) ? $c1r - $c2r : $c2r - $c1r;
172        $gdiff = ($c1g >= $c2g) ? $c1g - $c2g : $c2g - $c1g;
173        $bdiff = ($c1b >= $c2b) ? $c1b - $c2b : $c2b - $c1b;
174
175        if( $rdiff >= $min_diff or $gdiff >= $min_diff or $bdiff >= $min_diff )
176                return TRUE;
177        else
178                return FALSE;
179}
180
181function randomColor( $known_colors ) {
182
183        $start = hexdec( "004E00" );
184        $end = hexdec( "FFFFFF" );
185
186        if( count( $known_colors ) == 0 )
187                return dechex(rand( $start, $end ));
188
189        $color_changed = TRUE;
190
191        while( $color_changed ) {
192
193                $color_changed = FALSE;
194
195                foreach( $known_colors as $old ) {
196
197                        if( !isset( $new ) )
198                                $new = rand( $start, $end );
199
200                        if( !colorDiffer( dechex( $new ), $old ) )
201
202                                while( !colorDiffer( $new, $old ) ) {
203
204                                        $new = rand( $start, $end );
205                                        $color_changed = TRUE;
206                                }
207                }
208        }
209
210        // Whoa! Actually found a good color ;)
211        return dechex( $new );
212}
213
214function drawJobPie() {
215}
216
217function drawUserPie() {
218
219}
220
221function drawQueuePie() {
222
223}
224
225
226function drawPie() {
227
228        global $jobs, $gnodes, $piefilter, $filter;
229
230        $nodes = $gnodes;
231
232        if( isset($piefilter) ) 
233                $pie_args = "title=" . rawurlencode("Cluster ".$piefilter." usage");
234        else
235                $pie_args = "title=" . rawurlencode("Cluster queue usage");
236               
237        $pie_args .= "&size=250x150";
238
239        $queues = array();
240        $nr_jobs = count( $jobs );
241        $nr_nodes = count( $nodes );
242
243        $emptynodes = 0;
244
245        $job_weight = array();
246
247        foreach( $nodes as $node ) {
248
249                $myjobs = $node->getJobs();
250
251                if( count( $myjobs ) == 0 )
252                        $emptynodes++;
253        }
254        $used_nodes = $nr_nodes - $emptynodes;
255
256        $empty_percentage = ($emptynodes / $nr_nodes) * 100;
257        $job_percentage = 100 - $empty_percentage; 
258
259        $qcolors = array();
260        $color = randomColor( $qcolors );
261        $qcolors[] = $color;
262        $pie_args .= "&free=$empty_percentage,$color";
263
264        if( isset( $piefilter ) )
265                $filterpie = array();
266
267        foreach( $nodes as $node ) {
268
269                $node_jobs = $node->getJobs();
270                $nr_node_jobs = count( $node_jobs );
271                $myhost = $node->getHostname();
272
273                foreach( $node_jobs as $myjob ) {
274
275                        // Determine the weight of this job on the node it is running
276                        // - what percentage of the node is in use by this job
277                        //
278                        $job_weight[$myjob] = ( 100 / count( $node_jobs ) ) / 100;
279                        $qname = $jobs[$myjob][queue];
280
281                        if( isset($piefilter) ) {
282                                $countjob = 1;
283                                if( $piefilter == 'id' ) {
284                                        if( $myjob != $filter[$piefilter] )
285                                                $countjob = 0;
286                                } else if( $piefilter == 'user' ) {
287                                        if( $jobs[$myjob][owner] != $filter[$piefilter] )
288                                                $countjob = 0;
289                                } else {
290                                        if( $jobs[$myjob][$piefilter] != $filter[$piefilter] )
291                                                $countjob = 0;
292                                }
293
294                                if( $countjob ) {
295
296                                        if( !isset( $filterpie[$filter[$piefilter]] ) )
297                                                $filterpie[$filter[$piefilter]] = $job_weight[$myjob];
298                                        else
299                                                $filterpie[$filter[$piefilter]] = $filterpie[$filter[$piefilter]] + $job_weight[$myjob];
300                                } else {
301                                        if( !isset( $filterpie["other"] ) )
302                                                $filterpie["other"] = $job_weight[$myjob];
303                                        else
304                                                $filterpie["other"] = $filterpie["other"] + $job_weight[$myjob];
305
306                                }
307                               
308                        } else {
309
310                                if( !isset( $queues[$qname] ) )
311                                        $queues[$qname] = $job_weight[$myjob];
312                                else
313                                        $queues[$qname] = $queues[$qname] + $job_weight[$myjob];
314                        }
315                }
316        }
317
318        //$qcolors = array();
319        if( isset( $piefilter ) )
320                $graphvals = $filterpie;
321        else
322                $graphvals = $queues;
323
324        foreach( $graphvals as $name => $totalweight) {
325
326                $percentage = ( $totalweight / $used_nodes ) * $job_percentage;
327               
328                $color = randomColor( $qcolors );
329                $qcolors[] = $color;
330                $pie_args .= "&$name=$percentage,$color";
331        }
332        $pie = "../../pie.php?$pie_args";
333
334        return $pie;
335}
336
337
338function sortJobs( $jobs, $sortby, $sortorder ) {
339
340        $sorted = array();
341
342        $cmp = create_function( '$a, $b', 
343                "global \$sortby, \$sortorder;".
344
345                "if( \$a == \$b ) return 0;".
346
347                "if (\$sortorder==\"desc\")".
348                        "return ( \$a < \$b ) ? 1 : -1;".
349                "else if (\$sortorder==\"asc\")".
350                        "return ( \$a > \$b ) ? 1 : -1;" );
351
352        foreach( $jobs as $jobid => $jobattrs ) {
353
354                        $state = $jobattrs[status];
355                        $user = $jobattrs[owner];
356                        $queue = $jobattrs[queue];
357                        $name = $jobattrs[name];
358                        $req_cpu = $jobattrs[requested_time];
359                        $req_memory = $jobattrs[requested_memory];
360                        $nodes = count( $jobattrs[nodes] );
361                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
362                        $cpus = $nodes * $ppn;
363                        $start_time = (int) $jobattrs[start_timestamp];
364                        $runningtime = $report_time - $start_time;
365
366                        switch( $sortby ) {
367                                case "id":
368                                        $sorted[$jobid] = $jobid;
369                                        break;
370
371                                case "state":
372                                        $sorted[$jobid] = $state;
373                                        break;
374
375                                case "user":
376                                        $sorted[$jobid] = $user;
377                                        break;
378
379                                case "queue":
380                                        $sorted[$jobid] = $queue;
381                                        break;
382
383                                case "name":
384                                        $sorted[$jobid] = $name;
385                                        break;
386
387                                case "req_cpu":
388                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
389                                        break;
390
391                                case "req_mem":
392                                        $sorted[$jobid] = $req_memory;
393                                        break;
394
395                                case "nodes":
396                                        $sorted[$jobid] = $nodes;
397                                        break;
398
399                                case "cpus":
400                                        $sorted[$jobid] = $cpus;
401                                        break;
402
403                                case "start":
404                                        $sorted[$jobid] = $start_time;
405                                        break;
406
407                                case "runningtime":
408                                        $sorted[$jobid] = $runningtime;
409                                        break;
410
411                                default:
412                                        break;
413
414                        }
415        }
416
417        //uasort( $sorted, $cmp );
418        if( $sortorder == "asc" )
419                arsort( $sorted );
420        else if( $sortorder == "desc" )
421                asort( $sorted );
422
423        return $sorted;
424}
425
426function makeOverview() {
427
428        global $jobs, $nodes, $heartbeat, $clustername, $tpl;
429        global $sortorder, $sortby, $filter, $sh, $hc, $m;
430        global $cluster_url, $get_metric_string, $host_url, $metrics;
431        global $start, $end, $reports, $gnodes, $default_showhosts;
432
433        $metricname = $m;
434
435        $tpl->assign("sortorder", $sortorder );
436        $tpl->assign("sortby", $sortby );
437
438        $sorted_jobs = sortJobs( $jobs, $sortby, $sortorder );
439
440        $even = 1;
441
442        $overview_jobs = count( $sorted_jobs );
443        $overview_nodes = count( $gnodes );
444        $overview_cpus = 0;
445
446        $f_cpus = 0;
447        $f_jobs = 0;
448
449        foreach( $sorted_jobs as $jobid => $sortdec ) {
450
451                $report_time = $jobs[$jobid][reported];
452
453                $nodes = count( $jobs[$jobid][nodes] );
454                $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
455                $cpus = $nodes * $ppn;
456
457                $overview_cpus = $overview_cpus + $cpus;
458
459                if( $report_time == $heartbeat ) {
460
461                        $display_job = 1;
462
463                        foreach( $filter as $filtername=>$filtervalue ) {
464
465                                if( $filtername == 'id' && $jobid != $filtervalue )
466                                        $display_job = 0;
467                                else if( $filtername == 'state' && $jobs[$jobid][status] != $filtervalue )
468                                        $display_job = 0;
469                                else if( $filtername == 'queue' && $jobs[$jobid][queue] != $filtervalue )
470                                        $display_job = 0;
471                                else if( $filtername == 'user' && $jobs[$jobid][owner] != $filtervalue )
472                                        $display_job = 0;
473                        }
474
475                        if( $display_job ) {
476
477
478                                $tpl->newBlock("node");
479                                $tpl->assign( "clustername", $clustername );
480                                $tpl->assign("id", $jobid );
481                                $tpl->assign("state", $jobs[$jobid][status] );
482                                $tpl->assign("user", $jobs[$jobid][owner] );
483                                $tpl->assign("queue", $jobs[$jobid][queue] );
484                                $tpl->assign("name", $jobs[$jobid][name] );
485                                $domain = $jobs[$jobid][domain];
486                                $tpl->assign("req_cpu", makeTime( timeToEpoch( $jobs[$jobid][requested_time] ) ) );
487                                $tpl->assign("req_memory", $jobs[$jobid][requested_memory] );
488                                $nodes = count( $jobs[$jobid][nodes] );
489                                $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
490                                $cpus = $nodes * $ppn;
491                                $tpl->assign("nodes", $nodes );
492                                $tpl->assign("cpus", $cpus );
493                                $start_time = (int) $jobs[$jobid][start_timestamp];
494                                $job_start = $start_time;
495
496                                $f_cpus = $f_cpus + $cpus;
497                                $f_jobs++;
498
499                                if( $even ) {
500
501                                        $tpl->assign("nodeclass", "even");
502                                        $even = 0;
503                                } else {
504
505                                        $tpl->assign("nodeclass", "odd");
506                                        $even = 1;
507                                }
508
509                                if( $start_time ) {
510
511                                        $runningtime = makeTime( $report_time - $start_time );
512                                        $job_runningtime = $report_time - $start_time;
513                                        $tpl->assign("started", makeDate( $start_time ) );
514                                        $tpl->assign("runningtime", $runningtime );
515                                }
516                        }
517                }
518        }
519
520        $tpl->assignGlobal("cpus_nr", $overview_cpus );
521        $tpl->assignGlobal("jobs_nr", $overview_jobs );
522        $tpl->assignGlobal("nodes_nr", $overview_nodes );
523        $tpl->assignGlobal("report_time", makeDate( $heartbeat));
524       
525        $tpl->assignGlobal("f_cpus_nr", $f_cpus );
526        $tpl->assignGlobal("f_jobs_nr", $f_jobs );
527
528        if( array_key_exists( "id", $filter ) and $start_time ) {
529                $tpl->newBlock( "showhosts" );
530
531                # Present a width list
532                $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"toga_form.submit();\">\n";
533
534                $hostcols = ($hc) ? $hc : 4;
535
536                foreach(range(1,25) as $cols) {
537                        $cols_menu .= "<OPTION VALUE=$cols ";
538                        if ($cols == $hostcols)
539                                $cols_menu .= "SELECTED";
540                        $cols_menu .= ">$cols\n";
541                }
542                $cols_menu .= "</SELECT>\n";
543
544                //$tpl->assign("cluster", $clustername);
545                $tpl->assign("metric","$metricname $units");
546                $tpl->assign("id", $filter[id]);
547                # Host columns menu defined in header.php
548                $tpl->assign("cols_menu", $cols_menu);
549
550                $showhosts = isset($sh) ? $sh : $default_showhosts;
551                //if( !$showhosts) $showhosts = $default_showhosts;
552                $tpl->assign("checked$showhosts", "checked");
553
554                if( $showhosts ) {
555                        //-----
556
557                        if( !isset($start) ) $start="jobstart";
558                        if( !isset($stop) ) $stop="now";
559                        //$tpl->assign("start", $start);
560                        //$tpl->assign("stop", $stop);
561
562                        $sorted_hosts = array();
563                        $hosts_up = $jobs[$filter[id]][nodes];
564
565                        $r = intval($job_runningtime * 1.25);
566
567                        $jobrange = ($job_runningtime < 3600) ? -3600 : -$r ;
568                        $jobstart = $report_time - $job_runningtime;
569
570                        if ($reports[$metricname])
571                                $metricval = "g";
572                        else
573                                $metricval = "m";
574                                               
575                        foreach ($hosts_up as $host ) {
576                                $host = $host. '.'.$domain;
577                                $cpus = $metrics[$host]["cpu_num"][VAL];
578                                if (!$cpus) $cpus=1;
579                                $load_one  = $metrics[$host]["load_one"][VAL];
580                                $load = ((float) $load_one)/$cpus;
581                                $host_load[$host] = $load;
582                                $percent_hosts[load_color($load)] += 1;
583                                if ($metricname=="load_one")
584                                        $sorted_hosts[$host] = $load;
585                                else
586                                        $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
587                        }
588                        switch ($sort) {
589                                case "descending":
590                                        arsort($sorted_hosts);
591                                        break;
592                                case "by hostname":
593                                        ksort($sorted_hosts);
594                                        break;
595                                default:
596                                case "ascending":
597                                        asort($sorted_hosts);
598                                        break;
599                        }
600
601                        //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
602
603                        # First pass to find the max value in all graphs for this
604                        # metric. The $start,$end variables comes from get_context.php,
605                        # included in index.php.
606                        list($min, $max) = find_limits($sorted_hosts, $metricname);
607
608                        # Second pass to output the graphs or metrics.
609                        $i = 1;
610                        foreach ( $sorted_hosts as $host=>$value  ) {
611                                $tpl->newBlock ("sorted_list");
612                                //$host = $host. '.'.$domain;
613                                $host_url = rawurlencode($host);
614                                $cluster_url = rawurlencode($clustername);
615
616                                $textval = "";
617                                //printf("host = %s, value = %s", $host, $value);
618                                //echo "$host: $value, ";
619                                $val = $metrics[$host][$metricname];
620                                $class = "metric";
621                                $host_link="\"?c=$cluster_url&h=$host_url&r=job&jr=$jobrange&js=$jobstart\"";
622
623                                if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
624                                        $textval = date("r", $val[VAL]);
625                                } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
626                                        $textval = "$val[VAL] $val[UNITS]";
627                                } else {
628                                        $load_color = load_color($host_load[$host]);
629                                        $graphargs = ($reports[$metricname]) ? "g=$metricname&" : "m=$metricname&";
630                                        $graphargs .= "z=small&c=$cluster_url&h=$host_url&l=$load_color" ."&v=$val[VAL]&x=$max&n=$min&r=job&jr=$jobrange&js=$jobstart";
631                                }
632                                if ($textval) {
633                                        $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
634                                } else {
635                                        $cell="<td><a href=$host_link>".  "<img src=\"../../graph.php?$graphargs\" ".  "alt=\"$host\" height=112 width=225 border=0></a></td>";
636                                }
637
638                                $tpl->assign("metric_image", $cell);
639                                if (! ($i++ % $hostcols) )
640                                         $tpl->assign ("br", "</tr><tr>");
641                        }
642                }
643//---
644        }
645}
646?>
Note: See TracBrowser for help on using the repository browser.