perl plugin: Pass on identifiers to Perl flush callbacks.
[collectd.git] / src / perl.c
index 3a56da6..8106360 100644 (file)
 /* do not automatically get the thread specific perl interpreter */
 #define PERL_NO_GET_CONTEXT
 
+#define DONT_POISON_SPRINTF_YET 1
 #include "collectd.h"
+#undef DONT_POISON_SPRINTF_YET
 
 #include "configfile.h"
 
 #include <EXTERN.h>
 #include <perl.h>
 
+#if __GNUC__
+# pragma GCC poison sprintf
+#endif
+
 #include <XSUB.h>
 
 /* Some versions of Perl define their own version of DEBUG... :-/ */
@@ -78,8 +84,7 @@ void boot_DynaLoader (PerlInterpreter *, CV *);
 static XS (Collectd_plugin_register_ds);
 static XS (Collectd_plugin_unregister_ds);
 static XS (Collectd_plugin_dispatch_values);
-static XS (Collectd_plugin_flush_one);
-static XS (Collectd_plugin_flush_all);
+static XS (Collectd__plugin_flush);
 static XS (Collectd_plugin_dispatch_notification);
 static XS (Collectd_plugin_log);
 static XS (Collectd_call_by_name);
@@ -133,8 +138,7 @@ static struct {
        { "Collectd::plugin_register_data_set",   Collectd_plugin_register_ds },
        { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
        { "Collectd::plugin_dispatch_values",     Collectd_plugin_dispatch_values },
-       { "Collectd::plugin_flush_one",           Collectd_plugin_flush_one },
-       { "Collectd::plugin_flush_all",           Collectd_plugin_flush_all },
+       { "Collectd::_plugin_flush",              Collectd__plugin_flush },
        { "Collectd::plugin_dispatch_notification",
                Collectd_plugin_dispatch_notification },
        { "Collectd::plugin_log",                 Collectd_plugin_log },
@@ -567,7 +571,7 @@ static int pplugin_dispatch_values (pTHX_ HV *values)
                sstrncpy (list.host, SvPV_nolen (*tmp), sizeof (list.host));
        }
        else {
-               strcpy (list.host, hostname_g);
+               sstrncpy (list.host, hostname_g, sizeof (list.host));
        }
 
        if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0)))
@@ -763,8 +767,10 @@ static int pplugin_call_all (pTHX_ int type, ...)
        else if (PLUGIN_FLUSH == type) {
                /*
                 * $_[0] = $timeout;
+                * $_[1] = $identifier;
                 */
                XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
+               XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
        }
 
        PUTBACK;
@@ -912,52 +918,47 @@ static XS (Collectd_plugin_dispatch_values)
 } /* static XS (Collectd_plugin_dispatch_values) */
 
 /*
- * Collectd::plugin_flush_one (timeout, name).
+ * Collectd::_plugin_flush (plugin, timeout, identifier).
+ *
+ * plugin:
+ *   name of the plugin to flush
  *
  * timeout:
  *   timeout to use when flushing the data
  *
- * name:
- *   name of the plugin to flush
+ * identifier:
+ *   data-set identifier to flush
  */
-static XS (Collectd_plugin_flush_one)
+static XS (Collectd__plugin_flush)
 {
+       char *plugin  = NULL;
+       int   timeout = -1;
+       char *id      = NULL;
+
        dXSARGS;
 
-       if (2 != items) {
-               log_err ("Usage: Collectd::plugin_flush_one(timeout, name)");
+       if (3 != items) {
+               log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
                XSRETURN_EMPTY;
        }
 
-       log_debug ("Collectd::plugin_flush_one: timeout = %i, name = \"%s\"",
-                       (int)SvIV (ST (0)), SvPV_nolen (ST (1)));
+       if (SvOK (ST (0)))
+               plugin = SvPV_nolen (ST (0));
 
-       if (0 == plugin_flush_one ((int)SvIV (ST (0)), SvPV_nolen (ST (1))))
-               XSRETURN_YES;
-       else
-               XSRETURN_EMPTY;
-} /* static XS (Collectd_plugin_flush_one) */
+       if (SvOK (ST (1)))
+               timeout = (int)SvIV (ST (1));
 
-/*
- * Collectd::plugin_flush_all (timeout).
- *
- * timeout:
- *   timeout to use when flushing the data
- */
-static XS (Collectd_plugin_flush_all)
-{
-       dXSARGS;
+       if (SvOK (ST (2)))
+               id = SvPV_nolen (ST (2));
 
-       if (1 != items) {
-               log_err ("Usage: Collectd::plugin_flush_all(timeout)");
-               XSRETURN_EMPTY;
-       }
-
-       log_debug ("Collectd::plugin_flush_all: timeout = %i", (int)SvIV (ST (0)));
+       log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
+                       "id = \"%s\"", plugin, timeout, id);
 
-       plugin_flush_all ((int)SvIV (ST (0)));
-       XSRETURN_YES;
-} /* static XS (Collectd_plugin_flush_all) */
+       if (0 == plugin_flush (plugin, timeout, id))
+               XSRETURN_YES;
+       else
+               XSRETURN_EMPTY;
+} /* static XS (Collectd__plugin_flush) */
 
 /*
  * Collectd::plugin_dispatch_notification (notif).
@@ -1264,7 +1265,6 @@ static int perl_notify (const notification_t *notif)
        return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
 } /* static int perl_notify (const notification_t *) */
 
-/* TODO: Implement flushing of single identifiers. */
 static int perl_flush (int timeout, const char *identifier)
 {
        dTHX;
@@ -1281,7 +1281,7 @@ static int perl_flush (int timeout, const char *identifier)
 
                aTHX = t->interp;
        }
-       return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout);
+       return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
 } /* static int perl_flush (const int) */
 
 static int perl_shutdown (void)