source: trunk/web2/addons/job_monarch/jobstore.php @ 602

Last change on this file since 602 was 602, checked in by ramonb, 15 years ago

job_monarch/libtoga.php,
job_monarch/jobstore.php,
job_monarch/js/monarch.js:

  • changed host graph retrieval and display
File size: 16.2 KB
Line 
1<?php
2
3ini_set("memory_limit","200M");
4set_time_limit(0);
5
6$c                      = $_POST['c'];
7$clustername            = $c;
8$cluster                = $c;
9
10// Supplied by ExtJS when DataStore has remoteSort: true
11//
12$sortfield              = isset($_POST['sort'] ) ? $_POST['sort'] : "jid";
13$sortorder              = isset($_POST['dir'] ) ? $_POST['dir'] : "ASC"; // ASC or DESC
14
15// Search query from ext.SearchField
16//
17$query                  = isset($_POST['query']) ? $_POST['query'] : null;
18
19// Filter values
20//
21$jid                    = isset($_POST['jid']) ? $_POST['jid'] : null;
22$jids                   = isset($_POST['jids']) ? $_POST['jids'] : null;
23$owner                  = isset($_POST['owner']) ? $_POST['owner'] : null;
24$status                 = isset($_POST['status']) ? $_POST['status'] : null;
25$queue                  = isset($_POST['queue']) ? $_POST['queue'] : null;
26$host                   = isset($_POST['host']) ? $_POST['host'] : null;
27$metricname             = isset($_POST['metricname']) ? $_POST['metricname'] : 'load_one';
28
29if( $jids != null )
30{
31        $jobids = explode( ",", $jids );
32}
33else
34{
35        $jobids = null;
36}
37
38global $c, $clustername, $cluster, $metrics;
39
40// Grid Paging stuff
41//
42//$pstart       = (int) (isset($_POST['start']) ? $_POST['start'] : $_GET['pstart']);
43$pstart = (int) $_POST['start'];
44//$pend = (int) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['plimit']);
45$pend   = (int) $_POST['limit'];
46
47//echo $pend.'p ';
48// Need to fool Ganglia here: or it won't parse XML for our cluster
49//
50$HTTP_POST_VARS['c']    = $c;
51$_GET['c']              = $c;
52
53global $c, $clustername, $cluster;
54
55include_once "./libtoga.php";
56
57$ds             = new DataSource();
58$myxml_data     = &$ds->getData();
59
60session_start();
61unset( $_SESSION['data'] );
62$_SESSION['data']       = &$myxml_data;
63
64global $jobs, $metrics;
65
66$data_gatherer  = new DataGatherer( $clustername );
67$data_gatherer->parseXML( &$myxml_data );
68
69$heartbeat      = &$data_gatherer->getHeartbeat();
70$jobs           = &$data_gatherer->getJobs();
71//$gnodes         = $data_gatherer->getNodes();
72$cpus           = &$data_gatherer->getCpus();
73$use_fqdn       = &$data_gatherer->getUsingFQDN();
74
75// The ext grid script will send  a task field which will specify what it wants to do
76//$task = '';
77
78if( isset($_POST['task']) )
79{
80        $task = $_POST['task'];
81}
82if( isset( $HTTP_POST_VARS['task' ] ) )
83{
84        $task = $HTTP_POST_VARS['task'];
85}
86
87switch($task)
88{
89    case "GETJOBS":
90        getJobs();
91        break;         
92    case "GETNODES":
93        getNodes();
94        break;         
95    case "GETMETRICS":
96        getMetrics();
97        break;         
98    default:
99        echo "{failure:true}";
100        break;
101}
102
103function printCacheHeaders()
104{
105        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
106        header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
107        header ("Cache-Control: no-cache, must-revalidate");
108        header ("Pragma: no-cache");
109}
110
111function getMetrics( $host=null )
112{
113        global $metrics;
114
115        reset($metrics);
116        if( !$host)
117        {
118          $firsthost = key($metrics);
119        }
120        else
121        {
122          $firsthost = $host;
123        }
124
125        $first_metrics = $metrics[$firsthost];
126
127        $metric_list    = array();
128
129        $metric_count   = 0;
130
131        foreach( $first_metrics as $metricname => $metricval )
132        {
133                $metric         = array();
134                $metric['id']   = $metricname;
135                $metric['name'] = $metricname;
136
137                $metric_list[]  = $metric;
138                $metric_count   = $metric_count + 1;
139        }
140       
141        $results                = array();
142        $results['names']       = $metric_list;
143        $results['total']       = $metric_count;
144
145        printCacheHeaders();
146
147        $jsonresults    = JEncode( $results );
148
149        echo $jsonresults;
150
151        return 0;
152}
153
154function quickSearchJobs( $jobs, $query )
155{
156        $searchresults  = array();
157
158        foreach( $jobs as $jobid => $jobattrs )
159        {
160                if( $query != null )
161                {
162                        if( strpos( $jobattrs['jid'], $query ) !== false )
163                        {
164                                $searchresults[$jobid]  = $jobattrs;
165                        }
166                        if( strpos( $jobattrs['owner'], $query ) !== false )
167                        {
168                                $searchresults[$jobid]  = $jobattrs;
169                        }
170                        if( strpos( $jobattrs['queue'], $query ) !== false )
171                        {
172                                $searchresults[$jobid]  = $jobattrs;
173                        }
174                        if( strpos( $jobattrs['name'], $query ) !== false )
175                        {
176                                $searchresults[$jobid]  = $jobattrs;
177                        }
178                        if( is_array( $jobattrs['nodes'] ) )
179                        {
180                                foreach( $jobattrs['nodes'] as $jattr )
181                                {
182                                        if( strpos( $jattr, $query ) !== false )
183                                        {
184                                                $searchresults[$jobid]  = $jobattrs;
185                                        }
186                                }
187                        }
188                        if( strpos( $jobid, $query ) !== false )
189                        {
190                                $searchresults[$jobid]  = $jobattrs;
191                        }
192                }
193        }
194
195        return $searchresults;
196}
197
198function sortJobs( $jobs, $sortby, $sortorder )
199{
200        $sorted = array();
201
202        $cmp    = create_function( '$a, $b',
203                "global \$sortby, \$sortorder;".
204
205                "if( \$a == \$b ) return 0;".
206
207                "if (\$sortorder==\"DESC\")".
208                        "return ( \$a < \$b ) ? 1 : -1;".
209                "else if (\$sortorder==\"ASC\")".
210                        "return ( \$a > \$b ) ? 1 : -1;" );
211
212        if( isset( $jobs ) && count( $jobs ) > 0 )
213        {
214                foreach( $jobs as $jobid => $jobattrs )
215                {
216                                $state          = $jobattrs['status'];
217                                $user           = $jobattrs['owner'];
218                                $queue          = $jobattrs['queue'];
219                                $name           = $jobattrs['name'];
220                                $req_cpu        = $jobattrs['requested_time'];
221                                $req_memory     = $jobattrs['requested_memory'];
222
223                                $nodes          = $jobattrs['nodes'];
224
225                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
226
227                                if( $state == 'R' )
228                                {
229                                        $cpus           = count( $nodes ) * $ppn;
230                                }
231                                else
232                                {
233                                        $cpus           = ((int) $nodes ) * $ppn;
234                                }
235                                $queued_time    = (int) $jobattrs['queued_timestamp'];
236                                $start_time     = (int) $jobattrs['start_timestamp'];
237                                $runningtime    = $report_time - $start_time;
238
239                                switch( $sortby )
240                                {
241                                        case "jid":
242                                                $sorted[$jobid] = $jobid;
243                                                break;
244
245                                        case "status":
246                                                $sorted[$jobid] = $state;
247                                                break;
248
249                                        case "owner":
250                                                $sorted[$jobid] = $user;
251                                                break;
252
253                                        case "queue":
254                                                $sorted[$jobid] = $queue;
255                                                break;
256
257                                        case "name":
258                                                $sorted[$jobid] = $name;
259                                                break;
260
261                                        case "requested_time":
262                                                $sorted[$jobid] = timeToEpoch( $req_cpu );
263                                                break;
264
265                                        case "requested_memory":
266                                                $sorted[$jobid] = $req_memory;
267                                                break;
268
269                                        case "ppn":
270                                                $sorted[$jobid] = $ppn;
271                                                break;
272                                        case "nodect":
273                                                if( $state == 'Q' )
274                                                {
275                                                        $sorted[$jobid] = $nodes;
276                                                }
277                                                else
278                                                {
279                                                        $sorted[$jobid] = count( $nodes );
280                                                }
281                                                break;
282                                        case "cpus":
283                                                $sorted[$jobid] = $cpus;
284                                                break;
285
286                                        case "queued_timestamp":
287                                                $sorted[$jobid] = $queued_time;
288                                                break;
289
290                                        case "start_timestamp":
291                                                $sorted[$jobid] = $start_time;
292                                                break;
293
294                                        case "runningtime":
295                                                $sorted[$jobid] = $runningtime;
296                                                break;
297                                        case "nodes":
298                                                if( $state == 'R' )
299                                                {
300                                                        $sorted[$jobid] = $nodes[0];
301                                                }
302                                                else
303                                                {
304                                                        $sorted[$jobid] = $nodes;
305                                                }
306
307                                        default:
308                                                break;
309                                }
310                }
311        }
312
313        if( $sortorder == "ASC" )
314        {
315                asort( $sorted );
316        }
317        else if( $sortorder == "DESC" )
318        {
319                arsort( $sorted );
320        }
321
322        return $sorted;
323}
324
325function filterJobs( $jobs )
326{
327        global $jid, $owner, $queue,  $status, $host, $use_fqdn;
328
329        $filtered_jobs  = array();
330
331        if( isset( $jobs ) && count( $jobs ) > 0 )
332        {
333                foreach( $jobs as $jobid => $jobattrs )
334                {
335                                $state          = $jobattrs['status'];
336                                $user           = $jobattrs['owner'];
337                                $jqueue          = $jobattrs['queue'];
338                                $name           = $jobattrs['name'];
339                                $req_cpu        = $jobattrs['requested_time'];
340                                $req_memory     = $jobattrs['requested_memory'];
341
342                                if( $state == 'R' )
343                                {
344                                        $nodes = count( $jobattrs['nodes'] );
345
346                                        $mynodehosts = array();
347                                        foreach( $jobattrs['nodes'] as $mynode )
348                                        {
349                                                //if( $use_fqdn == 1)
350                                                //{
351                                                //      $mynode = $mynode.".".$jobattrs['domain'];
352                                                //}
353                                                $mynodehosts[]  = $mynode;
354                                        }
355                                        $jobattrs['nodes'] = $mynodehosts;
356                                }
357                                else
358                                {
359                                        $nodes = $jobattrs['nodes'];
360                                }
361
362                                $ppn            = (int) $jobattrs['ppn'] ? $jobattrs['ppn'] : 1;
363                                $cpus           = $nodes * $ppn;
364                                $queued_time    = (int) $jobattrs['queued_timestamp'];
365                                $start_time     = (int) $jobattrs['start_timestamp'];
366                                $runningtime    = $report_time - $start_time;
367
368                                $domain         = $jobattrs['domain'];
369                                $domain_len     = 0 - strlen( $domain );
370
371                                $keepjob        = true;
372
373                                if( $jid )
374                                {
375                                        if( $jobid != $jid )
376                                        {
377                                                $keepjob        = false;
378                                        }
379                                }
380                                else if( $host )
381                                {
382                                        if( $state == 'R' )
383                                        {
384                                                $jnodes         = $jobattrs['nodes'];
385
386                                                $keepjob        = false;
387
388                                                foreach( $jnodes as $jnode)
389                                                {
390                                                        if( $jnode == $host )
391                                                        {
392                                                                $keepjob = true;
393                                                        }
394                                                }
395                                        }
396                                        else
397                                        {
398                                                $keepjob        = false;
399                                        }
400                                }
401                                if( $owner )
402                                {
403                                        if( $user != $owner )
404                                        {
405                                                $keepjob        = false;
406                                        }
407                                }
408                                if( $queue )
409                                {
410                                        if( $jqueue != $queue )
411                                        {
412                                                $keepjob        = false;
413                                        }
414                                }
415                                if( $status )
416                                {
417                                        if( $state != $status )
418                                        {
419                                                $keepjob        = false;
420                                        }
421                                }
422                                if( $keepjob )
423                                {
424                                        $filtered_jobs[$jobid]  = $jobattrs;
425                                }
426                }
427        }
428
429        return $filtered_jobs;
430}
431
432function getNodes()
433{
434        global $jobs, $jobids, $clustername, $metrics, $jid, $metricname;
435        global $always_timestamp, $always_constant;
436
437        $display_nodes  = array();
438
439        printCacheHeaders();
440
441        if( !$jobids && !$jid )
442        {
443                // RB: todo replace with 0 result rows
444                //
445                printf("no jobid(s)\n");
446                return 1;
447        }
448
449        foreach( $jobs[$jid]['nodes'] as $jobnode )
450        {
451                if( !in_array( $jobnode, $display_nodes) )
452                {
453                        $display_nodes[]        = $jobnode;
454                }
455        }
456
457        $node_results   = array();
458        $result_count   = count( $display_nodes );
459
460        foreach( $display_nodes as $host )
461        {
462                $nr             = array();
463
464                $cpus           = $metrics[$host]['cpu_num']['VAL'];
465
466                //print_r( $jobs[$jid] );
467
468                if ( !$cpus )
469                {
470                        $cpus           = 1;
471                }
472
473                $load_one       = $metrics[$host]['load_one']['VAL'];
474                $load           = ((float) $load_one) / $cpus;
475                $load_color     = load_color($load);
476
477
478                // RB: something broken here with JR / JS
479                //
480                //$job_runtime  = intval( $jobs[$jid]['reported'] ) - intval( $jobs[$jid]['start_timestamp'] );
481                $job_runtime    = date( 'u' ) - intval( $jobs[$jid]['start_timestamp'] );
482                $job_window     = intval( $job_runtime ) * 1.2;
483
484                $jobrange       = -$job_window;
485                $jobstart       = $jobs[$jid]['start_timestamp'];
486
487                $nr['jid']      = $jid;
488
489                $hostar         = array( $host );
490
491                list($min,$max) = find_limits( $hostar, $metricname );
492
493                $host_url       = rawurlencode( $host );
494                $cluster_url    = rawurlencode( $clustername );
495
496                $textval        = "";
497
498                $val            = $metrics[$host][$metricname];
499
500                // RB: haven't used this yet: link to Ganglia's host overview
501                // maybe later to popup?
502                //
503                //$host_link      = "\"../../?c=$cluster_url&h=$host_url&r=job&jr=$jobrange&js=$jobstart\"";
504
505                if ( $val["TYPE"] == "timestamp" || $always_timestamp[$metricname] )
506                {
507                        $textval        = date( "r", $val["VAL"] );
508                }
509                elseif ( $val["TYPE"] == "string" || $val["SLOPE"] == "zero" || $always_constant[$metricname] )
510                {
511                        $textval        = $val["VAL"] . " " . $val["UNITS"];
512                }
513                else
514                {
515                        $graphargs      = ($reports[$metricname]) ? "g=$metricname&" : "m=$metricname&";
516                        $graphargs      .= "c=$cluster_url&h=$host_url&l=$load_color&v=".$val['VAL']."&r=job&jr=$jobrange&js=$jobstart";
517
518                        if( $max > 0 )
519                        {
520                                $graphargs      .= "&x=$max&n=$min";
521                        }
522                }
523
524                $nr['ga']       = $graphargs;
525
526                $node_results[] = $nr;
527        }
528
529
530        $jsonresults    = JEncode( $node_results );
531
532        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
533}
534
535function getJobs() 
536{
537        global $jobs, $hearbeat, $pstart, $pend;
538        global $sortfield, $sortorder, $query, $host;
539        global $jid, $owner, $queue,  $status;
540
541        $job_count              = count( $jobs );
542
543        printCacheHeaders();
544
545        if( $job_count == 0 )
546        {
547                echo '({"total":"0", "results":""})';
548                return 0;
549        }
550
551        $jobresults             = array();
552
553        $cur_job                = 0;
554
555        $sorted_jobs            = sortJobs( $jobs, $sortfield, $sortorder );
556
557        if( $query )
558        {
559                $jobs                   = quickSearchJobs( $jobs, $query );
560        }
561        if( $jid || $owner || $queue || $status || $host )
562        {
563                $jobs                   = filterJobs( $jobs );
564        }
565        $result_count           = count( $jobs );
566
567        foreach( $sorted_jobs as $jobid => $jobattrs )
568        {
569                //if( $jobattrs['reported'] != $heartbeat )
570                //{
571                //      continue;
572                //}
573
574                if( ! array_key_exists( $jobid, $jobs ) )
575                {
576                        continue;
577                }
578
579                $jr                     = array();
580                $jr['jid']              = strval( $jobid );
581                $jr['status']           = $jobs[$jobid]['status'];
582                $jr['owner']            = $jobs[$jobid]['owner'];
583                $jr['queue']            = $jobs[$jobid]['queue'];
584                $jr['name']             = $jobs[$jobid]['name'];
585                $jr['requested_time']   = makeTime( timeToEpoch( $jobs[$jobid]['requested_time'] ) );
586
587                if( $jr['status'] == 'R' )
588                {
589                        $nodes          = count( $jobs[$jobid]['nodes'] );
590                }
591                else
592                {
593                        $nodes          = (int) $jobs[$jobid]['nodes'];
594                }
595
596                $jr['ppn']              = strval( $jobs[$jobid]['ppn'] ? $jobs[$jobid]['ppn'] : 1 );
597                $jr['nodect']           = strval( $nodes );
598
599                if( $jr['status'] == 'R' )
600                {
601                        $jr['nodes']    = implode( ",", $jobs[$jobid]['nodes'] );
602                }
603                else
604                {
605                        $jr['nodes']    = "";
606                }
607
608                $jr['queued_timestamp'] = makeDate( $jobs[$jobid]['queued_timestamp'] );
609                $jr['start_timestamp']  = ($jobs[$jobid]['start_timestamp'] ? makeDate( $jobs[$jobid]['start_timestamp'] ) : "");
610
611                if( $jr['status'] == 'R' )
612                {
613                        $runningtime            = (int) $jobs[$jobid]['reported'] - (int) $jobs[$jobid]['start_timestamp'];
614                        $jr['runningtime']      = makeTime( $runningtime );
615                }
616                else
617                {
618                        $jr['runningtime']      = "";
619                }
620
621                if( ( $cur_job < $pstart ) || ( ($cur_job - $pstart) >= $pend ) )
622                {
623                        $cur_job        = $cur_job + 1;
624                        continue;
625                }
626                else
627                {
628                        $cur_job        = $cur_job + 1;
629                }
630
631                $jobresults[]           = $jr;
632        }
633
634        $jsonresults    = JEncode( $jobresults );
635
636
637        echo '{"total":"'. $result_count .'","results":'. $jsonresults .'}';
638
639        return 0;
640}
641
642// Encodes a SQL array into a JSON formated string: so that Javascript may understand it
643//
644function JEncode( $arr )
645{
646        if( version_compare( PHP_VERSION, "5.2", "<" ) )
647        {   
648                require_once( "./JSON.php" );           //if php<5.2 need JSON class
649
650                $json   = new Services_JSON();          //instantiate new json object
651                $data   = $json->encode( $arr );        //encode the data in json format
652        } 
653        else
654        {
655                $data   = json_encode( $arr );          //encode the data in json format
656        }
657
658        return $data;
659}
660
661?> 
Note: See TracBrowser for help on using the repository browser.