source: trunk/web/addons/job_monarch/jobstore.php @ 644

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

job_monarch/js/monarch.js:

  • fixed metric selection for graphs works now

job_monarch/jobstore.php:

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