contrib/php-collection: Add a PHP frontend for graph generation.
[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 ?>
62         <head>
63                 <title>Collectd graph viewer</title>
64                 <link rel="icon" href="favicon.png" type="image/png" />
65                 <style type="text/css">
66                         body, html { background-color: #EEEEEE; color: #000000; }
67                         h1 { text-align: center; }
68                         div.body { margin: auto; width: <?php echo 125+$config['rrd_width'] ?>px; background: #FFFFFF; border: 1px solid #DDDDDD; }
69                         div.selector { margin: 0.5em 2em; }
70                         div.selectorbox { padding: 5px; border: 1px solid #CCCCCC; background-color: #F8F8F8; }
71                         div.selectorbox table { border: none; }
72                         div.selectorbox table td.s1 { border-bottom: 1px dashed #F0F0F0; padding-right: 1em; vertical-align: middle; }
73                         div.selectorbox table td.s2 { border-bottom: 1px dashed #F0F0F0; vertical-align: middle; }
74                         div.selectorbox table td.s3 { vertical-align: middle; }
75                         div.selectorbox table td.sc { padding: 0.5em 2em; text-align: center; }
76                         a img { border: none; }
77                         div.graphs { border-top: 1px solid #DDDDDD; padding: 5px; overflow: auto; }
78                         div.graphs_t { display: table; }
79                         div.graph { display: table-row; }
80                         div.graph_img { display: table-cell; vertical-align: middle; text-align: right; }
81                         div.graph_opts { display: table-cell; vertical-align: middle; text-align: center; line-height: 2em; }
82                         select { width: 100%; }
83                 </style>
84                 <script type="text/javascript">// <![CDATA[
85 var dhtml_url = '<?php echo addslashes('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); ?>';
86 var graph_url = '<?php echo addslashes('http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/graph.php'); ?>';
87 //              ]]></script>
88                 <script type="text/javascript" src="browser.js"></script>
89         </head>
90
91         <body onload="ListRefreshHost()"><div class="body">
92                 <h1><img src="collectd-logo.png" align="bottom" alt="" /> Collectd browser for system statistics graphs</h1>
93                 <div class="selector"><a href="javascript:toggleDiv('selector')"><span id="selector_sw">Hide</span> graph selection tool</a><div id="selector" class="selectorbox">
94                         <table>
95                                 <tr>
96                                         <td class="s1">Host:</td>
97                                         <td class="s2"><select id="host_list"   name="host"   disabled="disabled" onchange="ListRefreshPlugin()">
98                                         </select></td>
99                                         <td class="s3"><a href="javascript:ListRefreshHost()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh host list" /></a></td>
100                                 </tr>
101                                 <tr>
102                                         <td class="s1">Plugin:</td>
103                                         <td class="s2"><select id="plugin_list" name="plugin" disabled="disabled" onchange="ListRefreshPluginInstance()">
104                                         </select></td>
105                                         <td class="s3"><a href="javascript:ListRefreshPlugin()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh plugin list" /></a></td>
106                                 </tr>
107                                 <tr>
108                                         <td class="s1">Plugin instance:</td>
109                                         <td class="s2"><select id="pinst_list"  name="pinst"  disabled="disabled" onchange="ListRefreshType()">
110                                         </select></td>
111                                         <td class="s3"><a href="javascript:ListRefreshPluginInstance()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh plugin instance list" /></a></td>
112                                 </tr>
113                                 <tr>
114                                         <td class="s1">Type:</td>
115                                         <td class="s2"><select id="type_list"   name="type"   disabled="disabled" onchange="ListRefreshTypeInstance()">
116                                         </select></td>
117                                         <td class="s3"><a href="javascript:ListRefreshType()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh type list" /></a></td>
118                                 </tr>
119                                 <tr>
120                                         <td class="s1">Type instance:</td>
121                                         <td class="s2"><select id="tinst_list"  name="tinst"  disabled="disabled" onchange="RefreshButtons()">
122                                         </select></td>
123                                         <td class="s3"><a href="javascript:ListRefreshTypeInstance()"><img src="refresh.png" width="16" height="16" alt="R" title="Refresh type instance list" /></a></td>
124                                 </tr>
125                                 <tr>
126                                         <td class="s1">Graph settings:</td>
127                                         <td class="s2"><select id="timespan" name="timespan">
128 <?php                           foreach ($config['timespan'] as &$timespan)
129                                                 printf("\t\t\t\t\t\t<option value=\"%s\">%s</option>\n", htmlspecialchars($timespan['name']), htmlspecialchars($timespan['label']));
130 ?>                                      </select>
131                                         <br /><label><input id="logarithmic"  name="logarithmic" type="checkbox" value="1" /> Logarithmic scale</label>
132                                         <br /><label><input id="tinylegend"  name="tinylegend" type="checkbox" value="1" /> Minimal legend</label></td>
133                                         <td class="s3"></td>
134                                 </tr>
135                                 <tr>
136                                         <td class="sc" colspan="3"><input id="btnAdd"     name="btnAdd"     type="button" disabled="disabled" onclick="GraphAppend()" value="Add graph" />
137                                         <input id="btnClear"   name="btnClear"   type="button" disabled="disabled" onclick="GraphDropAll()" value="Remove all graphs" />
138                                         <input id="btnRefresh" name="btnRefresh" type="button" disabled="disabled" onclick="GraphRefresh(null)" value="Refresh all graphs" /></td>
139                                 </tr>
140                         </table>
141                 </div></div>
142                 <div class="graphs"><div id="graphs" class="graphs_t">
143                         <div id="nograph">Please use above graph selection tool to add graphs to this area.</div>
144                 </div></div>
145         </div></body>
146 </html><?php
147 }
148
149
150 /*
151  * Select action based on user input
152  */
153 $action = read_var('action', $_POST, 'overview');
154 switch ($action) {
155         case 'list_hosts':
156                 // Generate a list of hosts
157                 $hosts = collectd_list_hosts();
158                 return dhtml_response_list($hosts, 'ListOfHost');
159
160         case 'list_plugins':
161                 // Generate list of plugins for selected hosts
162                 $arg_hosts = read_var('host', $_POST, array());
163                 if (!is_array($arg_hosts))
164                         $arg_hosts = array($arg_hosts);
165                 $plugins = collectd_list_plugins(reset($arg_hosts));
166                 return dhtml_response_list($plugins, 'ListOfPlugin');
167
168         case 'list_pinsts':
169                 // Generate list of plugin_instances for selected hosts and plugin
170                 $arg_hosts = read_var('host', $_POST, array());
171                 if (!is_array($arg_hosts))
172                         $arg_hosts = array($arg_hosts);
173                 $arg_plugin = read_var('plugin', $_POST, '');
174                 $pinsts = collectd_list_pinsts(reset($arg_hosts), $arg_plugin);
175                 return dhtml_response_list($pinsts, 'ListOfPluginInstance');
176
177         case 'list_types':
178                 // Generate list of types for selected hosts, plugin and plugin-instance
179                 $arg_hosts  = read_var('host', $_POST, array());
180                 if (!is_array($arg_hosts))
181                         $arg_hosts = array($arg_hosts);
182                 $arg_plugin = read_var('plugin', $_POST, '');
183                 $arg_pinst  = read_var('plugin_instance', $_POST, '');
184                 $types = collectd_list_types(reset($arg_hosts), $arg_plugin, $arg_pinst);
185                 return dhtml_response_list($types, 'ListOfType');
186
187         case 'list_tinsts':
188                 // Generate list of types for selected hosts, plugin and plugin-instance
189                 $arg_hosts  = read_var('host', $_POST, array());
190                 if (!is_array($arg_hosts))
191                         $arg_hosts = array($arg_hosts);
192                 $arg_plugin = read_var('plugin', $_POST, '');
193                 $arg_pinst  = read_var('plugin_instance', $_POST, '');
194                 $arg_type   = read_var('type', $_POST, '');
195                 $tinsts = collectd_list_tinsts(reset($arg_hosts), $arg_plugin, $arg_pinst, $arg_type);
196                 if (count($tinsts)) {
197                         require('definitions.php');
198                         load_graph_definitions();
199                         if (isset($MetaGraphDefs[$arg_type])) {
200                                 $meta_tinsts = array('@');
201                                 return dhtml_response_list($meta_tinsts, 'ListOfTypeInstance');
202                         }
203                 }
204                 return dhtml_response_list($tinsts, 'ListOfTypeInstance');
205
206         case 'overview':
207         default:
208                 return build_page();
209                 break;
210 }
211 ?>