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

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