source: trunk/web/addons/job_monarch/index.php @ 618

Last change on this file since 618 was 592, checked in by ramonb, 15 years ago

job_monarch/index.php:

  • code cleanup
  • associative array WARNINGs fixes
File size: 11.9 KB
Line 
1<?php
2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
22 * SVN $Id: index.php 527 2008-06-27 13:37:31Z ramonb $
23 */
24
25ini_set("memory_limit","200M");
26set_time_limit(0);
27
28
29$my_dir = getcwd();
30
31global $r, $range;
32
33include_once "./libtoga.php";
34
35if ( !empty( $_GET ) )
36{
37        extract( $_GET );
38}
39
40global $GANGLIA_PATH;
41chdir( $GANGLIA_PATH );
42
43include_once "./class.TemplatePower.inc.php";
44chdir( $my_dir );
45
46$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
47$clustername = $httpvars->getClusterName();
48$view = $httpvars->getHttpVar( "view" );
49
50$filter = array();
51
52if( !isset($view) ) $view = "overview";
53if( !isset($sortorder) ) $sortorder = "asc";
54if( !isset($sortby) ) $sortby = "id";
55
56if( isset( $filterorder ) && ($filterorder!='') )
57{
58        $myfilter_fields = explode( ",", $filterorder );
59}
60else
61{
62        if( isset($queue) && ($queue!='')) $filter['queue']=$queue;
63        if( isset($state) && ($state!='')) $filter['state']=$state;
64        if( isset($user) && ($user!='')) $filter['user']=$user;
65        if( isset($id) && ($id!='')) $filter['id']=$id;
66}
67
68// Fill filter array in order they were picked by user
69if( isset($myfilter_fields) )
70{
71        foreach( $myfilter_fields as $myfilter )
72        {
73                switch( $myfilter )
74                {
75                        case "queue":
76                                $filter['queue']=$queue;
77                                break;
78                        case "state":
79                                $filter['state']=$state;
80                                break;
81                        case "user":
82                                $filter['user']=$user;
83                                break;
84                        case "id":
85                                $filter['id']=$id;
86                                break;
87                }
88        }
89}
90
91function epochToDatetime( $epoch )
92{
93        return strftime( "%d-%m-%Y %H:%M:%S", $epoch );
94}
95
96function makeHeader( $page_call, $title, $longtitle )
97{
98        global $tpl, $grid, $context, $initgrid;
99        global $jobrange, $jobstart;
100        global $page, $gridwalk, $clustername;
101        global $parentgrid, $physical, $hostname;
102        global $self, $filter, $cluster_url, $get_metric_string;
103        global $metrics, $reports, $m, $default_metric;
104        global $default_refresh, $filterorder, $view;
105        global $JOB_ARCHIVE, $period_start, $period_stop, $h, $id;
106        global $job_start, $job_stop, $range, $r, $metricname;
107       
108        if( isset($default_metric) and !isset($m) )
109                $metricname = $default_metric;
110        else
111                if( isset( $m ) )
112                        $metricname = $m;
113                else
114                        $metricname = "load_one";
115
116        $header = "header";
117
118        # Maintain our path through the grid tree.
119        $me = $self . "@" . $grid[$self]['AUTHORITY'];
120
121        $gridstack = array();
122        $gridstack[] = $me;
123
124        if ($gridwalk=="fwd")
125        {
126                # push our info on gridstack, format is "name@url>name2@url".
127                if (end($gridstack) != $me)
128                {
129                        $gridstack[] = $me;
130                }
131        }
132        else if ($gridwalk=="back") 
133        {
134                # pop a single grid off stack.
135                if (end($gridstack) != $me) 
136                {
137                        array_pop($gridstack);
138                }
139        }
140
141        $gridstack_str = join(">", $gridstack);
142        $gridstack_url = rawurlencode($gridstack_str);
143
144        if ($initgrid or $gridwalk) 
145        {
146                # Use cookie so we dont have to pass gridstack around within this site.
147                # Cookie values are automatically urlencoded. Expires in a day.
148                setcookie("gs", $gridstack_str, time() + 86400);
149        }
150
151        # Invariant: back pointer is second-to-last element of gridstack. Grid stack never
152        # has duplicate entries.
153        list($parentgrid, $parentlink) = explode("@", $gridstack[count($gridstack)-2]);
154
155        # Setup a redirect to a remote server if you choose a grid from pulldown menu. Tell
156        # destination server that we're walking foward in the grid tree.
157        if (strstr($clustername, "http://")) 
158        {
159                $tpl->assign("refresh", "0");
160                $tpl->assign("redirect", ";URL=$clustername?gw=fwd&gs=$gridstack_url");
161                echo "<h2>Redirecting, please wait...</h2>";
162                $tpl->printToScreen();
163                exit;
164        }
165        $tpl->gotoBlock( "_ROOT" );
166
167        if( $view != "search" )
168                $tpl->assign( "refresh", $default_refresh );
169
170        $tpl->assign( "date", date("r") );
171        //printf("lg %s\n", $longtitle );
172        //printf("title %s\n", $title );
173        $tpl->assign( "longpage_title", $longtitle );
174        $tpl->assign( "page_title", $title );
175
176        # The page to go to when "Get Fresh Data" is pressed.
177        $tpl->assign("page","./");
178
179        # Templated Logo image
180        $tpl->assign("images","./templates/$template_name/images");
181
182        #
183        # Used when making graphs via graph.php. Included in most URLs
184        #
185        $sort_url=rawurlencode($sort);
186        $get_metric_string = "m=$metric&r=$range&s=$sort_url&hc=$hostcols";
187
188        if ($jobrange and $jobstart)
189                $get_metric_string .= "&jr=$jobrange&js=$jobstart";
190
191        # Set the Alternate view link.
192        $cluster_url=rawurlencode($clustername);
193        $node_url=rawurlencode($hostname);
194
195        # Make some information available to templates.
196        $tpl->assign("cluster_url", $cluster_url);
197        # Build the node_menu
198        $node_menu = "";
199
200        if ($parentgrid) 
201        {
202                $node_menu .= "<B>$parentgrid $meta_designator</B> ";
203                $node_menu .= "<B>&gt;</B>\n";
204        }
205
206        # Show grid.
207        $mygrid =  ($self == "unspecified") ? "" : $self;
208        $node_menu .= "<B><A HREF=\"../..\">$mygrid $meta_designator</A></B> ";
209        $node_menu .= "<B>&gt;</B>\n";
210
211        if ($physical)
212        {
213                $node_menu .= hiddenvar("p", $physical);
214        }
215
216        if ( $clustername ) 
217        {
218                $url = rawurlencode($clustername);
219                $node_menu .= "<B><A HREF=\"../../?c=".rawurlencode($clustername)."\">$clustername</A></B> ";
220                $node_menu .= "<B>&gt;</B>\n";
221                $node_menu .= hiddenvar("c", $clustername);
222        }
223
224        if (!count($metrics)) 
225        {
226                echo "<h4>Cannot find any metrics for selected cluster \"$clustername\", exiting.</h4>\n";
227                echo "Check ganglia XML tree (telnet $ganglia_ip $ganglia_port)\n";
228                exit;
229        }
230        reset($metrics);
231        $firsthost = key($metrics);
232
233        $mmfh   = array();
234
235        $mmfh   = $metrics[$firsthost];
236
237        $context_metrics        = array();
238
239        //foreach ($mmfh as $mm => $mfoo)
240        foreach( $mmfh as $mm => $bla )
241        {
242                $context_metrics[] = $mm;
243        }
244
245        foreach ($reports as $mr => $mfoo)
246                $context_metrics[] = $mr;
247
248        $node_menu .= "<B><A HREF=\"./?c=".rawurlencode($clustername)."\">Joblist</A></B> ";
249
250        if( isset( $hostname ) && ( $view != 'host' ) ) 
251        {
252                $node_menu .= "<B>&gt;</B>\n";
253                $href = "<A HREF=\"./?c=".rawurlencode($clustername)."&h=".$hostname."\">";
254                $node_menu .= "<B>$href";
255                $node_menu .= "host: $hostname</A></B> ";
256        }
257
258        if( count( $filter ) > 0 && $view != "search" ) 
259        {
260                $my_ct = 1;
261                $filter_nr = count( $filter );
262
263                foreach( $filter as $filtername=>$filterval ) 
264                {
265                        $node_menu .= "<B>&gt;</B>\n";
266
267                        $href = "<A HREF=\"./?c=".rawurlencode($clustername);
268                        $temp_ct = 0;
269                        $n_filter = $filter;
270                        $my_filterorder = "";
271                        $my_filters = array_keys( $filter );
272
273                        foreach( $n_filter as $n_filtername=>$n_filterval ) 
274                        {
275                                if( $temp_ct < $my_ct ) 
276                                {
277                                        $href .= "&". $n_filtername . "=" . $n_filterval;
278
279                                        if( $my_filterorder == "" )
280                                                $my_filterorder = $my_filters[$temp_ct];
281                                        else
282                                                $my_filterorder .= "," . $my_filters[$temp_ct];
283                                }
284
285                                $temp_ct++;
286                        }
287                        $href .= "&filterorder=$my_filterorder\">";
288
289                        if( $my_ct < $filter_nr )
290                                $node_menu .= "<B>$href$filtername: $filterval</A></B> ";
291                        else
292                                $node_menu .= "<B>$filtername: $filterval</B> ";
293
294                        $my_ct++;
295                }
296        }
297
298        $tpl->gotoBlock( "_ROOT" );
299        $tpl->assignGlobal("view", $view);
300
301        if( array_key_exists( "id", $filter ) or isset($hostname) )
302        {
303                $range = "job";
304
305                if( $page_call != "host_view" )
306                {
307                        if (is_array($context_metrics) )
308                        {
309                                $metric_menu = "<B>Metric</B>&nbsp;&nbsp;"
310                                        ."<SELECT NAME=\"m\" OnChange=\"toga_form.submit();\">\n";
311
312                                sort($context_metrics);
313                                foreach( $context_metrics as $k ) 
314                                {
315                                        $url = rawurlencode($k);
316                                        $metric_menu .= "<OPTION VALUE=\"$url\" ";
317                                        if ($k == $metricname )
318                                                $metric_menu .= "SELECTED";
319                                        $metric_menu .= ">$k\n";
320                                }
321                                $metric_menu .= "</SELECT>\n";
322                        }
323
324                }
325
326                $tpl->assign("metric_menu", $metric_menu );
327
328                if( $view == "search" or $view == "host" )
329                {
330                        $tpl->newBlock("timeperiod");
331                        if( is_numeric( $period_start ) )
332                        {
333                                $period_start = epochToDatetime( $period_start );
334                        }
335                        if( is_numeric( $period_stop ) )
336                        {
337                                $period_stop = epochToDatetime( $period_stop );
338                        }
339                        $tpl->assign("period_start", $period_start );
340                        $tpl->assign("period_stop", $period_stop );
341                        $tpl->assign("hostname", $hostname );
342
343                        if( $view == "host" )
344                        {
345                                $tpl->newBlock("hostview");
346                                $tpl->assign("job_start", $job_start );
347                                $tpl->assign("job_stop", $job_stop );
348                        }
349                }
350
351        }
352
353        if( $view != "search" )
354        {
355                $context_ranges[]="hour";
356                $context_ranges[]="day";
357                $context_ranges[]="week";
358                $context_ranges[]="month";
359                $context_ranges[]="year";
360                $context_ranges[]="job";
361
362                $range_menu = "<B>Last</B>&nbsp;&nbsp;" ."<SELECT NAME=\"r\" OnChange=\"toga_form.submit();\">\n";
363                foreach ($context_ranges as $v) 
364                {
365                        $url=rawurlencode($v);
366                        $range_menu .= "<OPTION VALUE=\"$url\" ";
367                        if ($v == $range)
368                                $range_menu .= "SELECTED";
369                        $range_menu .= ">$v\n";
370                }
371                $range_menu .= "</SELECT>\n";
372
373                $tpl->assign("range_menu", $range_menu);
374
375        }
376
377        if( $view == "search" or $view == "host" )
378        {
379                $node_menu .= "<B>&gt;</B>\n";
380                $node_menu .= "<B>Jobarchive</B> ";
381                $form_name = "archive_search_form";
382                $tpl->assignGlobal("form_name", $form_name );
383        }
384        else
385        {
386                $form_name = "toga_form";
387                $tpl->assignGlobal("form_name", $form_name );
388        }
389
390        if( $JOB_ARCHIVE && $page_call == 'overview' )
391        {
392                $tpl->newBlock( "search" );
393                $tpl->assignGlobal( "cluster_url", rawurlencode($clustername) );
394                $tpl->assignGlobal( "cluster", $clustername );
395        }
396        $tpl->gotoBlock( "_ROOT" );
397        $tpl->assignGlobal( "cluster", $clustername );
398        $tpl->assign("node_menu", $node_menu);
399
400        # Make sure that no data is cached..
401        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    # Date in the past
402        header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); # always modified
403        header ("Cache-Control: no-cache, must-revalidate");  # HTTP/1.1
404        header ("Pragma: no-cache");                          # HTTP/1.0
405}
406
407function makeFooter()
408{
409        global $tpl, $version, $parsetime, $monarchversion;
410
411        $tpl->gotoBlock( "_ROOT" );
412        $tpl->assign("webfrontend-version",$version["webfrontend"]);
413        $tpl->assign("monarch-version", $monarchversion);
414
415        if ($version["gmetad"])
416        {
417                $tpl->assign("webbackend-component", "gmetad");
418                $tpl->assign("webbackend-version",$version["gmetad"]);
419        }
420        else if ($version["gmond"])
421        {
422                $tpl->assign("webbackend-component", "gmond");
423                $tpl->assign("webbackend-version", $version["gmond"]);
424        }
425
426        $tpl->assign("parsetime", sprintf("%.4f", $parsetime) . "s");
427}
428
429function includeSearchpage()
430{
431        global $tpl;
432
433        $tpl->assignInclude( "main", "templates/search.tpl" );
434}
435
436function includeOverview()
437{
438        global $tpl;
439
440        $tpl->assignInclude( "main", "templates/overview.tpl" );
441}
442
443function includeHostPage()
444{
445        global $tpl;
446
447        $tpl->assignInclude( "main", "templates/host_view.tpl" );
448}
449
450$tpl = new TemplatePower( "templates/index.tpl" );
451
452$tpl->assignInclude( "header", "templates/header.tpl" );
453
454if( isset( $h ) and $h != '' )
455{
456        $hostname = $h;
457}
458
459switch( $view )
460{
461        case "overview":
462
463                includeOverview();
464                break;
465
466        case "search":
467
468                includeSearchPage();
469                break;
470
471        case "host":
472
473                includeHostPage();
474                break;
475
476        default:
477
478                includeOverview();
479                break;
480}
481
482$tpl->assignInclude( "footer", "templates/footer.tpl" );
483$tpl->prepare();
484
485$longtitle = "Batch Report :: Powered by Job Monarch!";
486$title = "Batch Report";
487$tpl->assign("cluster_url", rawurlencode($clustername) );
488$tpl->assign("cluster", $clustername );
489
490switch( $view )
491{
492        case "overview":
493
494                include "./overview.php";
495                makeOverview();
496                break;
497
498        case "search":
499
500                include "./search.php";
501                makeSearchPage();
502                break;
503
504        case "host":
505
506                include "./host_view.php";
507                makeHostView();
508                break;
509
510        default:
511
512                makeOverview();
513                break;
514}
515
516makeFooter();
517$tpl->printToScreen();
518?>
Note: See TracBrowser for help on using the repository browser.