From 1ee60c5976e9cb049bc5e650e5c1b9205b15910f Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Mon, 4 Oct 2010 11:45:17 +0200 Subject: inc/collectd: improve determining which files to group in 1 graph Previously graphs were created by collecting and merging all PIs, Ts and TIs separately. This went wrong when a part of a plugin had a PI and another part had not (processes plugin). It also went wrong when a P-PI combination had different Ts and TIs (bind plugin). This is solved by using grouped and deduplicated plugindata. -- P = plugin, PI = plugin instance, T = type, TI = type instance --- inc/collectd.inc.php | 77 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 33 deletions(-) (limited to 'inc') diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php index 7f88456..18f56c8 100644 --- a/inc/collectd.inc.php +++ b/inc/collectd.inc.php @@ -20,7 +20,7 @@ function collectd_hosts() { } # returns an array of plugins/pinstances/types/tinstances -function collectd_plugindata($host) { +function collectd_plugindata($host, $plugin=NULL) { global $CONFIG; if (!is_dir($CONFIG['datadir'].'/'.$host)) @@ -43,6 +43,15 @@ function collectd_plugindata($host) { ); } + # only return data about one plugin + if (!is_null($plugin)) { + foreach($data as $item) { + if ($item['p'] == $plugin) + $pdata[] = $item; + } + $data = $pdata; + } + return($data); } @@ -91,42 +100,44 @@ function collectd_plugindetail($host, $plugin, $detail, $where=NULL) { return $return; } +# group plugin files for graph generation +function group_plugindata($plugindata) { + global $CONFIG; + + # type instances should be grouped in 1 graph + foreach ($plugindata as $item) { + # backwards compatibility + if ($CONFIG['version'] >= 5 || !preg_match('/^(df|interface)$/', $item['p'])) + unset($item['ti']); + $data[] = $item; + } + + # remove duplicates + $data = array_map("unserialize", array_unique(array_map("serialize", $data))); + + return $data; +} + # generate graph url's for a plugin of a host function graphs_from_plugin($host, $plugin) { global $CONFIG; - $pis = collectd_plugindetail($host, $plugin, 'pi'); - $ts = collectd_plugindetail($host, $plugin, 't'); - $tis = collectd_plugindetail($host, $plugin, 'ti'); - if (!$pis) $pis = array('NULL'); - if (!$tis) $tis = array('NULL'); - # backwards compatibility - if ($CONFIG['version'] >= 5 || !preg_match('/^(df|interface)$/', $plugin)) - $tis = array('NULL'); - - foreach($pis as $pi) { - foreach ($tis as $ti) { - foreach ($ts as $t) { - $items = array( - 'h' => $host, - 'p' => $plugin, - 'pi' => $pi, - 't' => $t, - 'ti' => $ti - ); - - $time = array_key_exists($plugin, $CONFIG['time_range']) - ? $CONFIG['time_range'][$plugin] - : $CONFIG['time_range']['default']; - - printf(''."\n", - $CONFIG['weburl'], - build_url('detail.php', $items, $time), - $CONFIG['weburl'], - build_url('graph.php', $items, $time) - ); - } - } + $plugindata = collectd_plugindata($host, $plugin); + $plugindata = group_plugindata($plugindata); + + foreach ($plugindata as $items) { + $items['h'] = $host; + + $time = array_key_exists($plugin, $CONFIG['time_range']) + ? $CONFIG['time_range'][$plugin] + : $CONFIG['time_range']['default']; + + printf(''."\n", + $CONFIG['weburl'], + build_url('detail.php', $items, $time), + $CONFIG['weburl'], + build_url('graph.php', $items, $time) + ); } } -- cgit v1.1