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

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