diff options
author | Pim van den Berg | 2014-05-03 11:40:46 +0200 |
---|---|---|
committer | Pim van den Berg | 2014-05-03 19:19:05 +0200 |
commit | 1bb10c398d1a3198a8b86517bd780b8122e2e69d (patch) | |
tree | 669ea50b9b0efe268f4bb9153459f95ccfb167b5 /type | |
parent | type: move generic functions to base class (diff) | |
download | apt-panopticon_cgp-1bb10c398d1a3198a8b86517bd780b8122e2e69d.zip apt-panopticon_cgp-1bb10c398d1a3198a8b86517bd780b8122e2e69d.tar.gz apt-panopticon_cgp-1bb10c398d1a3198a8b86517bd780b8122e2e69d.tar.bz2 apt-panopticon_cgp-1bb10c398d1a3198a8b86517bd780b8122e2e69d.tar.xz |
move collectd_flush function to base class
Diffstat (limited to 'type')
-rw-r--r-- | type/Base.class.php | 70 |
1 files changed, 70 insertions, 0 deletions
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 { | |||
27 | var $tinstances; | 27 | var $tinstances; |
28 | var $identifiers; | 28 | var $identifiers; |
29 | 29 | ||
30 | var $flush_socket; | ||
31 | var $flush_type; | ||
32 | |||
30 | function __construct($config, $_get) { | 33 | function __construct($config, $_get) { |
31 | $this->datadir = $config['datadir']; | 34 | $this->datadir = $config['datadir']; |
32 | $this->rrdtool = $config['rrdtool']; | 35 | $this->rrdtool = $config['rrdtool']; |
@@ -39,6 +42,8 @@ class Type_Base { | |||
39 | $this->graph_type = $config['graph_type']; | 42 | $this->graph_type = $config['graph_type']; |
40 | $this->negative_io = $config['negative_io']; | 43 | $this->negative_io = $config['negative_io']; |
41 | $this->graph_smooth = $config['graph_smooth']; | 44 | $this->graph_smooth = $config['graph_smooth']; |
45 | $this->flush_socket = $config['socket']; | ||
46 | $this->flush_type = $config['flush_type']; | ||
42 | } | 47 | } |
43 | 48 | ||
44 | function rainbow_colors() { | 49 | function rainbow_colors() { |
@@ -167,6 +172,8 @@ class Type_Base { | |||
167 | } | 172 | } |
168 | 173 | ||
169 | function rrd_graph($debug = false) { | 174 | function rrd_graph($debug = false) { |
175 | $this->collectd_flush(); | ||
176 | |||
170 | if (!$this->colors) | 177 | if (!$this->colors) |
171 | $this->rainbow_colors(); | 178 | $this->rainbow_colors(); |
172 | 179 | ||
@@ -295,4 +302,67 @@ class Type_Base { | |||
295 | $this->ds_names[$index] = sprintf($format, $value); | 302 | $this->ds_names[$index] = sprintf($format, $value); |
296 | } | 303 | } |
297 | } | 304 | } |
305 | |||
306 | function socket_cmd($socket, $cmd) { | ||
307 | $r = fwrite($socket, $cmd, strlen($cmd)); | ||
308 | if ($r === false || $r != strlen($cmd)) { | ||
309 | error_log(sprintf('ERROR: Failed to write full command to unix-socket: %d out of %d written', | ||
310 | $r === false ? -1 : $r, strlen($cmd))); | ||
311 | return FALSE; | ||
312 | } | ||
313 | |||
314 | $resp = fgets($socket,128); | ||
315 | if ($resp === false) { | ||
316 | error_log(sprintf('ERROR: Failed to read response from collectd for command: %s', | ||
317 | trim($cmd))); | ||
318 | return FALSE; | ||
319 | } | ||
320 | |||
321 | $n = (int)$resp; | ||
322 | while ($n-- > 0) | ||
323 | fgets($socket,128); | ||
324 | |||
325 | return TRUE; | ||
326 | } | ||
327 | |||
328 | # tell collectd to FLUSH all data of the identifier(s) | ||
329 | function collectd_flush() { | ||
330 | $identifier = $this->identifiers; | ||
331 | |||
332 | if (!$this->flush_socket) | ||
333 | return FALSE; | ||
334 | |||
335 | if (!$identifier || (is_array($identifier) && count($identifier) == 0) || | ||
336 | !(is_string($identifier) || is_array($identifier))) | ||
337 | return FALSE; | ||
338 | |||
339 | if (!is_array($identifier)) | ||
340 | $identifier = array($identifier); | ||
341 | |||
342 | $u_errno = 0; | ||
343 | $u_errmsg = ''; | ||
344 | if (! $socket = @fsockopen($this->flush_socket, 0, $u_errno, $u_errmsg)) { | ||
345 | error_log(sprintf('ERROR: Failed to open unix-socket to %s (%d: %s)', | ||
346 | $this->flush_socket, $u_errno, $u_errmsg)); | ||
347 | return FALSE; | ||
348 | } | ||
349 | |||
350 | if ($this->flush_type == 'collectd'){ | ||
351 | $cmd = 'FLUSH'; | ||
352 | foreach ($identifier as $val) | ||
353 | $cmd .= sprintf(' identifier="%s"', $val); | ||
354 | $cmd .= "\n"; | ||
355 | $this->socket_cmd($socket, $cmd); | ||
356 | } | ||
357 | elseif ($this->flush_type == 'rrdcached') { | ||
358 | foreach ($identifier as $val) { | ||
359 | $cmd = sprintf("FLUSH %s.rrd\n", $val); | ||
360 | $this->socket_cmd($socket, $cmd); | ||
361 | } | ||
362 | } | ||
363 | |||
364 | fclose($socket); | ||
365 | |||
366 | return TRUE; | ||
367 | } | ||
298 | } | 368 | } |