From 488b4f3ea230b0bc3cb7fa7e5264691e21067cd0 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Wed, 4 Nov 2009 23:04:30 +0100 Subject: add unixsock flush support Via the UnixSock plugin of Collectd it is possible to send commands to the Collectd daemon. One of the commands is the FLUSH command, which you can use to let the daemon write cached data to the rrd files. CGP uses the FLUSH command before generating a graph and before showing the load information on the main page. In this case the information shown via CGP is always up-to-date. Also when you have configured the RRDTool plugin of Collectd with a CacheTimeout setting. This commit includes code based on functions collectd_identifier and collectd_flush from php-collection by Bruno Prémont. --- conf/config.php | 5 ++++ inc/collectd.inc.php | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ inc/html.inc.php | 2 ++ plugin/cpu.php | 2 ++ plugin/df.php | 3 ++ plugin/disk.php | 3 ++ plugin/entropy.php | 3 ++ plugin/interface.php | 3 ++ plugin/irq.php | 2 ++ plugin/load.php | 3 ++ plugin/memory.php | 2 ++ plugin/processes.php | 2 ++ plugin/sensors.php | 2 ++ plugin/swap.php | 2 ++ plugin/users.php | 3 ++ 15 files changed, 118 insertions(+) diff --git a/conf/config.php b/conf/config.php index cbddfb3..751bcf1 100644 --- a/conf/config.php +++ b/conf/config.php @@ -21,6 +21,11 @@ $CONFIG['groupby'] = array( 'sensors' => 'type', ); +# collectd's unix socket (unixsock plugin) +# enabled: 'unix:///var/run/collectd-unixsock' +# disabled: NULL +$CONFIG['socket'] = NULL; + if (file_exists(dirname(__FILE__).'/config.local.php')) include 'config.local.php'; diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php index 525b931..3ef9102 100644 --- a/inc/collectd.inc.php +++ b/inc/collectd.inc.php @@ -157,4 +157,85 @@ function build_url($base, $items, $s=86400) { 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; + + if (!$CONFIG['socket']) + return FALSE; + + if (!$identifier || (is_array($identifier) && count($identifier) == 0) || + !(is_string($identifier) || is_array($identifier))) + return FALSE; + + $u_errno = 0; + $u_errmsg = ''; + if ($socket = @fsockopen($CONFIG['socket'], 0, $u_errno, $u_errmsg)) { + $cmd = 'FLUSH plugin=rrdtool'; + if (is_array($identifier)) { + foreach ($identifier as $val) + $cmd .= sprintf(' identifier="%s"', $val); + } else + $cmd .= sprintf(' identifier="%s"', $identifier); + $cmd .= "\n"; + + $r = fwrite($socket, $cmd, strlen($cmd)); + if ($r === false || $r != strlen($cmd)) { + printf('ERROR: Failed to write full command to unix-socket: %d out of %d written', + $r === false ? -1 : $r, strlen($cmd)); + return FALSE; + } + + $resp = fgets($socket); + if ($resp === false) { + printf('ERROR: Failed to read response from collectd for command: %s', + trim($cmd)); + return FALSE; + } + + $n = (int)$resp; + while ($n-- > 0) + fgets($socket); + + fclose($socket); + + return TRUE; + } else { + printf('ERROR: Failed to open unix-socket to collectd: %d: %s', + $u_errno, $u_errmsg); + return FALSE; + } +} + +# 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/inc/html.inc.php b/inc/html.inc.php index cbe3ad2..09da518 100644 --- a/inc/html.inc.php +++ b/inc/html.inc.php @@ -2,6 +2,7 @@ require_once 'conf/common.inc.php'; require_once 'inc/rrdtool.class.php'; +require_once 'inc/collectd.inc.php'; function html_start() { global $CONFIG; @@ -46,6 +47,7 @@ function host_summary($hosts) { echo "\n"; foreach($hosts as $host) { + collectd_flush(sprintf('%s/load/load', $host)); $rrd_info = $rrd->rrd_info($CONFIG['datadir'].'/'.$host.'/load/load.rrd'); if (!$rrd_info) continue; diff --git a/plugin/cpu.php b/plugin/cpu.php index d750a3a..5f688f4 100644 --- a/plugin/cpu.php +++ b/plugin/cpu.php @@ -59,6 +59,8 @@ $obj->rrd_title = "CPU-$pinstance usage on $host"; $obj->rrd_vertical = 'Jiffies'; $obj->rrd_format = '%5.2lf'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/df.php b/plugin/df.php index f4dd5d6..6d7414b 100644 --- a/plugin/df.php +++ b/plugin/df.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericStacked.class.php'; +require_once 'inc/collectd.inc.php'; # LAYOUT # @@ -36,6 +37,8 @@ $obj->rrd_title = "Free space ($tinstance) on $host"; $obj->rrd_vertical = 'Bytes'; $obj->rrd_format = '%5.1lf%sB'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/disk.php b/plugin/disk.php index 0c18016..45b5fde 100644 --- a/plugin/disk.php +++ b/plugin/disk.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericIO.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # disk-XXXX/ @@ -57,6 +58,8 @@ switch($type) { break; } +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/entropy.php b/plugin/entropy.php index dcfbcc8..a4379ab 100644 --- a/plugin/entropy.php +++ b/plugin/entropy.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/Default.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # entropy/entropy.rrd @@ -31,6 +32,8 @@ $obj->rrd_title = "Available entropy on $host"; $obj->rrd_vertical = 'Bits'; $obj->rrd_format = '%4.0lf'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/interface.php b/plugin/interface.php index a3fca3d..45e3cf1 100644 --- a/plugin/interface.php +++ b/plugin/interface.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/GenericIO.class.php'; +require_once 'inc/collectd.inc.php'; # LAYOUT # interface/ @@ -48,6 +49,8 @@ switch($type) { break; } +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/irq.php b/plugin/irq.php index e442142..95bddcf 100644 --- a/plugin/irq.php +++ b/plugin/irq.php @@ -34,6 +34,8 @@ $obj->rrd_title = "Interrupts on $host"; $obj->rrd_vertical = 'IRQs/s'; $obj->rrd_format = '%6.1lf'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/load.php b/plugin/load.php index 0246edc..83fcf19 100644 --- a/plugin/load.php +++ b/plugin/load.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/Default.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # load/load.rrd @@ -35,6 +36,8 @@ $obj->rrd_title = "System load on $host"; $obj->rrd_vertical = 'System load'; $obj->rrd_format = '%.2lf'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/memory.php b/plugin/memory.php index 6cef5a7..7b0e47b 100644 --- a/plugin/memory.php +++ b/plugin/memory.php @@ -47,6 +47,8 @@ $obj->rrd_title = "Physical memory utilization on $host"; $obj->rrd_vertical = 'Bytes'; $obj->rrd_format = '%5.1lf%s'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/processes.php b/plugin/processes.php index c741a02..5fc0463 100644 --- a/plugin/processes.php +++ b/plugin/processes.php @@ -52,6 +52,8 @@ $obj->rrd_title = "Processes on $host"; $obj->rrd_vertical = 'Processes'; $obj->rrd_format = '%5.1lf%s'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/sensors.php b/plugin/sensors.php index b1b15cf..ddcf521 100644 --- a/plugin/sensors.php +++ b/plugin/sensors.php @@ -52,6 +52,8 @@ switch($type) { break; } +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/swap.php b/plugin/swap.php index 7160ec3..18c6242 100644 --- a/plugin/swap.php +++ b/plugin/swap.php @@ -44,6 +44,8 @@ $obj->rrd_title = "Swap utilization on $host"; $obj->rrd_vertical = 'Bytes'; $obj->rrd_format = '%5.1lf%s'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> diff --git a/plugin/users.php b/plugin/users.php index 00874a2..1555927 100644 --- a/plugin/users.php +++ b/plugin/users.php @@ -4,6 +4,7 @@ require_once 'conf/common.inc.php'; require_once 'type/Default.class.php'; +require_once 'inc/collectd.inc.php'; ## LAYOUT # users/users.rrd @@ -31,6 +32,8 @@ $obj->rrd_title = "Users on $host"; $obj->rrd_vertical = 'Users'; $obj->rrd_format = '%.1lf'; +collectd_flush(ident_from_args($obj->args)); + $obj->rrd_graph(); ?> -- cgit v1.1