From 5793a8cd003643974206e44ea752ab0966cfa8c0 Mon Sep 17 00:00:00 2001
From: Pim van den Berg
Date: Sat, 18 May 2013 16:05:43 +0200
Subject: integrate jsrrdgraph in CGP
---
.htaccess | 4 +-
conf/config.php | 3 +
detail.php | 7 ++-
inc/collectd.inc.php | 21 +++++--
inc/functions.inc.php | 15 +++++
inc/html.inc.php | 31 +++++++++++
js/CGP.js | 124 ++++++++++++++++++++++++++++++++++++++++++
rrd.php | 24 ++++++++
type/Default.class.php | 80 +++++++++++++++++----------
type/GenericIO.class.php | 18 +++---
type/GenericStacked.class.php | 16 +++---
type/Uptime.class.php | 16 +++---
12 files changed, 298 insertions(+), 61 deletions(-)
create mode 100644 js/CGP.js
create mode 100644 rrd.php
diff --git a/.htaccess b/.htaccess
index 956f31c..66eaee0 100644
--- a/.htaccess
+++ b/.htaccess
@@ -1,4 +1,6 @@
Options -Indexes
RewriteEngine On
-RewriteRule ^.git(ignore|/) - [F,L]
+RewriteRule ^.git(ignore|/) - [F,L]
+
+RewriteRule ^rrd/(.*) rrd.php?path=$1
diff --git a/conf/config.php b/conf/config.php
index 288e085..99190f1 100644
--- a/conf/config.php
+++ b/conf/config.php
@@ -42,6 +42,9 @@ $CONFIG['term'] = array(
# show graphs in bits or bytes
$CONFIG['network_datasize'] = 'bytes';
+# png or canvas graphs
+$CONFIG['graph_type'] = 'png';
+
# browser cache time for the graphs (in seconds)
$CONFIG['cache'] = 90;
diff --git a/detail.php b/detail.php
index 5aa38de..c7df632 100644
--- a/detail.php
+++ b/detail.php
@@ -47,7 +47,12 @@ foreach($CONFIG['term'] as $key => $s) {
}
print "\n";
-printf(''."\n", $CONFIG['weburl'], build_url('graph.php', $_GET));
+if ($CONFIG['graph_type'] == 'canvas') {
+ chdir($CONFIG['webdir']);
+ include $CONFIG['webdir'].'/plugin/'.$plugin.'.php';
+} else {
+ printf('
'."\n", $CONFIG['weburl'], build_url('graph.php', $_GET));
+}
echo '';
html_end();
diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php
index ad15774..1df8bb0 100644
--- a/inc/collectd.inc.php
+++ b/inc/collectd.inc.php
@@ -169,12 +169,21 @@ function graphs_from_plugin($host, $plugin, $overview=false) {
? $CONFIG['time_range'][$plugin]
: $CONFIG['time_range']['default'];
- printf('
'."\n",
- $CONFIG['weburl'],
- build_url('detail.php', $items, $time),
- $CONFIG['weburl'],
- build_url('graph.php', $items, $time)
- );
+ if ($CONFIG['graph_type'] == 'canvas') {
+ chdir($CONFIG['webdir']);
+ isset($items['p']) ? $_GET['p'] = $items['p'] : $_GET['p'] = '';
+ isset($items['pi']) ? $_GET['pi'] = $items['pi'] : $_GET['pi'] = '';
+ isset($items['t']) ? $_GET['t'] = $items['t'] : $_GET['t'] = '';
+ isset($items['ti']) ? $_GET['ti'] = $items['ti'] : $_GET['ti'] = '';
+ include $CONFIG['webdir'].'/plugin/'.$plugin.'.php';
+ } else {
+ printf('
'."\n",
+ $CONFIG['weburl'],
+ build_url('detail.php', $items, $time),
+ $CONFIG['weburl'],
+ build_url('graph.php', $items, $time)
+ );
+ }
}
}
diff --git a/inc/functions.inc.php b/inc/functions.inc.php
index a72a589..6642e51 100644
--- a/inc/functions.inc.php
+++ b/inc/functions.inc.php
@@ -30,6 +30,21 @@ function validate_get($value, $type) {
return $value;
}
+function validateRRDPath($base, $path) {
+ $realpath = realpath(sprintf('%s/%s', $base, $path));
+
+ if (strpos($realpath, $base) === false)
+ return false;
+
+ if (strpos($realpath, $base) !== 0)
+ return false;
+
+ if (!preg_match('/\.rrd$/', $realpath))
+ return false;
+
+ return $realpath;
+}
+
function crc32hex($str) {
return sprintf("%x",crc32($str));
}
diff --git a/inc/html.inc.php b/inc/html.inc.php
index a9e6a94..a0e2c8d 100644
--- a/inc/html.inc.php
+++ b/inc/html.inc.php
@@ -19,6 +19,26 @@ function html_start() {
'; - foreach ($graphdata as $d) { - printf("%s \\\n", $d); - } - print ''; - } else { - print '
'; - print_r($graphdata); - print ''; + $style = $debug !== false ? $debug : $this->graph_type; + switch ($style) { + case 'cmd': + print '
'; + foreach ($graphdata as $d) { + printf("%s \\\n", $d); + } + print ''; + break; + case 'canvas': + printf(''; + break; + case 'debug': + case 1: + print '
'; + print_r($graphdata); + print ''; + break; + case 'png': + default: + # caching + if (is_numeric($this->cache) && $this->cache > 0) + header("Expires: " . date(DATE_RFC822,strtotime($this->cache." seconds"))); + header("content-type: image/png"); + $graphdata = implode(' ', $graphdata); + echo `$graphdata`; + break; } } function rrd_options() { - $rrdgraph[] = $this->rrdtool; - $rrdgraph[] = 'graph - -a PNG'; + if ($this->graph_type != 'canvas') { + $rrdgraph[] = $this->rrdtool; + $rrdgraph[] = 'graph - -a PNG'; + } if ($this->rrdtool_opts != '') $rrdgraph[] = $this->rrdtool_opts; $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400); @@ -260,9 +284,9 @@ class Type_Default { $i=0; foreach ($this->tinstances as $tinstance) { foreach ($this->data_sources as $ds) { - $rrdgraph[] = sprintf('DEF:min_%s%s="%s":%s:MIN', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:avg_%s%s="%s":%s:AVERAGE', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:max_%s%s="%s":%s:MAX', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); $i++; } } @@ -292,11 +316,11 @@ class Type_Default { foreach ($sources as $source) { $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; $color = is_array($this->colors) ? (isset($this->colors[$source])?$this->colors[$source]:$this->colors[$c++]): $this->colors; - $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', crc32hex($source), $this->validate_color($color), $this->rrd_escape($dsname)); - $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\\l\'', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"LINE1:avg_%s#%s:%s"', crc32hex($source), $this->validate_color($color), $this->rrd_escape($dsname)); + $rrdgraph[] = sprintf('"GPRINT:min_%s:MIN:%s Min,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:avg_%s:AVERAGE:%s Avg,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:max_%s:MAX:%s Max,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:avg_%s:LAST:%s Last\\l"', crc32hex($source), $this->rrd_format); } return $rrdgraph; diff --git a/type/GenericIO.class.php b/type/GenericIO.class.php index 42314fd..0cf3e22 100644 --- a/type/GenericIO.class.php +++ b/type/GenericIO.class.php @@ -15,9 +15,9 @@ class Type_GenericIO extends Type_Default { $i=0; foreach ($this->tinstances as $tinstance) { foreach ($this->data_sources as $ds) { - $rrdgraph[] = sprintf('DEF:min_%s%s="%s":%s:MIN', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:avg_%s%s="%s":%s:AVERAGE', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:max_%s%s="%s":%s:MAX', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); if (!$this->scale) $rrdgraph[] = sprintf('VDEF:tot_%s=avg_%1$s,TOTAL', crc32hex($sources[$i])); $i++; @@ -52,12 +52,12 @@ class Type_GenericIO extends Type_Default { foreach($sources as $source) { $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; - $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', crc32hex($source), $this->colors[$source], $this->rrd_escape($dsname)); - $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:tot_%s:\'%s Total\l\'',crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"LINE1:avg_%s#%s:%s"', crc32hex($source), $this->colors[$source], $this->rrd_escape($dsname)); + $rrdgraph[] = sprintf('"GPRINT:min_%s:MIN:%s Min,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:avg_%s:AVERAGE:%s Avg,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:max_%s:MAX:%s Max,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:avg_%s:LAST:%s Last"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:tot_%s:%s Total\l"',crc32hex($source), $this->rrd_format); } return $rrdgraph; diff --git a/type/GenericStacked.class.php b/type/GenericStacked.class.php index 7c65ddd..5d1a556 100644 --- a/type/GenericStacked.class.php +++ b/type/GenericStacked.class.php @@ -15,9 +15,9 @@ class Type_GenericStacked extends Type_Default { $i=0; foreach ($this->tinstances as $tinstance) { foreach ($this->data_sources as $ds) { - $rrdgraph[] = sprintf('DEF:min_%s%s="%s":%s:MIN', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:avg_%s%s="%s":%s:AVERAGE', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:max_%s%s="%s":%s:MAX', crc32hex($sources[$i]), $raw, $this->rrd_escape($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', crc32hex($sources[$i]), $raw, $this->parse_filename($this->files[$tinstance]), $ds); $i++; } } @@ -51,11 +51,11 @@ class Type_GenericStacked extends Type_Default { foreach ($sources as $source) { $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; $color = is_array($this->colors) ? (isset($this->colors[$source])?$this->colors[$source]:$this->colors[$c++]) : $this->colors; - $rrdgraph[] = sprintf('LINE1:area_%s#%s:\'%s\'', crc32hex($source), $this->validate_color($color), $this->rrd_escape($dsname)); - $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', crc32hex($source), $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\\l\'', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"LINE1:area_%s#%s:%s"', crc32hex($source), $this->validate_color($color), $this->rrd_escape($dsname)); + $rrdgraph[] = sprintf('"GPRINT:min_%s:MIN:%s Min,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:avg_%s:AVERAGE:%s Avg,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:max_%s:MAX:%s Max,"', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"GPRINT:avg_%s:LAST:%s Last\\l"', crc32hex($source), $this->rrd_format); } return $rrdgraph; diff --git a/type/Uptime.class.php b/type/Uptime.class.php index aa91a6e..17bcb9a 100644 --- a/type/Uptime.class.php +++ b/type/Uptime.class.php @@ -12,8 +12,8 @@ class Type_Uptime extends Type_Default { $i=0; foreach ($this->tinstances as $tinstance) { foreach ($this->data_sources as $ds) { - $rrdgraph[] = sprintf('DEF:avg_%s="%s":%s:AVERAGE', crc32hex($sources[$i]), $this->rrd_escape($this->files[$tinstance]), $ds); - $rrdgraph[] = sprintf('DEF:max_%s="%s":%s:MAX', crc32hex($sources[$i]), $this->rrd_escape($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:avg_%s=%s:%s:AVERAGE', crc32hex($sources[$i]), $this->parse_filename($this->files[$tinstance]), $ds); + $rrdgraph[] = sprintf('DEF:max_%s=%s:%s:MAX', crc32hex($sources[$i]), $this->parse_filename($this->files[$tinstance]), $ds); $rrdgraph[] = sprintf('CDEF:c_avg_%s=avg_%1$s,86400,/', crc32hex($sources[$i])); $rrdgraph[] = sprintf('CDEF:c_max_%s=max_%1$s,86400,/', crc32hex($sources[$i])); @@ -45,16 +45,16 @@ class Type_Uptime extends Type_Default { $color = is_array($this->colors) ? (isset($this->colors[$source])?$this->colors[$source]:$this->colors[$c++]) : $this->colors; //current value - $rrdgraph[] = sprintf('LINE1:area_%s#%s:\'%s\'', crc32hex($source), $this->validate_color($color), $this->rrd_escape($dsname)); - $rrdgraph[] = sprintf('GPRINT:c_avg_%s:LAST:\'%s days\\l\'', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"LINE1:area_%s#%s:%s"', crc32hex($source), $this->validate_color($color), $this->rrd_escape($dsname)); + $rrdgraph[] = sprintf('"GPRINT:c_avg_%s:LAST:%s days\\l"', crc32hex($source), $this->rrd_format); //max value - $rrdgraph[] = sprintf('LINE1:v_max_%s#FF0000:\'Maximum\':dashes', crc32hex($source)); - $rrdgraph[] = sprintf('GPRINT:v_max_%s:\'%s days\\l\'', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"LINE1:v_max_%s#FF0000:Maximum:dashes"', crc32hex($source)); + $rrdgraph[] = sprintf('"GPRINT:v_max_%s:%s days\\l"', crc32hex($source), $this->rrd_format); //avg value - $rrdgraph[] = sprintf('LINE1:v_avg_%s#0000FF:\'Average\':dashes', crc32hex($source)); - $rrdgraph[] = sprintf('GPRINT:v_avg_%s:\'%s days\\l\'', crc32hex($source), $this->rrd_format); + $rrdgraph[] = sprintf('"LINE1:v_avg_%s#0000FF:Average:dashes"', crc32hex($source)); + $rrdgraph[] = sprintf('"GPRINT:v_avg_%s:%s days\\l"', crc32hex($source), $this->rrd_format); } return $rrdgraph; -- cgit v1.1