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

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

graph.php:

  • fixed load color background

overview.php:

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