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

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

graph.php,
overview.php:

  • display range in title
  • removed RRDs column count: now done via css

templates/overview.tpl:

  • removed RRDs column count: now done via css

index.php:

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