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

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