source: branches/0.4/web/addons/job_monarch/graph.php @ 751

Last change on this file since 751 was 751, checked in by ramonb, 11 years ago

job_monarch/libtoga.php:

  • why call Ganglia's get_ganglia.php Gmetad() if we are already parsing ourself
  • now create $metrics ourself while TorqueXMLHandler.parseXML() runs
  • more speedup...

job_monarch/host_view.php,
job_monarch/graph.php:

  • dont need job information, so no DataGatherer?: call get_ganglia.php now for $metrics
  • Property svn:keywords set to Id
File size: 15.9 KB
RevLine 
[143]1<?php
[225]2/*
3 *
4 * This file is part of Jobmonarch
5 *
6 * Copyright (C) 2006  Ramon Bastiaans
7 *
8 * Jobmonarch is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Jobmonarch is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 *
[231]22 * SVN $Id: graph.php 751 2013-03-25 20:41:14Z ramonb $
[225]23 */
24
[751]25global $rrds, $range, $start, $r, $conf, $m;
[409]26$range = $r;
[143]27
[409]28include "./libtoga.php";
29
[751]30$my_dir = getcwd();
31
32global $context;
33
34$context = 'cluster';
35
36chdir( $GANGLIA_PATH );
37
38include "./ganglia.php";
39include "./get_ganglia.php";
40
41chdir( $my_dir );
42
[717]43if ( !empty( $_GET ) ) 
44{
[143]45        extract( $_GET );
46}
47
[409]48$sourcetime = $st;
49
[143]50# Graph specific variables
[731]51$size = escapeshellcmd( rawurldecode( $_GET["z"] ));
52$graph = escapeshellcmd( rawurldecode( $_GET["g"] ));
53$grid = escapeshellcmd( rawurldecode( $_GET["G"] ));
54$self = escapeshellcmd( rawurldecode( $_GET["me"] ));
55$max = escapeshellcmd( rawurldecode( $_GET["x"] ));
56$min = escapeshellcmd( rawurldecode( $_GET["n"] ));
57$value = escapeshellcmd( rawurldecode( $_GET["v"] ));
58$load_color = escapeshellcmd( rawurldecode( $_GET["l"] ));
59$vlabel = escapeshellcmd( rawurldecode( $_GET["vl"] ));
[143]60
[145]61$cluster = $c;
62$metricname = ($g) ? $g : $m;
63$hostname = $h;
64
[143]65# Assumes we have a $start variable (set in get_context.php).
[717]66if ($size == "small") 
67{
68    $height = 40;
69    $width = 130;
70} 
71else if ($size == "medium") 
72{
73    $height = 75;
74    $width = 300;
75} 
[731]76else if ($size == "overview-medium") 
77{
78    $height = 75;
79    $width = 300;
80} 
[717]81else 
82{
83    $height = 100;
84    $width = 400;
[145]85}
[143]86
[731]87$jobstart_color = "FF0000";
88
[717]89if($command) 
90{
[145]91      $command = '';
92}
[143]93
[145]94$graph = $metricname;
[143]95
[731]96$rrd_dirs = Array();
97
[717]98if (isset($graph)) 
99{
100    $series = '';
[739]101    if( $size == 'overview-medium' )
[717]102    {
[739]103        $rrd_dirs[] = $conf['rrds'] . '/' . $cluster .'/'. $hostname;
[717]104    }
[739]105    else
106    {
107        $trd = new TarchRrdGraph( $cluster, $hostname );
108        $rrd_dirs = $trd->getRrdDirs( $period_start, $period_stop );
109    }
[143]110
[717]111    if($graph == "cpu_report") 
112    {
[433]113
[717]114        $style = "CPU";
[143]115
[717]116        $upper_limit = "--upper-limit 100 --rigid";
117        $lower_limit = "--lower-limit 0";
[143]118
[717]119        $vertical_label = "--vertical-label Percent ";
[143]120
[717]121        $def_nr = 0;
[143]122
[717]123        foreach( $rrd_dirs as $rrd_dir ) 
124        {
[143]125
[717]126            if( $def_nr == 0 ) 
127            {
[143]128
[717]129                $user_str = ":'User CPU'";
130                $nice_str = ":'Nice CPU'";
131                $system_str = ":'System CPU'";
132                $wio_str = ":'WAIT CPU'";
133                $idle_str = ":'Idle CPU'";
134            } 
135            else 
136            {
[143]137
[717]138                $user_str = "";
139                $nice_str = "";
140                $system_str = "";
141                $wio_str = "";
142                $idle_str = "";
143            }
[143]144
[717]145            $series .= "DEF:'cpu_user${def_nr}'='${rrd_dir}/cpu_user.rrd':'sum':AVERAGE "
146                ."DEF:'cpu_nice${def_nr}'='${rrd_dir}/cpu_nice.rrd':'sum':AVERAGE "
147                ."DEF:'cpu_system${def_nr}'='${rrd_dir}/cpu_system.rrd':'sum':AVERAGE "
148                ."DEF:'cpu_idle${def_nr}'='${rrd_dir}/cpu_idle.rrd':'sum':AVERAGE "
[731]149                ."AREA:'cpu_user${def_nr}'#".$conf['cpu_user_color']."${user_str} "
150                ."STACK:'cpu_nice${def_nr}'#".$conf['cpu_nice_color']."${nice_str} "
151                ."STACK:'cpu_system${def_nr}'#".$conf['cpu_system_color']."${system_str} ";
[143]152
[717]153            if (file_exists("$rrd_dir/cpu_wio.rrd")) 
154            {
155                $series .= "DEF:'cpu_wio${def_nr}'='${rrd_dir}/cpu_wio.rrd':'sum':AVERAGE "
[731]156                    ."STACK:'cpu_wio${def_nr}'#".$conf['cpu_wio_color']."${wio_str} ";
[717]157            }
[143]158
[731]159            $series .= "STACK:'cpu_idle${def_nr}'#".$conf['cpu_idle_color']."${idle_str} ";
[143]160
[717]161            $def_nr++;
162        }
[143]163
[717]164    } 
165    else if ($graph == "job_report") 
166    {
167        $style = "Jobs";
[409]168
[717]169        $lower_limit = "--lower-limit 0 --rigid";
170        $vertical_label = "--vertical-label Jobs";
[409]171
[717]172        $def_nr = 0;
[409]173
[717]174        foreach( $metrics as $bhost => $bmetric )
175        {
176            foreach( $bmetric as $mname => $mval )
177            {
[726]178                if( ( $mname == 'zplugin_monarch_rj' ) || ($mname == 'zplugin_monarch_qj') )
[717]179                {
180                    $rjqj_host      = $bhost;
181                }
182            }
183        }
[409]184
[725]185        $rrd_dir = $conf['rrds'] . "/$clustername/$rjqj_host/";
[409]186
[726]187        $rj_rrd    = $rrd_dir . "zplugin_monarch_rj.rrd";
188        $qj_rrd    = $rrd_dir . "zplugin_monarch_qj.rrd";
[409]189
[717]190        $sorted_hosts    = array();
[738]191        $sorted_hosts[]  = $rjqj_host;
[409]192
[717]193        $rj_str = ":'Running Jobs'";
194        $qj_str = ":'Queued Jobs'";
[409]195
[717]196        $series .= "DEF:'running_jobs'='${rj_rrd}':'sum':AVERAGE "
197            ."DEF:'queued_jobs'='${qj_rrd}':'sum':AVERAGE "
[409]198
[717]199       
200            ."LINE3:'running_jobs'#ff0000${rj_str} "
201            ."LINE3:'queued_jobs'#999999${qj_str} ";
[409]202
[717]203    } 
204    else if ($graph == "mem_report") 
205    {
206        $style = "Memory";
[143]207
[717]208        $lower_limit = "--lower-limit 0 --rigid";
209        $extras = "--base 1024";
210        $vertical_label = "--vertical-label Bytes";
[143]211
[717]212        $def_nr = 0;
[143]213
[717]214        foreach( $rrd_dirs as $rrd_dir ) 
215        {
[143]216
[717]217            if( $def_nr == 0 ) 
218            {
[143]219
[717]220                $memuse_str = ":'Memory Used'";
221                $memshared_str = ":'Memory Shared'";
222                $memcached_str = ":'Memory Cached'";
223                $membuff_str = ":'Memory Buffered'";
224                $memswap_str = ":'Memory Swapped'";
225                $total_str = ":'Total In-Core Memory'";
226            } 
227            else 
228            {
[143]229
[717]230                $memuse_str = "";
231                $memshared_str = "";
232                $memcached_str = "";
233                $membuff_str = "";
234                $memswap_str = "";
235                $total_str = "";
236            }
[143]237
[717]238            $series .= "DEF:'mem_total${def_nr}'='${rrd_dir}/mem_total.rrd':'sum':AVERAGE "
239                ."CDEF:'bmem_total${def_nr}'=mem_total${def_nr},1024,* "
240                ."DEF:'mem_shared${def_nr}'='${rrd_dir}/mem_shared.rrd':'sum':AVERAGE "
241                ."CDEF:'bmem_shared${def_nr}'=mem_shared${def_nr},1024,* "
242                ."DEF:'mem_free${def_nr}'='${rrd_dir}/mem_free.rrd':'sum':AVERAGE "
243                ."CDEF:'bmem_free${def_nr}'=mem_free${def_nr},1024,* "
244                ."DEF:'mem_cached${def_nr}'='${rrd_dir}/mem_cached.rrd':'sum':AVERAGE "
245                ."CDEF:'bmem_cached${def_nr}'=mem_cached${def_nr},1024,* "
246                ."DEF:'mem_buffers${def_nr}'='${rrd_dir}/mem_buffers.rrd':'sum':AVERAGE "
247                ."CDEF:'bmem_buffers${def_nr}'=mem_buffers${def_nr},1024,* "
248                ."CDEF:'bmem_used${def_nr}'='bmem_total${def_nr}','bmem_shared${def_nr}',-,'bmem_free${def_nr}',-,'bmem_cached${def_nr}',-,'bmem_buffers${def_nr}',- "
[731]249                ."AREA:'bmem_used${def_nr}'#".$conf['mem_used_color']."${memuse_str} "
250                ."STACK:'bmem_shared${def_nr}'#".$conf['mem_shared_color']."${memshared_str} "
251                ."STACK:'bmem_cached${def_nr}'#".$conf['mem_cached_color']."${memcached_str} "
252                ."STACK:'bmem_buffers${def_nr}'#".$conf['mem_buffered_color']."${membuff_str} ";
[145]253
[717]254            if (file_exists("$rrd_dir/swap_total.rrd")) 
255            {
256                $series .= "DEF:'swap_total${def_nr}'='${rrd_dir}/swap_total.rrd':'sum':AVERAGE "
257                    ."DEF:'swap_free${def_nr}'='${rrd_dir}/swap_free.rrd':'sum':AVERAGE "
258                    ."CDEF:'bmem_swapped${def_nr}'='swap_total${def_nr}','swap_free${def_nr}',-,1024,* "
[731]259                    ."STACK:'bmem_swapped${def_nr}'#".$conf['mem_swapped_color']."${memswap_str} ";
[717]260            }
[145]261
[731]262            $series .= "LINE2:'bmem_total${def_nr}'#".$conf['cpu_num_color']."${total_str} ";
[145]263
[717]264            $def_nr++;
265        }
[145]266
[717]267    } 
268    else if ($graph == "load_report") 
269    {
270        $style = "Load";
[145]271
[717]272        $lower_limit = "--lower-limit 0 --rigid";
273        $vertical_label = "--vertical-label 'Load/Procs'";
[145]274
[717]275        $def_nr = 0;
[145]276
[717]277        foreach( $rrd_dirs as $rrd_dir ) 
278        {
[145]279
[717]280            if( $def_nr == 0 ) 
281            {
[145]282
[717]283                $load_str = ":'1-min Load'";
284                $cpu_str = ":'CPUs'";
285                $run_str = ":'Running Processes'";
286            } 
287            else 
288            {
289                $load_str = "";
290                $cpu_str = "";
291                $run_str = "";
292            }
[145]293
[717]294            $series .= "DEF:'load_one${def_nr}'='${rrd_dir}/load_one.rrd':'sum':AVERAGE "
295                ."DEF:'proc_run${def_nr}'='${rrd_dir}/proc_run.rrd':'sum':AVERAGE "
296                ."DEF:'cpu_num${def_nr}'='${rrd_dir}/cpu_num.rrd':'sum':AVERAGE ";
[731]297            $series .="AREA:'load_one${def_nr}'#".$conf['load_one_color']."${load_str} ";
298            $series .="LINE2:'cpu_num${def_nr}'#".$conf['cpu_num_color']."${cpu_str} ";
299            $series .="LINE2:'proc_run${def_nr}'#".$conf['proc_run_color']."${run_str} ";
[145]300
[717]301            $def_nr++;
302        }
[145]303
[717]304    } 
305    else if ($graph == "network_report") 
306    {
307        $style = "Network";
[145]308
[717]309        $lower_limit = "--lower-limit 0 --rigid";
310        $extras = "--base 1024";
311        $vertical_label = "--vertical-label 'Bytes/sec'";
[145]312
[717]313        $def_nr = 0;
[145]314
[717]315        foreach( $rrd_dirs as $rrd_dir ) 
316        {
[145]317
[717]318            if( $def_nr == 0 ) 
319            {
[145]320
[717]321                $in_str = ":'In'";
322                $out_str = ":'Out'";
323            } 
324            else 
325            {
[145]326
[717]327                $in_str = "";
328                $out_str = "";
329            }
[145]330
[717]331            $series .= "DEF:'bytes_in${def_nr}'='${rrd_dir}/bytes_in.rrd':'sum':AVERAGE "
332                ."DEF:'bytes_out${def_nr}'='${rrd_dir}/bytes_out.rrd':'sum':AVERAGE "
[731]333                ."LINE2:'bytes_in${def_nr}'#".$conf['mem_cached_color']."${in_str} "
334                ."LINE2:'bytes_out${def_nr}'#".$conf['mem_used_color']."${out_str} ";
[145]335
[717]336            $def_nr++;
337        }
[145]338
[717]339    } 
340    else if ($graph == "packet_report") 
341    {
342        $style = "Packets";
[145]343
[717]344        $lower_limit = "--lower-limit 0 --rigid";
345        $extras = "--base 1024";
346        $vertical_label = "--vertical-label 'Packets/sec'";
[145]347
[717]348        $def_nr = 0;
[145]349
[717]350        foreach( $rrd_dirs as $rrd_dir ) 
351        {
[145]352
[717]353            if( $def_nr == 0 ) 
354            {
[145]355
[717]356                $in_str = ":'In'";
357                $out_str = ":'Out'";
358            } 
359            else 
360            {
[145]361
[717]362                $in_str = "";
363                $out_str = "";
364            }
[145]365
[717]366            $series .= "DEF:'bytes_in${def_nr}'='${rrd_dir}/pkts_in.rrd':'sum':AVERAGE "
367                ."DEF:'bytes_out${def_nr}'='${rrd_dir}/pkts_out.rrd':'sum':AVERAGE "
[731]368                ."LINE2:'bytes_in${def_nr}'#".$conf['mem_cached_color']."${in_str} "
369                ."LINE2:'bytes_out${def_nr}'#".$conf['mem_used_color']."${out_str} ";
[145]370
[717]371            $def_nr++;
372        }
[145]373
[717]374    } 
375    else 
376    {
377        /* Custom graph */
378        $style = "";
[145]379
[717]380        $subtitle = $metricname;
[736]381        if($context == "host")
[717]382        {
383            if ($size == "small")
384                $prefix = $metricname;
385            else
386                $prefix = $hostname;
[145]387
[717]388            $value = $value>1000 ? number_format($value) : number_format($value, 2);
389        }
[145]390
[717]391        if (is_numeric($max))
392            $upper_limit = "--upper-limit '$max' ";
393        if (is_numeric($min))
394            $lower_limit ="--lower-limit '$min' ";
[145]395
[717]396        if ($vlabel)
397        {
398            $vertical_label = "--vertical-label '$vlabel'";
399        }
400        else 
401        {
402            if ($upper_limit or $lower_limit) 
403            {
404                $max = $max>1000 ? number_format($max) : number_format($max, 2);
405                $min = $min>0 ? number_format($min,2) : $min;
[145]406
[717]407                $vertical_label ="--vertical-label '$min - $max' ";
408            }
409        }
[145]410
[717]411        $def_nr = 0;
[145]412
[717]413        foreach( $rrd_dirs as $rrd_dir ) 
414        {
[145]415
[717]416            if( $def_nr == 0 ) 
417            {
418                $title_str = ":'${subtitle}'";
419            } 
420            else 
421            {
422                $title_str = "";
423            }
[145]424
[717]425            $rrd_file = "$rrd_dir/$metricname.rrd";
426            $series .= "DEF:'sum${def_nr}'='$rrd_file':'sum':AVERAGE "
[731]427                ."AREA:'sum${def_nr}'#".$conf['default_metric_color']."${title_str} ";
[145]428
[717]429            $def_nr++;
430        }
[145]431
[717]432    }
433    if( $series != '' ) 
434    {
435        if ($job_start)
436            $series .= "VRULE:${job_start}#${jobstart_color} ";
437        if ($job_stop)
438            $series .= "VRULE:${job_stop}#${jobstart_color} ";
439    }
[145]440}
441
442$title = "$hostname";
443
[717]444function determineXGrid( $p_start, $p_stop ) 
445{
[152]446
[717]447    $period = intval( $p_stop - $p_start );
[152]448
[717]449    // Syntax: <minor_grid_lines_time_declr>:<major_grid_lines_time_declr>:<labels_time_declr>:<offset>:<format>
450    //
451    // Where each <*time_declr*> = <time_type>:<time_interval>
[152]452
[717]453    //$my_lines1 = intval( $period / 3.0 );
454    //$my_lines2 = intval( $period / 6.0 );
[159]455
[717]456    //$my_grid = "SECOND:$my_lines2:SECOND:$my_lines1:SECOND:$my_lines1:0:%R";
[159]457
[717]458    //return "--x-grid $my_grid";
[159]459
[717]460    // Less than 1 minute
461    if( $period < 60 ) 
462    {
[152]463
[717]464        $tm_formt = "%X";
465        $my_grid = "SECOND:15:SECOND:30:SECOND:30:0:$tm_formt";
[152]466
[717]467    // Less than 10 minutes
468    } 
469    else if( $period < 600 ) 
470    {
[152]471
[717]472        $tm_formt = "%R";
473        $my_grid = "MINUTE:1:MINUTE:3:MINUTE:3:0:$tm_formt";
[152]474
[717]475    // Less than 1 hour
476    } 
477    else if( $period < 3600 ) 
478    {
[152]479
[717]480        $tm_formt = "%R";
481        $my_grid = "MINUTE:5:MINUTE:15:MINUTE:15:0:$tm_formt";
[152]482
[717]483    // Less than 15 hour
484    } 
485    else if( $period < 3600 ) 
486    {
[159]487
[717]488        $tm_formt = "%R";
489        $my_grid = "HOUR:1:HOUR:2:HOUR:2:0:$tm_formt";
[159]490
[717]491    // Less than 1 day
492    //
493    } 
494    else if( $period < 86400 ) 
495    {
[152]496
[717]497        $tm_formt = "%R";
498        $my_grid = "HOUR:2:HOUR:5:HOUR:5:0:$tm_formt";
[152]499
[717]500    // Less than 15 days
501    //
502    } 
503    else if( $period < 1296000 ) 
504    {
[152]505
[717]506        $tm_formt = "%e-%m";
507        $my_grid = "HOUR:1:DAY:3:DAY:3:0:'$tm_formt'";
508       
509    // Less than 30 days (a month)
510    //
511    } 
512    else if( $period < 2592000 ) 
513    {
[152]514
[717]515        $tm_formt = "%e-%m";
516        $my_grid = "DAY:5:DAY:10:DAY:10:0:'$tm_formt'";
517    }
[152]518
[717]519    if( isset( $my_grid ) ) 
520    {
[152]521
[717]522        $ret_str = "--x-grid $my_grid";
523        return array($ret_str,$tm_formt);
[152]524
[717]525    } 
526    else 
527    {
528        return array( "", "" );
529    }
[152]530}
531
[193]532$lower_limit = "--lower-limit 0";
[159]533
[734]534if( !isset( $load_color ) or ( $load_color == '') )
535{
536    $load_color = 'FFFFFF';
537}
538
[409]539# Calculate time range.
[733]540if ( isset($sourcetime) )
[409]541{
[717]542    $end = $sourcetime;
543    # Get_context makes start negative.
544    $start = $sourcetime + $start;
[409]545
[717]546    # Fix from Phil Radden, but step is not always 15 anymore.
547    if ($range=="month")
[733]548    {
[717]549        $end = floor($end / 672) * 672;
[733]550    }
551        $command = $conf['rrdtool']. " graph - --start $start --end $end ".
552                "--width $width --height $height $lower_limit ".
553                "--title '$title' $extras $background ".
554                $series;
[409]555}
[733]556else
[717]557{
558    $command = $conf['rrdtool'] . " graph - --start $period_start --end $period_stop ".
[733]559               "--width $width --height $height $lower_limit --color BACK#$load_color ".
560               "--title '$title' $extras $background ".
561               $series;
[409]562}
563
[143]564$debug=0;
565
[733]566
[143]567# Did we generate a command?   Run it.
[717]568if($command) 
569{
570    /*Make sure the image is not cached*/
571    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   // Date in the past
572    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
573    header ("Cache-Control: no-cache, must-revalidate");   // HTTP/1.1
574    header ("Pragma: no-cache");                     // HTTP/1.0
575    if ($debug) 
576    {
577        header ("Content-type: text/html");
578        print "$command\n\n\n\n\n";
579    } 
580    else 
581    {
582        header ("Content-type: image/gif");
583        passthru($command);
584    }
[145]585}
[143]586?>
Note: See TracBrowser for help on using the repository browser.