From cd94ef34134d8f06af83ec97119018b4e80346a0 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sun, 20 Sep 2009 13:36:25 +0200 Subject: initial import of cgp Collectd Graph Panel is a frontend for Collectd written in PHP. The goal of CGP is to provide an easy-to-use frontend for Collectd, starting with page that shows an overview of all the hosts you are managing with Collectd. In this initial import there is support for the plugins that are default enabled in Collectd. The supported plugins are located in the plugin directory. --- inc/collectd.inc.php | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++ inc/html.inc.php | 45 +++++++++++++ inc/rrdtool.class.php | 25 +++++++ 3 files changed, 246 insertions(+) create mode 100644 inc/collectd.inc.php create mode 100644 inc/html.inc.php create mode 100644 inc/rrdtool.class.php (limited to 'inc') diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php new file mode 100644 index 0000000..2724c05 --- /dev/null +++ b/inc/collectd.inc.php @@ -0,0 +1,176 @@ + $v) { + if(!is_dir($CONFIG['datadir'].'/'.$v) || $v == '.' || $v == '..') + unset($dir[$k]); + } + return($dir); +} + +# returns an array of plugins/pinstances/types/tinstances +function collectd_plugindata($host) { + global $CONFIG; + + if (!is_dir($CONFIG['datadir'].'/'.$host)) + return false; + + chdir($CONFIG['datadir'].'/'.$host); + $files = glob("*/*.rrd"); + if (!$files) + return false; + + $data; + $i = 0; + foreach($files as $item) { + unset($part); + + # split item by plugin/type + $part = explode('/', $item); + $part[1] = preg_replace('/\.rrd/', '', $part[1]); + + # plugin + $data[$i]['p'] = preg_replace('/-.+/', '', $part[0]); + + # plugin instance + if(preg_match('/-/', $part[0])) + $data[$i]['pi'] = preg_replace('/^[a-z_]+\-/', '', $part[0]); + + # type + $data[$i]['t'] = preg_replace('/-.+/', '', $part[1]); + + # type instance + if(preg_match('/-/', $part[1])) + $data[$i]['ti'] = preg_replace('/^[a-z_]+\-/', '', $part[1]); + + $i++; + } + return($data); +} + +# returns an array of all plugins of a host +function collectd_plugins($host) { + $plugindata = collectd_plugindata($host); + + $plugins = array(); + foreach ($plugindata as $item) { + if (!in_array($item['p'], $plugins)) + $plugins[] = $item['p']; + } + + return $plugins; +} + +# returns an array of all pi/t/ti of an plugin +function collectd_plugindetail($host, $plugin, $detail, $where=NULL) { + $details = array('pi', 't', 'ti'); + if (!in_array($detail, $details)) + return false; + + $plugindata = collectd_plugindata($host); + + $return = array(); + foreach ($plugindata as $item) { + if ($item['p'] == $plugin && !in_array($item[$detail], $return) && isset($item[$detail])) { + if ($where) { + $add = true; + # add detail to returnvalue if all where is true + foreach($where as $key => $value) { + if ($item[$key] != $value) + $add = false; + } + if ($add) + $return[] = $item[$detail]; + } else { + $return[] = $item[$detail]; + } + } + } + + if (empty($return)) + return false; + + return $return; +} + +# 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'); + + foreach($pis as $pi) { + if ($CONFIG['groupby'][$plugin] == 'type') { + foreach ($ts as $t) { + $items = array( + 'h' => $host, + 'p' => $plugin, + 'pi' => $pi, + 't' => $t + ); + printf(''."\n", + $CONFIG['weburl'], + build_url('detail.php', $items).'&x=800&y=350', + $CONFIG['weburl'], + build_url('graph.php', $items) + ); + } + } else { + foreach ($tis as $ti) { + foreach ($ts as $t) { + $items = array( + 'h' => $host, + 'p' => $plugin, + 'pi' => $pi, + 't' => $t, + 'ti' => $ti + ); + printf(''."\n", + $CONFIG['weburl'], + build_url('detail.php', $items).'&x=800&y=350', + $CONFIG['weburl'], + build_url('graph.php', $items) + ); + } + } + } + } +} + +# generate an url with GET values from $items +function build_url($base, $items, $s=86400) { + if (!is_array($items)) + return false; + + if (!is_numeric($s)) + return false; + + $i=0; + foreach ($items as $key => $value) { + # don't include empty values + if ($value == 'NULL') + continue; + + $base .= sprintf('%s%s=%s', $i==0 ? '?' : '&', $key, $value); + $i++; + } + if (!isset($items['s'])) + $base .= '&s='.$s; + + return $base; +} + +?> diff --git a/inc/html.inc.php b/inc/html.inc.php new file mode 100644 index 0000000..d1d2a65 --- /dev/null +++ b/inc/html.inc.php @@ -0,0 +1,45 @@ + + + + + Collectd Graph Panel + + + + +EOT; +} + +function html_end() { + echo << + +EOT; +} + +require_once 'conf/common.inc.php'; +require_once 'inc/rrdtool.class.php'; +function host_summary($hosts) { + global $CONFIG; + + $rrd = new RRDTool; + + echo "\n"; + + foreach($hosts as $host) { + $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); + if (!$rrd_info) + continue; + printf(''."\n", + $CONFIG['weburl'],$host, $host, + $rrd_info["ds[shortterm].last_ds"], $rrd_info["ds[midterm].last_ds"], $rrd_info["ds[longterm].last_ds"]); + } + + echo "
%s%.2f%.2f%.2f
\n"; +} + +?> diff --git a/inc/rrdtool.class.php b/inc/rrdtool.class.php new file mode 100644 index 0000000..e1a7b4b --- /dev/null +++ b/inc/rrdtool.class.php @@ -0,0 +1,25 @@ + $info) { + if ($info != "") { + $item_info = explode(" = ", $info); + $item_info[1] = preg_replace('/"/', '', $item_info[1]); + $info_array[$item_info[0]] = $item_info[1]; + } + } + return($info_array); + } else { + return false; + } + } +} + +?> -- cgit v1.1