From e3f1cc22b6f392ba808034ef58e3b7946e3f0ba8 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Fri, 7 Sep 2012 13:48:21 +0200 Subject: add support for varnish plugin categories Since commit collectd-4.10.0-88-g02e12db the varnish plugin groups collected values in categories. The collectd filestructure used for varnish is now: --/- Because this isn't distinguishable from a regular plugin like df, ... df-var-tmp/df_complex-free.rrd ("var" isn't the category here) ... the category is only set with the varnish plugin. Reported-by: Jonathan Huot --- detail.php | 1 + inc/collectd.inc.php | 38 +++++++++++++++++--------------------- inc/functions.inc.php | 1 + plugin/varnish.php | 17 +++-------------- type/Default.class.php | 12 +++++++++--- 5 files changed, 31 insertions(+), 38 deletions(-) diff --git a/detail.php b/detail.php index a7b2cd4..503a411 100644 --- a/detail.php +++ b/detail.php @@ -14,6 +14,7 @@ if (empty($_GET['y'])) $host = validate_get(GET('h'), 'host'); $plugin = validate_get(GET('p'), 'plugin'); $pinstance = validate_get(GET('pi'), 'pinstance'); +$category = validate_get(GET('c'), 'category'); $type = validate_get(GET('t'), 'type'); $tinstance = validate_get(GET('ti'), 'tinstance'); $width = GET('x'); diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php index d5a3d83..3c016af 100644 --- a/inc/collectd.inc.php +++ b/inc/collectd.inc.php @@ -33,13 +33,22 @@ function collectd_plugindata($host, $plugin=NULL) { $data = array(); foreach($files as $item) { - preg_match('#([\w_]+)(?:\-(.+))?/([\w_]+)(?:\-(.+))?\.rrd#', $item, $matches); + preg_match('` + (?P

[\w_]+) # plugin + (?:(?<=varnish)(?:\-(?P[\w]+)))? # category + (?:\-(?P.+))? # plugin instance + / + (?P[\w_]+) # type + (?:\-(?P.+))? # type instance + \.rrd + `x', $item, $matches); $data[] = array( - 'p' => $matches[1], - 'pi' => isset($matches[2]) ? $matches[2] : '', - 't' => $matches[3], - 'ti' => isset($matches[4]) ? $matches[4] : '', + 'p' => $matches['p'], + 'c' => isset($matches['c']) ? $matches['c'] : '', + 'pi' => isset($matches['pi']) ? $matches['pi'] : '', + 't' => $matches['t'], + 'ti' => isset($matches['ti']) ? $matches['ti'] : '', ); } @@ -70,7 +79,7 @@ function collectd_plugins($host) { # returns an array of all pi/t/ti of an plugin function collectd_plugindetail($host, $plugin, $detail, $where=NULL) { - $details = array('pi', 't', 'ti'); + $details = array('pi', 'c', 't', 'ti'); if (!in_array($detail, $details)) return false; @@ -122,11 +131,12 @@ function group_plugindata($plugindata) { function plugin_sort($data) { foreach ($data as $key => $row) { $pi[$key] = $row['pi']; + $c[$key] = $row['c']; $ti[$key] = $row['ti']; $t[$key] = $row['t']; } - array_multisort($pi, SORT_ASC, $t, SORT_ASC, $ti, SORT_ASC, $data); + array_multisort($c, SORT_ASC, $pi, SORT_ASC, $t, SORT_ASC, $ti, SORT_ASC, $data); return $data; } @@ -180,20 +190,6 @@ function build_url($base, $items, $s=NULL) { return $base; } -# generate identifier that collectd's FLUSH command understands -function collectd_identifier($host, $plugin, $pinst, $type, $tinst) { - global $CONFIG; - - $identifier = sprintf('%s/%s%s%s/%s%s%s', $host, - $plugin, strlen($pinst) ? '-' : '', $pinst, - $type, strlen($tinst) ? '-' : '', $tinst); - - if (is_file($CONFIG['datadir'].'/'.$identifier.'.rrd')) - return $identifier; - else - return FALSE; -} - # tell collectd to FLUSH all data of the identifier(s) function collectd_flush($identifier) { global $CONFIG; diff --git a/inc/functions.inc.php b/inc/functions.inc.php index 39f1cd0..a72a589 100644 --- a/inc/functions.inc.php +++ b/inc/functions.inc.php @@ -15,6 +15,7 @@ function validate_get($value, $type) { return NULL; break; case 'plugin': + case 'category': case 'type': if (!preg_match('/^\w+$/u', $value)) return NULL; diff --git a/plugin/varnish.php b/plugin/varnish.php index 4de4463..8099691 100644 --- a/plugin/varnish.php +++ b/plugin/varnish.php @@ -26,19 +26,8 @@ $obj = new Type_Default($CONFIG); $obj->width = $width; $obj->heigth = $heigth; $obj->rrd_format = '%5.1lf%s'; -switch($obj->args['pinstance']) { - case 'default-backend': - $obj->rrd_title = 'backend'; - $obj->rrd_vertical = 'hits'; - break; - case 'default-cache': - $obj->rrd_title = 'cache'; - $obj->rrd_vertical = 'hits'; - break; - case 'default-connections': - $obj->rrd_title = 'connections'; - $obj->rrd_vertical = 'hits'; - break; -} +$obj->rrd_title = sprintf('%s (%s)', ucfirst($obj->args['pinstance']), $obj->args['category']); +$obj->rrd_vertical = 'hits'; + collectd_flush($obj->identifiers); $obj->rrd_graph(); diff --git a/type/Default.class.php b/type/Default.class.php index b8650dc..dec583b 100644 --- a/type/Default.class.php +++ b/type/Default.class.php @@ -62,6 +62,7 @@ class Type_Default { 'host' => GET('h'), 'plugin' => GET('p'), 'pinstance' => GET('pi'), + 'category' => GET('c'), 'type' => GET('t'), 'tinstance' => GET('ti'), ); @@ -120,9 +121,14 @@ class Type_Default { } 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']); + $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'] + ); $wildcard = strlen($this->args['tinstance']) ? '.' : '[-.]*'; -- cgit v1.1