[collectd] [PATCH] Collectd::Unixsock, cussh.pl: Added support for the "flush" command.

Sebastian Harl sh at tokkee.org
Wed Feb 27 22:00:17 CET 2008


Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
 bindings/perl/Collectd/Unixsock.pm |   44 +++++++++++++++++++++++++++++++++++
 contrib/cussh.pl                   |   45 +++++++++++++++++++++++++++++------
 2 files changed, 81 insertions(+), 8 deletions(-)

diff --git a/bindings/perl/Collectd/Unixsock.pm b/bindings/perl/Collectd/Unixsock.pm
index d3b77d0..b8ade84 100644
--- a/bindings/perl/Collectd/Unixsock.pm
+++ b/bindings/perl/Collectd/Unixsock.pm
@@ -400,6 +400,50 @@ sub putnotif
 	return;
 } # putnotif
 
+=item I<$obj>-E<gt>B<flush> (B<timeout> =E<gt> I<$timeout>);
+
+Flush cached data.
+
+Valid options are:
+
+=over 4
+
+=item B<timeout>
+
+If this option is specified, only data older than I<$timeout> seconds is
+flushed.
+
+=back
+
+=cut
+
+sub flush
+{
+	my $obj  = shift;
+	my %args = @_;
+
+	my $fh = $obj->{'sock'} or confess;
+
+	my $status = 0;
+	my $msg    = "FLUSH";
+
+	if ($args{'timeout'})
+	{
+		$msg .= " " . $args{'timeout'};
+	}
+	$msg .= "\n";
+
+	send ($fh, $msg, 0) or confess ("send: $!");
+	$msg = undef;
+	recv ($fh, $msg, 1024, 0) or confess ("recv: $!");
+
+	($status, $msg) = split (' ', $msg, 2);
+	return (1) if ($status == 0);
+
+	$obj->{'error'} = $msg;
+	return;
+}
+
 =item I<$obj>-E<gt>destroy ();
 
 Closes the socket before the object is destroyed. This function is also
diff --git a/contrib/cussh.pl b/contrib/cussh.pl
index 65c634e..bcc3a6a 100755
--- a/contrib/cussh.pl
+++ b/contrib/cussh.pl
@@ -56,12 +56,18 @@ use Collectd::Unixsock();
 	my $path = $ARGV[0] || "/var/run/collectd-unixsock";
 	my $sock = Collectd::Unixsock->new($path);
 
+	my $cmds = {
+		PUTVAL => \&putval,
+		GETVAL => \&getval,
+		FLUSH  => \&flush,
+	};
+
 	if (! $sock) {
 		print STDERR "Unable to connect to $path!\n";
 		exit 1;
 	}
 
-	print "cussh version 0.1, Copyright (C) 2007 Sebastian Harl\n"
+	print "cussh version 0.2, Copyright (C) 2007 Sebastian Harl\n"
 		. "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
 		. "and you are welcome to redistribute it under certain conditions.\n"
 		. "See the GNU General Public License 2 for more details.\n\n";
@@ -79,11 +85,8 @@ use Collectd::Unixsock();
 		$cmd = uc $cmd;
 
 		my $f = undef;
-		if ($cmd eq "PUTVAL") {
-			$f = \&putval;
-		}
-		elsif ($cmd eq "GETVAL") {
-			$f = \&getval;
+		if (defined $cmds->{$cmd}) {
+			$f = $cmds->{$cmd};
 		}
 		else {
 			print STDERR "ERROR: Unknown command $cmd!\n";
@@ -138,7 +141,10 @@ sub putval {
 
 	my $id = getid(\$line);
 
-	return if (! $id);
+	if (! $id) {
+		print STDERR $sock->{'error'} . $/;
+		return;
+	}
 
 	my ($time, @values) = split m/:/, $line;
 	return $sock->putval(%$id, $time, \@values);
@@ -150,7 +156,10 @@ sub getval {
 
 	my $id = getid(\$line);
 
-	return if (! $id);
+	if (! $id) {
+		print STDERR $sock->{'error'} . $/;
+		return;
+	}
 
 	my $vals = $sock->getval(%$id);
 
@@ -162,6 +171,26 @@ sub getval {
 	return 1;
 }
 
+sub flush {
+	my $sock = shift || return;
+	my $line = shift;
+
+	my $res;
+
+	if ($line) {
+		$res = $sock->flush(timeout => $line);
+	}
+	else {
+		$res = $sock->flush();
+	}
+
+	if (! $res) {
+		print STDERR $sock->{'error'} . $/;
+		return;
+	}
+	return 1;
+}
+
 =head1 SEE ALSO
 
 L<collectd(1)>, L<collectd-unisock(5)>
-- 
1.5.4.2.184.gb23b

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20080227/400ae1b0/attachment.pgp 


More information about the collectd mailing list