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

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