aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/type
diff options
context:
space:
mode:
authorPim van den Berg2014-05-03 11:23:52 +0200
committerPim van den Berg2014-05-03 11:37:26 +0200
commit8d0358af583d921da35d8ac9ae96d5c419ce5ba3 (patch)
treeaaaaada8c2b4848edd56374d7501becadca5e431 /type
parenttype/Default: don't depend on external function GET (diff)
downloadapt-panopticon_cgp-8d0358af583d921da35d8ac9ae96d5c419ce5ba3.zip
apt-panopticon_cgp-8d0358af583d921da35d8ac9ae96d5c419ce5ba3.tar.gz
apt-panopticon_cgp-8d0358af583d921da35d8ac9ae96d5c419ce5ba3.tar.bz2
apt-panopticon_cgp-8d0358af583d921da35d8ac9ae96d5c419ce5ba3.tar.xz
type: move generic functions to base class
Diffstat (limited to 'type')
-rw-r--r--type/Base.class.php298
-rw-r--r--type/Default.class.php296
-rw-r--r--type/GenericIO.class.php4
-rw-r--r--type/GenericStacked.class.php4
-rw-r--r--type/Uptime.class.php4
5 files changed, 306 insertions, 300 deletions
diff --git a/type/Base.class.php b/type/Base.class.php
new file mode 100644
index 0000000..2851280
--- /dev/null
+++ b/type/Base.class.php
@@ -0,0 +1,298 @@
1<?php
2
3# Collectd Default type
4
5class Type_Base {
6 var $datadir;
7 var $rrdtool;
8 var $rrdtool_opts;
9 var $cache;
10 var $args;
11 var $seconds;
12 var $data_sources = array('value');
13 var $order;
14 var $ds_names;
15 var $colors;
16 var $rrd_title;
17 var $rrd_vertical;
18 var $rrd_format;
19 var $scale = 1;
20 var $width;
21 var $height;
22 var $graph_type;
23 var $negative_io;
24 var $graph_smooth;
25
26 var $files;
27 var $tinstances;
28 var $identifiers;
29
30 function __construct($config, $_get) {
31 $this->datadir = $config['datadir'];
32 $this->rrdtool = $config['rrdtool'];
33 $this->rrdtool_opts = $config['rrdtool_opts'];
34 $this->cache = $config['cache'];
35 $this->parse_get($_get);
36 $this->rrd_files();
37 $this->width = isset($_get['x']) ? $_get['x'] : $config['width'];
38 $this->height = isset($_get['y']) ? $_get['y'] : $config['height'];
39 $this->graph_type = $config['graph_type'];
40 $this->negative_io = $config['negative_io'];
41 $this->graph_smooth = $config['graph_smooth'];
42 }
43
44 function rainbow_colors() {
45 $c = 0;
46 $sources = count($this->rrd_get_sources());
47 foreach ($this->rrd_get_sources() as $ds) {
48 # hue (saturnation=1, value=1)
49 $h = $sources > 1 ? 360 - ($c * (330/($sources-1))) : 360;
50
51 $h = ($h %= 360) / 60;
52 $f = $h - floor($h);
53 $q[0] = $q[1] = 0;
54 $q[2] = 1*(1-1*(1-$f));
55 $q[3] = $q[4] = 1;
56 $q[5] = 1*(1-1*$f);
57
58 $hex = '';
59 foreach(array(4,2,0) as $j) {
60 $hex .= sprintf('%02x', $q[(floor($h)+$j)%6] * 255);
61 }
62 $this->colors[$ds] = $hex;
63 $c++;
64 }
65 }
66
67 # parse $_GET values
68 function parse_get($_get) {
69 $this->args = array(
70 'host' => isset($_get['h']) ? $_get['h'] : null,
71 'plugin' => isset($_get['p']) ? $_get['p'] : null,
72 'pinstance' => isset($_get['pi']) ? $_get['pi'] : null,
73 'category' => isset($_get['c']) ? $_get['c'] : null,
74 'type' => isset($_get['t']) ? $_get['t'] : null,
75 'tinstance' => isset($_get['ti']) ? $_get['ti'] : null,
76 );
77 $this->seconds = isset($_get['s']) ? $_get['s'] : null;
78 }
79
80 function validate_color($color) {
81 if (!preg_match('/^[0-9a-f]{6}$/', $color))
82 return '000000';
83 else
84 return $color;
85 }
86
87 function get_faded_color($fgc, $bgc='ffffff', $percent=0.25) {
88 $fgc = $this->validate_color($fgc);
89 if (!is_numeric($percent))
90 $percent=0.25;
91
92 $rgb = array('r', 'g', 'b');
93
94 $fg['r'] = hexdec(substr($fgc,0,2));
95 $fg['g'] = hexdec(substr($fgc,2,2));
96 $fg['b'] = hexdec(substr($fgc,4,2));
97 $bg['r'] = hexdec(substr($bgc,0,2));
98 $bg['g'] = hexdec(substr($bgc,2,2));
99 $bg['b'] = hexdec(substr($bgc,4,2));
100
101 foreach ($rgb as $pri) {
102 $c[$pri] = dechex(round($percent * $fg[$pri]) + ((1.0 - $percent) * $bg[$pri]));
103 if ($c[$pri] == '0')
104 $c[$pri] = '00';
105 }
106
107 return $c['r'].$c['g'].$c['b'];
108 }
109
110 function rrd_escape($value) {
111 if ($this->graph_type == 'canvas') {
112 # http://oss.oetiker.ch/rrdtool/doc/rrdgraph_graph.en.html#IEscaping_the_colon
113 return str_replace(':', '\:', $value);
114 } else {
115 # php needs it double escaped to execute rrdtool correctly
116 return str_replace(':', '\\\:', $value);
117 }
118 }
119
120 function parse_filename($file) {
121 if ($this->graph_type == 'canvas') {
122 $file = str_replace($this->datadir . '/', '', $file);
123 # rawurlencode all but /
124 $file = 'rrd.php?' . str_replace('%2F', '/', rawurlencode($file));
125 } else {
126 # escape characters
127 $file = str_replace(array(' ', '(', ')'), array('\ ', '\(', '\)'), $file);
128 }
129 return $this->rrd_escape($file);
130 }
131
132 function rrd_files() {
133 $files = $this->get_filenames();
134
135 foreach($files as $filename) {
136 $basename=basename($filename,'.rrd');
137 $instance = strpos($basename,'-')
138 ? substr($basename, strpos($basename,'-') + 1)
139 : 'value';
140
141 $this->tinstances[] = $instance;
142 $this->files[$instance] = $filename;
143 $this->identifiers[$instance] = preg_replace("#^$this->datadir/(.*)\.rrd$#", '$1', $filename);
144 }
145
146 sort($this->tinstances);
147 ksort($this->files);
148 ksort($this->identifiers);
149 }
150
151 function get_filenames() {
152 $identifier = sprintf('%s/%s%s%s%s%s/%s%s%s',
153 $this->args['host'],
154 $this->args['plugin'],
155 strlen($this->args['category']) ? '-' : '', $this->args['category'],
156 strlen($this->args['pinstance']) ? '-' : '', $this->args['pinstance'],
157 $this->args['type'],
158 strlen($this->args['tinstance']) ? '-' : '', $this->args['tinstance']
159 );
160 $identifier = preg_replace("/([*?[])/", '[$1]', $identifier);
161
162 $wildcard = strlen($this->args['tinstance']) ? '.' : '[-.]*';
163
164 $files = glob($this->datadir .'/'. $identifier . $wildcard . 'rrd');
165
166 return $files;
167 }
168
169 function rrd_graph($debug = false) {
170 if (!$this->colors)
171 $this->rainbow_colors();
172
173 $graphdata = $this->rrd_gen_graph();
174
175 $style = $debug !== false ? $debug : $this->graph_type;
176 switch ($style) {
177 case 'cmd':
178 print '<pre>';
179 foreach ($graphdata as $d) {
180 printf("%s \\\n", $d);
181 }
182 print '</pre>';
183 break;
184 case 'canvas':
185 printf('<canvas id="%s" class="rrd">', sha1(serialize($graphdata)));
186 foreach ($graphdata as $d) {
187 printf("%s\n", $d);
188 }
189 print '</canvas>';
190 break;
191 case 'debug':
192 case 1:
193 print '<pre>';
194 print_r($graphdata);
195 print '</pre>';
196 break;
197 case 'svg':
198 # caching
199 if (is_numeric($this->cache) && $this->cache > 0)
200 header("Expires: " . date(DATE_RFC822,strtotime($this->cache." seconds")));
201 header("content-type: image/svg+xml");
202 $graphdata = implode(' ', $graphdata);
203 echo `$graphdata`;
204 break;
205 case 'png':
206 default:
207 # caching
208 if (is_numeric($this->cache) && $this->cache > 0)
209 header("Expires: " . date(DATE_RFC822,strtotime($this->cache." seconds")));
210 header("content-type: image/png");
211 $graphdata = implode(' ', $graphdata);
212 echo `$graphdata`;
213 break;
214 }
215 }
216
217 function rrd_options() {
218 switch ($this->graph_type) {
219 case 'png':
220 case 'hybrid':
221 $rrdgraph[] = $this->rrdtool;
222 $rrdgraph[] = 'graph - -a PNG';
223 break;
224 case 'svg':
225 $rrdgraph[] = $this->rrdtool;
226 $rrdgraph[] = 'graph - -a SVG';
227 break;
228 default:
229 break;
230 }
231 if ($this->rrdtool_opts != '')
232 $rrdgraph[] = $this->rrdtool_opts;
233 if ($this->graph_smooth)
234 $rrdgraph[] = '-E';
235 $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400);
236 $rrdgraph[] = sprintf('-h %d', is_numeric($this->height) ? $this->height : 175);
237 $rrdgraph[] = '-l 0';
238 $rrdgraph[] = sprintf('-t "%s on %s"', $this->rrd_title, $this->args['host']);
239 if ($this->rrd_vertical)
240 $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical);
241 $rrdgraph[] = sprintf('-s e-%d', is_numeric($this->seconds) ? $this->seconds : 86400);
242
243 return $rrdgraph;
244 }
245
246 function rrd_get_sources() {
247 # is the source spread over multiple files?
248 if (is_array($this->files) && count($this->files)>1) {
249 # and must it be ordered?
250 if (is_array($this->order)) {
251 $this->tinstances = array_merge(array_intersect($this->order, $this->tinstances));
252 }
253 # use tinstances as sources
254 if(is_array($this->data_sources) && count($this->data_sources)>1) {
255 $sources = array();
256 foreach($this->tinstances as $f) {
257 foreach($this->data_sources as $s) {
258 $sources[] = $f . '-' . $s;
259 }
260 }
261 }
262 else {
263 $sources = $this->tinstances;
264 }
265 }
266 # or one file with multiple data_sources
267 else {
268 if(is_array($this->data_sources) && count($this->data_sources)==1 && in_array('value', $this->data_sources)) {
269 # use tinstances as sources
270 $sources = $this->tinstances;
271 } else {
272 # use data_sources as sources
273 $sources = $this->data_sources;
274 }
275 }
276 $this->parse_ds_names($sources);
277 return $sources;
278 }
279
280 function parse_ds_names($sources) {
281 # fill ds_names if not defined by plugin
282 if (!is_array($this->ds_names))
283 $this->ds_names = array_combine($sources, $sources);
284
285 # detect length of longest ds_name
286 $max = 0;
287 foreach ($this->ds_names as $ds_name) {
288 if(strlen((string)$ds_name) > $max)
289 $max = strlen((string)$ds_name);
290 }
291
292 # make all ds_names equal in lenght
293 $format = sprintf("%%-%ds", $max);
294 foreach ($this->ds_names as $index => $value) {
295 $this->ds_names[$index] = sprintf($format, $value);
296 }
297 }
298}
diff --git a/type/Default.class.php b/type/Default.class.php
index e101e3b..52827c2 100644
--- a/type/Default.class.php
+++ b/type/Default.class.php
@@ -1,300 +1,8 @@
1<?php 1<?php
2 2
3# Collectd Default type 3require_once 'Base.class.php';
4 4
5class Type_Default { 5class Type_Default extends Type_Base {
6 var $datadir;
7 var $rrdtool;
8 var $rrdtool_opts;
9 var $cache;
10 var $args;
11 var $seconds;
12 var $data_sources = array('value');
13 var $order;
14 var $ds_names;
15 var $colors;
16 var $rrd_title;
17 var $rrd_vertical;
18 var $rrd_format;
19 var $scale = 1;
20 var $width;
21 var $height;
22 var $graph_type;
23 var $negative_io;
24 var $graph_smooth;
25
26 var $files;
27 var $tinstances;
28 var $identifiers;
29
30 function __construct($config, $_get) {
31 $this->datadir = $config['datadir'];
32 $this->rrdtool = $config['rrdtool'];
33 $this->rrdtool_opts = $config['rrdtool_opts'];
34 $this->cache = $config['cache'];
35 $this->parse_get($_get);
36 $this->rrd_files();
37 $this->width = isset($_get['x']) ? $_get['x'] : $config['width'];
38 $this->height = isset($_get['y']) ? $_get['y'] : $config['height'];
39 $this->graph_type = $config['graph_type'];
40 $this->negative_io = $config['negative_io'];
41 $this->graph_smooth = $config['graph_smooth'];
42 }
43
44 function rainbow_colors() {
45 $c = 0;
46 $sources = count($this->rrd_get_sources());
47 foreach ($this->rrd_get_sources() as $ds) {
48 # hue (saturnation=1, value=1)
49 $h = $sources > 1 ? 360 - ($c * (330/($sources-1))) : 360;
50
51 $h = ($h %= 360) / 60;
52 $f = $h - floor($h);
53 $q[0] = $q[1] = 0;
54 $q[2] = 1*(1-1*(1-$f));
55 $q[3] = $q[4] = 1;
56 $q[5] = 1*(1-1*$f);
57
58 $hex = '';
59 foreach(array(4,2,0) as $j) {
60 $hex .= sprintf('%02x', $q[(floor($h)+$j)%6] * 255);
61 }
62 $this->colors[$ds] = $hex;
63 $c++;
64 }
65 }
66
67 # parse $_GET values
68 function parse_get($_get) {
69 $this->args = array(
70 'host' => isset($_get['h']) ? $_get['h'] : null,
71 'plugin' => isset($_get['p']) ? $_get['p'] : null,
72 'pinstance' => isset($_get['pi']) ? $_get['pi'] : null,
73 'category' => isset($_get['c']) ? $_get['c'] : null,
74 'type' => isset($_get['t']) ? $_get['t'] : null,
75 'tinstance' => isset($_get['ti']) ? $_get['ti'] : null,
76 );
77 $this->seconds = isset($_get['s']) ? $_get['s'] : null;
78 }
79
80 function validate_color($color) {
81 if (!preg_match('/^[0-9a-f]{6}$/', $color))
82 return '000000';
83 else
84 return $color;
85 }
86
87 function get_faded_color($fgc, $bgc='ffffff', $percent=0.25) {
88 $fgc = $this->validate_color($fgc);
89 if (!is_numeric($percent))
90 $percent=0.25;
91
92 $rgb = array('r', 'g', 'b');
93
94 $fg['r'] = hexdec(substr($fgc,0,2));
95 $fg['g'] = hexdec(substr($fgc,2,2));
96 $fg['b'] = hexdec(substr($fgc,4,2));
97 $bg['r'] = hexdec(substr($bgc,0,2));
98 $bg['g'] = hexdec(substr($bgc,2,2));
99 $bg['b'] = hexdec(substr($bgc,4,2));
100
101 foreach ($rgb as $pri) {
102 $c[$pri] = dechex(round($percent * $fg[$pri]) + ((1.0 - $percent) * $bg[$pri]));
103 if ($c[$pri] == '0')
104 $c[$pri] = '00';
105 }
106
107 return $c['r'].$c['g'].$c['b'];
108 }
109
110 function rrd_escape($value) {
111 if ($this->graph_type == 'canvas') {
112 # http://oss.oetiker.ch/rrdtool/doc/rrdgraph_graph.en.html#IEscaping_the_colon
113 return str_replace(':', '\:', $value);
114 } else {
115 # php needs it double escaped to execute rrdtool correctly
116 return str_replace(':', '\\\:', $value);
117 }
118 }
119
120 function parse_filename($file) {
121 if ($this->graph_type == 'canvas') {
122 $file = str_replace($this->datadir . '/', '', $file);
123 # rawurlencode all but /
124 $file = 'rrd.php?' . str_replace('%2F', '/', rawurlencode($file));
125 } else {
126 # escape characters
127 $file = str_replace(array(' ', '(', ')'), array('\ ', '\(', '\)'), $file);
128 }
129 return $this->rrd_escape($file);
130 }
131
132 function rrd_files() {
133 $files = $this->get_filenames();
134
135 foreach($files as $filename) {
136 $basename=basename($filename,'.rrd');
137 $instance = strpos($basename,'-')
138 ? substr($basename, strpos($basename,'-') + 1)
139 : 'value';
140
141 $this->tinstances[] = $instance;
142 $this->files[$instance] = $filename;
143 $this->identifiers[$instance] = preg_replace("#^$this->datadir/(.*)\.rrd$#", '$1', $filename);
144 }
145
146 sort($this->tinstances);
147 ksort($this->files);
148 ksort($this->identifiers);
149 }
150
151 function get_filenames() {
152 $identifier = sprintf('%s/%s%s%s%s%s/%s%s%s',
153 $this->args['host'],
154 $this->args['plugin'],
155 strlen($this->args['category']) ? '-' : '', $this->args['category'],
156 strlen($this->args['pinstance']) ? '-' : '', $this->args['pinstance'],
157 $this->args['type'],
158 strlen($this->args['tinstance']) ? '-' : '', $this->args['tinstance']
159 );
160 $identifier = preg_replace("/([*?[])/", '[$1]', $identifier);
161
162 $wildcard = strlen($this->args['tinstance']) ? '.' : '[-.]*';
163
164 $files = glob($this->datadir .'/'. $identifier . $wildcard . 'rrd');
165
166 return $files;
167 }
168
169 function rrd_graph($debug = false) {
170 if (!$this->colors)
171 $this->rainbow_colors();
172
173 $graphdata = $this->rrd_gen_graph();
174
175 $style = $debug !== false ? $debug : $this->graph_type;
176 switch ($style) {
177 case 'cmd':
178 print '<pre>';
179 foreach ($graphdata as $d) {
180 printf("%s \\\n", $d);
181 }
182 print '</pre>';
183 break;
184 case 'canvas':
185 printf('<canvas id="%s" class="rrd">', sha1(serialize($graphdata)));
186 foreach ($graphdata as $d) {
187 printf("%s\n", $d);
188 }
189 print '</canvas>';
190 break;
191 case 'debug':
192 case 1:
193 print '<pre>';
194 print_r($graphdata);
195 print '</pre>';
196 break;
197 case 'svg':
198 # caching
199 if (is_numeric($this->cache) && $this->cache > 0)
200 header("Expires: " . date(DATE_RFC822,strtotime($this->cache." seconds")));
201 header("content-type: image/svg+xml");
202 $graphdata = implode(' ', $graphdata);
203 echo `$graphdata`;
204 break;
205 case 'png':
206 default:
207 # caching
208 if (is_numeric($this->cache) && $this->cache > 0)
209 header("Expires: " . date(DATE_RFC822,strtotime($this->cache." seconds")));
210 header("content-type: image/png");
211 $graphdata = implode(' ', $graphdata);
212 echo `$graphdata`;
213 break;
214 }
215 }
216
217 function rrd_options() {
218 switch ($this->graph_type) {
219 case 'png':
220 case 'hybrid':
221 $rrdgraph[] = $this->rrdtool;
222 $rrdgraph[] = 'graph - -a PNG';
223 break;
224 case 'svg':
225 $rrdgraph[] = $this->rrdtool;
226 $rrdgraph[] = 'graph - -a SVG';
227 break;
228 default:
229 break;
230 }
231 if ($this->rrdtool_opts != '')
232 $rrdgraph[] = $this->rrdtool_opts;
233 if ($this->graph_smooth)
234 $rrdgraph[] = '-E';
235 $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400);
236 $rrdgraph[] = sprintf('-h %d', is_numeric($this->height) ? $this->height : 175);
237 $rrdgraph[] = '-l 0';
238 $rrdgraph[] = sprintf('-t "%s on %s"', $this->rrd_title, $this->args['host']);
239 if ($this->rrd_vertical)
240 $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical);
241 $rrdgraph[] = sprintf('-s e-%d', is_numeric($this->seconds) ? $this->seconds : 86400);
242
243 return $rrdgraph;
244 }
245
246 function rrd_get_sources() {
247 # is the source spread over multiple files?
248 if (is_array($this->files) && count($this->files)>1) {
249 # and must it be ordered?
250 if (is_array($this->order)) {
251 $this->tinstances = array_merge(array_intersect($this->order, $this->tinstances));
252 }
253 # use tinstances as sources
254 if(is_array($this->data_sources) && count($this->data_sources)>1) {
255 $sources = array();
256 foreach($this->tinstances as $f) {
257 foreach($this->data_sources as $s) {
258 $sources[] = $f . '-' . $s;
259 }
260 }
261 }
262 else {
263 $sources = $this->tinstances;
264 }
265 }
266 # or one file with multiple data_sources
267 else {
268 if(is_array($this->data_sources) && count($this->data_sources)==1 && in_array('value', $this->data_sources)) {
269 # use tinstances as sources
270 $sources = $this->tinstances;
271 } else {
272 # use data_sources as sources
273 $sources = $this->data_sources;
274 }
275 }
276 $this->parse_ds_names($sources);
277 return $sources;
278 }
279
280 function parse_ds_names($sources) {
281 # fill ds_names if not defined by plugin
282 if (!is_array($this->ds_names))
283 $this->ds_names = array_combine($sources, $sources);
284
285 # detect length of longest ds_name
286 $max = 0;
287 foreach ($this->ds_names as $ds_name) {
288 if(strlen((string)$ds_name) > $max)
289 $max = strlen((string)$ds_name);
290 }
291
292 # make all ds_names equal in lenght
293 $format = sprintf("%%-%ds", $max);
294 foreach ($this->ds_names as $index => $value) {
295 $this->ds_names[$index] = sprintf($format, $value);
296 }
297 }
298 6
299 function rrd_gen_graph() { 7 function rrd_gen_graph() {
300 $rrdgraph = $this->rrd_options(); 8 $rrdgraph = $this->rrd_options();
diff --git a/type/GenericIO.class.php b/type/GenericIO.class.php
index 231c098..df3a057 100644
--- a/type/GenericIO.class.php
+++ b/type/GenericIO.class.php
@@ -1,8 +1,8 @@
1<?php 1<?php
2 2
3require_once 'Default.class.php'; 3require_once 'Base.class.php';
4 4
5class Type_GenericIO extends Type_Default { 5class Type_GenericIO extends Type_Base {
6 6
7 function rrd_gen_graph() { 7 function rrd_gen_graph() {
8 $rrdgraph = $this->rrd_options(); 8 $rrdgraph = $this->rrd_options();
diff --git a/type/GenericStacked.class.php b/type/GenericStacked.class.php
index 3f249c7..4a08c91 100644
--- a/type/GenericStacked.class.php
+++ b/type/GenericStacked.class.php
@@ -1,8 +1,8 @@
1<?php 1<?php
2 2
3require_once 'Default.class.php'; 3require_once 'Base.class.php';
4 4
5class Type_GenericStacked extends Type_Default { 5class Type_GenericStacked extends Type_Base {
6 6
7 function rrd_gen_graph() { 7 function rrd_gen_graph() {
8 $rrdgraph = $this->rrd_options(); 8 $rrdgraph = $this->rrd_options();
diff --git a/type/Uptime.class.php b/type/Uptime.class.php
index 4ada117..3f8a01e 100644
--- a/type/Uptime.class.php
+++ b/type/Uptime.class.php
@@ -1,8 +1,8 @@
1<?php 1<?php
2 2
3require_once 'Default.class.php'; 3require_once 'Base.class.php';
4 4
5class Type_Uptime extends Type_Default { 5class Type_Uptime extends Type_Base {
6 6
7 function rrd_gen_graph() { 7 function rrd_gen_graph() {
8 $rrdgraph = $this->rrd_options(); 8 $rrdgraph = $this->rrd_options();