diff options
Diffstat (limited to 'inc')
-rw-r--r-- | inc/functions.inc.php | 70 | ||||
-rw-r--r-- | inc/html.inc.php | 116 |
2 files changed, 114 insertions, 72 deletions
diff --git a/inc/functions.inc.php b/inc/functions.inc.php index abe0763..9e56868 100644 --- a/inc/functions.inc.php +++ b/inc/functions.inc.php | |||
@@ -2,28 +2,66 @@ | |||
2 | 2 | ||
3 | # global functions | 3 | # global functions |
4 | 4 | ||
5 | function GET($index) { | 5 | function GET($index = NULL, $value = NULL) { |
6 | if (isset($_GET[$index])) | 6 | # parse all values from $_GET when no index is given |
7 | return $_GET[$index]; | 7 | if ($index === NULL) { |
8 | return null; | 8 | $arr = array(); |
9 | } | 9 | foreach($_GET as $i => $v) { |
10 | $arr[$i] = GET($i); | ||
11 | } | ||
12 | return $arr; | ||
13 | } | ||
14 | |||
15 | if (!isset($_GET[$index])) | ||
16 | return NULL; | ||
17 | |||
18 | if ($value === NULL) | ||
19 | $value = $_GET[$index]; | ||
10 | 20 | ||
11 | function validate_get($value, $type) { | 21 | $desc = array( |
12 | switch($type) { | 22 | 'h' => 'host', |
13 | case 'host': | 23 | 'p' => 'plugin', |
14 | if (!preg_match('/^[\w-.]+$/u', $value)) | 24 | 'c' => 'category', |
25 | 't' => 'type', | ||
26 | 'pi' => 'plugin instance', | ||
27 | 'ti' => 'type instance', | ||
28 | 's' => 'seconds', | ||
29 | 'x' => 'x-axis', | ||
30 | 'y' => 'y-axis', | ||
31 | ); | ||
32 | |||
33 | switch($index) { | ||
34 | case 'h': # host | ||
35 | if (!preg_match('/^[\w-.]+$/u', $value)) { | ||
36 | error_log(sprintf('Invalid %s in $_GET["%s"]: "%s"', $desc[$index], $index, $value)); | ||
37 | return NULL; | ||
38 | } | ||
39 | break; | ||
40 | case 'p': # plugin | ||
41 | case 'c': # category | ||
42 | case 't': # type | ||
43 | if (!preg_match('/^\w+$/u', $value)) { | ||
44 | error_log(sprintf('Invalid %s in $_GET["%s"]: "%s"', $desc[$index], $index, $value)); | ||
15 | return NULL; | 45 | return NULL; |
46 | } | ||
16 | break; | 47 | break; |
17 | case 'plugin': | 48 | case 'pi': # plugin instance |
18 | case 'category': | 49 | case 'ti': # type instance |
19 | case 'type': | 50 | if (!preg_match('/^[\w-]+$/u', $value)) { |
20 | if (!preg_match('/^\w+$/u', $value)) | 51 | error_log(sprintf('Invalid %s in $_GET["%s"]: "%s"', $desc[$index], $index, $value)); |
21 | return NULL; | 52 | return NULL; |
53 | } | ||
22 | break; | 54 | break; |
23 | case 'pinstance': | 55 | case 's': # seconds |
24 | case 'tinstance': | 56 | case 'x': # x-axis |
25 | if (!preg_match('/^[\w-]+$/u', $value)) | 57 | case 'y': # y-axis |
58 | if (!is_numeric($value)) { | ||
59 | error_log(sprintf('Invalid %s in $_GET["%s"]: "%s"', $desc[$index], $index, $value)); | ||
26 | return NULL; | 60 | return NULL; |
61 | } | ||
62 | break; | ||
63 | default: | ||
64 | return NULL; | ||
27 | break; | 65 | break; |
28 | } | 66 | } |
29 | 67 | ||
diff --git a/inc/html.inc.php b/inc/html.inc.php index 2d745ec..7312601 100644 --- a/inc/html.inc.php +++ b/inc/html.inc.php | |||
@@ -19,8 +19,11 @@ function html_start() { | |||
19 | <head> | 19 | <head> |
20 | <meta charset="utf-8"> | 20 | <meta charset="utf-8"> |
21 | <title>CGP{$path}</title> | 21 | <title>CGP{$path}</title> |
22 | <meta name="viewport" content="width=device-width"> | ||
22 | <link rel="stylesheet" href="{$html_weburl}layout/style.css" type="text/css"> | 23 | <link rel="stylesheet" href="{$html_weburl}layout/style.css" type="text/css"> |
23 | <meta name="viewport" content="width=1050, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes"> | 24 | <link rel="stylesheet" href="{$html_weburl}layout/style-b.css" type="text/css" media="(max-width: 1000px),(max-device-width: 1000px) and (orientation: portrait),(max-device-width: 767px) and (orientation: landscape)"> |
25 | <link rel="stylesheet" href="{$html_weburl}layout/style-c.css" type="text/css" media="(max-width: 767px),(max-device-width: 767px) and (orientation: portrait),(max-device-width: 499px) and (orientation: landscape)"> | ||
26 | <link rel="stylesheet" href="{$html_weburl}layout/style-d.css" type="text/css" media="(max-width: 499px),(max-device-width: 499px) and (orientation: portrait)"> | ||
24 | 27 | ||
25 | EOT; | 28 | EOT; |
26 | if (isset($CONFIG['page_refresh']) && is_numeric($CONFIG['page_refresh'])) { | 29 | if (isset($CONFIG['page_refresh']) && is_numeric($CONFIG['page_refresh'])) { |
@@ -91,32 +94,33 @@ echo <<<EOT | |||
91 | EOT; | 94 | EOT; |
92 | } | 95 | } |
93 | 96 | ||
94 | function html_end() { | 97 | function html_end($footer = false) { |
95 | global $CONFIG; | 98 | global $CONFIG; |
96 | 99 | ||
97 | $git = '/usr/bin/git'; | 100 | if ($footer) { |
98 | $changelog = $CONFIG['webdir'].'/doc/CHANGELOG'; | 101 | $git = '/usr/bin/git'; |
99 | 102 | $changelog = $CONFIG['webdir'].'/doc/CHANGELOG'; | |
100 | $version = 'v?'; | ||
101 | if (file_exists($git) && is_dir($CONFIG['webdir'].'/.git')) { | ||
102 | chdir($CONFIG['webdir']); | ||
103 | $version = exec($git.' describe --tags'); | ||
104 | } elseif (file_exists($changelog)) { | ||
105 | $changelog = file($changelog); | ||
106 | $version = explode(' ', $changelog[0]); | ||
107 | $version = 'v'.$version[0]; | ||
108 | } | ||
109 | 103 | ||
110 | $html_weburl = htmlentities($CONFIG['weburl']); | 104 | $version = 'v?'; |
105 | if (file_exists($git) && is_dir($CONFIG['webdir'].'/.git')) { | ||
106 | chdir($CONFIG['webdir']); | ||
107 | $version = exec($git.' describe --tags'); | ||
108 | } elseif (file_exists($changelog)) { | ||
109 | $changelog = file($changelog); | ||
110 | $version = explode(' ', $changelog[0]); | ||
111 | $version = 'v'.$version[0]; | ||
112 | } | ||
111 | 113 | ||
112 | echo <<<EOT | 114 | $html_weburl = htmlentities($CONFIG['weburl']); |
115 | |||
116 | echo <<<EOT | ||
113 | </div> | 117 | </div> |
114 | <div id="footer"> | 118 | <div id="footer"> |
115 | <hr><span class="small"><a href="http://pommi.nethuis.nl/category/cgp/" rel="external">Collectd Graph Panel</a> ({$version}) is distributed under the <a href="{$html_weburl}doc/LICENSE" rel="licence">GNU General Public License (GPLv3)</a></span> | 119 | <hr><span class="small"><a href="http://pommi.nethuis.nl/category/cgp/" rel="external">Collectd Graph Panel</a> ({$version}) is distributed under the <a href="{$html_weburl}doc/LICENSE" rel="license">GNU General Public License (GPLv3)</a></span> |
116 | </div> | 120 | </div> |
117 | 121 | ||
118 | EOT; | 122 | EOT; |
119 | 123 | } | |
120 | if ($CONFIG['graph_type'] == 'canvas') { | 124 | if ($CONFIG['graph_type'] == 'canvas') { |
121 | if ($CONFIG['rrd_fetch_method'] == 'async') { | 125 | if ($CONFIG['rrd_fetch_method'] == 'async') { |
122 | $js_async = 'true'; | 126 | $js_async = 'true'; |
@@ -221,7 +225,7 @@ function host_summary($cat, $hosts) { | |||
221 | 225 | ||
222 | printf('<fieldset id="%s">', htmlentities($cat)); | 226 | printf('<fieldset id="%s">', htmlentities($cat)); |
223 | printf('<legend>%s</legend>', htmlentities($cat)); | 227 | printf('<legend>%s</legend>', htmlentities($cat)); |
224 | echo "<table class=\"summary\">\n"; | 228 | echo "<div class=\"summary\">\n"; |
225 | 229 | ||
226 | $row_style = array(0 => "even", 1 => "odd"); | 230 | $row_style = array(0 => "even", 1 => "odd"); |
227 | $host_counter = 0; | 231 | $host_counter = 0; |
@@ -229,12 +233,14 @@ function host_summary($cat, $hosts) { | |||
229 | foreach($hosts as $host) { | 233 | foreach($hosts as $host) { |
230 | $host_counter++; | 234 | $host_counter++; |
231 | 235 | ||
232 | printf('<tr class="%s">', $row_style[$host_counter % 2]); | 236 | printf('<div class="row %s">', $row_style[$host_counter % 2]); |
233 | printf('<th><a href="%shost.php?h=%s">%s</a></th>', | 237 | printf('<label><a href="%shost.php?h=%s">%s</a></label>', |
234 | htmlentities($CONFIG['weburl']), | 238 | htmlentities($CONFIG['weburl']), |
235 | urlencode($host), | 239 | urlencode($host), |
236 | htmlentities($host)); | 240 | htmlentities($host)); |
237 | 241 | ||
242 | echo "<div class=\"hostinfo\">"; | ||
243 | |||
238 | if ($CONFIG['showload']) { | 244 | if ($CONFIG['showload']) { |
239 | require_once 'type/Default.class.php'; | 245 | require_once 'type/Default.class.php'; |
240 | $load = array('h' => $host, 'p' => 'load', 't' => 'load'); | 246 | $load = array('h' => $host, 'p' => 'load', 't' => 'load'); |
@@ -243,11 +249,8 @@ function host_summary($cat, $hosts) { | |||
243 | 249 | ||
244 | $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); | 250 | $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); |
245 | 251 | ||
246 | # ignore if file does not exist | 252 | if ($rrd_info && |
247 | if (!$rrd_info) | 253 | isset($rrd_info['ds[shortterm].last_ds']) && |
248 | continue; | ||
249 | |||
250 | if (isset($rrd_info['ds[shortterm].last_ds']) && | ||
251 | isset($rrd_info['ds[midterm].last_ds']) && | 254 | isset($rrd_info['ds[midterm].last_ds']) && |
252 | isset($rrd_info['ds[longterm].last_ds'])) { | 255 | isset($rrd_info['ds[longterm].last_ds'])) { |
253 | 256 | ||
@@ -256,11 +259,11 @@ function host_summary($cat, $hosts) { | |||
256 | foreach (array('ds[shortterm].last_ds', 'ds[midterm].last_ds', 'ds[longterm].last_ds') as $info) { | 259 | foreach (array('ds[shortterm].last_ds', 'ds[midterm].last_ds', 'ds[longterm].last_ds') as $info) { |
257 | $class = ''; | 260 | $class = ''; |
258 | if ($cores > 0 && $rrd_info[$info] > $cores * 2) | 261 | if ($cores > 0 && $rrd_info[$info] > $cores * 2) |
259 | $class = ' class="crit"'; | 262 | $class = ' crit'; |
260 | elseif ($cores > 0 && $rrd_info[$info] > $cores) | 263 | elseif ($cores > 0 && $rrd_info[$info] > $cores) |
261 | $class = ' class="warn"'; | 264 | $class = ' warn'; |
262 | 265 | ||
263 | printf('<td%s>%.2f</td>', $class, $rrd_info[$info]); | 266 | printf('<div class="field%s">%.2f</div>', $class, $rrd_info[$info]); |
264 | } | 267 | } |
265 | } | 268 | } |
266 | } | 269 | } |
@@ -272,56 +275,57 @@ function host_summary($cat, $hosts) { | |||
272 | $rrd_info_ca = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/memory/memory-cached.rrd'); | 275 | $rrd_info_ca = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/memory/memory-cached.rrd'); |
273 | 276 | ||
274 | # ignore if file does not exist | 277 | # ignore if file does not exist |
275 | if (!$rrd_info_mu || !$rrd_info_mf || !$rrd_info_bf || !$rrd_info_ca) | 278 | if ($rrd_info_mu && $rrd_info_mf && $rrd_info_bf && $rrd_info_ca) { |
276 | continue; | 279 | $info='ds[value].last_ds'; |
280 | if (isset($rrd_info_mu[$info]) && isset($rrd_info_mf[$info]) && isset($rrd_info_bf[$info]) && isset($rrd_info_ca[$info]) ) { | ||
281 | $percent_mem = $rrd_info_mu[$info] * 100 / ($rrd_info_mu[$info] + $rrd_info_mf[$info] + $rrd_info_bf[$info] + $rrd_info_ca[$info]); | ||
277 | 282 | ||
278 | $info='ds[value].last_ds'; | 283 | $class = ''; |
279 | if (isset($rrd_info_mu[$info]) && isset($rrd_info_mf[$info]) && isset($rrd_info_bf[$info]) && isset($rrd_info_ca[$info]) ) { | 284 | if ($percent_mem > 90) |
280 | $percent_mem = $rrd_info_mu[$info] * 100 / ($rrd_info_mu[$info] + $rrd_info_mf[$info] + $rrd_info_bf[$info] + $rrd_info_ca[$info]); | 285 | $class = ' crit'; |
281 | 286 | elseif ($percent_mem > 70) | |
282 | $class = ''; | 287 | $class = ' warn'; |
283 | if ($percent_mem > 90) | ||
284 | $class = ' class="crit"'; | ||
285 | elseif ($percent_mem > 70) | ||
286 | $class = ' class="warn"'; | ||
287 | 288 | ||
288 | printf('<td%s>%d%%</td>', $class, $percent_mem); | 289 | printf('<div class="field%s">%d%%</div>', $class, $percent_mem); |
290 | } | ||
289 | } | 291 | } |
290 | } | 292 | } |
291 | 293 | ||
292 | if ($CONFIG['showtime']) { | 294 | if ($CONFIG['showtime']) { |
293 | $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); | 295 | $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); |
294 | $time = time() - $rrd_info['last_update']; | 296 | if ($rrd_info) { |
297 | $time = time() - $rrd_info['last_update']; | ||
295 | 298 | ||
296 | $class = 'wide'; | 299 | $class = 'wide'; |
297 | if ($time > 300) | 300 | if ($time > 300) |
298 | $class .= ' crit'; | 301 | $class .= ' crit'; |
299 | elseif ($time > 60) | 302 | elseif ($time > 60) |
300 | $class .= ' warn'; | 303 | $class .= ' warn'; |
301 | 304 | ||
302 | printf('<td class="%s"><time class="timeago" datetime="%s">%d seconds ago</time></td>', | 305 | printf('<div class="field %s"><time class="timeago" datetime="%s">%d seconds ago</time></div>', |
303 | $class, date('c', $rrd_info['last_update']), $time); | 306 | $class, date('c', $rrd_info['last_update']), $time); |
307 | } | ||
304 | } | 308 | } |
305 | 309 | ||
306 | print "</tr>\n"; | 310 | print "</div></div>\n"; |
307 | } | 311 | } |
308 | 312 | ||
309 | echo "</table>\n"; | 313 | echo "</div>\n"; |
310 | echo "</fieldset>\n"; | 314 | echo "</fieldset>\n"; |
311 | } | 315 | } |
312 | 316 | ||
313 | 317 | ||
314 | function breadcrumbs() { | 318 | function breadcrumbs() { |
315 | $path = ''; | 319 | $path = ''; |
316 | if (validate_get(GET('h'), 'host')) | 320 | if (GET('h')) |
317 | $path .= ' - '.ucfirst(GET('h')); | 321 | $path .= ' - '.ucfirst(GET('h')); |
318 | if (validate_get(GET('p'), 'plugin')) | 322 | if (GET('p')) |
319 | $path .= ' - '.ucfirst(GET('p')); | 323 | $path .= ' - '.ucfirst(GET('p')); |
320 | if (validate_get(GET('pi'), 'pinstance')) | 324 | if (GET('pi')) |
321 | $path .= ' - '.GET('pi'); | 325 | $path .= ' - '.GET('pi'); |
322 | if (validate_get(GET('t'), 'type') && validate_get(GET('p'), 'plugin') && GET('t') != GET('p')) | 326 | if (GET('t') && GET('p') && GET('t') != GET('p')) |
323 | $path .= ' - '.GET('t'); | 327 | $path .= ' - '.GET('t'); |
324 | if (validate_get(GET('ti'), 'tinstance')) | 328 | if (GET('ti')) |
325 | $path .= ' - '.GET('ti'); | 329 | $path .= ' - '.GET('ti'); |
326 | 330 | ||
327 | return $path; | 331 | return $path; |