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

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

toga/templates/overview.tpl:

  • Now readable job/node/cpu counting info

toga/overview.php:

  • Now correctly counts for with new queued job cpu/node numbers

toga/libtoga.php:

  • Changed node parsing from XML for new queued job cpu/node numbers
  • Misc RRD generation testing changes
File size: 16.7 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
361                        if( $state == 'R' )
362                                $nodes = count( $jobattrs[nodes] );
363                        else
364                                $nodes = $jobattrs[nodes];
365
366                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
367                        $cpus = $nodes * $ppn;
368                        $start_time = (int) $jobattrs[start_timestamp];
369                        $runningtime = $report_time - $start_time;
370
371                        switch( $sortby ) {
372                                case "id":
373                                        $sorted[$jobid] = $jobid;
374                                        break;
375
376                                case "state":
377                                        $sorted[$jobid] = $state;
378                                        break;
379
380                                case "user":
381                                        $sorted[$jobid] = $user;
382                                        break;
383
384                                case "queue":
385                                        $sorted[$jobid] = $queue;
386                                        break;
387
388                                case "name":
389                                        $sorted[$jobid] = $name;
390                                        break;
391
392                                case "req_cpu":
393                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
394                                        break;
395
396                                case "req_mem":
397                                        $sorted[$jobid] = $req_memory;
398                                        break;
399
400                                case "nodes":
401                                        $sorted[$jobid] = $nodes;
402                                        break;
403
404                                case "cpus":
405                                        $sorted[$jobid] = $cpus;
406                                        break;
407
408                                case "start":
409                                        $sorted[$jobid] = $start_time;
410                                        break;
411
412                                case "runningtime":
413                                        $sorted[$jobid] = $runningtime;
414                                        break;
415
416                                default:
417                                        break;
418
419                        }
420        }
421
422        //uasort( $sorted, $cmp );
423        if( $sortorder == "asc" )
424                arsort( $sorted );
425        else if( $sortorder == "desc" )
426                asort( $sorted );
427
428        return $sorted;
429}
430
431function makeOverview() {
432
433        global $jobs, $nodes, $heartbeat, $clustername, $tpl;
434        global $sortorder, $sortby, $filter, $sh, $hc, $m;
435        global $cluster_url, $get_metric_string, $host_url, $metrics;
436        global $start, $end, $reports, $gnodes, $default_showhosts;
437
438        $metricname = $m;
439
440        $tpl->assign("sortorder", $sortorder );
441        $tpl->assign("sortby", $sortby );
442
443        $sorted_jobs = sortJobs( $jobs, $sortby, $sortorder );
444
445        $even = 1;
446
447        $used_jobs = 0;
448        $used_cpus = 0;
449        $used_nodes = 0;
450
451        $all_used_nodes = array();
452
453        $avail_nodes = count( $gnodes );
454        $avail_cpus = cluster_sum("cpu_num", $metrics);
455
456        $view_cpus = 0;
457        $view_jobs = 0;
458        $view_nodes = 0;
459
460        $view_used_nodes = array();
461
462        foreach( $sorted_jobs as $jobid => $sortdec ) {
463
464                $report_time = $jobs[$jobid][reported];
465
466                $nodes = count( $jobs[$jobid][nodes] );
467                $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
468                $cpus = $nodes * $ppn;
469
470                foreach( $jobs[$jobid][nodes] as $tempnode )
471                        $all_used_nodes[] = $tempnode;
472
473                if( $jobs[$jobid][status] == 'R' ) {
474                        $used_cpus += $cpus;
475                        $used_jobs++;
476                }
477
478                if( $report_time == $heartbeat ) {
479
480                        $display_job = 1;
481
482                        foreach( $filter as $filtername=>$filtervalue ) {
483
484                                if( $filtername == 'id' && $jobid != $filtervalue )
485                                        $display_job = 0;
486                                else if( $filtername == 'state' && $jobs[$jobid][status] != $filtervalue )
487                                        $display_job = 0;
488                                else if( $filtername == 'queue' && $jobs[$jobid][queue] != $filtervalue )
489                                        $display_job = 0;
490                                else if( $filtername == 'user' && $jobs[$jobid][owner] != $filtervalue )
491                                        $display_job = 0;
492                        }
493
494                        if( $display_job ) {
495
496                                $tpl->newBlock("node");
497                                $tpl->assign( "clustername", $clustername );
498                                $tpl->assign("id", $jobid );
499                                $tpl->assign("state", $jobs[$jobid][status] );
500                                $tpl->assign("user", $jobs[$jobid][owner] );
501                                $tpl->assign("queue", $jobs[$jobid][queue] );
502                                $tpl->assign("name", $jobs[$jobid][name] );
503                                $domain = $jobs[$jobid][domain];
504                                $tpl->assign("req_cpu", makeTime( timeToEpoch( $jobs[$jobid][requested_time] ) ) );
505                                $tpl->assign("req_memory", $jobs[$jobid][requested_memory] );
506
507                                if( $jobs[$jobid][status] == 'R' )
508                                        $nodes = count( $jobs[$jobid][nodes] );
509                                else if( $jobs[$jobid][status] == 'Q' )
510                                        $nodes = $jobs[$jobid][nodes];
511
512                                $ppn = (int) $jobs[$jobid][ppn] ? $jobs[$jobid][ppn] : 1;
513                                $cpus = $nodes * $ppn;
514                                $tpl->assign("nodes", $nodes );
515                                $tpl->assign("cpus", $cpus );
516                                $start_time = (int) $jobs[$jobid][start_timestamp];
517                                $job_start = $start_time;
518
519                                $view_cpus += $cpus;
520                                $view_jobs++;
521
522                                if( $jobs[$jobid][status] == 'R' )
523                                        foreach( $jobs[$jobid][nodes] as $tempnode )
524                                                $view_used_nodes[] = $tempnode;
525                                else if( $jobs[$jobid][status] == 'Q' )
526                                        $view_nodes += $jobs[$jobid][nodes];
527
528                                if( $even ) {
529
530                                        $tpl->assign("nodeclass", "even");
531                                        $even = 0;
532                                } else {
533
534                                        $tpl->assign("nodeclass", "odd");
535                                        $even = 1;
536                                }
537
538                                if( $start_time ) {
539
540                                        $runningtime = makeTime( $report_time - $start_time );
541                                        $job_runningtime = $report_time - $start_time;
542                                        $tpl->assign("started", makeDate( $start_time ) );
543                                        $tpl->assign("runningtime", $runningtime );
544                                }
545                        }
546                }
547        }
548        array_unique( $all_used_nodes );
549        array_unique( $view_used_nodes );
550        $used_nodes = count( $all_used_nodes );
551        $view_nodes += count( $view_used_nodes );
552
553        //$tpl->assignGlobal("cpus_nr", $overview_cpus );
554        //$tpl->assignGlobal("jobs_nr", $overview_jobs );
555
556        $tpl->assignGlobal("avail_nodes", $avail_nodes );
557        $tpl->assignGlobal("avail_cpus", $avail_cpus );
558
559        $tpl->assignGlobal("used_nodes", $used_nodes );
560        $tpl->assignGlobal("used_jobs", $used_jobs );
561        $tpl->assignGlobal("used_cpus", $used_cpus );
562
563        $tpl->assignGlobal("view_nodes", $view_nodes );
564        $tpl->assignGlobal("view_jobs", $view_jobs );
565        $tpl->assignGlobal("view_cpus", $view_cpus );
566
567        $tpl->assignGlobal("report_time", makeDate( $heartbeat));
568       
569        //$tpl->assignGlobal("f_cpus_nr", $f_cpus );
570        //$tpl->assignGlobal("f_jobs_nr", $f_jobs );
571
572        if( array_key_exists( "id", $filter ) and $start_time ) {
573                $tpl->newBlock( "showhosts" );
574
575                # Present a width list
576                $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"toga_form.submit();\">\n";
577
578                $hostcols = ($hc) ? $hc : 4;
579
580                foreach(range(1,25) as $cols) {
581                        $cols_menu .= "<OPTION VALUE=$cols ";
582                        if ($cols == $hostcols)
583                                $cols_menu .= "SELECTED";
584                        $cols_menu .= ">$cols\n";
585                }
586                $cols_menu .= "</SELECT>\n";
587
588                //$tpl->assign("cluster", $clustername);
589                $tpl->assign("metric","$metricname $units");
590                $tpl->assign("id", $filter[id]);
591                # Host columns menu defined in header.php
592                $tpl->assign("cols_menu", $cols_menu);
593
594                $showhosts = isset($sh) ? $sh : $default_showhosts;
595                //if( !$showhosts) $showhosts = $default_showhosts;
596                $tpl->assign("checked$showhosts", "checked");
597
598                if( $showhosts ) {
599                        //-----
600
601                        if( !isset($start) ) $start="jobstart";
602                        if( !isset($stop) ) $stop="now";
603                        //$tpl->assign("start", $start);
604                        //$tpl->assign("stop", $stop);
605
606                        $sorted_hosts = array();
607                        $hosts_up = $jobs[$filter[id]][nodes];
608
609                        $r = intval($job_runningtime * 1.25);
610
611                        $jobrange = ($job_runningtime < 3600) ? -3600 : -$r ;
612                        $jobstart = $report_time - $job_runningtime;
613
614                        if ($reports[$metricname])
615                                $metricval = "g";
616                        else
617                                $metricval = "m";
618                                               
619                        foreach ($hosts_up as $host ) {
620                                $host = $host. '.'.$domain;
621                                $cpus = $metrics[$host]["cpu_num"][VAL];
622                                if (!$cpus) $cpus=1;
623                                $load_one  = $metrics[$host]["load_one"][VAL];
624                                $load = ((float) $load_one)/$cpus;
625                                $host_load[$host] = $load;
626                                $percent_hosts[load_color($load)] += 1;
627                                if ($metricname=="load_one")
628                                        $sorted_hosts[$host] = $load;
629                                else
630                                        $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
631                        }
632                        switch ($sort) {
633                                case "descending":
634                                        arsort($sorted_hosts);
635                                        break;
636                                case "by hostname":
637                                        ksort($sorted_hosts);
638                                        break;
639                                default:
640                                case "ascending":
641                                        asort($sorted_hosts);
642                                        break;
643                        }
644
645                        //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
646
647                        # First pass to find the max value in all graphs for this
648                        # metric. The $start,$end variables comes from get_context.php,
649                        # included in index.php.
650                        list($min, $max) = find_limits($sorted_hosts, $metricname);
651
652                        # Second pass to output the graphs or metrics.
653                        $i = 1;
654                        foreach ( $sorted_hosts as $host=>$value  ) {
655                                $tpl->newBlock ("sorted_list");
656                                //$host = $host. '.'.$domain;
657                                $host_url = rawurlencode($host);
658                                $cluster_url = rawurlencode($clustername);
659
660                                $textval = "";
661                                //printf("host = %s, value = %s", $host, $value);
662                                //echo "$host: $value, ";
663                                $val = $metrics[$host][$metricname];
664                                $class = "metric";
665                                $host_link="\"?c=$cluster_url&h=$host_url&r=job&jr=$jobrange&js=$jobstart\"";
666
667                                if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
668                                        $textval = date("r", $val[VAL]);
669                                } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
670                                        $textval = "$val[VAL] $val[UNITS]";
671                                } else {
672                                        $load_color = load_color($host_load[$host]);
673                                        $graphargs = ($reports[$metricname]) ? "g=$metricname&" : "m=$metricname&";
674                                        $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";
675                                }
676                                if ($textval) {
677                                        $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
678                                } else {
679                                        $cell="<td><a href=$host_link>".  "<img src=\"../../graph.php?$graphargs\" ".  "alt=\"$host\" height=112 width=225 border=0></a></td>";
680                                }
681
682                                $tpl->assign("metric_image", $cell);
683                                if (! ($i++ % $hostcols) )
684                                         $tpl->assign ("br", "</tr><tr>");
685                        }
686                }
687//---
688        }
689}
690?>
Note: See TracBrowser for help on using the repository browser.