source: branches/0.4/web/addons/job_monarch/index.php @ 719

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