2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
30 #define DONT_POISON_SPRINTF_YET 1
32 #undef DONT_POISON_SPRINTF_YET
34 #include "configfile.h"
39 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
40 # pragma GCC poison sprintf
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
50 /* ... while we want the definition found in plugin.h. */
54 #include "filter_chain.h"
58 #if !defined(USE_ITHREADS)
59 # error "Perl does not support ithreads!"
60 #endif /* !defined(USE_ITHREADS) */
62 /* clear the Perl sub's stack frame
63 * (this should only be used inside an XSUB) */
64 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
68 #define PLUGIN_WRITE 2
69 #define PLUGIN_SHUTDOWN 3
71 #define PLUGIN_NOTIF 5
72 #define PLUGIN_FLUSH 6
74 #define PLUGIN_TYPES 7
76 #define PLUGIN_CONFIG 254
77 #define PLUGIN_DATASET 255
84 #define FC_CB_CREATE 0
85 #define FC_CB_DESTROY 1
90 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
91 #define log_info(...) INFO ("perl: " __VA_ARGS__)
92 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
93 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
95 /* this is defined in DynaLoader.a */
96 void boot_DynaLoader (PerlInterpreter *, CV *);
98 static XS (Collectd_plugin_register_ds);
99 static XS (Collectd_plugin_unregister_ds);
100 static XS (Collectd_plugin_dispatch_values);
101 static XS (Collectd__plugin_write);
102 static XS (Collectd__plugin_flush);
103 static XS (Collectd_plugin_dispatch_notification);
104 static XS (Collectd_plugin_log);
105 static XS (Collectd__fc_register);
106 static XS (Collectd_call_by_name);
112 typedef struct c_ithread_s {
113 /* the thread's Perl interpreter */
114 PerlInterpreter *interp;
116 /* double linked list of threads */
117 struct c_ithread_s *prev;
118 struct c_ithread_s *next;
126 /* some usage stats */
127 int number_of_threads;
128 #endif /* COLLECT_DEBUG */
130 pthread_mutex_t mutex;
133 /* name / user_data for Perl matches / targets */
139 #define PFC_USER_DATA_FREE(data) \
141 sfree ((data)->name); \
142 if (NULL != (data)->user_data) \
143 sv_free ((data)->user_data); \
150 extern char **environ;
156 /* if perl_threads != NULL perl_threads->head must
157 * point to the "base" thread */
158 static c_ithread_list_t *perl_threads = NULL;
160 /* the key used to store each pthread's ithread */
161 static pthread_key_t perl_thr_key;
163 static int perl_argc = 0;
164 static char **perl_argv = NULL;
166 static char base_name[DATA_MAX_NAME_LEN] = "";
173 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
174 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
175 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
176 { "Collectd::_plugin_write", Collectd__plugin_write },
177 { "Collectd::_plugin_flush", Collectd__plugin_flush },
178 { "Collectd::plugin_dispatch_notification",
179 Collectd_plugin_dispatch_notification },
180 { "Collectd::plugin_log", Collectd_plugin_log },
181 { "Collectd::_fc_register", Collectd__fc_register },
182 { "Collectd::call_by_name", Collectd_call_by_name },
191 { "Collectd::TYPE_INIT", PLUGIN_INIT },
192 { "Collectd::TYPE_READ", PLUGIN_READ },
193 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
194 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
195 { "Collectd::TYPE_LOG", PLUGIN_LOG },
196 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
197 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
198 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
199 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
200 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
201 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
202 { "Collectd::LOG_ERR", LOG_ERR },
203 { "Collectd::LOG_WARNING", LOG_WARNING },
204 { "Collectd::LOG_NOTICE", LOG_NOTICE },
205 { "Collectd::LOG_INFO", LOG_INFO },
206 { "Collectd::LOG_DEBUG", LOG_DEBUG },
207 { "Collectd::FC_MATCH", FC_MATCH },
208 { "Collectd::FC_TARGET", FC_TARGET },
209 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
210 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
211 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
212 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
213 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
214 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
215 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
216 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
217 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
218 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
219 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
228 { "Collectd::hostname_g", hostname_g },
237 { "Collectd::interval_g", &interval_g },
242 * Helper functions for data type conversion.
257 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
261 if ((NULL == hash) || (NULL == ds))
264 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
265 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
268 log_err ("hv2data_source: No DS name given.");
272 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
273 ds->type = SvIV (*tmp);
275 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
276 log_err ("hv2data_source: Invalid DS type.");
281 ds->type = DS_TYPE_COUNTER;
284 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
285 ds->min = SvNV (*tmp);
289 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
290 ds->max = SvNV (*tmp);
294 } /* static int hv2data_source (HV *, data_source_t *) */
296 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
298 const data_set_t *ds;
302 if ((NULL == name) || (NULL == array) || (NULL == value))
305 if (av_len (array) < len - 1)
306 len = av_len (array) + 1;
311 ds = plugin_get_ds (name);
313 log_err ("av2value: Unknown dataset \"%s\"", name);
317 if (ds->ds_num < len) {
318 log_warn ("av2value: Value length exceeds data set length.");
322 for (i = 0; i < len; ++i) {
323 SV **tmp = av_fetch (array, i, 0);
326 if (DS_TYPE_COUNTER == ds->ds[i].type)
327 value[i].counter = SvIV (*tmp);
329 value[i].gauge = SvNV (*tmp);
336 } /* static int av2value (char *, AV *, value_t *, int) */
341 * values => [ @values ],
345 * plugin_instance => $pinstance,
346 * type_instance => $tinstance,
349 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
353 if ((NULL == hash) || (NULL == vl))
356 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
357 log_err ("hv2value_list: No type given.");
361 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
363 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
364 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
365 log_err ("hv2value_list: No valid values given.");
370 AV *array = (AV *)SvRV (*tmp);
371 int len = av_len (array) + 1;
376 vl->values = (value_t *)smalloc (len * sizeof (value_t));
377 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
380 if (-1 == vl->values_len) {
386 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
387 vl->time = (time_t)SvIV (*tmp);
389 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
390 vl->interval = SvIV (*tmp);
392 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
393 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
395 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
397 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
398 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
400 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
401 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
402 sizeof (vl->plugin_instance));
404 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
405 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
406 sizeof (vl->type_instance));
408 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
410 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
414 if ((NULL == array) || (NULL == name) || (NULL == ds))
417 len = av_len (array);
420 log_err ("av2data_set: Invalid data set.");
424 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
425 ds->ds_num = len + 1;
427 for (i = 0; i <= len; ++i) {
428 SV **elem = av_fetch (array, i, 0);
431 log_err ("av2data_set: Failed to fetch data source %i.", i);
435 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
436 log_err ("av2data_set: Invalid data source.");
440 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
443 log_debug ("av2data_set: "
444 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
445 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
448 sstrncpy (ds->type, name, sizeof (ds->type));
450 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
455 * severity => $severity,
461 * plugin_instance => $instance,
462 * type_instance => $type_instance,
463 * meta => [ { name => <name>, value => <value> }, ... ]
466 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
468 notification_meta_t **m = meta;
470 int len = av_len (array);
473 for (i = 0; i <= len; ++i) {
474 SV **tmp = av_fetch (array, i, 0);
480 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
481 log_warn ("av2notification_meta: Skipping invalid "
482 "meta information.");
486 hash = (HV *)SvRV (*tmp);
488 *m = (notification_meta_t *)smalloc (sizeof (**m));
490 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
491 log_warn ("av2notification_meta: Skipping invalid "
492 "meta information.");
496 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
498 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
499 log_warn ("av2notification_meta: Skipping invalid "
500 "meta information.");
507 (*m)->nm_value.nm_double = SvNVX (*tmp);
508 (*m)->type = NM_TYPE_DOUBLE;
510 else if (SvUOK (*tmp)) {
511 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
512 (*m)->type = NM_TYPE_UNSIGNED_INT;
514 else if (SvIOK (*tmp)) {
515 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
516 (*m)->type = NM_TYPE_SIGNED_INT;
519 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
520 (*m)->type = NM_TYPE_STRING;
527 } /* static int av2notification_meta (AV *, notification_meta_t *) */
529 static int hv2notification (pTHX_ HV *hash, notification_t *n)
533 if ((NULL == hash) || (NULL == n))
536 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
537 n->severity = SvIV (*tmp);
539 n->severity = NOTIF_FAILURE;
541 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
542 n->time = (time_t)SvIV (*tmp);
544 n->time = time (NULL);
546 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
547 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
549 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
550 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
552 sstrncpy (n->host, hostname_g, sizeof (n->host));
554 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
555 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
557 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
558 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
559 sizeof (n->plugin_instance));
561 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
562 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
564 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
565 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
566 sizeof (n->type_instance));
569 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
570 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
571 log_warn ("hv2notification: Ignoring invalid meta information.");
575 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
576 plugin_notification_meta_free (n->meta);
583 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
585 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
589 if ((NULL == ds) || (NULL == array))
592 av_extend (array, ds->ds_num);
594 for (i = 0; i < ds->ds_num; ++i) {
595 HV *source = newHV ();
597 if (NULL == hv_store (source, "name", 4,
598 newSVpv (ds->ds[i].name, 0), 0))
601 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
604 if (! isnan (ds->ds[i].min))
605 if (NULL == hv_store (source, "min", 3,
606 newSVnv (ds->ds[i].min), 0))
609 if (! isnan (ds->ds[i].max))
610 if (NULL == hv_store (source, "max", 3,
611 newSVnv (ds->ds[i].max), 0))
614 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
618 } /* static int data_set2av (data_set_t *, AV *) */
620 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
627 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
630 len = vl->values_len;
632 if (ds->ds_num < len) {
633 log_warn ("value2av: Value length exceeds data set length.");
638 av_extend (values, len - 1);
640 for (i = 0; i < len; ++i) {
643 if (DS_TYPE_COUNTER == ds->ds[i].type)
644 val = newSViv (vl->values[i].counter);
646 val = newSVnv (vl->values[i].gauge);
648 if (NULL == av_store (values, i, val)) {
654 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
658 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
661 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
664 if ('\0' != vl->host[0])
665 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
668 if ('\0' != vl->plugin[0])
669 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
672 if ('\0' != vl->plugin_instance[0])
673 if (NULL == hv_store (hash, "plugin_instance", 15,
674 newSVpv (vl->plugin_instance, 0), 0))
677 if ('\0' != vl->type[0])
678 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
681 if ('\0' != vl->type_instance[0])
682 if (NULL == hv_store (hash, "type_instance", 13,
683 newSVpv (vl->type_instance, 0), 0))
686 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
688 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
698 av_extend (array, meta_num);
700 for (i = 0; NULL != meta; meta = meta->next, ++i) {
704 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
707 if (NM_TYPE_STRING == meta->type)
708 value = newSVpv (meta->nm_value.nm_string, 0);
709 else if (NM_TYPE_SIGNED_INT == meta->type)
710 value = newSViv (meta->nm_value.nm_signed_int);
711 else if (NM_TYPE_UNSIGNED_INT == meta->type)
712 value = newSVuv (meta->nm_value.nm_unsigned_int);
713 else if (NM_TYPE_DOUBLE == meta->type)
714 value = newSVnv (meta->nm_value.nm_double);
715 else if (NM_TYPE_BOOLEAN == meta->type)
716 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
720 if (NULL == hv_store (m, "value", 5, value, 0)) {
725 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
732 } /* static int notification_meta2av (notification_meta_t *, AV *) */
734 static int notification2hv (pTHX_ notification_t *n, HV *hash)
736 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
740 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
743 if ('\0' != *n->message)
744 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
747 if ('\0' != *n->host)
748 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
751 if ('\0' != *n->plugin)
752 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
755 if ('\0' != *n->plugin_instance)
756 if (NULL == hv_store (hash, "plugin_instance", 15,
757 newSVpv (n->plugin_instance, 0), 0))
760 if ('\0' != *n->type)
761 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
764 if ('\0' != *n->type_instance)
765 if (NULL == hv_store (hash, "type_instance", 13,
766 newSVpv (n->type_instance, 0), 0))
769 if (NULL != n->meta) {
771 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
772 || (NULL == hv_store (hash, "meta", 4,
773 newRV_noinc ((SV *)meta), 0))) {
780 } /* static int notification2hv (notification_t *, HV *) */
782 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
789 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
793 if (0 < ci->values_num)
794 av_extend (values, ci->values_num);
796 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
802 for (i = 0; i < ci->values_num; ++i) {
805 switch (ci->values[i].type) {
806 case OCONFIG_TYPE_STRING:
807 value = newSVpv (ci->values[i].value.string, 0);
809 case OCONFIG_TYPE_NUMBER:
810 value = newSVnv ((NV)ci->values[i].value.number);
812 case OCONFIG_TYPE_BOOLEAN:
813 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
816 log_err ("oconfig_item2hv: Invalid value type %i.",
818 value = &PL_sv_undef;
821 if (NULL == av_store (values, i, value)) {
827 /* ignoring 'parent' member which is uninteresting in this case */
830 if (0 < ci->children_num)
831 av_extend (children, ci->children_num);
833 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
839 for (i = 0; i < ci->children_num; ++i) {
840 HV *child = newHV ();
842 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
848 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
855 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
858 * Internal functions.
861 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
863 if (base_name[0] == '\0')
864 status = ssnprintf (buf, buf_len, "%s", module);
866 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
867 if ((status < 0) || ((unsigned int)status >= buf_len))
870 } /* char *get_module_name */
873 * Add a plugin's data set definition.
875 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
881 if ((NULL == name) || (NULL == dataset))
884 if (0 != av2data_set (aTHX_ dataset, name, &ds))
887 ret = plugin_register_data_set (&ds);
891 } /* static int pplugin_register_data_set (char *, SV *) */
894 * Remove a plugin's data set definition.
896 static int pplugin_unregister_data_set (char *name)
900 return plugin_unregister_data_set (name);
901 } /* static int pplugin_unregister_data_set (char *) */
904 * Submit the values to the write functions.
906 static int pplugin_dispatch_values (pTHX_ HV *values)
908 value_list_t vl = VALUE_LIST_INIT;
915 if (0 != hv2value_list (aTHX_ values, &vl))
918 ret = plugin_dispatch_values (&vl);
922 } /* static int pplugin_dispatch_values (char *, HV *) */
925 * Submit the values to a single write function.
927 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
930 value_list_t vl = VALUE_LIST_INIT;
937 if (0 != hv2value_list (aTHX_ values, &vl))
940 if ((NULL != data_set)
941 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
944 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
946 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
947 NULL == plugin ? "<any>" : plugin, ret);
949 if (NULL != data_set)
953 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
956 * Dispatch a notification.
958 static int pplugin_dispatch_notification (pTHX_ HV *notif)
967 memset (&n, 0, sizeof (n));
969 if (0 != hv2notification (aTHX_ notif, &n))
972 ret = plugin_dispatch_notification (&n);
973 plugin_notification_meta_free (n.meta);
975 } /* static int pplugin_dispatch_notification (HV *) */
978 * Call all working functions of the given type.
980 static int pplugin_call_all (pTHX_ int type, ...)
989 if ((type < 0) || (type >= PLUGIN_TYPES))
999 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1001 if (PLUGIN_WRITE == type) {
1003 * $_[0] = $plugin_type;
1018 * values => [ $v1, ... ],
1020 * host => $hostname,
1021 * plugin => $plugin,
1023 * plugin_instance => $instance,
1024 * type_instance => $type_instance
1033 ds = va_arg (ap, data_set_t *);
1034 vl = va_arg (ap, value_list_t *);
1036 if (-1 == data_set2av (aTHX_ ds, pds)) {
1039 pds = (AV *)&PL_sv_undef;
1043 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1046 pvl = (HV *)&PL_sv_undef;
1050 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1051 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1052 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1054 else if (PLUGIN_LOG == type) {
1060 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1061 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1063 else if (PLUGIN_NOTIF == type) {
1067 * severity => $severity,
1071 * plugin => $plugin,
1073 * plugin_instance => $instance,
1074 * type_instance => $type_instance
1078 HV *notif = newHV ();
1080 n = va_arg (ap, notification_t *);
1082 if (-1 == notification2hv (aTHX_ n, notif)) {
1085 notif = (HV *)&PL_sv_undef;
1089 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1091 else if (PLUGIN_FLUSH == type) {
1094 * $_[1] = $identifier;
1096 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1097 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1102 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1117 } /* static int pplugin_call_all (int, ...) */
1120 * collectd's perl interpreter based thread implementation.
1122 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1125 /* must be called with perl_threads->mutex locked */
1126 static void c_ithread_destroy (c_ithread_t *ithread)
1128 dTHXa (ithread->interp);
1130 assert (NULL != perl_threads);
1132 PERL_SET_CONTEXT (aTHX);
1133 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1138 --perl_threads->number_of_threads;
1139 #endif /* COLLECT_DEBUG */
1141 perl_destruct (aTHX);
1144 if (NULL == ithread->prev)
1145 perl_threads->head = ithread->next;
1147 ithread->prev->next = ithread->next;
1149 if (NULL == ithread->next)
1150 perl_threads->tail = ithread->prev;
1152 ithread->next->prev = ithread->prev;
1156 } /* static void c_ithread_destroy (c_ithread_t *) */
1158 static void c_ithread_destructor (void *arg)
1160 c_ithread_t *ithread = (c_ithread_t *)arg;
1161 c_ithread_t *t = NULL;
1163 if (NULL == perl_threads)
1166 pthread_mutex_lock (&perl_threads->mutex);
1168 for (t = perl_threads->head; NULL != t; t = t->next)
1172 /* the ithread no longer exists */
1176 c_ithread_destroy (ithread);
1178 pthread_mutex_unlock (&perl_threads->mutex);
1180 } /* static void c_ithread_destructor (void *) */
1182 /* must be called with perl_threads->mutex locked */
1183 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1185 c_ithread_t *t = NULL;
1188 assert (NULL != perl_threads);
1190 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1191 memset (t, 0, sizeof (c_ithread_t));
1193 t->interp = (NULL == base)
1195 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1199 if ((NULL != base) && (NULL != PL_endav)) {
1200 av_clear (PL_endav);
1201 av_undef (PL_endav);
1206 ++perl_threads->number_of_threads;
1207 #endif /* COLLECT_DEBUG */
1211 if (NULL == perl_threads->tail) {
1212 perl_threads->head = t;
1216 perl_threads->tail->next = t;
1217 t->prev = perl_threads->tail;
1220 perl_threads->tail = t;
1222 pthread_setspecific (perl_thr_key, (const void *)t);
1224 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1227 * Filter chains implementation.
1230 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1237 notification_meta_t **meta = NULL;
1242 if ((type < 0) || (type >= FC_TYPES))
1245 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1248 va_start (ap, data);
1255 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1256 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1257 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1259 if (FC_CB_CREATE == cb_type) {
1262 * $_[1] = $user_data;
1265 HV *config = newHV ();
1267 ci = va_arg (ap, oconfig_item_t *);
1269 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1272 config = (HV *)&PL_sv_undef;
1276 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1278 else if (FC_CB_DESTROY == cb_type) {
1280 * $_[1] = $user_data;
1283 /* nothing to be done - the user data pointer
1284 * is pushed onto the stack later */
1286 else if (FC_CB_EXEC == cb_type) {
1291 * $_[3] = $user_data;
1299 ds = va_arg (ap, data_set_t *);
1300 vl = va_arg (ap, value_list_t *);
1301 meta = va_arg (ap, notification_meta_t **);
1303 if (0 != data_set2av (aTHX_ ds, pds)) {
1306 pds = (AV *)&PL_sv_undef;
1310 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1313 pvl = (HV *)&PL_sv_undef;
1320 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1323 pmeta = (AV *)&PL_sv_undef;
1328 pmeta = (AV *)&PL_sv_undef;
1331 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1332 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1333 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1336 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1340 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1342 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1343 assert (pmeta != NULL);
1345 plugin_notification_meta_free (*meta);
1346 av2notification_meta (aTHX_ pmeta, meta);
1353 /* the exec callbacks return a status, while
1354 * the others return a boolean value */
1355 if (FC_CB_EXEC == cb_type)
1357 else if (! SvTRUE (tmp))
1367 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1369 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1371 pfc_user_data_t *data;
1377 if (NULL == perl_threads)
1381 c_ithread_t *t = NULL;
1383 pthread_mutex_lock (&perl_threads->mutex);
1384 t = c_ithread_create (perl_threads->head->interp);
1385 pthread_mutex_unlock (&perl_threads->mutex);
1390 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1391 aTHX, perl_threads->number_of_threads);
1393 if ((1 != ci->values_num)
1394 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1395 log_warn ("A \"%s\" block expects a single string argument.",
1396 (FC_MATCH == type) ? "Match" : "Target");
1400 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1401 data->name = sstrdup (ci->values[0].value.string);
1402 data->user_data = newSV (0);
1404 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1407 PFC_USER_DATA_FREE (data);
1411 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1413 static int fc_destroy (int type, void **user_data)
1415 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1421 if ((NULL == perl_threads) || (NULL == data))
1425 c_ithread_t *t = NULL;
1427 pthread_mutex_lock (&perl_threads->mutex);
1428 t = c_ithread_create (perl_threads->head->interp);
1429 pthread_mutex_unlock (&perl_threads->mutex);
1434 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1435 aTHX, perl_threads->number_of_threads);
1437 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1439 PFC_USER_DATA_FREE (data);
1442 } /* static int fc_destroy (int, void **) */
1444 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1445 notification_meta_t **meta, void **user_data)
1447 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1451 if (NULL == perl_threads)
1454 assert (NULL != data);
1457 c_ithread_t *t = NULL;
1459 pthread_mutex_lock (&perl_threads->mutex);
1460 t = c_ithread_create (perl_threads->head->interp);
1461 pthread_mutex_unlock (&perl_threads->mutex);
1466 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1467 aTHX, perl_threads->number_of_threads);
1469 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1470 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1471 notification_meta_t **, void **) */
1473 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1475 return fc_create (FC_MATCH, ci, user_data);
1476 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1478 static int pmatch_destroy (void **user_data)
1480 return fc_destroy (FC_MATCH, user_data);
1481 } /* static int pmatch_destroy (void **) */
1483 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1484 notification_meta_t **meta, void **user_data)
1486 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1487 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1488 notification_meta_t **, void **) */
1490 static match_proc_t pmatch = {
1491 pmatch_create, pmatch_destroy, pmatch_match
1494 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1496 return fc_create (FC_TARGET, ci, user_data);
1497 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1499 static int ptarget_destroy (void **user_data)
1501 return fc_destroy (FC_TARGET, user_data);
1502 } /* static int ptarget_destroy (void **) */
1504 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1505 notification_meta_t **meta, void **user_data)
1507 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1508 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1509 notification_meta_t **, void **) */
1511 static target_proc_t ptarget = {
1512 ptarget_create, ptarget_destroy, ptarget_invoke
1516 * Exported Perl API.
1520 * Collectd::plugin_register_data_set (type, dataset).
1523 * type of the dataset
1526 * dataset to be registered
1528 static XS (Collectd_plugin_register_ds)
1535 log_warn ("Using plugin_register() to register new data-sets is "
1536 "deprecated - add new entries to a custom types.db instead.");
1539 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1543 log_debug ("Collectd::plugin_register_data_set: "
1544 "type = \"%s\", dataset = \"%s\"",
1545 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1549 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1550 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1554 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1562 } /* static XS (Collectd_plugin_register_ds) */
1565 * Collectd::plugin_unregister_data_set (type).
1568 * type of the dataset
1570 static XS (Collectd_plugin_unregister_ds)
1575 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1579 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1580 SvPV_nolen (ST (0)));
1582 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1586 } /* static XS (Collectd_plugin_register_ds) */
1589 * Collectd::plugin_dispatch_values (name, values).
1592 * name of the plugin
1595 * value list to submit
1597 static XS (Collectd_plugin_dispatch_values)
1607 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1608 "is deprecated - pass the type through values->{type}.");
1611 else if (1 != items) {
1612 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1616 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1617 SvPV_nolen (ST (values_idx)));
1619 values = ST (values_idx);
1621 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1622 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1626 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1629 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1630 newSVsv (ST (0)), 0))) {
1631 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1635 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1641 } /* static XS (Collectd_plugin_dispatch_values) */
1643 /* Collectd::plugin_write (plugin, ds, vl).
1646 * name of the plugin to call, may be 'undef'
1649 * data-set that describes the submitted values, may be 'undef'
1652 * value-list to be written
1654 static XS (Collectd__plugin_write)
1665 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1669 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1670 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1671 SvPV_nolen (ST (2)));
1673 if (! SvOK (ST (0)))
1676 plugin = SvPV_nolen (ST (0));
1679 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1680 ds_array = (AV *)SvRV (ds);
1681 else if (! SvOK (ds))
1684 log_err ("Collectd::plugin_write: Invalid data-set.");
1689 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1690 log_err ("Collectd::plugin_write: Invalid value-list.");
1694 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1700 } /* static XS (Collectd__plugin_write) */
1703 * Collectd::_plugin_flush (plugin, timeout, identifier).
1706 * name of the plugin to flush
1709 * timeout to use when flushing the data
1712 * data-set identifier to flush
1714 static XS (Collectd__plugin_flush)
1716 char *plugin = NULL;
1723 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1728 plugin = SvPV_nolen (ST (0));
1731 timeout = (int)SvIV (ST (1));
1734 id = SvPV_nolen (ST (2));
1736 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1737 "id = \"%s\"", plugin, timeout, id);
1739 if (0 == plugin_flush (plugin, timeout, id))
1743 } /* static XS (Collectd__plugin_flush) */
1746 * Collectd::plugin_dispatch_notification (notif).
1749 * notification to dispatch
1751 static XS (Collectd_plugin_dispatch_notification)
1760 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1764 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1765 SvPV_nolen (ST (0)));
1769 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1770 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1774 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1780 } /* static XS (Collectd_plugin_dispatch_notification) */
1783 * Collectd::plugin_log (level, message).
1786 * log level (LOG_DEBUG, ... LOG_ERR)
1791 static XS (Collectd_plugin_log)
1796 log_err ("Usage: Collectd::plugin_log(level, message)");
1800 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1802 } /* static XS (Collectd_plugin_log) */
1805 * Collectd::_fc_register (type, name)
1813 static XS (Collectd__fc_register)
1823 log_err ("Usage: Collectd::_fc_register(type, name)");
1827 type = SvIV (ST (0));
1828 name = SvPV_nolen (ST (1));
1830 if (FC_MATCH == type)
1831 ret = fc_register_match (name, pmatch);
1832 else if (FC_TARGET == type)
1833 ret = fc_register_target (name, ptarget);
1839 } /* static XS (Collectd_fc_register) */
1842 * Collectd::call_by_name (...).
1844 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1846 static XS (Collectd_call_by_name)
1851 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1852 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1857 name = SvPV_nolen (tmp);
1859 if (NULL == get_cv (name, 0)) {
1860 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1865 /* simply pass on the subroutine call without touching the stack,
1866 * thus leaving any arguments and return values in place */
1868 } /* static XS (Collectd_call_by_name) */
1871 * Interface to collectd.
1874 static int perl_init (void)
1878 if (NULL == perl_threads)
1882 c_ithread_t *t = NULL;
1884 pthread_mutex_lock (&perl_threads->mutex);
1885 t = c_ithread_create (perl_threads->head->interp);
1886 pthread_mutex_unlock (&perl_threads->mutex);
1891 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1892 aTHX, perl_threads->number_of_threads);
1893 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1894 } /* static int perl_init (void) */
1896 static int perl_read (void)
1900 if (NULL == perl_threads)
1904 c_ithread_t *t = NULL;
1906 pthread_mutex_lock (&perl_threads->mutex);
1907 t = c_ithread_create (perl_threads->head->interp);
1908 pthread_mutex_unlock (&perl_threads->mutex);
1913 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1914 aTHX, perl_threads->number_of_threads);
1915 return pplugin_call_all (aTHX_ PLUGIN_READ);
1916 } /* static int perl_read (void) */
1918 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1919 user_data_t __attribute__((unused)) *user_data)
1923 if (NULL == perl_threads)
1927 c_ithread_t *t = NULL;
1929 pthread_mutex_lock (&perl_threads->mutex);
1930 t = c_ithread_create (perl_threads->head->interp);
1931 pthread_mutex_unlock (&perl_threads->mutex);
1936 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1937 aTHX, perl_threads->number_of_threads);
1938 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1939 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1941 static void perl_log (int level, const char *msg,
1942 user_data_t __attribute__((unused)) *user_data)
1946 if (NULL == perl_threads)
1950 c_ithread_t *t = NULL;
1952 pthread_mutex_lock (&perl_threads->mutex);
1953 t = c_ithread_create (perl_threads->head->interp);
1954 pthread_mutex_unlock (&perl_threads->mutex);
1959 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1961 } /* static void perl_log (int, const char *) */
1963 static int perl_notify (const notification_t *notif,
1964 user_data_t __attribute__((unused)) *user_data)
1968 if (NULL == perl_threads)
1972 c_ithread_t *t = NULL;
1974 pthread_mutex_lock (&perl_threads->mutex);
1975 t = c_ithread_create (perl_threads->head->interp);
1976 pthread_mutex_unlock (&perl_threads->mutex);
1980 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1981 } /* static int perl_notify (const notification_t *) */
1983 static int perl_flush (int timeout, const char *identifier,
1984 user_data_t __attribute__((unused)) *user_data)
1988 if (NULL == perl_threads)
1992 c_ithread_t *t = NULL;
1994 pthread_mutex_lock (&perl_threads->mutex);
1995 t = c_ithread_create (perl_threads->head->interp);
1996 pthread_mutex_unlock (&perl_threads->mutex);
2000 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2001 } /* static int perl_flush (const int) */
2003 static int perl_shutdown (void)
2005 c_ithread_t *t = NULL;
2011 plugin_unregister_complex_config ("perl");
2013 if (NULL == perl_threads)
2017 c_ithread_t *t = NULL;
2019 pthread_mutex_lock (&perl_threads->mutex);
2020 t = c_ithread_create (perl_threads->head->interp);
2021 pthread_mutex_unlock (&perl_threads->mutex);
2026 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2027 aTHX, perl_threads->number_of_threads);
2029 plugin_unregister_log ("perl");
2030 plugin_unregister_notification ("perl");
2031 plugin_unregister_init ("perl");
2032 plugin_unregister_read ("perl");
2033 plugin_unregister_write ("perl");
2034 plugin_unregister_flush ("perl");
2036 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2038 pthread_mutex_lock (&perl_threads->mutex);
2039 t = perl_threads->tail;
2042 c_ithread_t *thr = t;
2044 /* the pointer has to be advanced before destroying
2045 * the thread as this will free the memory */
2048 c_ithread_destroy (thr);
2051 pthread_mutex_unlock (&perl_threads->mutex);
2052 pthread_mutex_destroy (&perl_threads->mutex);
2054 sfree (perl_threads);
2056 pthread_key_delete (perl_thr_key);
2060 plugin_unregister_shutdown ("perl");
2062 } /* static void perl_shutdown (void) */
2065 * Access functions for global variables.
2067 * These functions implement the "magic" used to access
2068 * the global variables from Perl.
2071 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2073 char *pv = mg->mg_ptr;
2076 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2078 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2080 char *pv = mg->mg_ptr;
2081 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2083 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2085 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2087 int *iv = (int *)mg->mg_ptr;
2088 sv_setiv (var, *iv);
2090 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2092 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2094 int *iv = (int *)mg->mg_ptr;
2095 *iv = (int)SvIV (var);
2097 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2099 static MGVTBL g_pv_vtbl = {
2100 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2101 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2105 static MGVTBL g_iv_vtbl = {
2106 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2107 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2112 /* bootstrap the Collectd module */
2113 static void xs_init (pTHX)
2117 char *file = __FILE__;
2123 /* enable usage of Perl modules using shared libraries */
2124 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2127 for (i = 0; NULL != api[i].f; ++i)
2128 newXS (api[i].name, api[i].f, file);
2130 stash = gv_stashpv ("Collectd", 1);
2132 /* export "constants" */
2133 for (i = 0; '\0' != constants[i].name[0]; ++i)
2134 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2136 /* export global variables
2137 * by adding "magic" to the SV's representing the globale variables
2138 * perl is able to automagically call the get/set function when
2139 * accessing any such variable (this is basically the same as using
2141 /* global strings */
2142 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2143 tmp = get_sv (g_strings[i].name, 1);
2144 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2145 g_strings[i].var, 0);
2148 /* global integers */
2149 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2150 tmp = get_sv (g_integers[i].name, 1);
2151 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2152 (char *)g_integers[i].var, 0);
2155 } /* static void xs_init (pTHX) */
2157 /* Initialize the global Perl interpreter. */
2158 static int init_pi (int argc, char **argv)
2162 if (NULL != perl_threads)
2165 log_info ("Initializing Perl interpreter...");
2170 for (i = 0; i < argc; ++i)
2171 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2173 #endif /* COLLECT_DEBUG */
2175 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2176 log_err ("init_pi: pthread_key_create failed");
2178 /* this must not happen - cowardly giving up if it does */
2183 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2184 * triggers a "value computed is not used" warning by gcc. */
2187 PERL_SYS_INIT3 (&argc, &argv, &environ);
2189 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2190 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2192 pthread_mutex_init (&perl_threads->mutex, NULL);
2193 /* locking the mutex should not be necessary at this point
2194 * but let's just do it for the sake of completeness */
2195 pthread_mutex_lock (&perl_threads->mutex);
2197 perl_threads->head = c_ithread_create (NULL);
2198 perl_threads->tail = perl_threads->head;
2200 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2201 log_err ("init_pi: Not enough memory.");
2205 aTHX = perl_threads->head->interp;
2206 pthread_mutex_unlock (&perl_threads->mutex);
2208 perl_construct (aTHX);
2210 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2212 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2213 SV *err = get_sv ("@", 1);
2214 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2217 perl_destruct (perl_threads->head->interp);
2218 perl_free (perl_threads->head->interp);
2219 sfree (perl_threads);
2221 pthread_key_delete (perl_thr_key);
2225 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2226 sv_setpv (get_sv ("0", 0), "collectd");
2230 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2231 plugin_register_notification ("perl", perl_notify,
2232 /* user_data = */ NULL);
2233 plugin_register_init ("perl", perl_init);
2235 plugin_register_read ("perl", perl_read);
2237 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2238 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2239 plugin_register_shutdown ("perl", perl_shutdown);
2241 } /* static int init_pi (const char **, const int) */
2244 * LoadPlugin "<Plugin>"
2246 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2248 char module_name[DATA_MAX_NAME_LEN];
2252 if ((0 != ci->children_num) || (1 != ci->values_num)
2253 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2254 log_err ("LoadPlugin expects a single string argument.");
2258 value = ci->values[0].value.string;
2260 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2261 log_err ("Invalid module name %s", value);
2265 if (0 != init_pi (perl_argc, perl_argv))
2268 assert (NULL != perl_threads);
2269 assert (NULL != perl_threads->head);
2271 aTHX = perl_threads->head->interp;
2273 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2274 load_module (PERL_LOADMOD_NOIMPORT,
2275 newSVpv (module_name, strlen (module_name)), Nullsv);
2277 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2282 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2286 if ((0 != ci->children_num) || (1 != ci->values_num)
2287 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2288 log_err ("BaseName expects a single string argument.");
2292 value = ci->values[0].value.string;
2294 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2295 sstrncpy (base_name, value, sizeof (base_name));
2297 } /* static int perl_config_basename (oconfig_item_it *) */
2300 * EnableDebugger "<Package>"|""
2302 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2306 if ((0 != ci->children_num) || (1 != ci->values_num)
2307 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2308 log_err ("EnableDebugger expects a single string argument.");
2312 if (NULL != perl_threads) {
2313 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2317 value = ci->values[0].value.string;
2319 perl_argv = (char **)realloc (perl_argv,
2320 (++perl_argc + 1) * sizeof (char *));
2322 if (NULL == perl_argv) {
2323 log_err ("perl_config: Not enough memory.");
2327 if ('\0' == value[0]) {
2328 perl_argv[perl_argc - 1] = "-d";
2331 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2332 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2333 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2336 perl_argv[perl_argc] = NULL;
2338 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2341 * IncludeDir "<Dir>"
2343 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2347 if ((0 != ci->children_num) || (1 != ci->values_num)
2348 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2349 log_err ("IncludeDir expects a single string argument.");
2353 value = ci->values[0].value.string;
2356 perl_argv = (char **)realloc (perl_argv,
2357 (++perl_argc + 1) * sizeof (char *));
2359 if (NULL == perl_argv) {
2360 log_err ("perl_config: Not enough memory.");
2364 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2365 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2366 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2368 perl_argv[perl_argc] = NULL;
2371 /* prepend the directory to @INC */
2372 av_unshift (GvAVn (PL_incgv), 1);
2373 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2376 } /* static int perl_config_includedir (oconfig_item_it *) */
2381 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2391 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2392 log_err ("LoadPlugin expects a single string argument.");
2396 plugin = ci->values[0].value.string;
2399 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2403 log_err ("Unable to convert configuration to a Perl hash value.");
2404 config = (HV *)&PL_sv_undef;
2412 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2413 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2417 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2432 } /* static int perl_config_plugin (oconfig_item_it *) */
2434 static int perl_config (oconfig_item_t *ci)
2441 for (i = 0; i < ci->children_num; ++i) {
2442 oconfig_item_t *c = ci->children + i;
2443 int current_status = 0;
2445 if (NULL != perl_threads)
2446 aTHX = PERL_GET_CONTEXT;
2448 if (0 == strcasecmp (c->key, "LoadPlugin"))
2449 current_status = perl_config_loadplugin (aTHX_ c);
2450 else if (0 == strcasecmp (c->key, "BaseName"))
2451 current_status = perl_config_basename (aTHX_ c);
2452 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2453 current_status = perl_config_enabledebugger (aTHX_ c);
2454 else if (0 == strcasecmp (c->key, "IncludeDir"))
2455 current_status = perl_config_includedir (aTHX_ c);
2456 else if (0 == strcasecmp (c->key, "Plugin"))
2457 current_status = perl_config_plugin (aTHX_ c);
2460 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2464 /* fatal error - it's up to perl_config_* to clean up */
2465 if (0 > current_status) {
2466 log_err ("Configuration failed with a fatal error - "
2467 "plugin disabled!");
2468 return current_status;
2471 status += current_status;
2474 } /* static int perl_config (oconfig_item_t *) */
2476 void module_register (void)
2479 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2481 /* default options for the Perl interpreter */
2483 perl_argv[1] = "-MCollectd";
2484 perl_argv[2] = "-e";
2486 perl_argv[4] = NULL;
2488 plugin_register_complex_config ("perl", perl_config);
2490 } /* void module_register (void) */
2492 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */