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

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