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

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

web/addons/toga/overview.php:

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