From 95c70c919837f588389f525fe2cf4aba479e9f91 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Thu, 31 Dec 2009 11:42:56 +0100 Subject: rewrite of type classes A constructor is added to the Type_Default class. The constructor will parse GET values (such as host, plugin, pinstance, type, tinstance, seconds), create an array of all needed rrd files to generate a graph and substract identifiers from these rrd files. Because of the constructor (and related functions) it is not needed to define an array of tinstances to be grouped and shown in one graph. Also $obj->args don't have to be defined per plugin. This will result in smaller plugin files. The type classes are based on the fact that a plugin has multiple type instances OR multiple rrd data sources. This is called the source and is retrieved by rrd_get_sources in each rrd_gen_graph function. Also variables in function rrd_gen_graph have been renamed to better ones. --- graph.php | 14 ++--- inc/collectd.inc.php | 17 ----- plugin/cpu.php | 21 ++----- plugin/df.php | 16 +---- plugin/disk.php | 24 +++---- plugin/entropy.php | 14 +---- plugin/interface.php | 23 +++---- plugin/irq.php | 23 +------ plugin/load.php | 14 +---- plugin/memory.php | 20 +----- plugin/processes.php | 20 +----- plugin/sensors.php | 28 +++------ plugin/swap.php | 22 ++----- plugin/users.php | 14 +---- type/Default.class.php | 142 ++++++++++++++++++++++++++++-------------- type/GenericIO.class.php | 51 ++++++++------- type/GenericStacked.class.php | 59 +++++++----------- 17 files changed, 198 insertions(+), 324 deletions(-) diff --git a/graph.php b/graph.php index cc3aeb1..77bfcb6 100644 --- a/graph.php +++ b/graph.php @@ -2,26 +2,20 @@ require_once 'conf/common.inc.php'; -$host = $_GET['h']; -$plugin = $_GET['p']; -$pinstance = $_GET['pi']; -$type = $_GET['t']; -$tinstance = $_GET['ti']; $width = empty($_GET['x']) ? $CONFIG['width'] : $_GET['x']; $heigth = empty($_GET['y']) ? $CONFIG['heigth'] : $_GET['y']; -$seconds = $_GET['s']; -if (!preg_match('/^[a-z]+$/', $plugin)) { +if (!preg_match('/^[a-z]+$/', $_GET['p'])) { die_img('Error: plugin contains unknown characters.'); exit; } -if (!file_exists($CONFIG['webdir']."/plugin/$plugin.php")) { - die_img(sprintf('Error: plugin not available (%s).', $plugin)); +if (!file_exists($CONFIG['webdir'].'/plugin/'.$_GET['p'].'.php')) { + die_img(sprintf('Error: plugin not available (%s).', $_GET['p'])); exit; } -include $CONFIG['webdir']."/plugin/$plugin.php"; +include $CONFIG['webdir'].'/plugin/'.$_GET['p'].'.php'; function die_img($msg) { diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php index 3ef9102..2a44fba 100644 --- a/inc/collectd.inc.php +++ b/inc/collectd.inc.php @@ -221,21 +221,4 @@ function collectd_flush($identifier) { } } -# generate identifiers from args -function ident_from_args($args) { - if (is_array($args['tinstance'])) { - foreach($args['tinstance'] as $ti) { - $instances[] = collectd_identifier($args['host'], - $args['plugin'], $args['pinstance'], - $args['type'], $ti); - } - } else { - $instances[] = collectd_identifier($args['host'], - $args['plugin'], $args['pinstance'], - $args['type'], $args['tinstance']); - } - - return $instances; -} - ?> diff --git a/plugin/cpu.php b/plugin/cpu.php index 4cc74f6..d4097b0 100644 --- a/plugin/cpu.php +++ b/plugin/cpu.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericStacked.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # cpu-X/ @@ -16,19 +17,7 @@ require_once 'type/GenericStacked.class.php'; # cpu-X/cpu-user.rrd # cpu-X/cpu-wait.rrd -# grouped -require_once 'inc/collectd.inc.php'; -$tinstance = collectd_plugindetail($host, $plugin, 'ti'); - -$obj = new Type_GenericStacked; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_GenericStacked($CONFIG['datadir']); $obj->data_sources = array('value'); $obj->order = array('idle', 'nice', 'user', 'wait', 'system', 'softirq', 'interrupt', 'steal'); $obj->ds_names = array( @@ -53,14 +42,12 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; -$obj->rrd_title = sprintf('CPU-%s usage', $pinstance); +$obj->rrd_title = sprintf('CPU-%s usage', $obj->args['pinstance']); $obj->rrd_vertical = 'Jiffies'; $obj->rrd_format = '%5.2lf'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/df.php b/plugin/df.php index bf9b850..79c5cb5 100644 --- a/plugin/df.php +++ b/plugin/df.php @@ -11,15 +11,7 @@ require_once 'inc/collectd.inc.php'; # df/ # df/df-XXXX.rrd -$obj = new Type_GenericStacked; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_GenericStacked($CONFIG['datadir']); $obj->data_sources = array('free', 'used'); $obj->ds_names = array( 'free' => 'Free', @@ -31,14 +23,12 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; -$obj->rrd_title = sprintf('Free space (%s)', $tinstance); +$obj->rrd_title = sprintf('Free space (%s)', $obj->args['tinstance']); $obj->rrd_vertical = 'Bytes'; $obj->rrd_format = '%5.1lf%sB'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/disk.php b/plugin/disk.php index 620b188..f1100d6 100644 --- a/plugin/disk.php +++ b/plugin/disk.php @@ -13,15 +13,7 @@ require_once 'inc/collectd.inc.php'; # disk-XXXX/disk_ops.rrd # disk-XXXX/disk_time.rrd -$obj = new Type_GenericIO; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_GenericIO($CONFIG['datadir']); $obj->data_sources = array('read', 'write'); $obj->ds_names = array( 'read' => 'Read ', @@ -33,33 +25,31 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; -switch($type) { +switch($obj->args['type']) { case 'disk_merged': - $obj->rrd_title = sprintf('Disk Merged Operations (%s)', $pinstance); + $obj->rrd_title = sprintf('Disk Merged Operations (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'Merged operations/s'; $obj->rrd_format = '%5.1lf'; break; case 'disk_octets': - $obj->rrd_title = sprintf('Disk Traffic (%s)', $pinstance); + $obj->rrd_title = sprintf('Disk Traffic (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'Bytes per second'; $obj->rrd_format = '%5.1lf%s'; break; case 'disk_ops': - $obj->rrd_title = sprintf('Disk Operations (%s)', $pinstance); + $obj->rrd_title = sprintf('Disk Operations (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'Ops per second'; $obj->rrd_format = '%5.1lf'; break; case 'disk_time': - $obj->rrd_title = sprintf('Disk time per operation (%s)', $pinstance); + $obj->rrd_title = sprintf('Disk time per operation (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'Avg. Time/Op'; $obj->rrd_format = '%5.1lf%ss'; $obj->scale = '0.001'; break; } -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/entropy.php b/plugin/entropy.php index 6ee96aa..8705894 100644 --- a/plugin/entropy.php +++ b/plugin/entropy.php @@ -9,15 +9,7 @@ require_once 'inc/collectd.inc.php'; ## LAYOUT # entropy/entropy.rrd -$obj = new Type_Default; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_Default($CONFIG['datadir']); $obj->data_sources = array('entropy'); $obj->ds_names = array( 'entropy' => 'Entropy bits', @@ -27,13 +19,11 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'Available entropy'; $obj->rrd_vertical = 'Bits'; $obj->rrd_format = '%4.0lf'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/interface.php b/plugin/interface.php index 7d8d929..89e4280 100644 --- a/plugin/interface.php +++ b/plugin/interface.php @@ -12,15 +12,7 @@ require_once 'inc/collectd.inc.php'; # interface/if_octets-XXXX.rrd # interface/if_packets-XXXX.rrd -$obj = new Type_GenericIO; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_GenericIO($CONFIG['datadir']); $obj->data_sources = array('rx', 'tx'); $obj->ds_names = array( 'rx' => 'Receive ', @@ -32,25 +24,24 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_format = '%5.1lf%s'; -switch($type) { + +switch($obj->args['type']) { case 'if_errors': - $obj->rrd_title = sprintf('Interface Errors (%s)', $tinstance); + $obj->rrd_title = sprintf('Interface Errors (%s)', $obj->args['tinstance']); $obj->rrd_vertical = 'Errors per second'; break; case 'if_octets': - $obj->rrd_title = sprintf('Interface Traffic (%s)', $tinstance); + $obj->rrd_title = sprintf('Interface Traffic (%s)', $obj->args['tinstance']); $obj->rrd_vertical = 'Bits per second'; break; case 'if_packets': - $obj->rrd_title = sprintf('Interface Packets (%s)', $tinstance); + $obj->rrd_title = sprintf('Interface Packets (%s)', $obj->args['tinstance']); $obj->rrd_vertical = 'Packets per second'; break; } -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/irq.php b/plugin/irq.php index d6b1e72..ba0e7cb 100644 --- a/plugin/irq.php +++ b/plugin/irq.php @@ -4,38 +4,21 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericStacked.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # irq/ # irq/irq-XX.rrd -# grouped -require_once 'inc/collectd.inc.php'; -$tinstance = collectd_plugindetail($host, $plugin, 'ti'); -sort($tinstance); - -$obj = new Type_GenericStacked; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); -$obj->data_sources = array('value'); -$obj->ds_names = NULL; -$obj->colors = NULL; +$obj = new Type_GenericStacked($CONFIG['datadir']); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'Interrupts'; $obj->rrd_vertical = 'IRQs/s'; $obj->rrd_format = '%6.1lf'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/load.php b/plugin/load.php index 523f14c..44baf45 100644 --- a/plugin/load.php +++ b/plugin/load.php @@ -9,15 +9,7 @@ require_once 'inc/collectd.inc.php'; ## LAYOUT # load/load.rrd -$obj = new Type_Default; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_Default($CONFIG['datadir']); $obj->data_sources = array('shortterm', 'midterm', 'longterm'); $obj->ds_names = array( 'shortterm' => ' 1 min', @@ -31,13 +23,11 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'System load'; $obj->rrd_vertical = 'System load'; $obj->rrd_format = '%.2lf'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/memory.php b/plugin/memory.php index b0f6b73..ade242c 100644 --- a/plugin/memory.php +++ b/plugin/memory.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericStacked.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # memory/ @@ -12,20 +13,7 @@ require_once 'type/GenericStacked.class.php'; # memory/memory-free.rrd # memory/memory-used.rrd -# grouped -require_once 'inc/collectd.inc.php'; -$tinstance = collectd_plugindetail($host, $plugin, 'ti'); - -$obj = new Type_GenericStacked; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); -$obj->data_sources = array('value'); +$obj = new Type_GenericStacked($CONFIG['datadir']); $obj->order = array('free', 'buffered', 'cached', 'used'); $obj->ds_names = array( 'free' => 'Free ', @@ -41,14 +29,12 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'Physical memory utilization'; $obj->rrd_vertical = 'Bytes'; $obj->rrd_format = '%5.1lf%s'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/processes.php b/plugin/processes.php index 65e1ddd..1487568 100644 --- a/plugin/processes.php +++ b/plugin/processes.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericStacked.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # processes/ @@ -14,20 +15,7 @@ require_once 'type/GenericStacked.class.php'; # processes/ps_state-running.rrd # processes/ps_state-sleeping.rrd -# grouped -require_once 'inc/collectd.inc.php'; -$tinstance = collectd_plugindetail($host, $plugin, 'ti'); - -$obj = new Type_GenericStacked; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); -$obj->data_sources = array('value'); +$obj = new Type_GenericStacked($CONFIG['datadir']); $obj->ds_names = array( 'paging' => 'Paging ', 'blocked' => 'Blocked ', @@ -46,14 +34,12 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'Processes'; $obj->rrd_vertical = 'Processes'; $obj->rrd_format = '%5.1lf%s'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/sensors.php b/plugin/sensors.php index 048f9f1..fda5432 100644 --- a/plugin/sensors.php +++ b/plugin/sensors.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/Default.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # disk-XXXX/ @@ -11,49 +12,34 @@ require_once 'type/Default.class.php'; # disk-XXXX/temerature-XXXX.rrd # disk-XXXX/voltage-XXXX.rrd -# grouped -require_once 'inc/collectd.inc.php'; -$tinstance = collectd_plugindetail($host, $plugin, 'ti', array('t' => $type)); - -$obj = new Type_Default; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); -$obj->data_sources = array('value'); +$obj = new Type_Default($CONFIG['datadir']); $obj->ds_names = array( 'value' => 'Value ', ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; -switch($type) { +switch($obj->args['type']) { case 'fanspeed': $obj->colors = '00ff00'; - $obj->rrd_title = sprintf('Fanspeed (%s)', $pinstance); + $obj->rrd_title = sprintf('Fanspeed (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'RPM'; $obj->rrd_format = '%5.1lf'; break; case 'temperature': $obj->colors = '0000ff'; - $obj->rrd_title = sprintf('Temperature (%s)', $pinstance); + $obj->rrd_title = sprintf('Temperature (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'Celius'; $obj->rrd_format = '%5.1lf%s'; break; case 'voltage': $obj->colors = 'ff0000'; - $obj->rrd_title = sprintf('Voltage (%s)', $pinstance); + $obj->rrd_title = sprintf('Voltage (%s)', $obj->args['pinstance']); $obj->rrd_vertical = 'Volt'; $obj->rrd_format = '%5.1lf'; break; } -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/swap.php b/plugin/swap.php index 638ddc3..e1527cc 100644 --- a/plugin/swap.php +++ b/plugin/swap.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericStacked.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # swap/ @@ -11,25 +12,12 @@ require_once 'type/GenericStacked.class.php'; # swap/swap-free.rrd # swap/swap-used.rrd -if ($type == 'swap_io') { +if ($_GET['t'] == 'swap_io') { die_img('Error: swap_io not supported yet'); exit; } -# grouped -require_once 'inc/collectd.inc.php'; -$tinstance = collectd_plugindetail($host, $plugin, 'ti', array('t' => $type)); - -$obj = new Type_GenericStacked; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); -$obj->data_sources = array('value'); +$obj = new Type_GenericStacked($CONFIG['datadir']); $obj->order = array('free', 'cached', 'used'); $obj->ds_names = array( 'free' => 'Free ', @@ -43,14 +31,12 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'Swap utilization'; $obj->rrd_vertical = 'Bytes'; $obj->rrd_format = '%5.1lf%s'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/plugin/users.php b/plugin/users.php index 5ebee43..d94a8c1 100644 --- a/plugin/users.php +++ b/plugin/users.php @@ -9,15 +9,7 @@ require_once 'inc/collectd.inc.php'; ## LAYOUT # users/users.rrd -$obj = new Type_Default; -$obj->datadir = $CONFIG['datadir']; -$obj->args = array( - 'host' => $host, - 'plugin' => $plugin, - 'pinstance' => $pinstance, - 'type' => $type, - 'tinstance' => $tinstance, -); +$obj = new Type_Default($CONFIG['datadir']); $obj->data_sources = array('users'); $obj->ds_names = array( 'users' => 'Users', @@ -27,13 +19,11 @@ $obj->colors = array( ); $obj->width = $width; $obj->heigth = $heigth; -$obj->seconds = $seconds; $obj->rrd_title = 'Users'; $obj->rrd_vertical = 'Users'; $obj->rrd_format = '%.1lf'; -collectd_flush(ident_from_args($obj->args)); - +collectd_flush($obj->identifiers); $obj->rrd_graph(); ?> diff --git a/type/Default.class.php b/type/Default.class.php index 6b1ab5c..0c992e2 100644 --- a/type/Default.class.php +++ b/type/Default.class.php @@ -5,7 +5,8 @@ class Type_Default { var $datadir; var $args; - var $data_sources; + var $seconds; + var $data_sources = array('value'); var $order; var $ds_names; var $colors; @@ -16,6 +17,29 @@ class Type_Default { var $width; var $heigth; + var $files; + var $tinstances; + var $identifiers; + + function __construct($datadir) { + $this->datadir = $datadir; + $this->parse_get(); + $this->rrd_files(); + $this->identifiers = $this->file2identifier($this->files); + } + + # parse $_GET values + function parse_get() { + $this->args = array( + 'host' => $_GET['h'], + 'plugin' => $_GET['p'], + 'pinstance' => $_GET['pi'], + 'type' => $_GET['t'], + 'tinstance' => $_GET['ti'], + ); + $this->seconds = $_GET['s']; + } + function validate_color($color) { if (!preg_match('/^[0-9a-f]{6}$/', $color)) return '000000'; @@ -46,26 +70,41 @@ class Type_Default { return $c[r].$c[g].$c[b]; } - function identifier($host, $plugin, $pinst, $type, $tinst) { - $identifier = sprintf('%s/%s%s%s/%s%s%s', $host, - $plugin, strlen($pinst) ? '-' : '', $pinst, - $type, strlen($tinst) ? '-' : '', $tinst); + function rrd_files() { + $files = $this->get_filenames(); - if (is_file($this->datadir.'/'.$identifier.'.rrd')) - return $identifier; - else - return FALSE; + foreach($files as $filename) { + preg_match("#^$this->datadir/{$this->args['host']}/[\w\d]+-?([\w\d-]+)?/[\w\d]+-?([\w\d-]+)?\.rrd#", $filename, $matches); + + $this->tinstances[] = $matches[2]; + $this->files[$matches[2]] = $filename; + } + + sort($this->tinstances); + ksort($this->files); } - function get_filename($tinstance=NULL) { - if (!is_array($this->args['tinstance']) && $tinstance == NULL) - $tinstance = $this->args['tinstance']; + function get_filenames() { + $identifier = sprintf('%s/%s%s%s/%s%s%s', $this->args['host'], + $this->args['plugin'], strlen($this->args['pinstance']) ? '-' : '', $this->args['pinstance'], + $this->args['type'], strlen($this->args['tinstance']) ? '-' : '', $this->args['tinstance']); + + $wildcard = strlen($this->args['tinstance']) ? '' : '*'; - $identifier = $this->identifier($this->args['host'], - $this->args['plugin'], $this->args['pinstance'], - $this->args['type'], $tinstance); + $files = glob($this->datadir .'/'. $identifier . $wildcard . '.rrd'); - return $this->datadir.'/'.$identifier.'.rrd'; + return $files; + } + + function file2identifier($files) { + foreach($files as $key => $file) { + if (is_file($file)) { + $files[$key] = preg_replace("#^$this->datadir/#", '', $files[$key]); + $files[$key] = preg_replace('#\.rrd$#', '', $files[$key]); + } + } + + return $files; } function rrd_graph($debug=false) { @@ -96,48 +135,57 @@ class Type_Default { return $rrdgraph; } - function rrd_gen_graph() { - $filename = $this->get_filename(); + 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_intersect($this->order, $this->tinstances); + } + # use tinstances as sources + $sources = $this->tinstances; + } + # or one file with multiple data_sources + else { + # use data_sources as sources + $sources = $this->data_sources; + } + return $sources; + } + function rrd_gen_graph() { $rrdgraph = $this->rrd_options(); - if (is_array($this->args['tinstance'])) - $array = is_array($this->order) ? $this->order : $this->args['tinstance']; - else - $array = $this->data_sources; + $sources = $this->rrd_get_sources(); $i=0; - foreach ($array as $value) { - if (is_array($this->args['tinstance'])) { - $filename = $this->get_filename($value); - $ds = $this->data_sources[0]; - } else { - $filename = $this->get_filename(); - $ds = $value; + foreach ($this->tinstances as $tinstance) { + foreach ($this->data_sources as $ds) { + $rrdgraph[] = sprintf('DEF:min_%s=%s:%s:MIN', $sources[$i], $this->files[$tinstance], $ds); + $rrdgraph[] = sprintf('DEF:avg_%s=%s:%s:AVERAGE', $sources[$i], $this->files[$tinstance], $ds); + $rrdgraph[] = sprintf('DEF:max_%s=%s:%s:MAX', $sources[$i], $this->files[$tinstance], $ds); + $i++; } - $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds); - $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds); - $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds); - $i++; } - if (!is_array($this->args['tinstance'])) { - $rrdgraph[] = sprintf('AREA:max0#%s', $this->get_faded_color($this->colors[$this->data_sources[0]])); - $rrdgraph[] = sprintf('AREA:min0#%s', 'ffffff'); + if(count($this->files)<=1) { + foreach ($sources as $source) { + $rrdgraph[] = sprintf('AREA:max_%s#%s', $source, $this->get_faded_color($this->colors[$source])); + $rrdgraph[] = sprintf('AREA:min_%s#%s', $source, 'ffffff'); + break; # only 1 area to draw + } } - $i=0; - foreach ($array as $value) { - $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value; - $color = is_array($this->colors) ? $this->colors[$value]: $this->colors; - $rrdgraph[] = sprintf('LINE1:avg%d#%s:\'%s\'', $i, $this->validate_color($color), $dsname); - $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format); - $i++; + foreach ($sources as $source) { + $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; + $color = is_array($this->colors) ? $this->colors[$source]: $this->colors; + $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $source, $this->validate_color($color), $dsname); + $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\\l\'', $source, $this->rrd_format); } - + return $rrdgraph; } } diff --git a/type/GenericIO.class.php b/type/GenericIO.class.php index e91cb16..ee04316 100644 --- a/type/GenericIO.class.php +++ b/type/GenericIO.class.php @@ -5,45 +5,54 @@ require_once 'Default.class.php'; class Type_GenericIO extends Type_Default { function rrd_gen_graph() { - $filename = $this->get_filename(); - $rrdgraph = $this->rrd_options(); + $sources = $this->rrd_get_sources(); + if ($this->scale) $raw = '_raw'; - foreach($this->data_sources as $ds) { - $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', $ds, $raw, $filename, $ds); - $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', $ds, $raw, $filename, $ds); - $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', $ds, $raw, $filename, $ds); + $i=0; + foreach ($this->tinstances as $tinstance) { + foreach ($this->data_sources as $ds) { + $rrdgraph[] = sprintf('DEF:min_%s%s=%s:%s:MIN', $sources[$i], $raw, $this->files[$tinstance], $ds); + $rrdgraph[] = sprintf('DEF:avg_%s%s=%s:%s:AVERAGE', $sources[$i], $raw, $this->files[$tinstance], $ds); + $rrdgraph[] = sprintf('DEF:max_%s%s=%s:%s:MAX', $sources[$i], $raw, $this->files[$tinstance], $ds); + $i++; + } } if ($this->scale) { - foreach($this->data_sources as $ds) { - $rrdgraph[] = sprintf('CDEF:min_%s=min_%s_raw,%s,*', $ds, $ds, $this->scale); - $rrdgraph[] = sprintf('CDEF:avg_%s=avg_%s_raw,%s,*', $ds, $ds, $this->scale); - $rrdgraph[] = sprintf('CDEF:max_%s=max_%s_raw,%s,*', $ds, $ds, $this->scale); + $i=0; + foreach ($this->tinstances as $tinstance) { + foreach ($this->data_sources as $ds) { + $rrdgraph[] = sprintf('CDEF:min_%s=min_%1$s_raw,%s,*', $sources[$i], $this->scale); + $rrdgraph[] = sprintf('CDEF:avg_%s=avg_%1$s_raw,%s,*', $sources[$i], $this->scale); + $rrdgraph[] = sprintf('CDEF:max_%s=max_%1$s_raw,%s,*', $sources[$i], $this->scale); + $i++; + } } } $rrdgraph[] = sprintf('CDEF:overlap=avg_%s,avg_%s,LT,avg_%1$s,avg_%2$s,IF', - $this->data_sources[0], $this->data_sources[1]); + $sources[0], $sources[1]); - foreach($this->data_sources as $ds) { - $rrdgraph[] = sprintf('AREA:avg_%s#%s', $ds, $this->get_faded_color($this->colors[$ds])); + foreach($sources as $source) { + $rrdgraph[] = sprintf('AREA:avg_%s#%s', $source, $this->get_faded_color($this->colors[$source])); } $rrdgraph[] = sprintf('AREA:overlap#%s', $this->get_faded_color( - $this->get_faded_color($this->colors[$this->data_sources[0]]), - $this->get_faded_color($this->colors[$this->data_sources[1]]) + $this->get_faded_color($this->colors[$sources[0]]), + $this->get_faded_color($this->colors[$sources[1]]) ) ); - foreach($this->data_sources as $ds) { - $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $ds, $this->colors[$ds], $this->ds_names[$ds]); - $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $ds, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $ds, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $ds, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\l\'', $ds, $this->rrd_format); + foreach($sources as $source) { + $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; + $rrdgraph[] = sprintf('LINE1:avg_%s#%s:\'%s\'', $source, $this->colors[$source], $dsname); + $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\l\'', $source, $this->rrd_format); } return $rrdgraph; diff --git a/type/GenericStacked.class.php b/type/GenericStacked.class.php index f024336..7595385 100644 --- a/type/GenericStacked.class.php +++ b/type/GenericStacked.class.php @@ -3,56 +3,41 @@ require_once 'Default.class.php'; class Type_GenericStacked extends Type_Default { - + function rrd_gen_graph() { $rrdgraph = $this->rrd_options(); - if (is_array($this->args['tinstance'])) - if (is_array($this->order)) - $array = array_intersect($this->order, $this->args['tinstance']); - else - $array = $this->args['tinstance']; - else - $array = $this->data_sources; + $sources = $this->rrd_get_sources(); $i=0; - foreach ($array as $value) { - if (is_array($this->args['tinstance'])) { - $filename = $this->get_filename($value); - $ds = $this->data_sources[0]; - } else { - $filename = $this->get_filename(); - $ds = $value; + foreach ($this->tinstances as $tinstance) { + foreach ($this->data_sources as $ds) { + $rrdgraph[] = sprintf('DEF:min_%s=%s:%s:MIN', $sources[$i], $this->files[$tinstance], $ds); + $rrdgraph[] = sprintf('DEF:avg_%s=%s:%s:AVERAGE', $sources[$i], $this->files[$tinstance], $ds); + $rrdgraph[] = sprintf('DEF:max_%s=%s:%s:MAX', $sources[$i], $this->files[$tinstance], $ds); + $i++; } - $rrdgraph[] = sprintf('DEF:min%s=%s:%s:MIN', $i, $filename, $ds); - $rrdgraph[] = sprintf('DEF:avg%s=%s:%s:AVERAGE', $i, $filename, $ds); - $rrdgraph[] = sprintf('DEF:max%s=%s:%s:MAX', $i, $filename, $ds); - $i++; } - for ($i=count($array)-1 ; $i>=0 ; $i--) { - if ($i == (count($array)-1)) - $rrdgraph[] = sprintf('CDEF:cdef%d=avg%d', $i, $i); + for ($i=count($sources)-1 ; $i>=0 ; $i--) { + if ($i == (count($sources)-1)) + $rrdgraph[] = sprintf('CDEF:area_%s=avg_%1$s', $sources[$i]); else - $rrdgraph[] = sprintf('CDEF:cdef%d=cdef%d,avg%d,+', $i, $i+1, $i); + $rrdgraph[] = sprintf('CDEF:area_%s=area_%s,avg_%1$s,+', $sources[$i], $sources[$i+1]); } - $i=0; - foreach ($array as $value) { - $color = $this->get_faded_color($this->colors[$value]); - $rrdgraph[] = sprintf('AREA:cdef%d#%s', $i, $color); - $i++; + foreach ($sources as $source) { + $color = $this->get_faded_color($this->colors[$source]); + $rrdgraph[] = sprintf('AREA:area_%s#%s', $source, $color); } - $i=0; - foreach ($array as $value) { - $dsname = $this->ds_names[$value] != '' ? $this->ds_names[$value] : $value; - $rrdgraph[] = sprintf('LINE1:cdef%d#%s:\'%s\'', $i, $this->validate_color($this->colors[$value]), $dsname); - $rrdgraph[] = sprintf('GPRINT:min%d:MIN:\'%s Min,\'', $i, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg%d:AVERAGE:\'%s Avg,\'', $i, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:max%d:MAX:\'%s Max,\'', $i, $this->rrd_format); - $rrdgraph[] = sprintf('GPRINT:avg%d:LAST:\'%s Last\\l\'', $i, $this->rrd_format); - $i++; + foreach ($sources as $source) { + $dsname = $this->ds_names[$source] != '' ? $this->ds_names[$source] : $source; + $rrdgraph[] = sprintf('LINE1:area_%s#%s:\'%s\'', $source, $this->validate_color($this->colors[$source]), $dsname); + $rrdgraph[] = sprintf('GPRINT:min_%s:MIN:\'%s Min,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:AVERAGE:\'%s Avg,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:max_%s:MAX:\'%s Max,\'', $source, $this->rrd_format); + $rrdgraph[] = sprintf('GPRINT:avg_%s:LAST:\'%s Last\\l\'', $source, $this->rrd_format); } return $rrdgraph; -- cgit v1.1