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

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

host_view.php,
templates/host_view.tpl:

graph.php:

  • set range as title if it's not job

libtoga.php:

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