source: trunk/web/addons/job_monarch/graph.php @ 306

Last change on this file since 306 was 231, checked in by bastiaans, 18 years ago

*.php:

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