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

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