perl plugin / Collectd.pm: Export plugin_write() to Perl.
authorSebastian Harl <sh@tokkee.org>
Mon, 9 Feb 2009 20:07:31 +0000 (21:07 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 9 Feb 2009 21:23:05 +0000 (22:23 +0100)
plugin_write() accepts, just like the C counterpart, three arguments,
'plugins', 'datasets' and 'valuelists'. In contrast to the C implementation,
all three arguments may either be a single scalar or a reference to an array -
just like the 'plugins' and 'identifiers' arguments of the Perl implementation
of plugin_flush().

bindings/perl/Collectd.pm
src/collectd-perl.pod
src/perl.c

index 738206b..6e53b0e 100644 (file)
@@ -42,6 +42,7 @@ our %EXPORT_TAGS = (
                        plugin_register
                        plugin_unregister
                        plugin_dispatch_values
                        plugin_register
                        plugin_unregister
                        plugin_dispatch_values
+                       plugin_write
                        plugin_flush
                        plugin_flush_one
                        plugin_flush_all
                        plugin_flush
                        plugin_flush_one
                        plugin_flush_all
@@ -318,6 +319,66 @@ sub plugin_unregister {
        }
 }
 
        }
 }
 
+sub plugin_write {
+       my %args = @_;
+
+       my @plugins    = ();
+       my @datasets   = ();
+       my @valuelists = ();
+
+       if (! defined $args{'valuelists'}) {
+               ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
+               return;
+       }
+
+       DEBUG ("Collectd::plugin_write:"
+               . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
+               . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
+               . " valueslists = $args{'valuelists'}");
+
+       if (defined ($args{'plugins'})) {
+               if ("ARRAY" eq ref ($args{'plugins'})) {
+                       @plugins = @{$args{'plugins'}};
+               }
+               else {
+                       @plugins = ($args{'plugins'});
+               }
+       }
+       else {
+               @plugins = (undef);
+       }
+
+       if ("ARRAY" eq ref ($args{'valuelists'})) {
+               @valuelists = @{$args{'valuelists'}};
+       }
+       else {
+               @valuelists = ($args{'valuelists'});
+       }
+
+       if (defined ($args{'datasets'})) {
+               if ("ARRAY" eq ref ($args{'datasets'})) {
+                       @datasets = @{$args{'datasets'}};
+               }
+               else {
+                       @datasets = ($args{'datasets'});
+               }
+       }
+       else {
+               @datasets = (undef) x scalar (@valuelists);
+       }
+
+       if ($#datasets != $#valuelists) {
+               ERROR ("Collectd::plugin_write: Invalid number of datasets.");
+               return;
+       }
+
+       foreach my $plugin (@plugins) {
+               for (my $i = 0; $i < scalar (@valuelists); ++$i) {
+                       _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
+               }
+       }
+}
+
 sub plugin_flush {
        my %args = @_;
 
 sub plugin_flush {
        my %args = @_;
 
index c3fcb10..18dbafd 100644 (file)
@@ -341,12 +341,26 @@ as the first argument to B<plugin_register>. This syntax is still supported
 for backwards compatibility but has been deprecated and will be removed in
 some future version of collectd.
 
 for backwards compatibility but has been deprecated and will be removed in
 some future version of collectd.
 
+=item B<plugin_write> ([B<plugins> => I<...>][, B<datasets> => I<...>],
+B<valuelists> => I<...>)
+
+Calls the write function of the given I<plugins> with the provided I<data
+sets> and I<value lists>. In contrast to B<plugin_dispatch_values>, it does
+not update collectd's internal cache and bypasses the filter mechanism (see
+L<collectd.conf(5)> for details). If the B<plugins> argument has been omitted,
+the values will be dispatched to all registered write plugins. If the
+B<datasets> argument has been omitted, the required data sets are looked up
+according to the C<type> member in the appropriate value list. The value of
+all three arguments may either be a single scalar or a reference to an array.
+If the B<datasets> argument has been specified, the number of data sets has to
+equal the number of specified value lists.
+
 =item B<plugin_flush> ([B<timeout> => I<timeout>][, B<plugins> => I<...>][,
 B<identifiers> => I<...>])
 
 Flush one or more plugins. I<timeout> and the specified I<identifiers> are
 passed on to the registered flush-callbacks. If omitted, the timeout defaults
 =item B<plugin_flush> ([B<timeout> => I<timeout>][, B<plugins> => I<...>][,
 B<identifiers> => I<...>])
 
 Flush one or more plugins. I<timeout> and the specified I<identifiers> are
 passed on to the registered flush-callbacks. If omitted, the timeout defaults
-to C<-1>. The identifier defaults to the undefined value. If the I<plugins>
+to C<-1>. The identifier defaults to the undefined value. If the B<plugins>
 argument has been specified, only named plugins will be flushed. The value of
 the B<plugins> and B<identifiers> arguments may either be a string or a
 reference to an array of strings.
 argument has been specified, only named plugins will be flushed. The value of
 the B<plugins> and B<identifiers> arguments may either be a string or a
 reference to an array of strings.
index 2d4cc86..9413d2c 100644 (file)
@@ -85,6 +85,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_register_ds);
 static XS (Collectd_plugin_unregister_ds);
 static XS (Collectd_plugin_dispatch_values);
+static XS (Collectd__plugin_write);
 static XS (Collectd__plugin_flush);
 static XS (Collectd_plugin_dispatch_notification);
 static XS (Collectd_plugin_log);
 static XS (Collectd__plugin_flush);
 static XS (Collectd_plugin_dispatch_notification);
 static XS (Collectd_plugin_log);
@@ -139,6 +140,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_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_write",              Collectd__plugin_write },
        { "Collectd::_plugin_flush",              Collectd__plugin_flush },
        { "Collectd::plugin_dispatch_notification",
                Collectd_plugin_dispatch_notification },
        { "Collectd::_plugin_flush",              Collectd__plugin_flush },
        { "Collectd::plugin_dispatch_notification",
                Collectd_plugin_dispatch_notification },
@@ -737,6 +739,37 @@ static int pplugin_dispatch_values (pTHX_ HV *values)
 } /* static int pplugin_dispatch_values (char *, HV *) */
 
 /*
 } /* static int pplugin_dispatch_values (char *, HV *) */
 
 /*
+ * Submit the values to a single write function.
+ */
+static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
+{
+       data_set_t   ds;
+       value_list_t vl = VALUE_LIST_INIT;
+
+       int ret;
+
+       if (NULL == values)
+               return -1;
+
+       if (0 != hv2value_list (aTHX_ values, &vl))
+               return -1;
+
+       if ((NULL != data_set)
+                       && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
+               return -1;
+
+       ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
+       if (0 != ret)
+               log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
+                               NULL == plugin ? "<any>" : plugin, ret);
+
+       if (NULL != data_set)
+               sfree (ds.ds);
+       sfree (vl.values);
+       return ret;
+} /* static int pplugin_write (const char *plugin, HV *, HV *) */
+
+/*
  * Dispatch a notification.
  */
 static int pplugin_dispatch_notification (pTHX_ HV *notif)
  * Dispatch a notification.
  */
 static int pplugin_dispatch_notification (pTHX_ HV *notif)
@@ -1021,6 +1054,65 @@ static XS (Collectd_plugin_dispatch_values)
                XSRETURN_EMPTY;
 } /* static XS (Collectd_plugin_dispatch_values) */
 
                XSRETURN_EMPTY;
 } /* static XS (Collectd_plugin_dispatch_values) */
 
+/* Collectd::plugin_write (plugin, ds, vl).
+ *
+ * plugin:
+ *   name of the plugin to call, may be 'undef'
+ *
+ * ds:
+ *   data-set that describes the submitted values, may be 'undef'
+ *
+ * vl:
+ *   value-list to be written
+ */
+static XS (Collectd__plugin_write)
+{
+       char *plugin;
+       SV   *ds, *vl;
+       AV   *ds_array;
+
+       int ret;
+
+       dXSARGS;
+
+       if (3 != items) {
+               log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
+               XSRETURN_EMPTY;
+       }
+
+       log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
+                       SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
+                       SvPV_nolen (ST (2)));
+
+       if (! SvOK (ST (0)))
+               plugin = NULL;
+       else
+               plugin = SvPV_nolen (ST (0));
+
+       ds = ST (1);
+       if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
+               ds_array = (AV *)SvRV (ds);
+       else if (! SvOK (ds))
+               ds_array = NULL;
+       else {
+               log_err ("Collectd::plugin_write: Invalid data-set.");
+               XSRETURN_EMPTY;
+       }
+
+       vl = ST (2);
+       if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
+               log_err ("Collectd::plugin_write: Invalid value-list.");
+               XSRETURN_EMPTY;
+       }
+
+       ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
+
+       if (0 == ret)
+               XSRETURN_YES;
+       else
+               XSRETURN_EMPTY;
+} /* static XS (Collectd__plugin_write) */
+
 /*
  * Collectd::_plugin_flush (plugin, timeout, identifier).
  *
 /*
  * Collectd::_plugin_flush (plugin, timeout, identifier).
  *