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

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

ALL:

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