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

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