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

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

templates/footer.tpl:

  • changed URL for rrdtool

graph.php,
overview.php,
libtoga.php:

  • changed seperator to _

libtoga.php:

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