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
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 736 2013-03-24 17:44:13Z 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 = "desc";
53if( !isset($sortby) ) $sortby = "id";
54if( !isset($range) or $range == '') $range= "job";
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($owner) && ($owner!='')) $filter['owner']=$owner;
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
72    foreach( $myfilter_fields as $myfilter ) 
73    {
74
75        switch( $myfilter ) 
76        {
77
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    }
92}
93
94function epochToDatetime( $epoch ) 
95{
96
97        return strftime( "%d-%m-%Y %H:%M:%S", $epoch );
98}
99
100function makeHeader( $page_call, $title, $longtitle ) 
101{
102
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;
108    global $metrics, $reports, $m, $conf;
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   
113    if( isset($conf['default_metric']) and !isset($m) )
114        $metricname = $conf['default_metric'];
115    else
116        if( isset( $m ) )
117            $metricname = $m;
118        else
119            $metricname = "load_one";
120
121    $header = "header";
122
123    # Maintain our path through the grid tree.
124    $me = $self . "@" . $grid[$self][AUTHORITY];
125
126    $gridstack = array();
127    $gridstack[] = $me;
128
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    }
145
146    $gridstack_str = join(">", $gridstack);
147    $gridstack_url = rawurlencode($gridstack_str);
148
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    }
155
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]);
159
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" );
171
172    if( $view != "search" )
173    {
174        $tpl->assign( "refresh", $default_refresh );
175    }
176
177    $tpl->assign( "date", date("r") );
178    $tpl->assign( "longpage_title", $longtitle );
179    $tpl->assign( "page_title", $title );
180
181    # The page to go to when "Get Fresh Data" is pressed.
182    $tpl->assign("page","./");
183
184    # Templated Logo image
185    $tpl->assign("images","./templates/$template_name/images");
186
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";
192
193    if ($jobrange and $jobstart)
194        $get_metric_string .= "&jr=$jobrange&js=$jobstart";
195
196    # Set the Alternate view link.
197    $cluster_url=rawurlencode($clustername);
198    $node_url=rawurlencode($hostname);
199
200    # Make some information available to templates.
201    $tpl->assign("cluster_url", $cluster_url);
202    # Build the node_menu
203    $node_menu = "";
204
205    if ($parentgrid) 
206    {
207        $node_menu .= "<B>$parentgrid $meta_designator</B> ";
208        $node_menu .= "<B>&gt;</B>\n";
209    }
210
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";
215
216    if ($physical)
217    {
218        $node_menu .= hiddenvar("p", $physical);
219    }
220
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    }
228
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);
237
238    $mmfh    = array();
239
240    $mmfh    = $metrics[$firsthost];
241
242    $context_metrics    = array();
243
244    foreach( $mmfh as $mm => $bla )
245    {
246        $context_metrics[] = $mm;
247    }
248
249    foreach ($reports as $mr => $mfoo)
250        $context_metrics[] = $mr;
251
252    $node_menu .= "<B><A HREF=\"./?c=".rawurlencode($clustername)."\">Joblist</A></B> ";
253
254    if( isset( $hostname ) && ( $view != 'host' ) ) 
255    {
256
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    }
262
263    if( count( $filter ) > 0 && $view != "search" ) 
264    {
265
266        $my_ct = 1;
267        $filter_nr = count( $filter );
268
269        foreach( $filter as $filtername=>$filterval ) 
270        {
271
272            $node_menu .= "<B>&gt;</B>\n";
273
274            $href = "<A HREF=\"./?c=".rawurlencode($clustername);
275            $temp_ct = 0;
276            $n_filter = $filter;
277            $my_filterorder = "";
278            $my_filters = array_keys( $filter );
279
280            foreach( $n_filter as $n_filtername=>$n_filterval ) 
281            {
282
283                if( $temp_ct < $my_ct ) 
284                {
285                    $href .= "&". $n_filtername . "=" . $n_filterval;
286
287                    if( $my_filterorder == "" )
288                        $my_filterorder = $my_filters[$temp_ct];
289                    else
290                        $my_filterorder .= "," . $my_filters[$temp_ct];
291                }
292
293                $temp_ct++;
294            }
295            $href .= "&filterorder=$my_filterorder\">";
296
297            if( $my_ct < $filter_nr )
298                $node_menu .= "<B>$href$filtername: $filterval</A></B> ";
299            else
300                $node_menu .= "<B>$filtername: $filterval</B> ";
301
302            $my_ct++;
303        }
304    }
305
306    $tpl->gotoBlock( "_ROOT" );
307    $tpl->assignGlobal("view", $view);
308
309
310    if( array_key_exists( "id", $filter ) or isset($hostname) )
311    {
312
313        $range = "job";
314
315        if( $page_call != "host_view" )
316        {
317
318            if (is_array($context_metrics) )
319            {
320                $metric_menu = "<B>Metric</B>&nbsp;&nbsp;"
321                    ."<SELECT NAME=\"m\" OnChange=\"toga_form.submit();\">\n";
322
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            }
334
335        }
336
337        $tpl->assign("metric_menu", $metric_menu );
338
339        if( $view == "search" or $view == "host" or $view == "overview-host" )
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 );
353
354            if( $view == "host" or $view == "overview-host" )
355            {
356                $tpl->newBlock("hostview");
357                $tpl->assign("job_start", $job_start );
358                $tpl->assign("job_stop", $job_stop );
359            }
360        }
361
362    }
363
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";
372
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";
383
384        $tpl->assign("range_menu", $range_menu);
385
386    }
387
388    if( $view == "search" or $view == "host" )
389    {
390
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 );
395
396    }
397    else
398    {
399        $form_name = "toga_form";
400        $tpl->assignGlobal("form_name", $form_name );
401    }
402
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);
412
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
418}
419
420function makeFooter()
421{
422    global $tpl, $version, $parsetime, $monarchversion;
423
424    $tpl->gotoBlock( "_ROOT" );
425    $tpl->assign("webfrontend-version",$version["webfrontend"]);
426    $tpl->assign("monarch-version", $monarchversion);
427
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    }
438
439    $tpl->assign("parsetime", sprintf("%.4f", $parsetime) . "s");
440}
441
442function includeSearchpage()
443{
444    global $tpl;
445
446    $tpl->assignInclude( "main", "templates/search.tpl" );
447
448}
449
450function includeOverview()
451{
452    global $tpl;
453
454    $tpl->assignInclude( "main", "templates/overview.tpl" );
455}
456
457function includeHostPage()
458{
459
460    global $tpl;
461
462    $tpl->assignInclude( "main", "templates/host_view.tpl" );
463}
464
465$tpl = new TemplatePower( "templates/index.tpl" );
466
467$tpl->assignInclude( "header", "templates/header.tpl" );
468
469if( isset( $h ) and $h != '' )
470{
471    $hostname = $h;
472}
473
474switch( $view )
475{
476
477    case "overview":
478
479        includeOverview();
480        break;
481
482    case "search":
483
484        includeSearchPage();
485        break;
486
487    case "host":
488
489        includeHostPage();
490        break;
491
492    case "overview-host":
493
494        includeHostPage();
495        break;
496
497    default:
498
499        includeOverview();
500        break;
501}
502
503$tpl->assignInclude( "footer", "templates/footer.tpl" );
504$tpl->prepare();
505
506$longtitle = "Batch Report :: Powered by Job Monarch!";
507$title = "Batch Report";
508$tpl->assign("cluster_url", rawurlencode($clustername) );
509$tpl->assign("cluster", $clustername );
510
511switch( $view )
512{
513
514    case "overview":
515
516        include "./overview.php";
517        makeOverview();
518        break;
519
520    case "search":
521
522        include "./search.php";
523        makeSearchPage();
524        break;
525
526    case "host":
527
528        include "./host_view.php";
529        makeHostView();
530        break;
531
532    case "overview-host":
533
534        include "./host_view.php";
535        makeHostView();
536        break;
537
538    default:
539
540        makeOverview();
541        break;
542}
543
544makeFooter();
545$tpl->printToScreen();
546?>
Note: See TracBrowser for help on using the repository browser.