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

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

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

  • Changed form action to /

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

  • Added showhosts HTML

web/addons/toga/index.php:

  • Header will now show metricmenu
  • Form changed to 1 global form

web/addons/toga/overview.php:

  • Setup for different pie graphs for filters
  • Added showhosts code for a job

web/addons/toga/libtoga.php:

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