source: trunk/web/addons/job_monarch/search.php @ 306

Last change on this file since 306 was 299, checked in by bastiaans, 17 years ago

web/addons/job_monarch/conf.php:

  • added COLUMN_NODES option to display job node hostnames column

web/addons/job_monarch/search.php,
web/addons/job_monarch/overview.php:

  • added handling of COLUMN_NODES

web/addons/job_monarch/templates/search.tpl,
web/addons/job_monarch/templates/overview.tpl:

  • added COLUMN_NODES to html
  • Property svn:keywords set to Id
File size: 18.2 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: search.php 299 2007-04-06 11:21:35Z bastiaans $
23 */
24
25global $clustername, $tpl;
26
27function validateFormInput() {
28        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
29        global $end_from_time, $end_to_time, $period_start, $period_stop;
30
31        $error = 0;
32        $error_msg = "<FONT COLOR=\"red\"><B>";
33        $show_msg = 0;
34
35        $none_set = 0;
36
37        if( $id == '' and $user == '' and $name == '' and $start_from_time == '' and $start_to_time == '' and $queue == '' and $end_from_time == '' and $end_to_time == '') {
38                $error = 1;
39                $show_msg = 1;
40                $error_msg .= "No search criteria set!";
41        }
42
43        if( !is_numeric($id) and !$error and $id != '') {
44
45                $error = 1;
46                $show_msg = 1;
47                $error_msg .= "Id must be a number";
48        }
49
50        //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
51
52        if( !$error and $period_start != '' ) {
53                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
54                $pstart_epoch = datetimeToEpoch( $period_start );
55                //printf( "period_start = %s period_stop = %s\n", $period_start, $period_stop );
56                if( $period_stop != '' ) {
57
58                        $pstop_epoch = datetimeToEpoch( $period_stop );
59                        //printf( "pstop_epoch = %s pstart_epoch = %s\n", $pstop_epoch, $pstart_epoch );
60
61                        if( $pstart_epoch > $pstop_epoch ) {
62
63                                $show_msg = 1;
64                                $error_msg .= "Graph timeperiod reset: start date/time can't be later than end";
65                                $period_stop = '';
66                                $period_start = '';
67                        } else if( $pstop_epoch == $pstart_epoch ) {
68
69                                $show_msg = 1;
70                                $error_msg .= "Graph timeperiod reset: start and end date/time can't be the same";
71                                $period_stop = '';
72                                $period_start = '';
73                        }
74                }
75        }
76
77        $error_msg .= "</B></FONT>";
78        // doe checks en set error en error_msg in case shit
79
80        if( $show_msg )
81                $tpl->assign( "form_error_msg", $error_msg );
82
83        return ($error ? 0 : 1 );
84}
85
86function makeTime( $time ) {
87
88        $days = intval( $time / 86400 );
89        $time = ($days>0) ? $time % ($days * 86400) : $time;
90
91        //printf( "time = %s, days = %s\n", $time, $days );
92
93        $date_str = '';
94        $day_str = '';
95
96        if( $days > 0 ) {
97                if( $days > 1 )
98                        $day_str .= $days . ' days';
99                else
100                        $day_str .= $days . ' day';
101        }
102
103        $hours = intval( $time / 3600 );
104        $time = $hours ? $time % ($hours * 3600) : $time;
105
106        //printf( "time = %s, days = %s, hours = %s\n", $time, $days, $hours );
107
108        if( $hours > 0 ) {
109                $date_str .= $hours . ':';
110                $date_unit = 'hours';
111        }
112
113        $minutes = intval( $time / 60 );
114        $seconds = $minutes ? $time % ($minutes * 60) : $time;
115
116        if( $minutes > 0 ) {
117
118                if( $minutes >= 10 )
119                        $date_str .= $minutes . ':';
120                else
121                        $date_str .= '0' . $minutes . ':';
122
123                $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
124        } else {
125                if($hours > 0 ) {
126                        $date_str .= '00:';
127                        $date_unit = (!isset($date_unit)) ? 'minutes' : $date_unit;
128                }
129        }
130
131
132        $date_unit = (!isset($date_unit)) ? 'seconds' : $date_unit;
133
134        if( $seconds > 0 ) {
135
136                if( $seconds >= 10 )
137                        $date_str .= $seconds . ' ' . $date_unit;
138                else
139                        $date_str .= '0' . $seconds . ' ' . $date_unit;
140
141        } else if ( $hours > 0 or $minutes > 0 )
142
143                $date_str .= '00 ' . $date_unit;
144
145        if( $days > 0) {
146
147                if( $hours > 0 or $minutes > 0 or $seconds > 0 )
148                        $date_str = $day_str . ' - ' . $date_str;
149                else
150                        $date_str = $day_str;
151        }
152
153        return $date_str;
154}
155
156//function makeDate( $time ) {
157//        return strftime( "%a %d %b %Y %H:%M:%S", $time );
158//}
159
160function datetimeToEpoch( $datetime ) {
161
162        //printf("datetime = %s\n", $datetime );
163        $datetime_fields = explode( ' ', $datetime );
164
165        $date = $datetime_fields[0];
166        $time = $datetime_fields[1];
167
168        $date_fields = explode( '-', $date );
169
170        $days = $date_fields[0];
171        $months = $date_fields[1];
172        $years = $date_fields[2];
173
174        //printf( "days = %s months = %s years = %s\n", $days, $months, $years );
175
176        $time_fields = explode( ':', $time );
177
178        $hours = $time_fields[0];
179        $minutes = $time_fields[1];
180        $seconds = $time_fields[2];
181
182        //printf( "hours = %s minutes = %s seconds = %s\n", $hours, $minutes, $seconds );
183
184        $timestamp = mktime( $hours, $minutes, $seconds, $months, $days, $years );
185
186        //printf( "timestamp = %s\n", $timestamp );
187
188        return $timestamp;
189}
190
191function timeToEpoch( $time ) {
192
193        $time_fields = explode( ':', $time );
194
195        if( count($time_fields) == 3 ) {
196
197                $hours = $time_fields[0];
198                $minutes = $time_fields[1];
199                $seconds = $time_fields[2];
200
201        } else if( count($time_fields) == 2 ) {
202
203                $hours = 0;
204                $minutes = $time_fields[0];
205                $seconds = $time_fields[1];
206
207        } else if( count($time_fields) == 1 ) {
208
209                $hours = 0;
210                $minutes = 0;
211                $seconds = $time_fields[0];
212        }
213
214        $myepoch = intval( $seconds + (intval( $minutes * 60 )) + (intval( $hours * 3600 )) );
215
216        return $myepoch;
217}
218
219function sortJobs( $jobs, $nodes, $sortby, $sortorder ) {
220
221        //printf("sortby = %s sortorder = %s\n", $sortby, $sortorder );
222
223        $sorted = array();
224
225        $cmp = create_function( '$a, $b',
226                "global \$sortby, \$sortorder;".
227
228                "if( \$a == \$b ) return 0;".
229
230                "if (\$sortorder==\"desc\")".
231                        "return ( \$a < \$b ) ? 1 : -1;".
232                "else if (\$sortorder==\"asc\")".
233                        "return ( \$a > \$b ) ? 1 : -1;" );
234
235        //print_r( $jobs );
236
237        foreach( $jobs as $jobid => $jobattrs ) {
238
239                        $state = $jobattrs[status];
240                        $user = $jobattrs[owner];
241                        $queue = $jobattrs[queue];
242                        $name = $jobattrs[name];
243                        $req_cpu = $jobattrs[requested_time];
244                        $req_memory = $jobattrs[requested_memory];
245
246                        //if( $state == 'R' )
247                        $mynodes = count( $nodes[$jobid] );
248                        //else
249                        //        $nodes = $jobattrs[nodes];
250
251                        $ppn = (int) $jobattrs[ppn] ? $jobattrs[ppn] : 1;
252                        $cpus = $mynodes * $ppn;
253                        $start_time = (int) $jobattrs[start_timestamp];
254                        $stop_time = (int) $jobattrs[stop_timestamp];
255                        $runningtime = $stop_time - $start_time;
256
257                        switch( $sortby ) {
258                                case "id":
259                                        $sorted[$jobid] = $jobid;
260                                        break;
261
262                                case "state":
263                                        $sorted[$jobid] = $state;
264                                        break;
265
266                                case "user":
267                                        $sorted[$jobid] = $user;
268                                        break;
269
270                                case "queue":
271                                        $sorted[$jobid] = $queue;
272                                        break;
273
274                                case "name":
275                                        $sorted[$jobid] = $name;
276                                        break;
277
278                                case "req_cpu":
279                                        $sorted[$jobid] = timeToEpoch( $req_cpu );
280                                        break;
281
282                                case "req_mem":
283                                        $sorted[$jobid] = $req_memory;
284                                        break;
285
286                                case "nodes":
287                                        $sorted[$jobid] = $mynodes;
288                                        break;
289
290                                case "cpus":
291                                        $sorted[$jobid] = $cpus;
292                                        break;
293
294                                case "start":
295                                        $sorted[$jobid] = $start_time;
296                                        break;
297
298                                case "finished":
299                                        $sorted[$jobid] = $stop_time;
300                                        break;
301
302                                case "runningtime":
303                                        $sorted[$jobid] = $runningtime;
304                                        break;
305
306                                default:
307                                        break;
308
309                        }
310        }
311
312        //uasort( $sorted, $cmp );
313        if( $sortorder == "asc" )
314                arsort( $sorted );
315        else if( $sortorder == "desc" )
316                asort( $sorted );
317
318        //print_r( $sorted );
319
320        return array_keys( $sorted );
321}
322
323function makeSearchPage() {
324        global $clustername, $tpl, $id, $user, $name, $start_from_time, $start_to_time, $queue;
325        global $end_from_time, $end_to_time, $filter, $default_showhosts, $m, $hosts_up, $hc;
326        global $period_start, $period_stop, $sortby, $sortorder, $COLUMN_REQUESTED_MEMORY;
327        global $SEARCH_RESULT_LIMIT, $COLUMN_NODES;
328
329        $metricname = $m;
330        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
331        //printf("start = %s stop = %s\n", $start, $stop );
332
333        $tpl->assign( "cluster", $clustername );
334        $tpl->assign( "id_value", $id );
335        $tpl->assign( "user_value", $user );
336        $tpl->assign( "queue_value", $queue );
337        $tpl->assign( "name_value", $name );
338        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
339        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
340        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
341        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
342
343        if( validateFormInput() ) {
344
345                $tpl->newBlock( "search_results" );
346                $tpl->assign( "sortby", $sortby);
347                $tpl->assign( "sortorder", $sortorder);
348                $tdb = new TarchDbase( "127.0.0.1" );
349                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
350                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
351                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
352                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
353                $search_ids = $tdb->searchDbase( $id, $queue, $user, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
354
355                if( ($tdb->resultcount) > (int) $SEARCH_RESULT_LIMIT ) {
356                        $tpl->gotoBlock( "_ROOT" );
357               
358                        $tpl->assign( "form_error_msg", "Got " . $tdb->resultcount . " search results, output limited to last " . $SEARCH_RESULT_LIMIT . " jobs." );
359                        $tpl->gotoBlock( "search_results" );
360                }
361
362                $jobs = array();
363                $nodes = array();
364
365                $even = 1;
366
367                //print_r( $search_ids );
368
369                foreach( $search_ids as $myid ) {
370
371                        //printf( "myid %s\n", $myid );
372                        $jobs[$myid] = $tdb->getJobArray( $myid );
373                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
374                }
375
376                if( $COLUMN_REQUESTED_MEMORY ) {
377                        $tpl->newBlock( "column_header_req_mem" );
378                }
379                if( $COLUMN_NODES ) {
380                        $tpl->newBlock( "column_header_nodes" );
381                }
382
383                //print_r( $nodes );
384                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
385
386                //print_r( $sorted_search );
387                foreach( $sorted_search as $sortid ) {
388
389                        $job = $jobs[$sortid];
390                        //print_r( $job );
391                        $foundid = $job[id];
392                        //printf( "foundid %s\n", $foundid );
393
394                        //$job = $tdb->getJobArray( $foundid );
395                        //$nodes = $tdb->getNodesForJob( $foundid );
396
397                        $tpl->newBlock( "node" );
398                        $tpl->assign( "id", $job[id] );
399                        $tpl->assign( "state", $job[status] );
400                        $tpl->assign( "user", $job[owner] );
401                        $tpl->assign( "queue", $job[queue] );
402                        $tpl->assign( "name", $job[name] );
403                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job[requested_time] ) ) );
404
405                        if( $COLUMN_REQUESTED_MEMORY ) {
406                                $tpl->newBlock( "column_req_mem" );
407                                //$tpl->assign( "req_memory", $jobs[$jobid][requested_memory] );
408                                $tpl->assign( "req_memory", $job[requested_memory] );
409                                $tpl->gotoBlock( "node" );
410                        }
411                        if( $COLUMN_NODES) {
412
413                                foreach( $nodes[$foundid] as $mynode )
414                                        $job_nodes[] = $mynode[hostname];
415
416                                $tpl->newBlock( "column_nodes" );
417                                $nodes_hostnames = implode( " ", $job_nodes );
418                                $tpl->assign( "nodes_hostnames", $nodes_hostnames );
419                                $tpl->gotoBlock( "node" );
420                        }
421
422                        $nodes_nr = count( $nodes[$foundid] );
423
424                        if( $even ) {
425
426                                $tpl->assign("nodeclass", "even");
427                                $even = 0;
428                        } else {
429
430                                $tpl->assign("nodeclass", "odd");
431                                $even = 1;
432                        }
433
434
435                        // need to replace later with domain stored from dbase
436                        //
437                        //$job_domain = $job[domain];
438
439                        //$myhost = $_SERVER[HTTP_HOST];
440                        //$myhf = explode( '.', $myhost );
441                        //$myhf = array_reverse( $myhf );
442                        //array_pop( $myhf );
443                        //$myhf = array_reverse( $myhf );
444                        //$job_domain = implode( '.', $myhf );
445                       
446                        //print_r( $job );
447                        //printf( "job domain = %s\n", $job_domain);
448                        $ppn = (int) $job[ppn] ? $job[ppn] : 1;
449                        $cpus = $nodes_nr * $ppn;
450
451                        $tpl->assign( "nodes", $nodes_nr );
452                        $tpl->assign( "cpus", $cpus );
453
454                        $job_start = $job[start_timestamp];
455                        $job_stop = $job[stop_timestamp];
456                        $runningtime = intval( $job_stop - $job_start );
457                        $tpl->assign( "started", makeDate( $job_start ) );
458                        $tpl->assign( "finished", makeDate( $job_stop ) );
459                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
460                       
461                        //print_r( $job );
462                        //print_r( $nodes );
463                }
464
465                if( count( $search_ids ) == 1 ) {
466
467                        $tpl->newBlock( "showhosts" );
468
469                        $showhosts = isset($sh) ? $sh : $default_showhosts;
470                        //if( !$showhosts) $showhosts = $default_showhosts;
471                        $tpl->assign("checked$showhosts", "checked");
472
473                        # Present a width list
474                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
475
476                        $hostcols = ($hc) ? $hc : 4;
477
478                        foreach(range(1,25) as $cols) {
479                                $cols_menu .= "<OPTION VALUE=$cols ";
480                                if ($cols == $hostcols)
481                                        $cols_menu .= "SELECTED";
482                                $cols_menu .= ">$cols\n";
483                        }
484                        $cols_menu .= "</SELECT>\n";
485
486                        $tpl->assign("metric","$metricname $units");
487                        $tpl->assign("id", $id);
488                        # Host columns menu defined in header.php
489                        $tpl->assign("cols_menu", $cols_menu);
490
491                        if( $showhosts ) {
492                                //bla
493
494                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
495                                //printf("start = %s stop = %s\n", $start, $stop );
496
497                                if( !$period_start ) // Add an extra 10% to graphstart
498                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
499                                else
500                                        $period_start = datetimeToEpoch( $period_start );
501
502                                if( !$period_stop ) // Add an extra 10% to graphend
503                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
504                                else
505                                        $period_stop = datetimeToEpoch( $period_stop );
506
507                                //printf("start = %s stop = %s\n", $start, $stop );
508
509                                $tpl->gotoBlock( "timeperiod" );
510
511                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
512                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
513
514                                $tpl->gotoBlock( "_ROOT" );
515
516                                $hosts_up = array();
517
518                                foreach( $nodes[$id] as $mynode )
519                                        $hosts_up[] = $mynode[hostname];
520
521                                $sorted_hosts = array();
522                                //$hosts_up = $jobs[$filter[id]][nodes];
523
524                                foreach ($hosts_up as $host ) {
525                                        //$host = $host. '.'.$job_domain;
526                                        $cpus = $metrics[$host]["cpu_num"][VAL];
527                                        if (!$cpus) $cpus=1;
528                                        $load_one  = $metrics[$host]["load_one"][VAL];
529                                        $load = ((float) $load_one)/$cpus;
530                                        $host_load[$host] = $load;
531                                        $percent_hosts[load_color($load)] += 1;
532                                        if ($metricname=="load_one")
533                                                $sorted_hosts[$host] = $load;
534                                        else
535                                                $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
536                                }
537                                switch ($sort) {
538                                        case "descending":
539                                                arsort($sorted_hosts);
540                                                break;
541                                        case "by hostname":
542                                                ksort($sorted_hosts);
543                                                break;
544                                        default:
545                                        case "ascending":
546                                                asort($sorted_hosts);
547                                                break;
548                                }
549
550                                //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
551
552                                # First pass to find the max value in all graphs for this
553                                # metric. The $start,$end variables comes from get_context.php,
554                                # included in index.php.
555                                list($min, $max) = find_limits($sorted_hosts, $metricname);
556
557                                # Second pass to output the graphs or metrics.
558                                $i = 1;
559                                foreach ( $sorted_hosts as $host=>$value  ) {
560                                        $tpl->newBlock ("sorted_list");
561                                        //$host = $host. '.'.$domain;
562                                        $host_url = rawurlencode($host);
563                                        $cluster_url = rawurlencode($clustername);
564
565                                        $textval = "";
566                                        //printf("host = %s, value = %s", $host, $value);
567                                        //echo "$host: $value, ";
568                                        $val = $metrics[$host][$metricname];
569                                        $class = "metric";
570                                        $host_link="\"?c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
571
572                                        if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
573                                                $textval = date("r", $val[VAL]);
574                                        } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
575                                                $textval = "$val[VAL] $val[UNITS]";
576                                        } else {
577                                                $graphargs = "z=small&c=$cluster_url&m=$metricname&h=$host_url&v=$val[VAL]&x=$max&n=$min&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop&min=$min&max=$max";
578                                        }
579                                        if ($textval) {
580                                                $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
581                                        } else {
582                                                $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
583                                        }
584
585                                        $tpl->assign("metric_image", $cell);
586                                        if (! ($i++ % $hostcols) )
587                                                 $tpl->assign ("br", "</tr><tr>");
588                                }
589
590                                //einde bla
591                        }
592                }
593
594        }
595}
596?>
Note: See TracBrowser for help on using the repository browser.