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. --- inc/collectd.inc.php | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ inc/html.inc.php | 2 ++ 2 files changed, 83 insertions(+) (limited to 'inc') 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; -- cgit v1.1