php-collection: Add support for named graph lists as favorites
[collectd.git] / contrib / php-collection / index.php
1 <?php // vim:fenc=utf-8:filetype=php:ts=4
2 /*
3  * Copyright (C) 2009  Bruno PrĂ©mont <bonbons AT linux-vserver.org>
4  *
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.
8  *
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
12  * details.
13  *
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.
17  */
18
19 error_reporting(E_ALL | E_NOTICE | E_WARNING);
20
21 require('config.php');
22 require('functions.php');
23
24 /**
25  * Send back new list content
26  * @items Array of options values to return to browser
27  * @method Name of Javascript method that will be called to process data
28  */
29 function dhtml_response_list(&$items, $method) {
30         header("Content-Type: text/xml");
31
32         print('<?xml version="1.0" encoding="utf-8" ?>'."\n");
33         print("<response>\n");
34         printf(" <method>%s</method>\n", htmlspecialchars($method));
35         print(" <result>\n");
36         foreach ($items as &$item)
37                 printf('  <option>%s</option>'."\n", htmlspecialchars($item));
38         print(" </result>\n");
39         print("</response>");
40 }
41
42 /**
43  * Product page body with selection fields
44  */
45 function build_page() {
46         global $config;
47
48         if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/compatible; MSIE [0-9]+.[0-9];/', $_SERVER['HTTP_USER_AGENT'])) {
49                 // Internet Explorer does not support XHTML
50                 header("Content-Type: text/html");
51
52                 print('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">');
53                 print('<html lang="en">'."\n");
54         } else {
55                 header("Content-Type: application/xhtml+xml");
56
57                 print('<?xml version="1.0" encoding="utf-8" ?>'."\n");
58                 print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n");
59                 print('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">'."\n");
60         }
61         $url_base = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/';
62 ?>
63         <head>
64                 <title>Collectd graph viewer</title>
65                 <link rel="icon" href="favicon.png" type="image/png" />
66                 <style type="text/css">
67                         body, html { background-color: #EEEEEE; color: #000000; }
68                         h1 { text-align: center; }
69                         div.body { margin: auto; width: <?php echo isset($config['rrd_width']) ? 125+(int)$config['rrd_width'] : 600; ?>px; background: #FFFFFF; border: 1px solid #DDDDDD; }
70                         p.error { color: #CC0000; margin: 0em; }
71                         div.selector { margin: 0.5em 2em; }
72                         div.selectorbox { padding: 5px; border: 1px solid #CCCCCC; background-color: #F8F8F8; }
73                         div.selectorbox table { border: none; }
74                         div.selectorbox table td.s1 { border-bottom: 1px dashed #F0F0F0; padding-right: 1em; vertical-align: middle; }
75                         div.selectorbox table td.s2 { border-bottom: 1px dashed #F0F0F0; vertical-align: middle; }
76                         div.selectorbox table td.s3 { vertical-align: middle; }
77                         div.selectorbox table td.sc { padding: 0.5em 2em; text-align: center; }
78                         a img { border: none; }
79                         div.graphs { border-top: 1px solid #DDDDDD; padding: 5px; overflow: auto; }
80                         div.graphs_t { display: table; }
81                         div.graph { display: table-row; }
82                         div.graph_img { display: table-cell; vertical-align: middle; text-align: right; }
83                         div.graph_opts { display: table-cell; vertical-align: middle; text-align: center; line-height: 2em; }
84                         select { width: 100%; }
85                 </style>
86                 <script type="text/javascript">// <![CDATA[
87 var dhtml_url = '<?php echo addslashes($url_base.basename($_SERVER['PHP_SELF'])); ?>';
88 var graph_url = '<?php echo addslashes($url_base.'graph.php'); ?>';
89 //              ]]></script>
90                 <script type="text/javascript" src="<?php echo htmlspecialchars($url_base.'browser.js'); ?>"></script>
91         </head>
92
93         <body onload="ListRefreshHost(); GraphListRefresh(); "><div class="body">
94                 <h1><img src="collectd-logo.png" align="bottom" alt="" /> Collectd browser for system statistics graphs</h1>
95                 <div class="selector"><a href="javascript:toggleDiv('selector')"><span id="selector_sw">Hide</span> graph selection tool</a><div id="selector" class="selectorbox">
96                         <table>
97                                 <tr>
98                                         <td class="s1">Host:</td>
99                                         <td class="s2"><select id="host_list"   name="host"   disabled="disabled" onchange="ListRefreshPlugin()">
100                                         </select></td>
101                                         <td class="s3"><a href="javascript:ListRefreshHost()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh host list" /></a></td>
102                                 </tr>
103                                 <tr>
104                                         <td class="s1">Plugin:</td>
105                                         <td class="s2"><select id="plugin_list" name="plugin" disabled="disabled" onchange="ListRefreshPluginInstance()">
106                                         </select></td>
107                                         <td class="s3"><a href="javascript:ListRefreshPlugin()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh plugin list" /></a></td>
108                                 </tr>
109                                 <tr>
110                                         <td class="s1">Plugin instance:</td>
111                                         <td class="s2"><select id="pinst_list"  name="pinst"  disabled="disabled" onchange="ListRefreshType()">
112                                         </select></td>
113                                         <td class="s3"><a href="javascript:ListRefreshPluginInstance()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh plugin instance list" /></a></td>
114                                 </tr>
115                                 <tr>
116                                         <td class="s1">Type:</td>
117                                         <td class="s2"><select id="type_list"   name="type"   disabled="disabled" onchange="ListRefreshTypeInstance()">
118                                         </select></td>
119                                         <td class="s3"><a href="javascript:ListRefreshType()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh type list" /></a></td>
120                                 </tr>
121                                 <tr>
122                                         <td class="s1">Type instance:</td>
123                                         <td class="s2"><select id="tinst_list"  name="tinst"  disabled="disabled" onchange="RefreshButtons()">
124                                         </select></td>
125                                         <td class="s3"><a href="javascript:ListRefreshTypeInstance()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh type instance list" /></a></td>
126                                 </tr>
127                                 <tr>
128                                         <td class="s1">Graph settings:</td>
129                                         <td class="s2"><select id="timespan" name="timespan">
130 <?php                           foreach ($config['timespan'] as &$timespan)
131                                                 printf("\t\t\t\t\t\t<option value=\"%s\">%s</option>\n", htmlspecialchars($timespan['name']), htmlspecialchars($timespan['label']));
132 ?>                                      </select>
133                                         <br /><label><input id="logarithmic"  name="logarithmic" type="checkbox" value="1" /> Logarithmic scale</label>
134                                         <br /><label><input id="tinylegend"  name="tinylegend" type="checkbox" value="1" /> Minimal legend</label></td>
135                                         <td class="s3"></td>
136                                 </tr>
137                                 <tr>
138                                         <td class="sc" colspan="3"><input id="btnAdd"     name="btnAdd"     type="button" disabled="disabled" onclick="GraphAppend()" value="Add graph" />
139                                         <input id="btnClear"   name="btnClear"   type="button" disabled="disabled" onclick="GraphDropAll()" value="Remove all graphs" />
140                                         <input id="btnRefresh" name="btnRefresh" type="button" disabled="disabled" onclick="GraphRefresh(null)" value="Refresh all graphs" /></td>
141                                 </tr>
142                                 <tr>
143                                         <td class="s1" rowspan="2">Graph list favorites:</td>
144                                         <td class="s3"><input type="text" style="width: 100%" maxlength="30" id="GraphListName" name="GraphListName" value="default" onchange="GraphListCheckName(false)" /></td>
145                                         <td class="s3"><a href="javascript:GraphSave()"><img src="save.png" width="16" height="16" alt="S" title="Save graph list to cookie" /></a></td>
146                                 </tr>
147                                 <tr>
148                                         <td class="s2"><select id="GraphList" name="GraphList" onfocus="GraphListRefresh()">
149                                                 <option value="default">default</option>
150                                         </select></td>
151                                         <td class="s3"><a href="javascript:GraphLoad()"><img src="load.png" width="16" height="16" alt="L" title="Load graph list from cookie" /></a><a href="javascript:GraphDrop()"><img src="delete.png" width="16" height="16" alt="D" title="Delete graph list from cookie" /></a></td>
152                                 </tr>
153                         </table>
154                 </div></div>
155                 <div class="graphs"><div id="graphs" class="graphs_t">
156                         <div id="nograph">Please use above graph selection tool to add graphs to this area.<?php
157                         // Config checking
158                         if (!isset($config['datadirs']))
159                                 echo '<p class="error">Config error: $config["datadirs"] is not set</p>';
160                         else if (!is_array($config['datadirs']))
161                                 echo '<p class="error">Config error: $config["datadirs"] is not an array</p>';
162                         else if (count($config['datadirs']) == 0)
163                                 echo '<p class="error">Config error: $config["datadirs"] is empty</p>';
164                         else foreach ($config['datadirs'] as $datadir)
165                                 if (!is_dir($datadir))
166                                         echo '<p class="error">Config error: $config["datadirs"], '.htmlspecialchars($datadir).' does not exist</p>';
167                         if (!isset($config['rrd_width']))
168                                 echo '<p class="error">Config error: $config["rrd_width"] is not set</p>';
169                         else if (10 > (int)$config['rrd_width'])
170                                 echo '<p class="error">Config error: $config["rrd_width"] is invalid. Integer >= 10 expected</p>';
171                         if (!isset($config['rrd_height']))
172                                 echo '<p class="error">Config error: $config["rrd_height"] is not set</p>';
173                         else if (10 > (int)$config['rrd_height'])
174                                 echo '<p class="error">Config error: $config["rrd_height"] is invalid. Integer >= 10 expected</p>';
175                         if (!isset($config['rrd_opts']))
176                                 echo '<p class="error">Config error: $config["rrd_opts"] is not set</p>';
177                         else if (!is_array($config['rrd_opts']))
178                                 echo '<p class="error">Config error: $config["rrd_opts"] is not an array</p>';
179                         if (!isset($config['timespan']))
180                                 echo '<p class="error">Config error: $config["timespan"] is not set</p>';
181                         else if (!is_array($config['timespan']))
182                                 echo '<p class="error">Config error: $config["timespan"] is not an array</p>';
183                         else if (count($config['timespan']) == 0)
184                                 echo '<p class="error">Config error: $config["timespan"] is empty</p>';
185                         else foreach ($config['timespan'] as &$timespan)
186                                 if (!is_array($timespan) || !isset($timespan['name']) || !isset($timespan['label']) || !isset($timespan['seconds']) || 10 > (int)$timespan['seconds'])
187                                         echo '<p class="error">Config error: $config["timespan"], invalid entry found</p>';
188                         if (!is_null($config['collectd_sock']) && strncmp('unix://', $config['collectd_sock'], 7) != 0)
189                                 echo '<p class="error">Config error: $config["collectd_sock"] is not valid</p>';
190                         if (!defined('RRDTOOL'))
191                                 echo '<p class="error">Config error: RRDTOOL is not defined</p>';
192                         else if (!is_executable(RRDTOOL))
193                                 echo '<p class="error">Config error: RRDTOOL ('.htmlspecialchars(RRDTOOL).') is not executable</p>';
194                         ?></div>
195                 </div></div>
196         </div></body>
197 </html><?php
198 }
199
200
201 /*
202  * Select action based on user input
203  */
204 $action = read_var('action', $_POST, 'overview');
205 switch ($action) {
206         case 'list_hosts':
207                 // Generate a list of hosts
208                 $hosts = collectd_list_hosts();
209                 return dhtml_response_list($hosts, 'ListOfHost');
210
211         case 'list_plugins':
212                 // Generate list of plugins for selected hosts
213                 $arg_hosts = read_var('host', $_POST, array());
214                 if (!is_array($arg_hosts))
215                         $arg_hosts = array($arg_hosts);
216                 $plugins = collectd_list_plugins(reset($arg_hosts));
217                 return dhtml_response_list($plugins, 'ListOfPlugin');
218
219         case 'list_pinsts':
220                 // Generate list of plugin_instances for selected hosts and plugin
221                 $arg_hosts = read_var('host', $_POST, array());
222                 if (!is_array($arg_hosts))
223                         $arg_hosts = array($arg_hosts);
224                 $arg_plugin = read_var('plugin', $_POST, '');
225                 $pinsts = collectd_list_pinsts(reset($arg_hosts), $arg_plugin);
226                 return dhtml_response_list($pinsts, 'ListOfPluginInstance');
227
228         case 'list_types':
229                 // Generate list of types for selected hosts, plugin and plugin-instance
230                 $arg_hosts  = read_var('host', $_POST, array());
231                 if (!is_array($arg_hosts))
232                         $arg_hosts = array($arg_hosts);
233                 $arg_plugin = read_var('plugin', $_POST, '');
234                 $arg_pinst  = read_var('plugin_instance', $_POST, '');
235                 $types = collectd_list_types(reset($arg_hosts), $arg_plugin, $arg_pinst);
236                 return dhtml_response_list($types, 'ListOfType');
237
238         case 'list_tinsts':
239                 // Generate list of types for selected hosts, plugin and plugin-instance
240                 $arg_hosts  = read_var('host', $_POST, array());
241                 if (!is_array($arg_hosts))
242                         $arg_hosts = array($arg_hosts);
243                 $arg_plugin = read_var('plugin', $_POST, '');
244                 $arg_pinst  = read_var('plugin_instance', $_POST, '');
245                 $arg_type   = read_var('type', $_POST, '');
246                 $tinsts = collectd_list_tinsts(reset($arg_hosts), $arg_plugin, $arg_pinst, $arg_type);
247                 if (count($tinsts)) {
248                         require('definitions.php');
249                         load_graph_definitions();
250                         if (isset($MetaGraphDefs[$arg_type])) {
251                                 $meta_tinsts = array('@');
252                                 return dhtml_response_list($meta_tinsts, 'ListOfTypeInstance');
253                         }
254                 }
255                 return dhtml_response_list($tinsts, 'ListOfTypeInstance');
256
257         case 'overview':
258         default:
259                 return build_page();
260                 break;
261 }
262 ?>