[collectd] [PATCH] perl plugin, Collectd.pm: Added support to flush Perl plugins.

Sebastian Harl sh at tokkee.org
Sun Apr 27 20:53:49 CEST 2008


Perl plugins may now register a callback of type Collectd::TYPE_FLUSH. A
single integer argument, representing the timeout in seconds, will be
passed to such callbacks.

Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
 bindings/perl/Collectd.pm |    4 +++-
 src/perl.c                |   31 ++++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/bindings/perl/Collectd.pm b/bindings/perl/Collectd.pm
index 4377570..399fca8 100644
--- a/bindings/perl/Collectd.pm
+++ b/bindings/perl/Collectd.pm
@@ -52,6 +52,7 @@ our %EXPORT_TAGS = (
 			TYPE_SHUTDOWN
 			TYPE_LOG
 			TYPE_NOTIF
+			TYPE_FLUSH
 			TYPE_DATASET
 	) ],
 	'ds_types' => [ qw(
@@ -101,7 +102,8 @@ my %types = (
 	TYPE_WRITE,    "write",
 	TYPE_SHUTDOWN, "shutdown",
 	TYPE_LOG,      "log",
-	TYPE_NOTIF,    "notify"
+	TYPE_NOTIF,    "notify",
+	TYPE_FLUSH,    "flush"
 );
 
 foreach my $type (keys %types) {
diff --git a/src/perl.c b/src/perl.c
index 96e8562..beea288 100644
--- a/src/perl.c
+++ b/src/perl.c
@@ -61,8 +61,9 @@
 #define PLUGIN_SHUTDOWN 3
 #define PLUGIN_LOG      4
 #define PLUGIN_NOTIF    5
+#define PLUGIN_FLUSH    6
 
-#define PLUGIN_TYPES    6
+#define PLUGIN_TYPES    7
 
 #define PLUGIN_DATASET  255
 
@@ -148,6 +149,7 @@ struct {
 	{ "Collectd::TYPE_SHUTDOWN",   PLUGIN_SHUTDOWN },
 	{ "Collectd::TYPE_LOG",        PLUGIN_LOG },
 	{ "Collectd::TYPE_NOTIF",      PLUGIN_NOTIF },
+	{ "Collectd::TYPE_FLUSH",      PLUGIN_FLUSH },
 	{ "Collectd::TYPE_DATASET",    PLUGIN_DATASET },
 	{ "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
 	{ "Collectd::DS_TYPE_GAUGE",   DS_TYPE_GAUGE },
@@ -755,6 +757,12 @@ static int pplugin_call_all (pTHX_ int type, ...)
 
 		XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
 	}
+	else if (PLUGIN_FLUSH == type) {
+		/*
+		 * $_[0] = $timeout;
+		 */
+		XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
+	}
 
 	PUTBACK;
 
@@ -1195,6 +1203,25 @@ static int perl_notify (const notification_t *notif)
 	return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
 } /* static int perl_notify (const notification_t *) */
 
+static int perl_flush (const int timeout)
+{
+	dTHX;
+
+	if (NULL == perl_threads)
+		return 0;
+
+	if (NULL == aTHX) {
+		c_ithread_t *t = NULL;
+
+		pthread_mutex_lock (&perl_threads->mutex);
+		t = c_ithread_create (perl_threads->head->interp);
+		pthread_mutex_unlock (&perl_threads->mutex);
+
+		aTHX = t->interp;
+	}
+	return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout);
+} /* static int perl_flush (const int) */
+
 static int perl_shutdown (void)
 {
 	c_ithread_t *t = NULL;
@@ -1226,6 +1253,7 @@ static int perl_shutdown (void)
 	plugin_unregister_init ("perl");
 	plugin_unregister_read ("perl");
 	plugin_unregister_write ("perl");
+	plugin_unregister_flush ("perl");
 
 	ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
 
@@ -1406,6 +1434,7 @@ static int init_pi (int argc, char **argv)
 	plugin_register_read ("perl", perl_read);
 
 	plugin_register_write ("perl", perl_write);
+	plugin_register_flush ("perl", perl_flush);
 	plugin_register_shutdown ("perl", perl_shutdown);
 	return 0;
 } /* static int init_pi (const char **, const int) */
-- 
1.5.5.1.79.g57cf

-------------- 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/20080427/918ba454/attachment.pgp 


More information about the collectd mailing list