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

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

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

  • Use block to include link to archive search

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

  • Cleanup
  • Javascript moved to seperate .js file

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

  • Added search results and future showhosts block section

web/addons/toga/index.php:

  • Use block instead of include for archive search link

web/addons/toga/search.php:

  • Setup of working search style

web/addons/toga/libtoga.php:

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