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

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