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

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

job_monarch/templates/header.tpl:

job_monarch/image.php:

  • cache XML data during jobmond's polling interval

job_monarch/jobstore.php:

  • cache XML data during jobmond's polling interval
  • set host link

job_monarch/js/monarch.js:

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