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

Last change on this file since 980 was 648, checked in by ramonb, 14 years ago

job_monarch/overview.php,
job_monarch/index.php:

  • old code cleanup

job_monarch/index.php,
job_monarch/image.php,
job_monarch/jobstore.php

  • rewrote session handling

job_monarch/image.php:

job_monarch/templates/header.tpl:

  • add session to clusterimage
  • (temp?) disabled pie chart

job_monarch/libtoga.php:

  • wrote new SessionHandler? class to handle all sessions
  • DataSource? now uses interactive port so can retrieve only relevant XML
  • disable XML_OPTION_CASE_FOLDING to speed up xml parsing
  • use jobmond poll interval to only reload XML when useful
  • use strpos instead of strstr, should be faster xml parsing
  • dont parse XML in ClusterImage? anymore, instead supply DataGather? object


job_monarch/conf.php:

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