From 1bb10c398d1a3198a8b86517bd780b8122e2e69d Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Sat, 3 May 2014 11:40:46 +0200 Subject: move collectd_flush function to base class --- type/Base.class.php | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'type/Base.class.php') diff --git a/type/Base.class.php b/type/Base.class.php index 2851280..fe31814 100644 --- a/type/Base.class.php +++ b/type/Base.class.php @@ -27,6 +27,9 @@ class Type_Base { var $tinstances; var $identifiers; + var $flush_socket; + var $flush_type; + function __construct($config, $_get) { $this->datadir = $config['datadir']; $this->rrdtool = $config['rrdtool']; @@ -39,6 +42,8 @@ class Type_Base { $this->graph_type = $config['graph_type']; $this->negative_io = $config['negative_io']; $this->graph_smooth = $config['graph_smooth']; + $this->flush_socket = $config['socket']; + $this->flush_type = $config['flush_type']; } function rainbow_colors() { @@ -167,6 +172,8 @@ class Type_Base { } function rrd_graph($debug = false) { + $this->collectd_flush(); + if (!$this->colors) $this->rainbow_colors(); @@ -295,4 +302,67 @@ class Type_Base { $this->ds_names[$index] = sprintf($format, $value); } } + + function socket_cmd($socket, $cmd) { + $r = fwrite($socket, $cmd, strlen($cmd)); + if ($r === false || $r != strlen($cmd)) { + error_log(sprintf('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,128); + if ($resp === false) { + error_log(sprintf('ERROR: Failed to read response from collectd for command: %s', + trim($cmd))); + return FALSE; + } + + $n = (int)$resp; + while ($n-- > 0) + fgets($socket,128); + + return TRUE; + } + + # tell collectd to FLUSH all data of the identifier(s) + function collectd_flush() { + $identifier = $this->identifiers; + + if (!$this->flush_socket) + return FALSE; + + if (!$identifier || (is_array($identifier) && count($identifier) == 0) || + !(is_string($identifier) || is_array($identifier))) + return FALSE; + + if (!is_array($identifier)) + $identifier = array($identifier); + + $u_errno = 0; + $u_errmsg = ''; + if (! $socket = @fsockopen($this->flush_socket, 0, $u_errno, $u_errmsg)) { + error_log(sprintf('ERROR: Failed to open unix-socket to %s (%d: %s)', + $this->flush_socket, $u_errno, $u_errmsg)); + return FALSE; + } + + if ($this->flush_type == 'collectd'){ + $cmd = 'FLUSH'; + foreach ($identifier as $val) + $cmd .= sprintf(' identifier="%s"', $val); + $cmd .= "\n"; + $this->socket_cmd($socket, $cmd); + } + elseif ($this->flush_type == 'rrdcached') { + foreach ($identifier as $val) { + $cmd = sprintf("FLUSH %s.rrd\n", $val); + $this->socket_cmd($socket, $cmd); + } + } + + fclose($socket); + + return TRUE; + } } -- cgit v1.1