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

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

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

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