aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/collectd.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/collectd.inc.php')
-rw-r--r--inc/collectd.inc.php75
1 files changed, 44 insertions, 31 deletions
diff --git a/inc/collectd.inc.php b/inc/collectd.inc.php
index 327b5e6..e364180 100644
--- a/inc/collectd.inc.php
+++ b/inc/collectd.inc.php
@@ -235,6 +235,28 @@ function build_url($base, $items, $s=NULL) {
235 return $base; 235 return $base;
236} 236}
237 237
238function socket_cmd($socket, $cmd) {
239 $r = fwrite($socket, $cmd, strlen($cmd));
240 if ($r === false || $r != strlen($cmd)) {
241 error_log(sprintf('ERROR: Failed to write full command to unix-socket: %d out of %d written',
242 $r === false ? -1 : $r, strlen($cmd)));
243 return FALSE;
244 }
245
246 $resp = fgets($socket,128);
247 if ($resp === false) {
248 error_log(sprintf('ERROR: Failed to read response from collectd for command: %s',
249 trim($cmd)));
250 return FALSE;
251 }
252
253 $n = (int)$resp;
254 while ($n-- > 0)
255 fgets($socket,128);
256
257 return TRUE;
258}
259
238# tell collectd to FLUSH all data of the identifier(s) 260# tell collectd to FLUSH all data of the identifier(s)
239function collectd_flush($identifier) { 261function collectd_flush($identifier) {
240 global $CONFIG; 262 global $CONFIG;
@@ -246,43 +268,34 @@ function collectd_flush($identifier) {
246 !(is_string($identifier) || is_array($identifier))) 268 !(is_string($identifier) || is_array($identifier)))
247 return FALSE; 269 return FALSE;
248 270
271 if (!is_array($identifier))
272 $identifier = array($identifier);
273
249 $u_errno = 0; 274 $u_errno = 0;
250 $u_errmsg = ''; 275 $u_errmsg = '';
251 if ($socket = @fsockopen($CONFIG['socket'], 0, $u_errno, $u_errmsg)) { 276 if (! $socket = @fsockopen($CONFIG['socket'], 0, $u_errno, $u_errmsg)) {
252 $cmd = 'FLUSH plugin=rrdtool'; 277 error_log(sprintf('ERROR: Failed to open unix-socket to %s (%d: %s)',
253 if (is_array($identifier)) { 278 $CONFIG['socket'], $u_errno, $u_errmsg));
254 foreach ($identifier as $val) 279 return FALSE;
255 $cmd .= sprintf(' identifier="%s"', $val); 280 }
256 } else
257 $cmd .= sprintf(' identifier="%s"', $identifier);
258 $cmd .= "\n";
259
260 $r = fwrite($socket, $cmd, strlen($cmd));
261 if ($r === false || $r != strlen($cmd)) {
262 error_log(sprintf('ERROR: Failed to write full command to unix-socket: %d out of %d written',
263 $r === false ? -1 : $r, strlen($cmd)));
264 return FALSE;
265 }
266 281
267 $resp = fgets($socket); 282 if ($CONFIG['flush_type'] == 'collectd'){
268 if ($resp === false) { 283 $cmd = 'FLUSH';
269 error_log(sprintf('ERROR: Failed to read response from collectd for command: %s', 284 foreach ($identifier as $val)
270 trim($cmd))); 285 $cmd .= sprintf(' identifier="%s"', $val);
271 return FALSE; 286 $cmd .= "\n";
287 socket_cmd($socket, $cmd);
288 }
289 elseif ($CONFIG['flush_type'] == 'rrdcached') {
290 foreach ($identifier as $val) {
291 $cmd = sprintf("FLUSH %s.rrd\n", $val);
292 socket_cmd($socket, $cmd);
272 } 293 }
294 }
273 295
274 $n = (int)$resp; 296 fclose($socket);
275 while ($n-- > 0)
276 fgets($socket);
277
278 fclose($socket);
279 297
280 return TRUE; 298 return TRUE;
281 } else {
282 error_log(sprintf('ERROR: Failed to open unix-socket to collectd: %d: %s',
283 $u_errno, $u_errmsg));
284 return FALSE;
285 }
286} 299}
287 300
288?> 301?>