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

Last change on this file since 231 was 231, checked in by bastiaans, 18 years ago

*.php:

  • added svn keywords propset
  • Property svn:keywords set to Id
File size: 17.1 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 231 2006-03-10 16:36:38Z 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
156function 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;
326        global $period_start, $period_stop, $sortby, $sortorder;
327
328        $metricname = $m;
329        //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
330        //printf("start = %s stop = %s\n", $start, $stop );
331
332        $tpl->assign( "cluster", $clustername );
333        $tpl->assign( "id_value", $id );
334        $tpl->assign( "user_value", $user );
335        $tpl->assign( "queue_value", $queue );
336        $tpl->assign( "name_value", $name );
337        $tpl->assign( "start_from_value", rawurldecode( $start_from_time ) );
338        $tpl->assign( "start_to_value", rawurldecode( $start_to_time ) );
339        $tpl->assign( "end_from_value", rawurldecode( $end_from_time ) );
340        $tpl->assign( "end_to_value", rawurldecode( $end_to_time ) );
341
342        if( validateFormInput() ) {
343
344                $tpl->newBlock( "search_results" );
345                $tpl->assign( "sortby", $sortby);
346                $tpl->assign( "sortorder", $sortorder);
347                $tdb = new TarchDbase( "127.0.0.1" );
348                if( $start_from_time ) $start_from_time = datetimeToEpoch( $start_from_time );
349                if( $start_to_time ) $start_to_time = datetimeToEpoch( $start_to_time );
350                if( $end_from_time ) $end_from_time = datetimeToEpoch( $end_from_time );
351                if( $end_to_time ) $end_to_time = datetimeToEpoch( $end_to_time );
352                $search_ids = $tdb->searchDbase( $id, $queue, $user, $name, $start_from_time, $start_to_time, $end_from_time, $end_to_time );
353
354                $jobs = array();
355                $nodes = array();
356
357                //print_r( $search_ids );
358
359                foreach( $search_ids as $myid ) {
360
361                        //printf( "myid %s\n", $myid );
362                        $jobs[$myid] = $tdb->getJobArray( $myid );
363                        $nodes[$myid] = $tdb->getNodesForJob( $myid );
364                }
365
366                //print_r( $nodes );
367                $sorted_search = sortJobs( $jobs, $nodes, $sortby, $sortorder );
368
369                //print_r( $sorted_search );
370                foreach( $sorted_search as $sortid ) {
371
372                        $job = $jobs[$sortid];
373                        //print_r( $job );
374                        $foundid = $job[id];
375                        //printf( "foundid %s\n", $foundid );
376
377                        //$job = $tdb->getJobArray( $foundid );
378                        //$nodes = $tdb->getNodesForJob( $foundid );
379
380                        $tpl->newBlock( "node" );
381                        $tpl->assign( "id", $job[id] );
382                        $tpl->assign( "state", $job[status] );
383                        $tpl->assign( "user", $job[owner] );
384                        $tpl->assign( "queue", $job[queue] );
385                        $tpl->assign( "name", $job[name] );
386                        $tpl->assign( "req_cpu", makeTime( TimeToEpoch( $job[requested_time] ) ) );
387                        $tpl->assign( "req_memory", $job[requested_memory] );
388
389                        $nodes_nr = count( $nodes[$foundid] );
390
391                        // need to replace later with domain stored from dbase
392                        //
393                        //$job_domain = $job[domain];
394
395                        //$myhost = $_SERVER[HTTP_HOST];
396                        //$myhf = explode( '.', $myhost );
397                        //$myhf = array_reverse( $myhf );
398                        //array_pop( $myhf );
399                        //$myhf = array_reverse( $myhf );
400                        //$job_domain = implode( '.', $myhf );
401                       
402                        //print_r( $job );
403                        //printf( "job domain = %s\n", $job_domain);
404                        $ppn = (int) $job[ppn] ? $job[ppn] : 1;
405                        $cpus = $nodes_nr * $ppn;
406
407                        $tpl->assign( "nodes", $nodes_nr );
408                        $tpl->assign( "cpus", $cpus );
409
410                        $job_start = $job[start_timestamp];
411                        $job_stop = $job[stop_timestamp];
412                        $runningtime = intval( $job_stop - $job_start );
413                        $tpl->assign( "started", makeDate( $job_start ) );
414                        $tpl->assign( "finished", makeDate( $job_stop ) );
415                        $tpl->assign( "runningtime", makeTime( $runningtime ) );
416                       
417                        //print_r( $job );
418                        //print_r( $nodes );
419                }
420
421                if( count( $search_ids ) == 1 ) {
422
423                        $tpl->newBlock( "showhosts" );
424
425                        $showhosts = isset($sh) ? $sh : $default_showhosts;
426                        //if( !$showhosts) $showhosts = $default_showhosts;
427                        $tpl->assign("checked$showhosts", "checked");
428
429                        # Present a width list
430                        $cols_menu = "<SELECT NAME=\"hc\" OnChange=\"archive_search_form.submit();\">\n";
431
432                        $hostcols = ($hc) ? $hc : 4;
433
434                        foreach(range(1,25) as $cols) {
435                                $cols_menu .= "<OPTION VALUE=$cols ";
436                                if ($cols == $hostcols)
437                                        $cols_menu .= "SELECTED";
438                                $cols_menu .= ">$cols\n";
439                        }
440                        $cols_menu .= "</SELECT>\n";
441
442                        $tpl->assign("metric","$metricname $units");
443                        $tpl->assign("id", $id);
444                        # Host columns menu defined in header.php
445                        $tpl->assign("cols_menu", $cols_menu);
446
447                        if( $showhosts ) {
448                                //bla
449
450                                //printf("job_start = %s job_stop = %s\n", $job_start, $job_stop );
451                                //printf("start = %s stop = %s\n", $start, $stop );
452
453                                if( !$period_start ) // Add an extra 10% to graphstart
454                                        $period_start = intval( $job_start - (intval( $runningtime * 0.10 ) ) );
455                                else
456                                        $period_start = datetimeToEpoch( $period_start );
457
458                                if( !$period_stop ) // Add an extra 10% to graphend
459                                        $period_stop = intval( $job_stop + (intval( $runningtime * 0.10 ) ) );
460                                else
461                                        $period_stop = datetimeToEpoch( $period_stop );
462
463                                //printf("start = %s stop = %s\n", $start, $stop );
464
465                                $tpl->gotoBlock( "timeperiod" );
466
467                                $tpl->assign("period_start", epochToDatetime( $period_start ) );
468                                $tpl->assign("period_stop", epochToDatetime( $period_stop ) );
469
470                                $tpl->gotoBlock( "_ROOT" );
471
472                                $hosts_up = array();
473
474                                foreach( $nodes[$id] as $mynode )
475                                        $hosts_up[] = $mynode[hostname];
476
477                                $sorted_hosts = array();
478                                //$hosts_up = $jobs[$filter[id]][nodes];
479
480                                foreach ($hosts_up as $host ) {
481                                        //$host = $host. '.'.$job_domain;
482                                        $cpus = $metrics[$host]["cpu_num"][VAL];
483                                        if (!$cpus) $cpus=1;
484                                        $load_one  = $metrics[$host]["load_one"][VAL];
485                                        $load = ((float) $load_one)/$cpus;
486                                        $host_load[$host] = $load;
487                                        $percent_hosts[load_color($load)] += 1;
488                                        if ($metricname=="load_one")
489                                                $sorted_hosts[$host] = $load;
490                                        else
491                                                $sorted_hosts[$host] = $metrics[$host][$metricname][VAL];
492                                }
493                                switch ($sort) {
494                                        case "descending":
495                                                arsort($sorted_hosts);
496                                                break;
497                                        case "by hostname":
498                                                ksort($sorted_hosts);
499                                                break;
500                                        default:
501                                        case "ascending":
502                                                asort($sorted_hosts);
503                                                break;
504                                }
505
506                                //$sorted_hosts = array_merge($down_hosts, $sorted_hosts);
507
508                                # First pass to find the max value in all graphs for this
509                                # metric. The $start,$end variables comes from get_context.php,
510                                # included in index.php.
511                                list($min, $max) = find_limits($sorted_hosts, $metricname);
512
513                                # Second pass to output the graphs or metrics.
514                                $i = 1;
515                                foreach ( $sorted_hosts as $host=>$value  ) {
516                                        $tpl->newBlock ("sorted_list");
517                                        //$host = $host. '.'.$domain;
518                                        $host_url = rawurlencode($host);
519                                        $cluster_url = rawurlencode($clustername);
520
521                                        $textval = "";
522                                        //printf("host = %s, value = %s", $host, $value);
523                                        //echo "$host: $value, ";
524                                        $val = $metrics[$host][$metricname];
525                                        $class = "metric";
526                                        $host_link="\"?c=$cluster_url&h=$host_url&job_start=$job_start&job_stop=$job_stop&period_start=$period_start&period_stop=$period_stop\"";
527
528                                        if ($val[TYPE]=="timestamp" or $always_timestamp[$metricname]) {
529                                                $textval = date("r", $val[VAL]);
530                                        } elseif ($val[TYPE]=="string" or $val[SLOPE]=="zero" or $always_constant[$metricname] or ($max_graphs > 0 and $i > $max_graphs )) {
531                                                $textval = "$val[VAL] $val[UNITS]";
532                                        } else {
533                                                $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";
534                                        }
535                                        if ($textval) {
536                                                $cell="<td class=$class>".  "<b><a href=$host_link>$host</a></b><br>".  "<i>$metricname:</i> <b>$textval</b></td>";
537                                        } else {
538                                                $cell="<td><a href=$host_link>".  "<img src=\"./graph.php?$graphargs\" ".  "alt=\"$host\" border=0></a></td>";
539                                        }
540
541                                        $tpl->assign("metric_image", $cell);
542                                        if (! ($i++ % $hostcols) )
543                                                 $tpl->assign ("br", "</tr><tr>");
544                                }
545
546                                //einde bla
547                        }
548                }
549
550        }
551}
552?>
Note: See TracBrowser for help on using the repository browser.