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

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

graph.php:

  • fixed rrd dir: running/queued jobs graph working again

templates/footer.tpl:

  • renamed Ganglia to Ganglia web

libtoga.php:

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