From 8d0358af583d921da35d8ac9ae96d5c419ce5ba3 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sat, 3 May 2014 11:23:52 +0200 Subject: type: move generic functions to base class --- type/Default.class.php | 296 +------------------------------------------------ 1 file changed, 2 insertions(+), 294 deletions(-) (limited to 'type/Default.class.php') 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 @@ datadir = $config['datadir']; - $this->rrdtool = $config['rrdtool']; - $this->rrdtool_opts = $config['rrdtool_opts']; - $this->cache = $config['cache']; - $this->parse_get($_get); - $this->rrd_files(); - $this->width = isset($_get['x']) ? $_get['x'] : $config['width']; - $this->height = isset($_get['y']) ? $_get['y'] : $config['height']; - $this->graph_type = $config['graph_type']; - $this->negative_io = $config['negative_io']; - $this->graph_smooth = $config['graph_smooth']; - } - - function rainbow_colors() { - $c = 0; - $sources = count($this->rrd_get_sources()); - foreach ($this->rrd_get_sources() as $ds) { - # hue (saturnation=1, value=1) - $h = $sources > 1 ? 360 - ($c * (330/($sources-1))) : 360; - - $h = ($h %= 360) / 60; - $f = $h - floor($h); - $q[0] = $q[1] = 0; - $q[2] = 1*(1-1*(1-$f)); - $q[3] = $q[4] = 1; - $q[5] = 1*(1-1*$f); - - $hex = ''; - foreach(array(4,2,0) as $j) { - $hex .= sprintf('%02x', $q[(floor($h)+$j)%6] * 255); - } - $this->colors[$ds] = $hex; - $c++; - } - } - - # parse $_GET values - function parse_get($_get) { - $this->args = array( - 'host' => isset($_get['h']) ? $_get['h'] : null, - 'plugin' => isset($_get['p']) ? $_get['p'] : null, - 'pinstance' => isset($_get['pi']) ? $_get['pi'] : null, - 'category' => isset($_get['c']) ? $_get['c'] : null, - 'type' => isset($_get['t']) ? $_get['t'] : null, - 'tinstance' => isset($_get['ti']) ? $_get['ti'] : null, - ); - $this->seconds = isset($_get['s']) ? $_get['s'] : null; - } - - function validate_color($color) { - if (!preg_match('/^[0-9a-f]{6}$/', $color)) - return '000000'; - else - return $color; - } - - function get_faded_color($fgc, $bgc='ffffff', $percent=0.25) { - $fgc = $this->validate_color($fgc); - if (!is_numeric($percent)) - $percent=0.25; - - $rgb = array('r', 'g', 'b'); - - $fg['r'] = hexdec(substr($fgc,0,2)); - $fg['g'] = hexdec(substr($fgc,2,2)); - $fg['b'] = hexdec(substr($fgc,4,2)); - $bg['r'] = hexdec(substr($bgc,0,2)); - $bg['g'] = hexdec(substr($bgc,2,2)); - $bg['b'] = hexdec(substr($bgc,4,2)); - - foreach ($rgb as $pri) { - $c[$pri] = dechex(round($percent * $fg[$pri]) + ((1.0 - $percent) * $bg[$pri])); - if ($c[$pri] == '0') - $c[$pri] = '00'; - } - - return $c['r'].$c['g'].$c['b']; - } - - function rrd_escape($value) { - if ($this->graph_type == 'canvas') { - # http://oss.oetiker.ch/rrdtool/doc/rrdgraph_graph.en.html#IEscaping_the_colon - return str_replace(':', '\:', $value); - } else { - # php needs it double escaped to execute rrdtool correctly - return str_replace(':', '\\\:', $value); - } - } - - function parse_filename($file) { - if ($this->graph_type == 'canvas') { - $file = str_replace($this->datadir . '/', '', $file); - # rawurlencode all but / - $file = 'rrd.php?' . str_replace('%2F', '/', rawurlencode($file)); - } else { - # escape characters - $file = str_replace(array(' ', '(', ')'), array('\ ', '\(', '\)'), $file); - } - return $this->rrd_escape($file); - } - - function rrd_files() { - $files = $this->get_filenames(); - - foreach($files as $filename) { - $basename=basename($filename,'.rrd'); - $instance = strpos($basename,'-') - ? substr($basename, strpos($basename,'-') + 1) - : 'value'; - - $this->tinstances[] = $instance; - $this->files[$instance] = $filename; - $this->identifiers[$instance] = preg_replace("#^$this->datadir/(.*)\.rrd$#", '$1', $filename); - } - - sort($this->tinstances); - ksort($this->files); - ksort($this->identifiers); - } - - function get_filenames() { - $identifier = sprintf('%s/%s%s%s%s%s/%s%s%s', - $this->args['host'], - $this->args['plugin'], - strlen($this->args['category']) ? '-' : '', $this->args['category'], - strlen($this->args['pinstance']) ? '-' : '', $this->args['pinstance'], - $this->args['type'], - strlen($this->args['tinstance']) ? '-' : '', $this->args['tinstance'] - ); - $identifier = preg_replace("/([*?[])/", '[$1]', $identifier); - - $wildcard = strlen($this->args['tinstance']) ? '.' : '[-.]*'; - - $files = glob($this->datadir .'/'. $identifier . $wildcard . 'rrd'); - - return $files; - } - - function rrd_graph($debug = false) { - if (!$this->colors) - $this->rainbow_colors(); - - $graphdata = $this->rrd_gen_graph(); - - $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('', sha1(serialize($graphdata))); - foreach ($graphdata as $d) { - printf("%s\n", $d); - } - print ''; - break; - case 'debug': - case 1: - print '
';
-				print_r($graphdata);
-				print '
'; - break; - case 'svg': - # caching - if (is_numeric($this->cache) && $this->cache > 0) - header("Expires: " . date(DATE_RFC822,strtotime($this->cache." seconds"))); - header("content-type: image/svg+xml"); - $graphdata = implode(' ', $graphdata); - echo `$graphdata`; - 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() { - switch ($this->graph_type) { - case 'png': - case 'hybrid': - $rrdgraph[] = $this->rrdtool; - $rrdgraph[] = 'graph - -a PNG'; - break; - case 'svg': - $rrdgraph[] = $this->rrdtool; - $rrdgraph[] = 'graph - -a SVG'; - break; - default: - break; - } - if ($this->rrdtool_opts != '') - $rrdgraph[] = $this->rrdtool_opts; - if ($this->graph_smooth) - $rrdgraph[] = '-E'; - $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400); - $rrdgraph[] = sprintf('-h %d', is_numeric($this->height) ? $this->height : 175); - $rrdgraph[] = '-l 0'; - $rrdgraph[] = sprintf('-t "%s on %s"', $this->rrd_title, $this->args['host']); - if ($this->rrd_vertical) - $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); - $rrdgraph[] = sprintf('-s e-%d', is_numeric($this->seconds) ? $this->seconds : 86400); - - return $rrdgraph; - } - - function rrd_get_sources() { - # is the source spread over multiple files? - if (is_array($this->files) && count($this->files)>1) { - # and must it be ordered? - if (is_array($this->order)) { - $this->tinstances = array_merge(array_intersect($this->order, $this->tinstances)); - } - # use tinstances as sources - if(is_array($this->data_sources) && count($this->data_sources)>1) { - $sources = array(); - foreach($this->tinstances as $f) { - foreach($this->data_sources as $s) { - $sources[] = $f . '-' . $s; - } - } - } - else { - $sources = $this->tinstances; - } - } - # or one file with multiple data_sources - else { - if(is_array($this->data_sources) && count($this->data_sources)==1 && in_array('value', $this->data_sources)) { - # use tinstances as sources - $sources = $this->tinstances; - } else { - # use data_sources as sources - $sources = $this->data_sources; - } - } - $this->parse_ds_names($sources); - return $sources; - } - - function parse_ds_names($sources) { - # fill ds_names if not defined by plugin - if (!is_array($this->ds_names)) - $this->ds_names = array_combine($sources, $sources); - - # detect length of longest ds_name - $max = 0; - foreach ($this->ds_names as $ds_name) { - if(strlen((string)$ds_name) > $max) - $max = strlen((string)$ds_name); - } - - # make all ds_names equal in lenght - $format = sprintf("%%-%ds", $max); - foreach ($this->ds_names as $index => $value) { - $this->ds_names[$index] = sprintf($format, $value); - } - } +class Type_Default extends Type_Base { function rrd_gen_graph() { $rrdgraph = $this->rrd_options(); -- cgit v1.1