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
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 714 2013-03-21 20:25:19Z 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        extract( $_GET );
37}
38
39
40global $GANGLIA_PATH;
41
42include_once "./class.TemplatePower.inc.php";
43
44$httpvars = new HTTPVariables( $HTTP_GET_VARS, $_GET );
45$clustername = $httpvars->getClusterName();
46$view = $httpvars->getHttpVar( "j_view" );
47
48$filter = array();
49
50if( !isset($view) ) $view = "overview";
51if( !isset($sortorder) ) $sortorder = "asc";
52if( !isset($sortby) ) $sortby = "id";
53
54if( isset( $filterorder ) && ($filterorder!='') ) {
55        $myfilter_fields = explode( ",", $filterorder );
56} else {
57        if( isset($queue) && ($queue!='')) $filter['queue']=$queue;
58        if( isset($state) && ($state!='')) $filter['state']=$state;
59        if( isset($owner) && ($owner!='')) $filter['owner']=$owner;
60        if( isset($id) && ($id!='')) $filter['id']=$id;
61}
62
63// Fill filter array in order they were picked by user
64if( isset($myfilter_fields) ) {
65
66        foreach( $myfilter_fields as $myfilter ) {
67
68                switch( $myfilter ) {
69
70                        case "queue":
71                                $filter['queue']=$queue;
72                                break;
73                        case "state":
74                                $filter['state']=$state;
75                                break;
76                        case "owner":
77                                $filter['owner']=$owner;
78                                break;
79                        case "id":
80                                $filter['id']=$id;
81                                break;
82                }
83        }
84}
85
86function epochToDatetime( $epoch ) {
87
88        return strftime( "%d-%m-%Y %H:%M:%S", $epoch );
89}
90
91function makeHeader( $page_call, $title, $longtitle ) {
92
93        global $tpl, $grid, $context, $initgrid;
94        global $jobrange, $jobstart;
95        global $page, $gridwalk, $clustername;
96        global $parentgrid, $physical, $hostname;
97        global $self, $filter, $cluster_url, $get_metric_string;
98        global $metrics, $reports, $m, $default_metric;
99        global $default_refresh, $filterorder, $view;
100        global $JOB_ARCHIVE, $period_start, $period_stop, $h, $id;
101        global $job_start, $job_stop, $range, $r, $metricname;
102       
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
111        $header = "header";
112
113        # Maintain our path through the grid tree.
114        $me = $self . "@" . $grid[$self][AUTHORITY];
115
116        $gridstack = array();
117        $gridstack[] = $me;
118
119        if ($gridwalk=="fwd") {
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        }
153        $tpl->gotoBlock( "_ROOT" );
154
155        if( $view != "search" )
156                $tpl->assign( "refresh", $default_refresh );
157
158        $tpl->assign( "date", date("r") );
159        $tpl->assign( "longpage_title", $longtitle );
160        $tpl->assign( "page_title", $title );
161
162        # The page to go to when "Get Fresh Data" is pressed.
163        $tpl->assign("page","./");
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;
193        $node_menu .= "<B><A HREF=\"../..\">$mygrid $meta_designator</A></B> ";
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);
201                $node_menu .= "<B><A HREF=\"../../?c=".rawurlencode($clustername)."\">$clustername</A></B> ";
202                $node_menu .= "<B>&gt;</B>\n";
203                $node_menu .= hiddenvar("c", $clustername);
204        }
205
206        if (!count($metrics)) {
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";
209                exit;
210        }
211        reset($metrics);
212        $firsthost = key($metrics);
213
214        $mmfh   = array();
215
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
228        $node_menu .= "<B><A HREF=\"./?c=".rawurlencode($clustername)."\">Joblist</A></B> ";
229
230        if( isset( $hostname ) && ( $view != 'host' ) ) {
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
238        if( count( $filter ) > 0 && $view != "search" ) {
239
240                $my_ct = 1;
241                $filter_nr = count( $filter );
242
243                foreach( $filter as $filtername=>$filterval ) {
244
245                        $node_menu .= "<B>&gt;</B>\n";
246
247                        $href = "<A HREF=\"./?c=".rawurlencode($clustername);
248                        $temp_ct = 0;
249                        $n_filter = $filter;
250                        $my_filterorder = "";
251                        $my_filters = array_keys( $filter );
252
253                        foreach( $n_filter as $n_filtername=>$n_filterval ) {
254
255                                if( $temp_ct < $my_ct ) {
256                                        $href .= "&". $n_filtername . "=" . $n_filterval;
257
258                                        if( $my_filterorder == "" )
259                                                $my_filterorder = $my_filters[$temp_ct];
260                                        else
261                                                $my_filterorder .= "," . $my_filters[$temp_ct];
262                                }
263
264                                $temp_ct++;
265                        }
266                        $href .= "&filterorder=$my_filterorder\">";
267
268                        if( $my_ct < $filter_nr )
269                                $node_menu .= "<B>$href$filtername: $filterval</A></B> ";
270                        else
271                                $node_menu .= "<B>$filtername: $filterval</B> ";
272
273                        $my_ct++;
274                }
275        }
276
277        $tpl->gotoBlock( "_ROOT" );
278        $tpl->assignGlobal("view", $view);
279
280
281        if( array_key_exists( "id", $filter ) or isset($hostname) ) {
282
283                $range = "job";
284
285                if( $page_call != "host_view" )
286                {
287
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";
301                        }
302
303                }
304
305                $tpl->assign("metric_menu", $metric_menu );
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
326        }
327
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";
336
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
349        }
350
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
363        if( $JOB_ARCHIVE && $page_call == 'overview' ) {
364                $tpl->newBlock( "search" );
365                $tpl->assignGlobal( "cluster_url", rawurlencode($clustername) );
366                $tpl->assignGlobal( "cluster", $clustername );
367        }
368        $tpl->gotoBlock( "_ROOT" );
369        $tpl->assignGlobal( "cluster", $clustername );
370        $tpl->assign("node_menu", $node_menu);
371
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() {
380        global $tpl, $version, $parsetime, $monarchversion;
381
382        $tpl->gotoBlock( "_ROOT" );
383        $tpl->assign("webfrontend-version",$version["webfrontend"]);
384        $tpl->assign("monarch-version", $monarchversion);
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
397function includeSearchpage() {
398        global $tpl;
399
400        $tpl->assignInclude( "main", "templates/search.tpl" );
401
402}
403
404function includeOverview() {
405        global $tpl;
406
407        $tpl->assignInclude( "main", "templates/overview.tpl" );
408}
409
410function includeHostPage() {
411
412        global $tpl;
413
414        $tpl->assignInclude( "main", "templates/host_view.tpl" );
415}
416
417$tpl = new TemplatePower( "templates/index.tpl" );
418
419$tpl->assignInclude( "header", "templates/header.tpl" );
420
421if( isset( $h ) and $h != '' ) {
422        $hostname = $h;
423}
424
425switch( $view ) {
426
427        case "overview":
428
429                includeOverview();
430                break;
431
432        case "search":
433
434                includeSearchPage();
435                break;
436
437        case "host":
438
439                includeHostPage();
440                break;
441
442        default:
443
444                includeOverview();
445                break;
446}
447
448$tpl->assignInclude( "footer", "templates/footer.tpl" );
449$tpl->prepare();
450
451$longtitle = "Batch Report :: Powered by Job Monarch!";
452$title = "Batch Report";
453$tpl->assign("cluster_url", rawurlencode($clustername) );
454$tpl->assign("cluster", $clustername );
455
456switch( $view ) {
457
458        case "overview":
459
460                include "./overview.php";
461                makeOverview();
462                break;
463
464        case "search":
465
466                include "./search.php";
467                makeSearchPage();
468                break;
469
470        case "host":
471
472                include "./host_view.php";
473                makeHostView();
474                break;
475
476        default:
477
478                makeOverview();
479                break;
480}
481
482makeFooter();
483$tpl->printToScreen();
484?>
Note: See TracBrowser for help on using the repository browser.