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;
115 _Bool running; /* thread is inside Perl interpreter */
119 /* double linked list of threads */
120 struct c_ithread_s *prev;
121 struct c_ithread_s *next;
129 /* some usage stats */
130 int number_of_threads;
131 #endif /* COLLECT_DEBUG */
133 pthread_mutex_t mutex;
134 pthread_mutexattr_t mutexattr;
137 /* name / user_data for Perl matches / targets */
143 #define PFC_USER_DATA_FREE(data) \
145 sfree ((data)->name); \
146 if (NULL != (data)->user_data) \
147 sv_free ((data)->user_data); \
154 extern char **environ;
160 /* if perl_threads != NULL perl_threads->head must
161 * point to the "base" thread */
162 static c_ithread_list_t *perl_threads = NULL;
164 /* the key used to store each pthread's ithread */
165 static pthread_key_t perl_thr_key;
167 static int perl_argc = 0;
168 static char **perl_argv = NULL;
170 static char base_name[DATA_MAX_NAME_LEN] = "";
177 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
178 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
179 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
180 { "Collectd::_plugin_write", Collectd__plugin_write },
181 { "Collectd::_plugin_flush", Collectd__plugin_flush },
182 { "Collectd::plugin_dispatch_notification",
183 Collectd_plugin_dispatch_notification },
184 { "Collectd::plugin_log", Collectd_plugin_log },
185 { "Collectd::_fc_register", Collectd__fc_register },
186 { "Collectd::call_by_name", Collectd_call_by_name },
195 { "Collectd::TYPE_INIT", PLUGIN_INIT },
196 { "Collectd::TYPE_READ", PLUGIN_READ },
197 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
198 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
199 { "Collectd::TYPE_LOG", PLUGIN_LOG },
200 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
201 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
202 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
203 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
204 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
205 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
206 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
207 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
208 { "Collectd::LOG_ERR", LOG_ERR },
209 { "Collectd::LOG_WARNING", LOG_WARNING },
210 { "Collectd::LOG_NOTICE", LOG_NOTICE },
211 { "Collectd::LOG_INFO", LOG_INFO },
212 { "Collectd::LOG_DEBUG", LOG_DEBUG },
213 { "Collectd::FC_MATCH", FC_MATCH },
214 { "Collectd::FC_TARGET", FC_TARGET },
215 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
216 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
217 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
218 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
219 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
220 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
221 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
222 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
223 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
224 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
225 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
234 { "Collectd::hostname_g", hostname_g },
243 { "Collectd::interval_g", &interval_g },
248 * Helper functions for data type conversion.
263 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
267 if ((NULL == hash) || (NULL == ds))
270 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
271 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
274 log_err ("hv2data_source: No DS name given.");
278 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
279 ds->type = SvIV (*tmp);
281 if ((DS_TYPE_COUNTER != ds->type)
282 && (DS_TYPE_GAUGE != ds->type)
283 && (DS_TYPE_DERIVE != ds->type)
284 && (DS_TYPE_ABSOLUTE != ds->type)) {
285 log_err ("hv2data_source: Invalid DS type.");
290 ds->type = DS_TYPE_COUNTER;
293 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
294 ds->min = SvNV (*tmp);
298 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
299 ds->max = SvNV (*tmp);
303 } /* static int hv2data_source (HV *, data_source_t *) */
305 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
307 const data_set_t *ds;
311 if ((NULL == name) || (NULL == array) || (NULL == value))
314 if (av_len (array) < len - 1)
315 len = av_len (array) + 1;
320 ds = plugin_get_ds (name);
322 log_err ("av2value: Unknown dataset \"%s\"", name);
326 if (ds->ds_num < len) {
327 log_warn ("av2value: Value length exceeds data set length.");
331 for (i = 0; i < len; ++i) {
332 SV **tmp = av_fetch (array, i, 0);
335 if (DS_TYPE_COUNTER == ds->ds[i].type)
336 value[i].counter = SvIV (*tmp);
337 else if (DS_TYPE_GAUGE == ds->ds[i].type)
338 value[i].gauge = SvNV (*tmp);
339 else if (DS_TYPE_DERIVE == ds->ds[i].type)
340 value[i].derive = SvIV (*tmp);
341 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
342 value[i].absolute = SvIV (*tmp);
349 } /* static int av2value (char *, AV *, value_t *, int) */
354 * values => [ @values ],
358 * plugin_instance => $pinstance,
359 * type_instance => $tinstance,
362 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
366 if ((NULL == hash) || (NULL == vl))
369 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
370 log_err ("hv2value_list: No type given.");
374 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
376 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
377 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
378 log_err ("hv2value_list: No valid values given.");
383 AV *array = (AV *)SvRV (*tmp);
384 int len = av_len (array) + 1;
389 vl->values = (value_t *)smalloc (len * sizeof (value_t));
390 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
393 if (-1 == vl->values_len) {
399 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
400 vl->time = (time_t)SvIV (*tmp);
402 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
403 vl->interval = SvIV (*tmp);
405 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
406 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
408 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
410 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
411 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
413 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
414 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
415 sizeof (vl->plugin_instance));
417 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
418 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
419 sizeof (vl->type_instance));
421 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
423 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
427 if ((NULL == array) || (NULL == name) || (NULL == ds))
430 len = av_len (array);
433 log_err ("av2data_set: Invalid data set.");
437 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
438 ds->ds_num = len + 1;
440 for (i = 0; i <= len; ++i) {
441 SV **elem = av_fetch (array, i, 0);
444 log_err ("av2data_set: Failed to fetch data source %i.", i);
448 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
449 log_err ("av2data_set: Invalid data source.");
453 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
456 log_debug ("av2data_set: "
457 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
458 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
461 sstrncpy (ds->type, name, sizeof (ds->type));
463 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
468 * severity => $severity,
474 * plugin_instance => $instance,
475 * type_instance => $type_instance,
476 * meta => [ { name => <name>, value => <value> }, ... ]
479 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
481 notification_meta_t **m = meta;
483 int len = av_len (array);
486 for (i = 0; i <= len; ++i) {
487 SV **tmp = av_fetch (array, i, 0);
493 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
494 log_warn ("av2notification_meta: Skipping invalid "
495 "meta information.");
499 hash = (HV *)SvRV (*tmp);
501 *m = (notification_meta_t *)smalloc (sizeof (**m));
503 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
504 log_warn ("av2notification_meta: Skipping invalid "
505 "meta information.");
509 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
511 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
512 log_warn ("av2notification_meta: Skipping invalid "
513 "meta information.");
520 (*m)->nm_value.nm_double = SvNVX (*tmp);
521 (*m)->type = NM_TYPE_DOUBLE;
523 else if (SvUOK (*tmp)) {
524 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
525 (*m)->type = NM_TYPE_UNSIGNED_INT;
527 else if (SvIOK (*tmp)) {
528 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
529 (*m)->type = NM_TYPE_SIGNED_INT;
532 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
533 (*m)->type = NM_TYPE_STRING;
540 } /* static int av2notification_meta (AV *, notification_meta_t *) */
542 static int hv2notification (pTHX_ HV *hash, notification_t *n)
546 if ((NULL == hash) || (NULL == n))
549 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
550 n->severity = SvIV (*tmp);
552 n->severity = NOTIF_FAILURE;
554 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
555 n->time = (time_t)SvIV (*tmp);
557 n->time = time (NULL);
559 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
560 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
562 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
563 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
565 sstrncpy (n->host, hostname_g, sizeof (n->host));
567 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
568 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
570 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
571 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
572 sizeof (n->plugin_instance));
574 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
575 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
577 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
578 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
579 sizeof (n->type_instance));
582 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
583 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
584 log_warn ("hv2notification: Ignoring invalid meta information.");
588 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
589 plugin_notification_meta_free (n->meta);
596 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
598 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
602 if ((NULL == ds) || (NULL == array))
605 av_extend (array, ds->ds_num);
607 for (i = 0; i < ds->ds_num; ++i) {
608 HV *source = newHV ();
610 if (NULL == hv_store (source, "name", 4,
611 newSVpv (ds->ds[i].name, 0), 0))
614 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
617 if (! isnan (ds->ds[i].min))
618 if (NULL == hv_store (source, "min", 3,
619 newSVnv (ds->ds[i].min), 0))
622 if (! isnan (ds->ds[i].max))
623 if (NULL == hv_store (source, "max", 3,
624 newSVnv (ds->ds[i].max), 0))
627 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
631 } /* static int data_set2av (data_set_t *, AV *) */
633 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
640 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
643 len = vl->values_len;
645 if (ds->ds_num < len) {
646 log_warn ("value2av: Value length exceeds data set length.");
651 av_extend (values, len - 1);
653 for (i = 0; i < len; ++i) {
656 if (DS_TYPE_COUNTER == ds->ds[i].type)
657 val = newSViv (vl->values[i].counter);
658 else if (DS_TYPE_GAUGE == ds->ds[i].type)
659 val = newSVnv (vl->values[i].gauge);
660 else if (DS_TYPE_DERIVE == ds->ds[i].type)
661 val = newSViv (vl->values[i].derive);
662 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
663 val = newSViv (vl->values[i].absolute);
665 if (NULL == av_store (values, i, val)) {
671 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
675 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
678 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
681 if ('\0' != vl->host[0])
682 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
685 if ('\0' != vl->plugin[0])
686 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
689 if ('\0' != vl->plugin_instance[0])
690 if (NULL == hv_store (hash, "plugin_instance", 15,
691 newSVpv (vl->plugin_instance, 0), 0))
694 if ('\0' != vl->type[0])
695 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
698 if ('\0' != vl->type_instance[0])
699 if (NULL == hv_store (hash, "type_instance", 13,
700 newSVpv (vl->type_instance, 0), 0))
703 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
705 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
715 av_extend (array, meta_num);
717 for (i = 0; NULL != meta; meta = meta->next, ++i) {
721 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
724 if (NM_TYPE_STRING == meta->type)
725 value = newSVpv (meta->nm_value.nm_string, 0);
726 else if (NM_TYPE_SIGNED_INT == meta->type)
727 value = newSViv (meta->nm_value.nm_signed_int);
728 else if (NM_TYPE_UNSIGNED_INT == meta->type)
729 value = newSVuv (meta->nm_value.nm_unsigned_int);
730 else if (NM_TYPE_DOUBLE == meta->type)
731 value = newSVnv (meta->nm_value.nm_double);
732 else if (NM_TYPE_BOOLEAN == meta->type)
733 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
737 if (NULL == hv_store (m, "value", 5, value, 0)) {
742 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
749 } /* static int notification_meta2av (notification_meta_t *, AV *) */
751 static int notification2hv (pTHX_ notification_t *n, HV *hash)
753 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
757 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
760 if ('\0' != *n->message)
761 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
764 if ('\0' != *n->host)
765 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
768 if ('\0' != *n->plugin)
769 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
772 if ('\0' != *n->plugin_instance)
773 if (NULL == hv_store (hash, "plugin_instance", 15,
774 newSVpv (n->plugin_instance, 0), 0))
777 if ('\0' != *n->type)
778 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
781 if ('\0' != *n->type_instance)
782 if (NULL == hv_store (hash, "type_instance", 13,
783 newSVpv (n->type_instance, 0), 0))
786 if (NULL != n->meta) {
788 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
789 || (NULL == hv_store (hash, "meta", 4,
790 newRV_noinc ((SV *)meta), 0))) {
797 } /* static int notification2hv (notification_t *, HV *) */
799 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
806 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
810 if (0 < ci->values_num)
811 av_extend (values, ci->values_num);
813 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
819 for (i = 0; i < ci->values_num; ++i) {
822 switch (ci->values[i].type) {
823 case OCONFIG_TYPE_STRING:
824 value = newSVpv (ci->values[i].value.string, 0);
826 case OCONFIG_TYPE_NUMBER:
827 value = newSVnv ((NV)ci->values[i].value.number);
829 case OCONFIG_TYPE_BOOLEAN:
830 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
833 log_err ("oconfig_item2hv: Invalid value type %i.",
835 value = &PL_sv_undef;
838 if (NULL == av_store (values, i, value)) {
844 /* ignoring 'parent' member which is uninteresting in this case */
847 if (0 < ci->children_num)
848 av_extend (children, ci->children_num);
850 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
856 for (i = 0; i < ci->children_num; ++i) {
857 HV *child = newHV ();
859 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
865 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
872 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
875 * Internal functions.
878 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
880 if (base_name[0] == '\0')
881 status = ssnprintf (buf, buf_len, "%s", module);
883 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
884 if ((status < 0) || ((unsigned int)status >= buf_len))
887 } /* char *get_module_name */
890 * Add a plugin's data set definition.
892 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
898 if ((NULL == name) || (NULL == dataset))
901 if (0 != av2data_set (aTHX_ dataset, name, &ds))
904 ret = plugin_register_data_set (&ds);
908 } /* static int pplugin_register_data_set (char *, SV *) */
911 * Remove a plugin's data set definition.
913 static int pplugin_unregister_data_set (char *name)
917 return plugin_unregister_data_set (name);
918 } /* static int pplugin_unregister_data_set (char *) */
921 * Submit the values to the write functions.
923 static int pplugin_dispatch_values (pTHX_ HV *values)
925 value_list_t vl = VALUE_LIST_INIT;
932 if (0 != hv2value_list (aTHX_ values, &vl))
935 ret = plugin_dispatch_values (&vl);
939 } /* static int pplugin_dispatch_values (char *, HV *) */
942 * Submit the values to a single write function.
944 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
947 value_list_t vl = VALUE_LIST_INIT;
954 if (0 != hv2value_list (aTHX_ values, &vl))
957 if ((NULL != data_set)
958 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
961 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
963 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
964 NULL == plugin ? "<any>" : plugin, ret);
966 if (NULL != data_set)
970 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
973 * Dispatch a notification.
975 static int pplugin_dispatch_notification (pTHX_ HV *notif)
984 memset (&n, 0, sizeof (n));
986 if (0 != hv2notification (aTHX_ notif, &n))
989 ret = plugin_dispatch_notification (&n);
990 plugin_notification_meta_free (n.meta);
992 } /* static int pplugin_dispatch_notification (HV *) */
995 * Call perl sub with thread locking flags handled.
997 static int call_pv_locked (pTHX_ const char* sub_name)
1002 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1003 if (t == NULL) /* thread destroyed */
1006 old_running = t->running;
1010 t->running = old_running;
1014 ret = call_pv (sub_name, G_SCALAR);
1016 t->running = old_running;
1018 } /* static int call_pv_locked (pTHX, *sub_name) */
1021 * Call all working functions of the given type.
1023 static int pplugin_call_all (pTHX_ int type, ...)
1032 if ((type < 0) || (type >= PLUGIN_TYPES))
1035 va_start (ap, type);
1042 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1044 if (PLUGIN_WRITE == type) {
1046 * $_[0] = $plugin_type;
1061 * values => [ $v1, ... ],
1063 * host => $hostname,
1064 * plugin => $plugin,
1066 * plugin_instance => $instance,
1067 * type_instance => $type_instance
1076 ds = va_arg (ap, data_set_t *);
1077 vl = va_arg (ap, value_list_t *);
1079 if (-1 == data_set2av (aTHX_ ds, pds)) {
1082 pds = (AV *)&PL_sv_undef;
1086 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1089 pvl = (HV *)&PL_sv_undef;
1093 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1094 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1095 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1097 else if (PLUGIN_LOG == type) {
1103 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1104 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1106 else if (PLUGIN_NOTIF == type) {
1110 * severity => $severity,
1114 * plugin => $plugin,
1116 * plugin_instance => $instance,
1117 * type_instance => $type_instance
1121 HV *notif = newHV ();
1123 n = va_arg (ap, notification_t *);
1125 if (-1 == notification2hv (aTHX_ n, notif)) {
1128 notif = (HV *)&PL_sv_undef;
1132 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1134 else if (PLUGIN_FLUSH == type) {
1137 * $_[1] = $identifier;
1139 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1140 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1145 retvals = call_pv_locked (aTHX_ "Collectd::plugin_call_all");
1160 } /* static int pplugin_call_all (int, ...) */
1163 * collectd's Perl interpreter based thread implementation.
1165 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1168 /* must be called with perl_threads->mutex locked */
1169 static void c_ithread_destroy (c_ithread_t *ithread)
1171 dTHXa (ithread->interp);
1173 assert (NULL != perl_threads);
1175 PERL_SET_CONTEXT (aTHX);
1176 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1181 --perl_threads->number_of_threads;
1182 #endif /* COLLECT_DEBUG */
1184 perl_destruct (aTHX);
1187 if (NULL == ithread->prev)
1188 perl_threads->head = ithread->next;
1190 ithread->prev->next = ithread->next;
1192 if (NULL == ithread->next)
1193 perl_threads->tail = ithread->prev;
1195 ithread->next->prev = ithread->prev;
1199 } /* static void c_ithread_destroy (c_ithread_t *) */
1201 static void c_ithread_destructor (void *arg)
1203 c_ithread_t *ithread = (c_ithread_t *)arg;
1204 c_ithread_t *t = NULL;
1206 if (NULL == perl_threads)
1209 pthread_mutex_lock (&perl_threads->mutex);
1211 for (t = perl_threads->head; NULL != t; t = t->next)
1215 /* the ithread no longer exists */
1219 c_ithread_destroy (ithread);
1221 pthread_mutex_unlock (&perl_threads->mutex);
1223 } /* static void c_ithread_destructor (void *) */
1225 /* must be called with perl_threads->mutex locked */
1226 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1228 c_ithread_t *t = NULL;
1231 assert (NULL != perl_threads);
1233 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1234 memset (t, 0, sizeof (c_ithread_t));
1236 t->interp = (NULL == base)
1238 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1242 if ((NULL != base) && (NULL != PL_endav)) {
1243 av_clear (PL_endav);
1244 av_undef (PL_endav);
1249 ++perl_threads->number_of_threads;
1250 #endif /* COLLECT_DEBUG */
1254 if (NULL == perl_threads->tail) {
1255 perl_threads->head = t;
1259 perl_threads->tail->next = t;
1260 t->prev = perl_threads->tail;
1263 t->pthread = pthread_self();
1266 perl_threads->tail = t;
1268 pthread_setspecific (perl_thr_key, (const void *)t);
1270 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1273 * Filter chains implementation.
1276 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1283 notification_meta_t **meta = NULL;
1288 if ((type < 0) || (type >= FC_TYPES))
1291 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1294 va_start (ap, data);
1301 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1302 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1303 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1305 if (FC_CB_CREATE == cb_type) {
1308 * $_[1] = $user_data;
1311 HV *config = newHV ();
1313 ci = va_arg (ap, oconfig_item_t *);
1315 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1318 config = (HV *)&PL_sv_undef;
1322 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1324 else if (FC_CB_DESTROY == cb_type) {
1326 * $_[1] = $user_data;
1329 /* nothing to be done - the user data pointer
1330 * is pushed onto the stack later */
1332 else if (FC_CB_EXEC == cb_type) {
1337 * $_[3] = $user_data;
1345 ds = va_arg (ap, data_set_t *);
1346 vl = va_arg (ap, value_list_t *);
1347 meta = va_arg (ap, notification_meta_t **);
1349 if (0 != data_set2av (aTHX_ ds, pds)) {
1352 pds = (AV *)&PL_sv_undef;
1356 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1359 pvl = (HV *)&PL_sv_undef;
1366 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1369 pmeta = (AV *)&PL_sv_undef;
1374 pmeta = (AV *)&PL_sv_undef;
1377 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1378 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1379 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1382 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1386 retvals = call_pv_locked (aTHX_ "Collectd::fc_call");
1388 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1389 assert (pmeta != NULL);
1391 plugin_notification_meta_free (*meta);
1392 av2notification_meta (aTHX_ pmeta, meta);
1399 /* the exec callbacks return a status, while
1400 * the others return a boolean value */
1401 if (FC_CB_EXEC == cb_type)
1403 else if (! SvTRUE (tmp))
1413 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1415 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1417 pfc_user_data_t *data;
1423 if (NULL == perl_threads)
1427 c_ithread_t *t = NULL;
1429 pthread_mutex_lock (&perl_threads->mutex);
1430 t = c_ithread_create (perl_threads->head->interp);
1431 pthread_mutex_unlock (&perl_threads->mutex);
1436 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1437 aTHX, perl_threads->number_of_threads);
1439 if ((1 != ci->values_num)
1440 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1441 log_warn ("A \"%s\" block expects a single string argument.",
1442 (FC_MATCH == type) ? "Match" : "Target");
1446 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1447 data->name = sstrdup (ci->values[0].value.string);
1448 data->user_data = newSV (0);
1450 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1453 PFC_USER_DATA_FREE (data);
1457 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1459 static int fc_destroy (int type, void **user_data)
1461 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1467 if ((NULL == perl_threads) || (NULL == data))
1471 c_ithread_t *t = NULL;
1473 pthread_mutex_lock (&perl_threads->mutex);
1474 t = c_ithread_create (perl_threads->head->interp);
1475 pthread_mutex_unlock (&perl_threads->mutex);
1480 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1481 aTHX, perl_threads->number_of_threads);
1483 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1485 PFC_USER_DATA_FREE (data);
1488 } /* static int fc_destroy (int, void **) */
1490 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1491 notification_meta_t **meta, void **user_data)
1493 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1497 if (NULL == perl_threads)
1500 assert (NULL != data);
1503 c_ithread_t *t = NULL;
1505 pthread_mutex_lock (&perl_threads->mutex);
1506 t = c_ithread_create (perl_threads->head->interp);
1507 pthread_mutex_unlock (&perl_threads->mutex);
1512 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1513 aTHX, perl_threads->number_of_threads);
1515 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1516 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1517 notification_meta_t **, void **) */
1519 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1521 return fc_create (FC_MATCH, ci, user_data);
1522 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1524 static int pmatch_destroy (void **user_data)
1526 return fc_destroy (FC_MATCH, user_data);
1527 } /* static int pmatch_destroy (void **) */
1529 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1530 notification_meta_t **meta, void **user_data)
1532 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1533 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1534 notification_meta_t **, void **) */
1536 static match_proc_t pmatch = {
1537 pmatch_create, pmatch_destroy, pmatch_match
1540 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1542 return fc_create (FC_TARGET, ci, user_data);
1543 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1545 static int ptarget_destroy (void **user_data)
1547 return fc_destroy (FC_TARGET, user_data);
1548 } /* static int ptarget_destroy (void **) */
1550 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1551 notification_meta_t **meta, void **user_data)
1553 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1554 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1555 notification_meta_t **, void **) */
1557 static target_proc_t ptarget = {
1558 ptarget_create, ptarget_destroy, ptarget_invoke
1562 * Exported Perl API.
1566 * Collectd::plugin_register_data_set (type, dataset).
1569 * type of the dataset
1572 * dataset to be registered
1574 static XS (Collectd_plugin_register_ds)
1581 log_warn ("Using plugin_register() to register new data-sets is "
1582 "deprecated - add new entries to a custom types.db instead.");
1585 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1589 log_debug ("Collectd::plugin_register_data_set: "
1590 "type = \"%s\", dataset = \"%s\"",
1591 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1595 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1596 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1600 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1608 } /* static XS (Collectd_plugin_register_ds) */
1611 * Collectd::plugin_unregister_data_set (type).
1614 * type of the dataset
1616 static XS (Collectd_plugin_unregister_ds)
1621 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1625 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1626 SvPV_nolen (ST (0)));
1628 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1632 } /* static XS (Collectd_plugin_register_ds) */
1635 * Collectd::plugin_dispatch_values (name, values).
1638 * name of the plugin
1641 * value list to submit
1643 static XS (Collectd_plugin_dispatch_values)
1653 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1654 "is deprecated - pass the type through values->{type}.");
1657 else if (1 != items) {
1658 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1662 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1663 SvPV_nolen (ST (values_idx)));
1665 values = ST (values_idx);
1667 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1668 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1672 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1675 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1676 newSVsv (ST (0)), 0))) {
1677 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1681 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1687 } /* static XS (Collectd_plugin_dispatch_values) */
1689 /* Collectd::plugin_write (plugin, ds, vl).
1692 * name of the plugin to call, may be 'undef'
1695 * data-set that describes the submitted values, may be 'undef'
1698 * value-list to be written
1700 static XS (Collectd__plugin_write)
1711 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1715 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1716 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1717 SvPV_nolen (ST (2)));
1719 if (! SvOK (ST (0)))
1722 plugin = SvPV_nolen (ST (0));
1725 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1726 ds_array = (AV *)SvRV (ds);
1727 else if (! SvOK (ds))
1730 log_err ("Collectd::plugin_write: Invalid data-set.");
1735 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1736 log_err ("Collectd::plugin_write: Invalid value-list.");
1740 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1746 } /* static XS (Collectd__plugin_write) */
1749 * Collectd::_plugin_flush (plugin, timeout, identifier).
1752 * name of the plugin to flush
1755 * timeout to use when flushing the data
1758 * data-set identifier to flush
1760 static XS (Collectd__plugin_flush)
1762 char *plugin = NULL;
1769 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1774 plugin = SvPV_nolen (ST (0));
1777 timeout = (int)SvIV (ST (1));
1780 id = SvPV_nolen (ST (2));
1782 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1783 "id = \"%s\"", plugin, timeout, id);
1785 if (0 == plugin_flush (plugin, timeout, id))
1789 } /* static XS (Collectd__plugin_flush) */
1792 * Collectd::plugin_dispatch_notification (notif).
1795 * notification to dispatch
1797 static XS (Collectd_plugin_dispatch_notification)
1806 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1810 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1811 SvPV_nolen (ST (0)));
1815 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1816 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1820 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1826 } /* static XS (Collectd_plugin_dispatch_notification) */
1829 * Collectd::plugin_log (level, message).
1832 * log level (LOG_DEBUG, ... LOG_ERR)
1837 static XS (Collectd_plugin_log)
1842 log_err ("Usage: Collectd::plugin_log(level, message)");
1846 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1848 } /* static XS (Collectd_plugin_log) */
1851 * Collectd::_fc_register (type, name)
1859 static XS (Collectd__fc_register)
1869 log_err ("Usage: Collectd::_fc_register(type, name)");
1873 type = SvIV (ST (0));
1874 name = SvPV_nolen (ST (1));
1876 if (FC_MATCH == type)
1877 ret = fc_register_match (name, pmatch);
1878 else if (FC_TARGET == type)
1879 ret = fc_register_target (name, ptarget);
1885 } /* static XS (Collectd_fc_register) */
1888 * Collectd::call_by_name (...).
1890 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1892 static XS (Collectd_call_by_name)
1897 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1898 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1903 name = SvPV_nolen (tmp);
1905 if (NULL == get_cv (name, 0)) {
1906 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1911 /* simply pass on the subroutine call without touching the stack,
1912 * thus leaving any arguments and return values in place */
1914 } /* static XS (Collectd_call_by_name) */
1917 * Interface to collectd.
1920 static int perl_init (void)
1925 if (NULL == perl_threads)
1929 c_ithread_t *t = NULL;
1931 pthread_mutex_lock (&perl_threads->mutex);
1932 t = c_ithread_create (perl_threads->head->interp);
1933 pthread_mutex_unlock (&perl_threads->mutex);
1938 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1939 aTHX, perl_threads->number_of_threads);
1941 /* Lock the base thread to avoid race conditions with c_ithread_create().
1942 * See https://github.com/collectd/collectd/issues/9 and
1943 * https://github.com/collectd/collectd/issues/1706 for details.
1945 assert (aTHX == perl_threads->head->interp);
1946 pthread_mutex_lock (&perl_threads->mutex);
1948 status = pplugin_call_all (aTHX_ PLUGIN_INIT);
1950 pthread_mutex_unlock (&perl_threads->mutex);
1953 } /* static int perl_init (void) */
1955 static int perl_read (void)
1959 if (NULL == perl_threads)
1963 c_ithread_t *t = NULL;
1965 pthread_mutex_lock (&perl_threads->mutex);
1966 t = c_ithread_create (perl_threads->head->interp);
1967 pthread_mutex_unlock (&perl_threads->mutex);
1972 /* Assert that we're not running as the base thread. Otherwise, we might
1973 * run into concurrency issues with c_ithread_create(). See
1974 * https://github.com/collectd/collectd/issues/9 for details. */
1975 assert (aTHX != perl_threads->head->interp);
1977 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1978 aTHX, perl_threads->number_of_threads);
1979 return pplugin_call_all (aTHX_ PLUGIN_READ);
1980 } /* static int perl_read (void) */
1982 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1983 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);
2001 /* Lock the base thread if this is not called from one of the read threads
2002 * to avoid race conditions with c_ithread_create(). See
2003 * https://github.com/collectd/collectd/issues/9 for details. */
2004 if (aTHX == perl_threads->head->interp)
2005 pthread_mutex_lock (&perl_threads->mutex);
2007 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
2008 aTHX, perl_threads->number_of_threads);
2009 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
2011 if (aTHX == perl_threads->head->interp)
2012 pthread_mutex_unlock (&perl_threads->mutex);
2015 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2017 static void perl_log (int level, const char *msg,
2018 user_data_t __attribute__((unused)) *user_data)
2022 if (NULL == perl_threads)
2026 c_ithread_t *t = NULL;
2028 pthread_mutex_lock (&perl_threads->mutex);
2029 t = c_ithread_create (perl_threads->head->interp);
2030 pthread_mutex_unlock (&perl_threads->mutex);
2035 /* Lock the base thread if this is not called from one of the read threads
2036 * to avoid race conditions with c_ithread_create(). See
2037 * https://github.com/collectd/collectd/issues/9 for details.
2040 if (aTHX == perl_threads->head->interp)
2041 pthread_mutex_lock (&perl_threads->mutex);
2043 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2045 if (aTHX == perl_threads->head->interp)
2046 pthread_mutex_unlock (&perl_threads->mutex);
2049 } /* static void perl_log (int, const char *) */
2051 static int perl_notify (const notification_t *notif,
2052 user_data_t __attribute__((unused)) *user_data)
2056 if (NULL == perl_threads)
2060 c_ithread_t *t = NULL;
2062 pthread_mutex_lock (&perl_threads->mutex);
2063 t = c_ithread_create (perl_threads->head->interp);
2064 pthread_mutex_unlock (&perl_threads->mutex);
2068 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2069 } /* static int perl_notify (const notification_t *) */
2071 static int perl_flush (int timeout, const char *identifier,
2072 user_data_t __attribute__((unused)) *user_data)
2076 if (NULL == perl_threads)
2080 c_ithread_t *t = NULL;
2082 pthread_mutex_lock (&perl_threads->mutex);
2083 t = c_ithread_create (perl_threads->head->interp);
2084 pthread_mutex_unlock (&perl_threads->mutex);
2088 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2089 } /* static int perl_flush (const int) */
2091 static int perl_shutdown (void)
2093 c_ithread_t *t = NULL;
2099 plugin_unregister_complex_config ("perl");
2101 if (NULL == perl_threads)
2105 c_ithread_t *t = NULL;
2107 pthread_mutex_lock (&perl_threads->mutex);
2108 t = c_ithread_create (perl_threads->head->interp);
2109 pthread_mutex_unlock (&perl_threads->mutex);
2114 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2115 aTHX, perl_threads->number_of_threads);
2117 plugin_unregister_log ("perl");
2118 plugin_unregister_notification ("perl");
2119 plugin_unregister_init ("perl");
2120 plugin_unregister_read ("perl");
2121 plugin_unregister_write ("perl");
2122 plugin_unregister_flush ("perl");
2124 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2126 pthread_mutex_lock (&perl_threads->mutex);
2127 t = perl_threads->tail;
2130 struct timespec ts_wait;
2131 c_ithread_t *thr = t;
2133 /* the pointer has to be advanced before destroying
2134 * the thread as this will free the memory */
2139 /* Give some time to thread to exit from Perl interpreter */
2140 WARNING ("perl shutdown: Thread is running inside Perl. Waiting.");
2142 ts_wait.tv_nsec = 500000;
2143 nanosleep (&ts_wait, NULL);
2146 pthread_kill (thr->pthread, SIGTERM);
2147 ERROR ("perl shutdown: Thread hangs inside Perl. Thread killed.");
2149 c_ithread_destroy (thr);
2152 pthread_mutex_unlock (&perl_threads->mutex);
2153 pthread_mutex_destroy (&perl_threads->mutex);
2154 pthread_mutexattr_destroy (&perl_threads->mutexattr);
2156 sfree (perl_threads);
2158 pthread_key_delete (perl_thr_key);
2162 plugin_unregister_shutdown ("perl");
2164 } /* static void perl_shutdown (void) */
2167 * Access functions for global variables.
2169 * These functions implement the "magic" used to access
2170 * the global variables from Perl.
2173 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2175 char *pv = mg->mg_ptr;
2178 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2180 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2182 char *pv = mg->mg_ptr;
2183 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2185 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2187 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2189 int *iv = (int *)mg->mg_ptr;
2190 sv_setiv (var, *iv);
2192 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2194 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2196 int *iv = (int *)mg->mg_ptr;
2197 *iv = (int)SvIV (var);
2199 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2201 static MGVTBL g_pv_vtbl = {
2202 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2203 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2207 static MGVTBL g_iv_vtbl = {
2208 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2209 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2214 /* bootstrap the Collectd module */
2215 static void xs_init (pTHX)
2219 char *file = __FILE__;
2225 /* enable usage of Perl modules using shared libraries */
2226 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2229 for (i = 0; NULL != api[i].f; ++i)
2230 newXS (api[i].name, api[i].f, file);
2232 stash = gv_stashpv ("Collectd", 1);
2234 /* export "constants" */
2235 for (i = 0; '\0' != constants[i].name[0]; ++i)
2236 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2238 /* export global variables
2239 * by adding "magic" to the SV's representing the globale variables
2240 * perl is able to automagically call the get/set function when
2241 * accessing any such variable (this is basically the same as using
2243 /* global strings */
2244 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2245 tmp = get_sv (g_strings[i].name, 1);
2246 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2247 g_strings[i].var, 0);
2250 /* global integers */
2251 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2252 tmp = get_sv (g_integers[i].name, 1);
2253 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2254 (char *)g_integers[i].var, 0);
2257 } /* static void xs_init (pTHX) */
2259 /* Initialize the global Perl interpreter. */
2260 static int init_pi (int argc, char **argv)
2264 if (NULL != perl_threads)
2267 log_info ("Initializing Perl interpreter...");
2272 for (i = 0; i < argc; ++i)
2273 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2275 #endif /* COLLECT_DEBUG */
2277 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2278 log_err ("init_pi: pthread_key_create failed");
2280 /* this must not happen - cowardly giving up if it does */
2285 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2286 * triggers a "value computed is not used" warning by gcc. */
2289 PERL_SYS_INIT3 (&argc, &argv, &environ);
2291 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2292 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2294 pthread_mutexattr_init(&perl_threads->mutexattr);
2295 pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE);
2296 pthread_mutex_init (&perl_threads->mutex, &perl_threads->mutexattr);
2297 /* locking the mutex should not be necessary at this point
2298 * but let's just do it for the sake of completeness */
2299 pthread_mutex_lock (&perl_threads->mutex);
2301 perl_threads->head = c_ithread_create (NULL);
2302 perl_threads->tail = perl_threads->head;
2304 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2305 log_err ("init_pi: Not enough memory.");
2309 aTHX = perl_threads->head->interp;
2310 pthread_mutex_unlock (&perl_threads->mutex);
2312 perl_construct (aTHX);
2314 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2316 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2317 SV *err = get_sv ("@", 1);
2318 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2321 perl_destruct (perl_threads->head->interp);
2322 perl_free (perl_threads->head->interp);
2323 sfree (perl_threads);
2325 pthread_key_delete (perl_thr_key);
2329 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2330 sv_setpv (get_sv ("0", 0), "collectd");
2334 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2335 plugin_register_notification ("perl", perl_notify,
2336 /* user_data = */ NULL);
2337 plugin_register_init ("perl", perl_init);
2339 plugin_register_read ("perl", perl_read);
2341 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2342 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2343 plugin_register_shutdown ("perl", perl_shutdown);
2345 } /* static int init_pi (const char **, const int) */
2348 * LoadPlugin "<Plugin>"
2350 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2352 char module_name[DATA_MAX_NAME_LEN];
2356 if ((0 != ci->children_num) || (1 != ci->values_num)
2357 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2358 log_err ("LoadPlugin expects a single string argument.");
2362 value = ci->values[0].value.string;
2364 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2365 log_err ("Invalid module name %s", value);
2369 if (0 != init_pi (perl_argc, perl_argv))
2372 assert (NULL != perl_threads);
2373 assert (NULL != perl_threads->head);
2375 aTHX = perl_threads->head->interp;
2377 log_debug ("perl_config: Loading Perl plugin \"%s\"", value);
2378 load_module (PERL_LOADMOD_NOIMPORT,
2379 newSVpv (module_name, strlen (module_name)), Nullsv);
2381 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2386 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2390 if ((0 != ci->children_num) || (1 != ci->values_num)
2391 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2392 log_err ("BaseName expects a single string argument.");
2396 value = ci->values[0].value.string;
2398 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2399 sstrncpy (base_name, value, sizeof (base_name));
2401 } /* static int perl_config_basename (oconfig_item_it *) */
2404 * EnableDebugger "<Package>"|""
2406 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2410 if ((0 != ci->children_num) || (1 != ci->values_num)
2411 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2412 log_err ("EnableDebugger expects a single string argument.");
2416 if (NULL != perl_threads) {
2417 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2421 value = ci->values[0].value.string;
2423 perl_argv = (char **)realloc (perl_argv,
2424 (++perl_argc + 1) * sizeof (char *));
2426 if (NULL == perl_argv) {
2427 log_err ("perl_config: Not enough memory.");
2431 if ('\0' == value[0]) {
2432 perl_argv[perl_argc - 1] = "-d";
2435 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2436 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2437 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2440 perl_argv[perl_argc] = NULL;
2442 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2445 * IncludeDir "<Dir>"
2447 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2451 if ((0 != ci->children_num) || (1 != ci->values_num)
2452 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2453 log_err ("IncludeDir expects a single string argument.");
2457 value = ci->values[0].value.string;
2460 perl_argv = (char **)realloc (perl_argv,
2461 (++perl_argc + 1) * sizeof (char *));
2463 if (NULL == perl_argv) {
2464 log_err ("perl_config: Not enough memory.");
2468 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2469 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2470 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2472 perl_argv[perl_argc] = NULL;
2475 /* prepend the directory to @INC */
2476 av_unshift (GvAVn (PL_incgv), 1);
2477 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2480 } /* static int perl_config_includedir (oconfig_item_it *) */
2485 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2495 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2496 log_err ("LoadPlugin expects a single string argument.");
2500 plugin = ci->values[0].value.string;
2503 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2507 log_err ("Unable to convert configuration to a Perl hash value.");
2508 config = (HV *)&PL_sv_undef;
2516 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2517 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2521 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2536 } /* static int perl_config_plugin (oconfig_item_it *) */
2538 static int perl_config (oconfig_item_t *ci)
2545 for (i = 0; i < ci->children_num; ++i) {
2546 oconfig_item_t *c = ci->children + i;
2547 int current_status = 0;
2549 if (NULL != perl_threads)
2550 aTHX = PERL_GET_CONTEXT;
2552 if (0 == strcasecmp (c->key, "LoadPlugin"))
2553 current_status = perl_config_loadplugin (aTHX_ c);
2554 else if (0 == strcasecmp (c->key, "BaseName"))
2555 current_status = perl_config_basename (aTHX_ c);
2556 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2557 current_status = perl_config_enabledebugger (aTHX_ c);
2558 else if (0 == strcasecmp (c->key, "IncludeDir"))
2559 current_status = perl_config_includedir (aTHX_ c);
2560 else if (0 == strcasecmp (c->key, "Plugin"))
2561 current_status = perl_config_plugin (aTHX_ c);
2564 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2568 /* fatal error - it's up to perl_config_* to clean up */
2569 if (0 > current_status) {
2570 log_err ("Configuration failed with a fatal error - "
2571 "plugin disabled!");
2572 return current_status;
2575 status += current_status;
2578 } /* static int perl_config (oconfig_item_t *) */
2580 void module_register (void)
2583 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2585 /* default options for the Perl interpreter */
2587 perl_argv[1] = "-MCollectd";
2588 perl_argv[2] = "-e";
2590 perl_argv[4] = NULL;
2592 plugin_register_complex_config ("perl", perl_config);
2594 } /* void module_register (void) */
2596 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */