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

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