aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/graph.php
diff options
context:
space:
mode:
authorPim van den Berg2014-05-03 19:17:16 +0200
committerPim van den Berg2014-05-12 21:32:24 +0200
commit0a547add2f4cc264380d2dab2c472efe5a1d7094 (patch)
tree55bb43a3e31c5814848d61eea92c8438e4a37886 /graph.php
parenttype/base: set default title to "Plugin Type (PluginInstance) (Category)" (diff)
downloadapt-panopticon_cgp-0a547add2f4cc264380d2dab2c472efe5a1d7094.zip
apt-panopticon_cgp-0a547add2f4cc264380d2dab2c472efe5a1d7094.tar.gz
apt-panopticon_cgp-0a547add2f4cc264380d2dab2c472efe5a1d7094.tar.bz2
apt-panopticon_cgp-0a547add2f4cc264380d2dab2c472efe5a1d7094.tar.xz
graph.php: use JSON plugins instead of including PHP plugin files
A couple of big changes here. A lot of logic moved to graph.php. The PHP plugin files have been rewritten to JSON. In these JSON files *everything* is optional. Also *NOT* having a JSON plugin file won't block you from having a graph. The JSON will just make the graphs prettier (by having a title, y-axis title, legend, colors, etc..). The Collectd types.db file is parsed and used to determine RRD content. When things are not defined in the JSON it will fallback to a default.
Diffstat (limited to 'graph.php')
-rw-r--r--graph.php85
1 files changed, 77 insertions, 8 deletions
diff --git a/graph.php b/graph.php
index d170a6c..be46653 100644
--- a/graph.php
+++ b/graph.php
@@ -2,8 +2,10 @@
2 2
3require_once 'conf/common.inc.php'; 3require_once 'conf/common.inc.php';
4require_once 'inc/functions.inc.php'; 4require_once 'inc/functions.inc.php';
5require_once 'inc/collectd.inc.php';
5 6
6$plugin = validate_get(GET('p'), 'plugin'); 7$plugin = validate_get(GET('p'), 'plugin');
8$type = validate_get(GET('t'), 'type');
7$width = empty($_GET['x']) ? $CONFIG['width'] : $_GET['x']; 9$width = empty($_GET['x']) ? $CONFIG['width'] : $_GET['x'];
8$height = empty($_GET['y']) ? $CONFIG['height'] : $_GET['y']; 10$height = empty($_GET['y']) ? $CONFIG['height'] : $_GET['y'];
9 11
@@ -12,18 +14,85 @@ if (validate_get(GET('h'), 'host') === NULL) {
12 error_image(); 14 error_image();
13} 15}
14 16
15if (!file_exists($CONFIG['webdir'].'/plugin/'.$plugin.'.php')) {
16 error_log(sprintf('CGP Error: plugin "%s" is not available', $plugin));
17 error_image();
18}
19
20if ($width > $CONFIG['max-width'] || $height > $CONFIG['max-height']) { 17if ($width > $CONFIG['max-width'] || $height > $CONFIG['max-height']) {
21 error_log('Resquested image is too large. Please configure max-width and max-height.'); 18 error_log('Resquested image is too large. Please configure max-width and max-height.');
22 error_image(); 19 error_image();
23} 20}
24 21
25# load plugin 22$typesdb = parse_typesdb_file($CONFIG['typesdb']);
26include $CONFIG['webdir'].'/plugin/'.$plugin.'.php'; 23
24if ($plugin == 'aggregation') {
25 $pi = explode("-", GET('pi'));
26 $plugin = $_GET['p'] = $pi[0];
27}
28
29# plugin json
30if (file_exists('plugin/'.$plugin.'.json')) {
31 $json = file_get_contents('plugin/'.$plugin.'.json');
32 $plugin_json = json_decode($json, true);
33
34 if (is_null($plugin_json))
35 error_log('CGP Error: invalid json in plugin/'.$plugin.'.json');
36}
37
38switch ($plugin_json[$type]['type']) {
39 case 'stacked':
40 require_once 'type/GenericStacked.class.php';
41 $obj = new Type_GenericStacked($CONFIG, $_GET);
42 break;
43 case 'io':
44 require_once 'type/GenericIO.class.php';
45 $obj = new Type_GenericIO($CONFIG, $_GET);
46 break;
47 case 'uptime':
48 require_once 'type/Uptime.class.php';
49 $obj = new Type_Uptime($CONFIG, $_GET);
50 break;
51 default:
52 require_once 'type/Default.class.php';
53 $obj = new Type_Default($CONFIG, $_GET);
54 break;
55}
56
57if (isset($typesdb[$type])) {
58 $obj->data_sources = array();
59 foreach ($typesdb[$type] as $ds => $property) {
60 $obj->data_sources[] = $ds;
61 }
62}
63
64if (isset($plugin_json[$type]['legend'])) {
65 $obj->order = array();
66 foreach ($plugin_json[$type]['legend'] as $rrd => $property) {
67 $obj->order[] = $rrd;
68 $obj->legend[$rrd] = isset($property['name']) ? $property['name'] : $rrd;
69 if (isset($property['color']))
70 $obj->colors[$rrd] = $property['color'];
71 }
72}
73
74if (isset($plugin_json[$type]['title'])) {
75 $obj->rrd_title = $plugin_json[$type]['title'];
76 $obj->rrd_title = str_replace('{{PI}}', GET('pi'), $obj->rrd_title);
77 $obj->rrd_title = str_replace('{{TI}}', GET('ti'), $obj->rrd_title);
78}
79
80if (isset($plugin_json[$type]['vertical'])) {
81 $obj->rrd_vertical = $plugin_json[$type]['vertical'];
82 $obj->rrd_vertical = str_replace('{{ND}}', ucfirst($CONFIG['network_datasize']), $obj->rrd_vertical);
83}
84
85if (isset($plugin_json[$type]['rrdtool_opts'])) {
86 $obj->rrdtool_opts = $plugin_json[$type]['rrdtool_opts'];
87}
88
89if (isset($plugin_json[$type]['datasize']) and $plugin_json[$type]['datasize'])
90 $obj->scale = $CONFIG['network_datasize'] == 'bits' ? 8 : 1;
91
92if (isset($plugin_json[$type]['scale']))
93 $obj->scale = $plugin_json[$type]['scale'];
27 94
95if (isset($plugin_json[$type]['legend_format']))
96 $obj->rrd_format = $plugin_json[$type]['legend_format'];
28 97
29?> 98$obj->rrd_graph();