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

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

graph.php:

  • fixed GET variables
  • added overview graph size
  • set jobstart color
  • set rrd_dirs to current ganglia RRDs if overview-graph
  • fixed RRD colors to new Ganglia conf array

overview.php:

  • use our graph.php for RRDs
  • set job start and period start/end
  • Property svn:keywords set to Id
File size: 15.7 KB
Line 
1<?php
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 *
22 * SVN $Id: graph.php 731 2013-03-24 01:07:26Z ramonb $
23 */
24
25global $metrics, $rrds, $range, $start, $r, $conf;
26$range = $r;
27
28include "./libtoga.php";
29
30if ( !empty( $_GET ) ) 
31{
32        extract( $_GET );
33}
34
35$sourcetime = $st;
36
37# Graph specific variables
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"] ));
47
48$cluster = $c;
49$metricname = ($g) ? $g : $m;
50$hostname = $h;
51
52# Assumes we have a $start variable (set in get_context.php).
53if ($size == "small") 
54{
55    $height = 40;
56    $width = 130;
57} 
58else if ($size == "medium") 
59{
60    $height = 75;
61    $width = 300;
62} 
63else if ($size == "overview-medium") 
64{
65    $height = 75;
66    $width = 300;
67} 
68else 
69{
70    $height = 100;
71    $width = 400;
72}
73
74$jobstart_color = "FF0000";
75
76if($command) 
77{
78      $command = '';
79}
80
81$trd = new TarchRrdGraph( $cluster, $hostname );
82
83$graph = $metricname;
84
85$rrd_dirs = Array();
86
87if (isset($graph)) 
88{
89    $series = '';
90    if( isset( $period_start ) && isset( $period_stop ) )
91    {
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        }
100    }
101
102    if($graph == "cpu_report") 
103    {
104
105        $style = "CPU";
106
107        $upper_limit = "--upper-limit 100 --rigid";
108        $lower_limit = "--lower-limit 0";
109
110        $vertical_label = "--vertical-label Percent ";
111
112        $def_nr = 0;
113
114        foreach( $rrd_dirs as $rrd_dir ) 
115        {
116
117            if( $def_nr == 0 ) 
118            {
119
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            {
128
129                $user_str = "";
130                $nice_str = "";
131                $system_str = "";
132                $wio_str = "";
133                $idle_str = "";
134            }
135
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 "
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
144            if (file_exists("$rrd_dir/cpu_wio.rrd")) 
145            {
146                $series .= "DEF:'cpu_wio${def_nr}'='${rrd_dir}/cpu_wio.rrd':'sum':AVERAGE "
147                    ."STACK:'cpu_wio${def_nr}'#".$conf['cpu_wio_color']."${wio_str} ";
148            }
149
150            $series .= "STACK:'cpu_idle${def_nr}'#".$conf['cpu_idle_color']."${idle_str} ";
151
152            $def_nr++;
153        }
154
155    } 
156    else if ($graph == "job_report") 
157    {
158        $style = "Jobs";
159
160        $lower_limit = "--lower-limit 0 --rigid";
161        $vertical_label = "--vertical-label Jobs";
162
163        $def_nr = 0;
164
165        foreach( $metrics as $bhost => $bmetric )
166        {
167            foreach( $bmetric as $mname => $mval )
168            {
169                if( ( $mname == 'zplugin_monarch_rj' ) || ($mname == 'zplugin_monarch_qj') )
170                {
171                    $rjqj_host      = $bhost;
172                }
173            }
174        }
175
176        $rrd_dir = $conf['rrds'] . "/$clustername/$rjqj_host/";
177
178        $rj_rrd    = $rrd_dir . "zplugin_monarch_rj.rrd";
179        $qj_rrd    = $rrd_dir . "zplugin_monarch_qj.rrd";
180
181        $sorted_hosts    = array();
182        $sorted_hosts[]    = $rjqj_host;
183
184        $rj_str = ":'Running Jobs'";
185        $qj_str = ":'Queued Jobs'";
186
187        $series .= "DEF:'running_jobs'='${rj_rrd}':'sum':AVERAGE "
188            ."DEF:'queued_jobs'='${qj_rrd}':'sum':AVERAGE "
189
190       
191            ."LINE3:'running_jobs'#ff0000${rj_str} "
192            ."LINE3:'queued_jobs'#999999${qj_str} ";
193
194    } 
195    else if ($graph == "mem_report") 
196    {
197        $style = "Memory";
198
199        $lower_limit = "--lower-limit 0 --rigid";
200        $extras = "--base 1024";
201        $vertical_label = "--vertical-label Bytes";
202
203        $def_nr = 0;
204
205        foreach( $rrd_dirs as $rrd_dir ) 
206        {
207
208            if( $def_nr == 0 ) 
209            {
210
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            {
220
221                $memuse_str = "";
222                $memshared_str = "";
223                $memcached_str = "";
224                $membuff_str = "";
225                $memswap_str = "";
226                $total_str = "";
227            }
228
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}',- "
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} ";
244
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,* "
250                    ."STACK:'bmem_swapped${def_nr}'#".$conf['mem_swapped_color']."${memswap_str} ";
251            }
252
253            $series .= "LINE2:'bmem_total${def_nr}'#".$conf['cpu_num_color']."${total_str} ";
254
255            $def_nr++;
256        }
257
258    } 
259    else if ($graph == "load_report") 
260    {
261        $style = "Load";
262
263        $lower_limit = "--lower-limit 0 --rigid";
264        $vertical_label = "--vertical-label 'Load/Procs'";
265
266        $def_nr = 0;
267
268        foreach( $rrd_dirs as $rrd_dir ) 
269        {
270
271            if( $def_nr == 0 ) 
272            {
273
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            }
284
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 ";
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} ";
291
292            $def_nr++;
293        }
294
295    } 
296    else if ($graph == "network_report") 
297    {
298        $style = "Network";
299
300        $lower_limit = "--lower-limit 0 --rigid";
301        $extras = "--base 1024";
302        $vertical_label = "--vertical-label 'Bytes/sec'";
303
304        $def_nr = 0;
305
306        foreach( $rrd_dirs as $rrd_dir ) 
307        {
308
309            if( $def_nr == 0 ) 
310            {
311
312                $in_str = ":'In'";
313                $out_str = ":'Out'";
314            } 
315            else 
316            {
317
318                $in_str = "";
319                $out_str = "";
320            }
321
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 "
324                ."LINE2:'bytes_in${def_nr}'#".$conf['mem_cached_color']."${in_str} "
325                ."LINE2:'bytes_out${def_nr}'#".$conf['mem_used_color']."${out_str} ";
326
327            $def_nr++;
328        }
329
330    } 
331    else if ($graph == "packet_report") 
332    {
333        $style = "Packets";
334
335        $lower_limit = "--lower-limit 0 --rigid";
336        $extras = "--base 1024";
337        $vertical_label = "--vertical-label 'Packets/sec'";
338
339        $def_nr = 0;
340
341        foreach( $rrd_dirs as $rrd_dir ) 
342        {
343
344            if( $def_nr == 0 ) 
345            {
346
347                $in_str = ":'In'";
348                $out_str = ":'Out'";
349            } 
350            else 
351            {
352
353                $in_str = "";
354                $out_str = "";
355            }
356
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 "
359                ."LINE2:'bytes_in${def_nr}'#".$conf['mem_cached_color']."${in_str} "
360                ."LINE2:'bytes_out${def_nr}'#".$conf['mem_used_color']."${out_str} ";
361
362            $def_nr++;
363        }
364
365    } 
366    else 
367    {
368        /* Custom graph */
369        $style = "";
370
371        $subtitle = $metricname;
372        if ($context == "host") 
373        {
374            if ($size == "small")
375                $prefix = $metricname;
376            else
377                $prefix = $hostname;
378
379            $value = $value>1000 ? number_format($value) : number_format($value, 2);
380        }
381
382        if (is_numeric($max))
383            $upper_limit = "--upper-limit '$max' ";
384        if (is_numeric($min))
385            $lower_limit ="--lower-limit '$min' ";
386
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;
397
398                $vertical_label ="--vertical-label '$min - $max' ";
399            }
400        }
401
402        $def_nr = 0;
403
404        foreach( $rrd_dirs as $rrd_dir ) 
405        {
406
407            if( $def_nr == 0 ) 
408            {
409                $title_str = ":'${subtitle}'";
410            } 
411            else 
412            {
413                $title_str = "";
414            }
415
416            $rrd_file = "$rrd_dir/$metricname.rrd";
417            $series .= "DEF:'sum${def_nr}'='$rrd_file':'sum':AVERAGE "
418                ."AREA:'sum${def_nr}'#".$conf['default_metric_color']."${title_str} ";
419
420            $def_nr++;
421        }
422
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    }
431}
432
433$title = "$hostname";
434
435function determineXGrid( $p_start, $p_stop ) 
436{
437
438    $period = intval( $p_stop - $p_start );
439
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>
443
444    //$my_lines1 = intval( $period / 3.0 );
445    //$my_lines2 = intval( $period / 6.0 );
446
447    //$my_grid = "SECOND:$my_lines2:SECOND:$my_lines1:SECOND:$my_lines1:0:%R";
448
449    //return "--x-grid $my_grid";
450
451    // Less than 1 minute
452    if( $period < 60 ) 
453    {
454
455        $tm_formt = "%X";
456        $my_grid = "SECOND:15:SECOND:30:SECOND:30:0:$tm_formt";
457
458    // Less than 10 minutes
459    } 
460    else if( $period < 600 ) 
461    {
462
463        $tm_formt = "%R";
464        $my_grid = "MINUTE:1:MINUTE:3:MINUTE:3:0:$tm_formt";
465
466    // Less than 1 hour
467    } 
468    else if( $period < 3600 ) 
469    {
470
471        $tm_formt = "%R";
472        $my_grid = "MINUTE:5:MINUTE:15:MINUTE:15:0:$tm_formt";
473
474    // Less than 15 hour
475    } 
476    else if( $period < 3600 ) 
477    {
478
479        $tm_formt = "%R";
480        $my_grid = "HOUR:1:HOUR:2:HOUR:2:0:$tm_formt";
481
482    // Less than 1 day
483    //
484    } 
485    else if( $period < 86400 ) 
486    {
487
488        $tm_formt = "%R";
489        $my_grid = "HOUR:2:HOUR:5:HOUR:5:0:$tm_formt";
490
491    // Less than 15 days
492    //
493    } 
494    else if( $period < 1296000 ) 
495    {
496
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    {
505
506        $tm_formt = "%e-%m";
507        $my_grid = "DAY:5:DAY:10:DAY:10:0:'$tm_formt'";
508    }
509
510    if( isset( $my_grid ) ) 
511    {
512
513        $ret_str = "--x-grid $my_grid";
514        return array($ret_str,$tm_formt);
515
516    } 
517    else 
518    {
519        return array( "", "" );
520    }
521}
522
523$lower_limit = "--lower-limit 0";
524
525# Calculate time range.
526if( isset($sourcetime) )
527{
528    $end = $sourcetime;
529    # Get_context makes start negative.
530    $start = $sourcetime + $start;
531
532    # Fix from Phil Radden, but step is not always 15 anymore.
533    if ($range=="month")
534        $end = floor($end / 672) * 672;
535
536    $command = $conf['rrdtool']. " graph - --start $start --end $end ".
537        "--width $width --height $height $lower_limit ".
538        "--title '$title' $extras $background ".
539        $series;
540}
541else 
542{
543    $command = $conf['rrdtool'] . " graph - --start $period_start --end $period_stop ".
544        "--width $width --height $height $lower_limit ".
545        "--title '$title' $extras $background ".
546        $series;
547}
548
549$debug=0;
550
551# Did we generate a command?   Run it.
552if($command) 
553{
554    /*Make sure the image is not cached*/
555    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   // Date in the past
556    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
557    header ("Cache-Control: no-cache, must-revalidate");   // HTTP/1.1
558    header ("Pragma: no-cache");                     // HTTP/1.0
559    if ($debug) 
560    {
561        header ("Content-type: text/html");
562        print "$command\n\n\n\n\n";
563    } 
564    else 
565    {
566        header ("Content-type: image/gif");
567        passthru($command);
568    }
569}
570?>
Note: See TracBrowser for help on using the repository browser.