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. --- .htaccess | 2 + conf/.gitignore | 1 + conf/common.inc.php | 8 ++ conf/config.php | 19 +++++ detail.php | 52 +++++++++++++ graph.php | 39 ++++++++++ host.php | 43 +++++++++++ inc/collectd.inc.php | 176 ++++++++++++++++++++++++++++++++++++++++++ inc/html.inc.php | 45 +++++++++++ inc/rrdtool.class.php | 25 ++++++ index.php | 30 +++++++ layout/style.css | 23 ++++++ plugin/cpu.php | 65 ++++++++++++++++ plugin/df.php | 42 ++++++++++ plugin/disk.php | 63 +++++++++++++++ plugin/entropy.php | 37 +++++++++ plugin/interface.php | 54 +++++++++++++ plugin/irq.php | 40 ++++++++++ plugin/load.php | 41 ++++++++++ plugin/memory.php | 53 +++++++++++++ plugin/processes.php | 58 ++++++++++++++ plugin/sensors.php | 58 ++++++++++++++ plugin/swap.php | 50 ++++++++++++ plugin/users.php | 37 +++++++++ type/Default.class.php | 127 ++++++++++++++++++++++++++++++ type/GenericIO.class.php | 59 ++++++++++++++ type/GenericStacked.class.php | 65 ++++++++++++++++ 27 files changed, 1312 insertions(+) create mode 100644 .htaccess create mode 100644 conf/.gitignore create mode 100644 conf/common.inc.php create mode 100644 conf/config.php create mode 100644 detail.php create mode 100644 graph.php create mode 100644 host.php create mode 100644 inc/collectd.inc.php create mode 100644 inc/html.inc.php create mode 100644 inc/rrdtool.class.php create mode 100644 index.php create mode 100644 layout/style.css create mode 100644 plugin/cpu.php create mode 100644 plugin/df.php create mode 100644 plugin/disk.php create mode 100644 plugin/entropy.php create mode 100644 plugin/interface.php create mode 100644 plugin/irq.php create mode 100644 plugin/load.php create mode 100644 plugin/memory.php create mode 100644 plugin/processes.php create mode 100644 plugin/sensors.php create mode 100644 plugin/swap.php create mode 100644 plugin/users.php create mode 100644 type/Default.class.php create mode 100644 type/GenericIO.class.php create mode 100644 type/GenericStacked.class.php diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..9e0a727 --- /dev/null +++ b/.htaccess @@ -0,0 +1,2 @@ +RewriteEngine On +RewriteRule ^.git(ignore|/) - [F,L] diff --git a/conf/.gitignore b/conf/.gitignore new file mode 100644 index 0000000..529bf01 --- /dev/null +++ b/conf/.gitignore @@ -0,0 +1 @@ +config.local.php diff --git a/conf/common.inc.php b/conf/common.inc.php new file mode 100644 index 0000000..c4884d6 --- /dev/null +++ b/conf/common.inc.php @@ -0,0 +1,8 @@ + diff --git a/conf/config.php b/conf/config.php new file mode 100644 index 0000000..4fb7aec --- /dev/null +++ b/conf/config.php @@ -0,0 +1,19 @@ + 'type', + 'irq' => 'type', + 'memory' => 'type', + 'processes' => 'type', + 'swap' => 'type', + 'sensors' => 'type', +); + +include 'config.local.php'; + +?> diff --git a/detail.php b/detail.php new file mode 100644 index 0000000..e12334d --- /dev/null +++ b/detail.php @@ -0,0 +1,52 @@ +« %s'."\n", + $CONFIG['weburl'].'/host.php?h='.htmlentities($host), $host +); + +$term = array( + '2hour' => 3600*2, + '6hour' => 3600*6, + 'day' => 86400, + 'week' => 86400*7, + 'month' => 86400*31, + 'quarter'=> 86400*31*3, + 'year' => 86400*365, +); + +$args = $_GET; +foreach($term as $key => $s) { + $args['s'] = $s; + printf('%s'."\n", + $CONFIG['weburl'], build_url('detail.php', $args), $key); +} + +print "
\n"; + +$plugins = collectd_plugins($host); + +if(!$plugins) { + echo "Unknown host\n"; + return false; +} + +# show graph +printf(''."\n", $CONFIG['weburl'], build_url('graph.php', $_GET)); + +html_end(); + +?> diff --git a/graph.php b/graph.php new file mode 100644 index 0000000..4747552 --- /dev/null +++ b/graph.php @@ -0,0 +1,39 @@ + diff --git a/host.php b/host.php new file mode 100644 index 0000000..22112e6 --- /dev/null +++ b/host.php @@ -0,0 +1,43 @@ +« %s'."\n", $CONFIG['weburl'], $host); + +$plugins = collectd_plugins($host); + +if(!$plugins) { + echo "Unknown host\n"; + return false; +} + +# first the ones defined in overview +foreach($CONFIG['overview'] as $plugin) { + if (in_array($plugin, $plugins)) { + printf("

[-] %s

\n", $plugin); + graphs_from_plugin($host, $plugin); + } +} + +# other plugins +foreach($plugins as $plugin) { + if (!in_array($plugin, $CONFIG['overview'])) { + $url = sprintf('%s'."\n", $CONFIG['weburl'], $host, $plugin, $plugin); + if ($splugin == $plugin) { + printf("

[-] %s

\n", $url); + graphs_from_plugin($host, $plugin); + } else { + printf("

[+] %s

\n", $url); + } + } +} + +html_end(); + +?> 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; + } + } +} + +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..f937bcc --- /dev/null +++ b/index.php @@ -0,0 +1,30 @@ + $hosts) { + printf("

%s

\n", $cat); + host_summary($hosts); + $h = array_merge($h, $hosts); +} + +# search for uncategorized hosts +$chosts = collectd_hosts(); +$uhosts = array_diff($chosts, $h); + +# show all uncategorized hosts +if ($uhosts) { + echo "

uncategorized

\n"; + host_summary($uhosts); +} + +html_end(); + +?> diff --git a/layout/style.css b/layout/style.css new file mode 100644 index 0000000..813f98d --- /dev/null +++ b/layout/style.css @@ -0,0 +1,23 @@ +h1 { + font-size: 1.5em; + margin: 0.5em 0; +} + +h2 { + font-size: 1em; +} + +table.summary th { + width: 150px; + font-weight: normal; + text-align: left; +} + +table.summary td { + width: 50px; + text-align: right; +} + +img { + border: 0; +} diff --git a/plugin/cpu.php b/plugin/cpu.php new file mode 100644 index 0000000..10fbf56 --- /dev/null +++ b/plugin/cpu.php @@ -0,0 +1,65 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}-{pinstance}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('value'); +$obj->order = array('idle', 'nice', 'user', 'wait', 'system', 'softirq', 'interrupt', 'steal'); +$obj->ds_names = array( + 'idle' => 'Idle ', + 'nice' => 'Nice ', + 'user' => 'User ', + 'wait' => 'Wait-IO', + 'system' => 'System ', + 'softirq' => 'SoftIRQ', + 'interrupt' => 'IRQ ', + 'steal' => 'Steal ', +); +$obj->colors = array( + 'idle' => 'e8e8e8', + 'nice' => '00e000', + 'user' => '0000ff', + 'wait' => 'ffb000', + 'system' => 'ff0000', + 'softirq' => 'ff00ff', + 'interrupt' => 'a000a0', + 'steal' => '000000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; + +$obj->rrd_title = "CPU-$pinstance usage on $host"; +$obj->rrd_vertical = 'Jiffies'; +$obj->rrd_format = '%5.2lf'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/df.php b/plugin/df.php new file mode 100644 index 0000000..6398249 --- /dev/null +++ b/plugin/df.php @@ -0,0 +1,42 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('free', 'used'); +$obj->ds_names = array( + 'free' => 'Free', + 'used' => 'Used', +); +$obj->colors = array( + 'free' => '00ff00', + 'used' => 'ff0000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; + +$obj->rrd_title = "Free space ($tinstance) on $host"; +$obj->rrd_vertical = 'Bytes'; +$obj->rrd_format = '%5.1lf%sB'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/disk.php b/plugin/disk.php new file mode 100644 index 0000000..298c179 --- /dev/null +++ b/plugin/disk.php @@ -0,0 +1,63 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}-{pinstance}/{type}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('read', 'write'); +$obj->ds_names = array( + 'read' => 'Read ', + 'write' => 'Written', +); +$obj->colors = array( + 'read' => '0000ff', + 'write' => '00b000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; +switch($type) { + case 'disk_merged': + $obj->rrd_title = "Disk Merged Operations ($pinstance) on $host"; + $obj->rrd_vertical = 'Merged operations/s'; + $obj->rrd_format = '%5.1lf'; + break; + case 'disk_octets': + $obj->rrd_title = "Disk Traffic ($pinstance) on $host"; + $obj->rrd_vertical = 'Bytes per second'; + $obj->rrd_format = '%5.1lf%s'; + break; + case 'disk_ops': + $obj->rrd_title = "Disk Operations ($pinstance) on $host"; + $obj->rrd_vertical = 'Ops per second'; + $obj->rrd_format = '%5.1lf'; + break; + case 'disk_time': + $obj->rrd_title = "Disk time per operation ($pinstance) on $host"; + $obj->rrd_vertical = 'Avg. Time/Op'; + $obj->rrd_format = '%5.1lf%ss'; + $obj->scale = '0.001'; + break; +} + +$obj->rrd_graph(); + +?> diff --git a/plugin/entropy.php b/plugin/entropy.php new file mode 100644 index 0000000..55159dc --- /dev/null +++ b/plugin/entropy.php @@ -0,0 +1,37 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('entropy'); +$obj->ds_names = array( + 'entropy' => 'Entropy bits', +); +$obj->colors = array( + 'entropy' => '0000f0', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; +$obj->rrd_title = "Available entropy on $host"; +$obj->rrd_vertical = 'Bits'; +$obj->rrd_format = '%4.0lf'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/interface.php b/plugin/interface.php new file mode 100644 index 0000000..2f345f9 --- /dev/null +++ b/plugin/interface.php @@ -0,0 +1,54 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('rx', 'tx'); +$obj->ds_names = array( + 'rx' => 'Receive ', + 'tx' => 'Transmit', +); +$obj->colors = array( + 'rx' => '0000ff', + 'tx' => '00b000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; +$obj->rrd_format = '%5.1lf%s'; +switch($type) { + case 'if_errors': + $obj->rrd_title = "Interface Errors ($tinstance) on $host"; + $obj->rrd_vertical = 'Errors per second'; + break; + case 'if_octets': + $obj->rrd_title = "Interface Traffic ($tinstance) on $host"; + $obj->rrd_vertical = 'Bits per second'; + break; + case 'if_packets': + $obj->rrd_title = "Interface Packets ($tinstance) on $host"; + $obj->rrd_vertical = 'Packets per second'; + break; +} + +$obj->rrd_graph(); + +?> diff --git a/plugin/irq.php b/plugin/irq.php new file mode 100644 index 0000000..2759c0e --- /dev/null +++ b/plugin/irq.php @@ -0,0 +1,40 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}-{tinstance}.rrd'; +$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->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; + +$obj->rrd_title = "Interrupts on $host"; +$obj->rrd_vertical = 'IRQs/s'; +$obj->rrd_format = '%6.1lf'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/load.php b/plugin/load.php new file mode 100644 index 0000000..d7db740 --- /dev/null +++ b/plugin/load.php @@ -0,0 +1,41 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('shortterm', 'midterm', 'longterm'); +$obj->ds_names = array( + 'shortterm' => ' 1 min', + 'midterm' => ' 5 min', + 'longterm' => '15 min', +); +$obj->colors = array( + 'shortterm' => '00ff00', + 'midterm' => '0000ff', + 'longterm' => 'ff0000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; +$obj->rrd_title = "System load on $host"; +$obj->rrd_vertical = 'System load'; +$obj->rrd_format = '%.2lf'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/memory.php b/plugin/memory.php new file mode 100644 index 0000000..7944238 --- /dev/null +++ b/plugin/memory.php @@ -0,0 +1,53 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('value'); +$obj->order = array('free', 'buffered', 'cached', 'used'); +$obj->ds_names = array( + 'free' => 'Free ', + 'cached' => 'Cached ', + 'buffered' => 'Buffered', + 'used' => 'Used ', +); +$obj->colors = array( + 'free' => '00e000', + 'cached' => '0000ff', + 'buffered' => 'ffb000', + 'used' => 'ff0000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; + +$obj->rrd_title = "Physical memory utilization on $host"; +$obj->rrd_vertical = 'Bytes'; +$obj->rrd_format = '%5.1lf%s'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/processes.php b/plugin/processes.php new file mode 100644 index 0000000..445e1e4 --- /dev/null +++ b/plugin/processes.php @@ -0,0 +1,58 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('value'); +$obj->ds_names = array( + 'paging' => 'Paging ', + 'blocked' => 'Blocked ', + 'zombies' => 'Zombies ', + 'stopped' => 'Stopped ', + 'running' => 'Running ', + 'sleeping' => 'Sleeping', +); +$obj->colors = array( + 'paging' => 'ffb000', + 'blocked' => 'ff00ff', + 'zombies' => 'ff0000', + 'stopped' => 'a000a0', + 'running' => '00e000', + 'sleeping' => '0000ff', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; + +$obj->rrd_title = "Processes on $host"; +$obj->rrd_vertical = 'Processes'; +$obj->rrd_format = '%5.1lf%s'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/sensors.php b/plugin/sensors.php new file mode 100644 index 0000000..ad43378 --- /dev/null +++ b/plugin/sensors.php @@ -0,0 +1,58 @@ + $type)); + +$obj = new Type_Default; +$obj->datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}-{pinstance}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('value'); +$obj->ds_names = array( + 'value' => 'Value ', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; +switch($type) { + case 'fanspeed': + $obj->colors = '00ff00'; + $obj->rrd_title = "Fanspeed ($pinstance) on $host"; + $obj->rrd_vertical = 'RPM'; + $obj->rrd_format = '%5.1lf'; + break; + case 'temperature': + $obj->colors = '0000ff'; + $obj->rrd_title = "Temperature ($pinstance) on $host"; + $obj->rrd_vertical = 'Celius'; + $obj->rrd_format = '%5.1lf%s'; + break; + case 'voltage': + $obj->colors = 'ff0000'; + $obj->rrd_title = "Voltage ($pinstance) on $host"; + $obj->rrd_vertical = 'Volt'; + $obj->rrd_format = '%5.1lf'; + break; +} + +$obj->rrd_graph(); + +?> diff --git a/plugin/swap.php b/plugin/swap.php new file mode 100644 index 0000000..dfd9ba1 --- /dev/null +++ b/plugin/swap.php @@ -0,0 +1,50 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}-{tinstance}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('value'); +$obj->order = array('free', 'cached', 'used'); +$obj->ds_names = array( + 'free' => 'Free ', + 'cached' => 'Cached ', + 'used' => 'Used ', +); +$obj->colors = array( + 'free' => '00e000', + 'cached' => '0000ff', + 'used' => 'ff0000', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; + +$obj->rrd_title = "Swap utilization on $host"; +$obj->rrd_vertical = 'Bytes'; +$obj->rrd_format = '%5.1lf%s'; + +$obj->rrd_graph(); + +?> diff --git a/plugin/users.php b/plugin/users.php new file mode 100644 index 0000000..dfcaee3 --- /dev/null +++ b/plugin/users.php @@ -0,0 +1,37 @@ +datadir = $CONFIG['datadir']; +$obj->path_format = '{host}/{plugin}/{type}.rrd'; +$obj->args = array( + 'host' => $host, + 'plugin' => $plugin, + 'pinstance' => $pinstance, + 'type' => $type, + 'tinstance' => $tinstance, +); +$obj->data_sources = array('users'); +$obj->ds_names = array( + 'users' => 'Users', +); +$obj->colors = array( + 'users' => '0000f0', +); +$obj->width = $width; +$obj->heigth = $heigth; +$obj->seconds = $seconds; +$obj->rrd_title = "Users on $host"; +$obj->rrd_vertical = 'Users'; +$obj->rrd_format = '%.1lf'; + +$obj->rrd_graph(); + +?> diff --git a/type/Default.class.php b/type/Default.class.php new file mode 100644 index 0000000..8a5dd72 --- /dev/null +++ b/type/Default.class.php @@ -0,0 +1,127 @@ +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 get_filename($tinstance=NULL) { + if (!is_array($this->args['tinstance']) && $tinstance == NULL) + $tinstance = $this->args['tinstance']; + + $search = array('{host}', '{plugin}', '{pinstance}', '{type}', '{tinstance}'); + $replace = array($this->args['host'], $this->args['plugin'], $this->args['pinstance'], $this->args['type'], $tinstance); + $f = $this->datadir . '/' . str_replace($search, $replace, $this->path_format); + return $f; + } + + function rrd_graph($debug=false) { + $graphdata = $this->rrd_gen_graph(); + + if(!$debug) { + # caching + header("Expires: " . date(DATE_RFC822,strtotime("90 seconds"))); + header("content-type: image/png"); + $graphdata = implode(' ', $graphdata); + echo `$graphdata`; + } else { + print '
';
+			print_r($graphdata);
+			print '
'; + } + } + + function rrd_gen_graph() { + $filename = $this->get_filename(); + + $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG'; + $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400); + $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175); + $rrdgraph[] = '-l 0'; + $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title); + $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); + $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400); + + if (is_array($this->args['tinstance'])) + $array = is_array($this->order) ? $this->order : $this->args['tinstance']; + else + $array = $this->data_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; + } + $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'); + } + + $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++; + } + + return $rrdgraph; + } +} + +?> diff --git a/type/GenericIO.class.php b/type/GenericIO.class.php new file mode 100644 index 0000000..573abde --- /dev/null +++ b/type/GenericIO.class.php @@ -0,0 +1,59 @@ +get_filename(); + + $rrdgraph[] = '/usr/bin/rrdtool graph - -a PNG'; + $rrdgraph[] = sprintf('-w %d', is_numeric($this->width) ? $this->width : 400); + $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175); + $rrdgraph[] = '-l 0'; + $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title); + $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); + $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400); + + 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); + } + 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); + } + } + + $rrdgraph[] = sprintf('CDEF:overlap=avg_%s,avg_%s,LT,avg_%1$s,avg_%2$s,IF', + $this->data_sources[0], $this->data_sources[1]); + + foreach($this->data_sources as $ds) { + $rrdgraph[] = sprintf('AREA:avg_%s#%s', $ds, $this->get_faded_color($this->colors[$ds])); + } + + $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]]) + ) + ); + + 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); + } + + return $rrdgraph; + } +} + +?> diff --git a/type/GenericStacked.class.php b/type/GenericStacked.class.php new file mode 100644 index 0000000..99f05eb --- /dev/null +++ b/type/GenericStacked.class.php @@ -0,0 +1,65 @@ +width) ? $this->width : 400); + $rrdgraph[] = sprintf('-h %d', is_numeric($this->heigth) ? $this->heigth : 175); + $rrdgraph[] = '-l 0'; + $rrdgraph[] = sprintf('-t "%s"', $this->rrd_title); + $rrdgraph[] = sprintf('-v "%s"', $this->rrd_vertical); + $rrdgraph[] = sprintf('-s -%d', is_numeric($this->seconds) ? $this->seconds : 86400); + + if (is_array($this->args['tinstance'])) + $array = is_array($this->order) ? $this->order : $this->args['tinstance']; + else + $array = $this->data_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; + } + $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); + else + $rrdgraph[] = sprintf('CDEF:cdef%d=cdef%d,avg%d,+', $i, $i+1, $i); + } + + $i=0; + foreach ($array as $value) { + $color = $this->get_faded_color($this->colors[$value]); + $rrdgraph[] = sprintf('AREA:cdef%d#%s', $i, $color); + $i++; + } + + $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++; + } + + return $rrdgraph; + } +} + +?> -- cgit v1.1