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

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