1 <?php // vim:fenc=utf-8:filetype=php:ts=4
3 * Copyright (C) 2009 Bruno Prémont <bonbons AT linux-vserver.org>
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 error_reporting(E_ALL | E_NOTICE | E_WARNING);
21 require('config.php');
22 require('functions.php');
23 require('definitions.php');
25 function makeTextBlock($text, $fontfile, $fontsize, $width) {
26 // TODO: handle explicit line-break!
27 $words = explode(' ', $text);
28 $lines = array($words[0]);
30 foreach ($words as $word) {
31 $lineSize = imagettfbbox($fontsize, 0, $fontfile, $lines[$currentLine] . ' ' . $word);
32 if($lineSize[2] - $lineSize[0] < $width) {
33 $lines[$currentLine] .= ' ' . $word;
36 $lines[$currentLine] = $word;
39 error_log(sprintf('Handles message "%s", %d words => %d/%d lines', $text, count($words), $currentLine, count($lines)));
40 return implode("\n", $lines);
44 * No RRD files found that could match request
45 * @code HTTP error code
46 * @code_msg Short text description of HTTP error code
47 * @title Title for fake RRD graph
48 * @msg Complete error message to display in place of graph content
50 function error($code, $code_msg, $title, $msg) {
52 header(sprintf("HTTP/1.0 %d %s", $code, $code_msg));
53 header("Pragma: no-cache");
54 header("Expires: Mon, 01 Jan 2008 00:00:00 CET");
55 header("Content-Type: image/png");
56 $w = $config['rrd_width']+81;
57 $h = $config['rrd_height']+79;
59 $png = imagecreate($w, $h);
60 $c_bkgnd = imagecolorallocate($png, 240, 240, 240);
61 $c_fgnd = imagecolorallocate($png, 255, 255, 255);
62 $c_blt = imagecolorallocate($png, 208, 208, 208);
63 $c_brb = imagecolorallocate($png, 160, 160, 160);
64 $c_grln = imagecolorallocate($png, 114, 114, 114);
65 $c_grarr = imagecolorallocate($png, 128, 32, 32);
66 $c_txt = imagecolorallocate($png, 0, 0, 0);
67 $c_etxt = imagecolorallocate($png, 64, 0, 0);
69 if (function_exists('imageantialias'))
70 imageantialias($png, true);
71 imagefilledrectangle($png, 0, 0, $w, $h, $c_bkgnd);
72 imagefilledrectangle($png, 51, 33, $w-31, $h-47, $c_fgnd);
73 imageline($png, 51, 30, 51, $h-43, $c_grln);
74 imageline($png, 48, $h-46, $w-28, $h-46, $c_grln);
75 imagefilledpolygon($png, array(49, 30, 51, 26, 53, 30), 3, $c_grarr);
76 imagefilledpolygon($png, array($w-28, $h-48, $w-24, $h-46, $w-28, $h-44), 3, $c_grarr);
77 imageline($png, 0, 0, $w, 0, $c_blt);
78 imageline($png, 0, 1, $w, 1, $c_blt);
79 imageline($png, 0, 0, 0, $h, $c_blt);
80 imageline($png, 1, 0, 1, $h, $c_blt);
81 imageline($png, $w-1, 0, $w-1, $h, $c_brb);
82 imageline($png, $w-2, 1, $w-2, $h, $c_brb);
83 imageline($png, 1, $h-2, $w, $h-2, $c_brb);
84 imageline($png, 0, $h-1, $w, $h-1, $c_brb);
86 imagestring($png, 4, ceil(($w-strlen($title)*imagefontwidth(4)) / 2), 10, $title, $c_txt);
87 imagestring($png, 5, 60, 35, sprintf('%s [%d]', $code_msg, $code), $c_etxt);
88 if (function_exists('imagettfbbox') && is_file($config['error_font'])) {
89 // Detailed error message
90 $fmt_msg = makeTextBlock($msg, $errorfont, 10, $w-86);
91 $fmtbox = imagettfbbox(12, 0, $errorfont, $fmt_msg);
92 imagettftext($png, 10, 0, 55, 35+3+imagefontwidth(5)-$fmtbox[7]+$fmtbox[1], $c_txt, $errorfont, $fmt_msg);
94 imagestring($png, 4, 53, 35+6+imagefontwidth(5), $msg, $c_txt);
102 * No RRD files found that could match request
104 function error404($title, $msg) {
105 return error(404, "Not found", $title, $msg);
109 * Incomplete / invalid request
111 function error400($title, $msg) {
112 return error(400, "Bad request", $title, $msg);
116 * Incomplete / invalid request
118 function error500($title, $msg) {
119 return error(500, "Internal error", $title, $msg);
122 // Process input arguments
123 $host = read_var('host', $_GET, null);
125 return error400("?/?-?/?", "Missing host name");
126 else if (!is_string($host))
127 return error400("?/?-?/?", "Expecting exactly 1 host name");
128 else if (strlen($host) == 0)
129 return error400("?/?-?/?", "Host name may not be blank");
131 $plugin = read_var('plugin', $_GET, null);
132 if (is_null($plugin))
133 return error400($host.'/?-?/?', "Missing plugin name");
134 else if (!is_string($plugin))
135 return error400($host.'/?-?/?', "Plugin name must be a string");
136 else if (strlen($plugin) == 0)
137 return error400($host.'/?-?/?', "Plugin name may not be blank");
139 $pinst = read_var('plugin_instance', $_GET, '');
140 if (!is_string($pinst))
141 return error400($host.'/'.$plugin.'-?/?', "Plugin instance name must be a string");
143 $type = read_var('type', $_GET, '');
145 return error400($host.'/'.$plugin.(strlen($pinst) ? '-'.$pinst : '').'/?', "Missing type name");
146 else if (!is_string($type))
147 return error400($host.'/'.$plugin.(strlen($pinst) ? '-'.$pinst : '').'/?', "Type name must be a string");
148 else if (strlen($type) == 0)
149 return error400($host.'/'.$plugin.(strlen($pinst) ? '-'.$pinst : '').'/?', "Type name may not be blank");
151 $tinst = read_var('type_instance', $_GET, '');
153 $graph_identifier = $host.'/'.$plugin.(strlen($pinst) ? '-'.$pinst : '').'/'.$type.(strlen($tinst) ? '-'.$tinst : '-*');
155 $timespan = read_var('timespan', $_GET, $config['timespan'][0]['name']);
156 $timespan_ok = false;
157 foreach ($config['timespan'] as &$ts)
158 if ($ts['name'] == $timespan)
161 return error400($graph_identifier, "Unknown timespan requested");
163 $logscale = (boolean)read_var('logarithmic', $_GET, false);
164 $tinylegend = (boolean)read_var('tinylegend', $_GET, false);
166 // Check that at least 1 RRD exists for the specified request
167 $all_tinst = collectd_list_types($host, $plugin, $pinst, $type);
168 if (count($all_tinst) == 0)
169 return error404($graph_identifier, "No rrd file found for graphing");
171 // Now that we are read, do the bulk work
172 load_graph_definitions($logscale, $tinylegend);
174 $pinst = strlen($pinst) == 0 ? null : $pinst;
175 $tinst = strlen($tinst) == 0 ? null : $tinst;
178 $opts['timespan'] = $timespan;
180 $opts['logarithmic'] = 1;
182 $opts['tinylegend'] = 1;
185 if ((is_null($tinst) || $tinst == '@merge') && isset($MetaGraphDefs[$type])) {
186 $identifiers = array();
187 foreach ($all_tinst as &$atinst)
188 $identifiers[] = collectd_identifier($host, $plugin, is_null($pinst) ? '' : $pinst, $type, $atinst);
189 collectd_flush($identifiers);
190 $rrd_cmd = $MetaGraphDefs[$type]($host, $plugin, $pinst, $type, $all_tinst, $opts);
192 if (!in_array(is_null($tinst) ? '' : $tinst, $all_tinst))
193 return error404($host.'/'.$plugin.(!is_null($pinst) ? '-'.$pinst : '').'/'.$type.(!is_null($tinst) ? '-'.$tinst : ''), "No rrd file found for graphing");
194 collectd_flush(collectd_identifier($host, $plugin, is_null($pinst) ? '' : $pinst, $type, is_null($tinst) ? '' : $tinst));
195 if (isset($GraphDefs[$type]))
196 $rrd_cmd = collectd_draw_generic($timespan, $host, $plugin, $pinst, $type, $tinst);
198 $rrd_cmd = collectd_draw_rrd($host, $plugin, $pinst, $type, $tinst);
201 if (isset($_GET['debug'])) {
202 header('Content-Type: text/plain; charset=utf-8');
203 printf("Would have executed:\n%s\n", $rrd_cmd);
205 } else if ($rrd_cmd) {
206 header('Content-Type: image/png');
207 header('Cache-Control: max-age=60');
209 passthru($rrd_cmd, $rt);
211 return error500($graph_identifier, "RRD failed to generate the graph: ".$rt);
214 return error500($graph_identifier, "Failed to tell RRD how to generate the graph");