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

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