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

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