source: trunk/web/addons/toga/graph.php @ 195

Last change on this file since 195 was 193, checked in by bastiaans, 19 years ago

toga/graph.php:

  • Lower_limit on 0 for graphs

toga/overview.php:

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