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

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

job_monarch/libtoga.php:

  • why call Ganglia's get_ganglia.php Gmetad() if we are already parsing ourself
  • now create $metrics ourself while TorqueXMLHandler.parseXML() runs
  • more speedup...

job_monarch/host_view.php,
job_monarch/graph.php:

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