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

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