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"
43 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
44 # pragma GCC poison sprintf
49 /* Some versions of Perl define their own version of DEBUG... :-/ */
54 /* ... while we want the definition found in plugin.h. */
58 #include "filter_chain.h"
62 #if !defined(USE_ITHREADS)
63 # error "Perl does not support ithreads!"
64 #endif /* !defined(USE_ITHREADS) */
66 /* clear the Perl sub's stack frame
67 * (this should only be used inside an XSUB) */
68 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
72 #define PLUGIN_WRITE 2
73 #define PLUGIN_SHUTDOWN 3
75 #define PLUGIN_NOTIF 5
76 #define PLUGIN_FLUSH 6
78 #define PLUGIN_TYPES 7
80 #define PLUGIN_CONFIG 254
81 #define PLUGIN_DATASET 255
88 #define FC_CB_CREATE 0
89 #define FC_CB_DESTROY 1
94 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
95 #define log_info(...) INFO ("perl: " __VA_ARGS__)
96 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
97 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
99 /* this is defined in DynaLoader.a */
100 void boot_DynaLoader (PerlInterpreter *, CV *);
102 static XS (Collectd_plugin_register_ds);
103 static XS (Collectd_plugin_unregister_ds);
104 static XS (Collectd_plugin_dispatch_values);
105 static XS (Collectd_plugin_get_interval);
106 static XS (Collectd__plugin_write);
107 static XS (Collectd__plugin_flush);
108 static XS (Collectd_plugin_dispatch_notification);
109 static XS (Collectd_plugin_log);
110 static XS (Collectd__fc_register);
111 static XS (Collectd_call_by_name);
117 typedef struct c_ithread_s {
118 /* the thread's Perl interpreter */
119 PerlInterpreter *interp;
120 _Bool running; /* thread is inside pi */
124 /* double linked list of threads */
125 struct c_ithread_s *prev;
126 struct c_ithread_s *next;
134 /* some usage stats */
135 int number_of_threads;
136 #endif /* COLLECT_DEBUG */
138 pthread_mutex_t mutex;
141 /* name / user_data for Perl matches / targets */
147 #define PFC_USER_DATA_FREE(data) \
149 sfree ((data)->name); \
150 if (NULL != (data)->user_data) \
151 sv_free ((data)->user_data); \
158 extern char **environ;
164 /* if perl_threads != NULL perl_threads->head must
165 * point to the "base" thread */
166 static c_ithread_list_t *perl_threads = NULL;
168 /* the key used to store each pthread's ithread */
169 static pthread_key_t perl_thr_key;
171 static int perl_argc = 0;
172 static char **perl_argv = NULL;
174 static char base_name[DATA_MAX_NAME_LEN] = "";
181 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
182 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
183 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
184 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
185 { "Collectd::_plugin_write", Collectd__plugin_write },
186 { "Collectd::_plugin_flush", Collectd__plugin_flush },
187 { "Collectd::plugin_dispatch_notification",
188 Collectd_plugin_dispatch_notification },
189 { "Collectd::plugin_log", Collectd_plugin_log },
190 { "Collectd::_fc_register", Collectd__fc_register },
191 { "Collectd::call_by_name", Collectd_call_by_name },
200 { "Collectd::TYPE_INIT", PLUGIN_INIT },
201 { "Collectd::TYPE_READ", PLUGIN_READ },
202 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
203 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
204 { "Collectd::TYPE_LOG", PLUGIN_LOG },
205 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
206 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
207 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
208 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
209 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
210 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
211 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
212 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
213 { "Collectd::LOG_ERR", LOG_ERR },
214 { "Collectd::LOG_WARNING", LOG_WARNING },
215 { "Collectd::LOG_NOTICE", LOG_NOTICE },
216 { "Collectd::LOG_INFO", LOG_INFO },
217 { "Collectd::LOG_DEBUG", LOG_DEBUG },
218 { "Collectd::FC_MATCH", FC_MATCH },
219 { "Collectd::FC_TARGET", FC_TARGET },
220 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
221 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
222 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
223 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
224 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
225 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
226 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
227 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
228 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
229 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
230 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
239 { "Collectd::hostname_g", hostname_g },
244 * Helper functions for data type conversion.
259 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
263 if ((NULL == hash) || (NULL == ds))
266 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
267 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
270 log_err ("hv2data_source: No DS name given.");
274 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
275 ds->type = SvIV (*tmp);
277 if ((DS_TYPE_COUNTER != ds->type)
278 && (DS_TYPE_GAUGE != ds->type)
279 && (DS_TYPE_DERIVE != ds->type)
280 && (DS_TYPE_ABSOLUTE != ds->type)) {
281 log_err ("hv2data_source: Invalid DS type.");
286 ds->type = DS_TYPE_COUNTER;
289 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
290 ds->min = SvNV (*tmp);
294 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
295 ds->max = SvNV (*tmp);
299 } /* static int hv2data_source (HV *, data_source_t *) */
301 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
303 const data_set_t *ds;
307 if ((NULL == name) || (NULL == array) || (NULL == value))
310 if (av_len (array) < len - 1)
311 len = av_len (array) + 1;
316 ds = plugin_get_ds (name);
318 log_err ("av2value: Unknown dataset \"%s\"", name);
322 if (ds->ds_num < len) {
323 log_warn ("av2value: Value length exceeds data set length.");
327 for (i = 0; i < len; ++i) {
328 SV **tmp = av_fetch (array, i, 0);
331 if (DS_TYPE_COUNTER == ds->ds[i].type)
332 value[i].counter = SvIV (*tmp);
333 else if (DS_TYPE_GAUGE == ds->ds[i].type)
334 value[i].gauge = SvNV (*tmp);
335 else if (DS_TYPE_DERIVE == ds->ds[i].type)
336 value[i].derive = SvIV (*tmp);
337 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
338 value[i].absolute = SvIV (*tmp);
345 } /* static int av2value (char *, AV *, value_t *, int) */
350 * values => [ @values ],
354 * plugin_instance => $pinstance,
355 * type_instance => $tinstance,
358 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
362 if ((NULL == hash) || (NULL == vl))
365 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
366 log_err ("hv2value_list: No type given.");
370 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
372 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
373 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
374 log_err ("hv2value_list: No valid values given.");
379 AV *array = (AV *)SvRV (*tmp);
380 int len = av_len (array) + 1;
385 vl->values = (value_t *)smalloc (len * sizeof (value_t));
386 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
389 if (-1 == vl->values_len) {
395 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
397 double t = SvNV (*tmp);
398 vl->time = DOUBLE_TO_CDTIME_T (t);
401 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
403 double t = SvNV (*tmp);
404 vl->interval = DOUBLE_TO_CDTIME_T (t);
407 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
408 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
410 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
412 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
413 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
415 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
416 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
417 sizeof (vl->plugin_instance));
419 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
420 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
421 sizeof (vl->type_instance));
423 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
425 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
429 if ((NULL == array) || (NULL == name) || (NULL == ds))
432 len = av_len (array);
435 log_err ("av2data_set: Invalid data set.");
439 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
440 ds->ds_num = len + 1;
442 for (i = 0; i <= len; ++i) {
443 SV **elem = av_fetch (array, i, 0);
446 log_err ("av2data_set: Failed to fetch data source %i.", i);
450 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
451 log_err ("av2data_set: Invalid data source.");
455 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
458 log_debug ("av2data_set: "
459 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
460 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
463 sstrncpy (ds->type, name, sizeof (ds->type));
465 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
470 * severity => $severity,
476 * plugin_instance => $instance,
477 * type_instance => $type_instance,
478 * meta => [ { name => <name>, value => <value> }, ... ]
481 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
483 notification_meta_t **m = meta;
485 int len = av_len (array);
488 for (i = 0; i <= len; ++i) {
489 SV **tmp = av_fetch (array, i, 0);
495 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
496 log_warn ("av2notification_meta: Skipping invalid "
497 "meta information.");
501 hash = (HV *)SvRV (*tmp);
503 *m = (notification_meta_t *)smalloc (sizeof (**m));
505 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
506 log_warn ("av2notification_meta: Skipping invalid "
507 "meta information.");
511 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
513 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
514 log_warn ("av2notification_meta: Skipping invalid "
515 "meta information.");
521 (*m)->nm_value.nm_double = SvNVX (*tmp);
522 (*m)->type = NM_TYPE_DOUBLE;
524 else if (SvUOK (*tmp)) {
525 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
526 (*m)->type = NM_TYPE_UNSIGNED_INT;
528 else if (SvIOK (*tmp)) {
529 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
530 (*m)->type = NM_TYPE_SIGNED_INT;
533 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
534 (*m)->type = NM_TYPE_STRING;
541 } /* static int av2notification_meta (AV *, notification_meta_t *) */
543 static int hv2notification (pTHX_ HV *hash, notification_t *n)
547 if ((NULL == hash) || (NULL == n))
550 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
551 n->severity = SvIV (*tmp);
553 n->severity = NOTIF_FAILURE;
555 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
557 double t = SvNV (*tmp);
558 n->time = DOUBLE_TO_CDTIME_T (t);
563 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
564 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
566 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
567 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
569 sstrncpy (n->host, hostname_g, sizeof (n->host));
571 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
572 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
574 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
575 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
576 sizeof (n->plugin_instance));
578 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
579 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
581 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
582 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
583 sizeof (n->type_instance));
586 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
587 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
588 log_warn ("hv2notification: Ignoring invalid meta information.");
592 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
593 plugin_notification_meta_free (n->meta);
600 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
602 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
606 if ((NULL == ds) || (NULL == array))
609 av_extend (array, ds->ds_num);
611 for (i = 0; i < ds->ds_num; ++i) {
612 HV *source = newHV ();
614 if (NULL == hv_store (source, "name", 4,
615 newSVpv (ds->ds[i].name, 0), 0))
618 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
621 if (! isnan (ds->ds[i].min))
622 if (NULL == hv_store (source, "min", 3,
623 newSVnv (ds->ds[i].min), 0))
626 if (! isnan (ds->ds[i].max))
627 if (NULL == hv_store (source, "max", 3,
628 newSVnv (ds->ds[i].max), 0))
631 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
635 } /* static int data_set2av (data_set_t *, AV *) */
637 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
644 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
647 len = vl->values_len;
649 if (ds->ds_num < len) {
650 log_warn ("value2av: Value length exceeds data set length.");
655 av_extend (values, len - 1);
657 for (i = 0; i < len; ++i) {
660 if (DS_TYPE_COUNTER == ds->ds[i].type)
661 val = newSViv (vl->values[i].counter);
662 else if (DS_TYPE_GAUGE == ds->ds[i].type)
663 val = newSVnv (vl->values[i].gauge);
664 else if (DS_TYPE_DERIVE == ds->ds[i].type)
665 val = newSViv (vl->values[i].derive);
666 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
667 val = newSViv (vl->values[i].absolute);
669 if (NULL == av_store (values, i, val)) {
675 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
680 double t = CDTIME_T_TO_DOUBLE (vl->time);
681 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
686 double t = CDTIME_T_TO_DOUBLE (vl->interval);
687 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
691 if ('\0' != vl->host[0])
692 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
695 if ('\0' != vl->plugin[0])
696 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
699 if ('\0' != vl->plugin_instance[0])
700 if (NULL == hv_store (hash, "plugin_instance", 15,
701 newSVpv (vl->plugin_instance, 0), 0))
704 if ('\0' != vl->type[0])
705 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
708 if ('\0' != vl->type_instance[0])
709 if (NULL == hv_store (hash, "type_instance", 13,
710 newSVpv (vl->type_instance, 0), 0))
713 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
715 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
725 av_extend (array, meta_num);
727 for (i = 0; NULL != meta; meta = meta->next, ++i) {
731 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
734 if (NM_TYPE_STRING == meta->type)
735 value = newSVpv (meta->nm_value.nm_string, 0);
736 else if (NM_TYPE_SIGNED_INT == meta->type)
737 value = newSViv (meta->nm_value.nm_signed_int);
738 else if (NM_TYPE_UNSIGNED_INT == meta->type)
739 value = newSVuv (meta->nm_value.nm_unsigned_int);
740 else if (NM_TYPE_DOUBLE == meta->type)
741 value = newSVnv (meta->nm_value.nm_double);
742 else if (NM_TYPE_BOOLEAN == meta->type)
743 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
747 if (NULL == hv_store (m, "value", 5, value, 0)) {
752 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
759 } /* static int notification_meta2av (notification_meta_t *, AV *) */
761 static int notification2hv (pTHX_ notification_t *n, HV *hash)
763 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
768 double t = CDTIME_T_TO_DOUBLE (n->time);
769 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
773 if ('\0' != *n->message)
774 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
777 if ('\0' != *n->host)
778 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
781 if ('\0' != *n->plugin)
782 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
785 if ('\0' != *n->plugin_instance)
786 if (NULL == hv_store (hash, "plugin_instance", 15,
787 newSVpv (n->plugin_instance, 0), 0))
790 if ('\0' != *n->type)
791 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
794 if ('\0' != *n->type_instance)
795 if (NULL == hv_store (hash, "type_instance", 13,
796 newSVpv (n->type_instance, 0), 0))
799 if (NULL != n->meta) {
801 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
802 || (NULL == hv_store (hash, "meta", 4,
803 newRV_noinc ((SV *)meta), 0))) {
810 } /* static int notification2hv (notification_t *, HV *) */
812 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
819 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
823 if (0 < ci->values_num)
824 av_extend (values, ci->values_num);
826 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
832 for (i = 0; i < ci->values_num; ++i) {
835 switch (ci->values[i].type) {
836 case OCONFIG_TYPE_STRING:
837 value = newSVpv (ci->values[i].value.string, 0);
839 case OCONFIG_TYPE_NUMBER:
840 value = newSVnv ((NV)ci->values[i].value.number);
842 case OCONFIG_TYPE_BOOLEAN:
843 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
846 log_err ("oconfig_item2hv: Invalid value type %i.",
848 value = &PL_sv_undef;
851 if (NULL == av_store (values, i, value)) {
857 /* ignoring 'parent' member which is uninteresting in this case */
860 if (0 < ci->children_num)
861 av_extend (children, ci->children_num);
863 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
869 for (i = 0; i < ci->children_num; ++i) {
870 HV *child = newHV ();
872 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
878 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
885 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
888 * Internal functions.
891 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
893 if (base_name[0] == '\0')
894 status = ssnprintf (buf, buf_len, "%s", module);
896 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
897 if ((status < 0) || ((unsigned int)status >= buf_len))
900 } /* char *get_module_name */
903 * Add a plugin's data set definition.
905 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
911 if ((NULL == name) || (NULL == dataset))
914 if (0 != av2data_set (aTHX_ dataset, name, &ds))
917 ret = plugin_register_data_set (&ds);
921 } /* static int pplugin_register_data_set (char *, SV *) */
924 * Remove a plugin's data set definition.
926 static int pplugin_unregister_data_set (char *name)
930 return plugin_unregister_data_set (name);
931 } /* static int pplugin_unregister_data_set (char *) */
934 * Submit the values to the write functions.
936 static int pplugin_dispatch_values (pTHX_ HV *values)
938 value_list_t vl = VALUE_LIST_INIT;
945 if (0 != hv2value_list (aTHX_ values, &vl))
948 ret = plugin_dispatch_values (&vl);
952 } /* static int pplugin_dispatch_values (char *, HV *) */
955 * Submit the values to a single write function.
957 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
960 value_list_t vl = VALUE_LIST_INIT;
967 if (0 != hv2value_list (aTHX_ values, &vl))
970 if ((NULL != data_set)
971 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
974 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
976 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
977 NULL == plugin ? "<any>" : plugin, ret);
979 if (NULL != data_set)
983 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
986 * Dispatch a notification.
988 static int pplugin_dispatch_notification (pTHX_ HV *notif)
997 memset (&n, 0, sizeof (n));
999 if (0 != hv2notification (aTHX_ notif, &n))
1002 ret = plugin_dispatch_notification (&n);
1003 plugin_notification_meta_free (n.meta);
1005 } /* static int pplugin_dispatch_notification (HV *) */
1008 * Call all working functions of the given type.
1010 static int pplugin_call_all (pTHX_ int type, ...)
1020 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1021 if (t == NULL) /* thread destroyed ( c_ithread_destroy*() -> log_debug() ) */
1024 old_running = t->running;
1028 t->running = old_running;
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) {
1139 * $_[1] = $identifier;
1141 timeout = va_arg (ap, cdtime_t);
1143 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1144 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1149 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1162 t->running = old_running;
1165 } /* static int pplugin_call_all (int, ...) */
1168 * collectd's perl interpreter based thread implementation.
1170 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1173 /* must be called with perl_threads->mutex locked */
1174 static void c_ithread_destroy (c_ithread_t *ithread)
1176 dTHXa (ithread->interp);
1178 assert (NULL != perl_threads);
1180 PERL_SET_CONTEXT (aTHX);
1181 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1186 --perl_threads->number_of_threads;
1187 #endif /* COLLECT_DEBUG */
1189 perl_destruct (aTHX);
1192 if (NULL == ithread->prev)
1193 perl_threads->head = ithread->next;
1195 ithread->prev->next = ithread->next;
1197 if (NULL == ithread->next)
1198 perl_threads->tail = ithread->prev;
1200 ithread->next->prev = ithread->prev;
1204 } /* static void c_ithread_destroy (c_ithread_t *) */
1206 static void c_ithread_destructor (void *arg)
1208 c_ithread_t *ithread = (c_ithread_t *)arg;
1209 c_ithread_t *t = NULL;
1211 if (NULL == perl_threads)
1214 pthread_mutex_lock (&perl_threads->mutex);
1216 for (t = perl_threads->head; NULL != t; t = t->next)
1220 /* the ithread no longer exists */
1223 pthread_mutex_unlock (&perl_threads->mutex);
1227 c_ithread_destroy (ithread);
1229 pthread_mutex_unlock (&perl_threads->mutex);
1231 } /* static void c_ithread_destructor (void *) */
1233 /* must be called with perl_threads->mutex locked */
1234 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1236 c_ithread_t *t = NULL;
1239 assert (NULL != perl_threads);
1241 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1242 memset (t, 0, sizeof (c_ithread_t));
1244 t->interp = (NULL == base)
1246 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1250 if ((NULL != base) && (NULL != PL_endav)) {
1251 av_clear (PL_endav);
1252 av_undef (PL_endav);
1257 ++perl_threads->number_of_threads;
1258 #endif /* COLLECT_DEBUG */
1262 if (NULL == perl_threads->tail) {
1263 perl_threads->head = t;
1267 perl_threads->tail->next = t;
1268 t->prev = perl_threads->tail;
1271 t->pthread = pthread_self();
1274 perl_threads->tail = t;
1276 pthread_setspecific (perl_thr_key, (const void *)t);
1278 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1281 * Filter chains implementation.
1284 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1292 notification_meta_t **meta = NULL;
1297 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1298 if (t == NULL) /* thread destroyed */
1301 old_running = t->running;
1305 t->running = old_running;
1309 if ((type < 0) || (type >= FC_TYPES))
1312 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1315 va_start (ap, data);
1322 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1323 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1324 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1326 if (FC_CB_CREATE == cb_type) {
1329 * $_[1] = $user_data;
1332 HV *config = newHV ();
1334 ci = va_arg (ap, oconfig_item_t *);
1336 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1339 config = (HV *)&PL_sv_undef;
1343 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1345 else if (FC_CB_DESTROY == cb_type) {
1347 * $_[1] = $user_data;
1350 /* nothing to be done - the user data pointer
1351 * is pushed onto the stack later */
1353 else if (FC_CB_EXEC == cb_type) {
1358 * $_[3] = $user_data;
1366 ds = va_arg (ap, data_set_t *);
1367 vl = va_arg (ap, value_list_t *);
1368 meta = va_arg (ap, notification_meta_t **);
1370 if (0 != data_set2av (aTHX_ ds, pds)) {
1373 pds = (AV *)&PL_sv_undef;
1377 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1380 pvl = (HV *)&PL_sv_undef;
1387 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1390 pmeta = (AV *)&PL_sv_undef;
1395 pmeta = (AV *)&PL_sv_undef;
1398 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1399 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1400 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1403 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1407 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1409 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1410 assert (pmeta != NULL);
1412 plugin_notification_meta_free (*meta);
1413 av2notification_meta (aTHX_ pmeta, meta);
1420 /* the exec callbacks return a status, while
1421 * the others return a boolean value */
1422 if (FC_CB_EXEC == cb_type)
1424 else if (! SvTRUE (tmp))
1432 t->running = old_running;
1435 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1437 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1439 pfc_user_data_t *data;
1445 if (NULL == perl_threads)
1449 c_ithread_t *t = NULL;
1451 pthread_mutex_lock (&perl_threads->mutex);
1452 t = c_ithread_create (perl_threads->head->interp);
1453 pthread_mutex_unlock (&perl_threads->mutex);
1458 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1459 aTHX, perl_threads->number_of_threads);
1461 if ((1 != ci->values_num)
1462 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1463 log_warn ("A \"%s\" block expects a single string argument.",
1464 (FC_MATCH == type) ? "Match" : "Target");
1468 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1469 data->name = sstrdup (ci->values[0].value.string);
1470 data->user_data = newSV (0);
1472 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1475 PFC_USER_DATA_FREE (data);
1479 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1481 static int fc_destroy (int type, void **user_data)
1483 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1489 if ((NULL == perl_threads) || (NULL == data))
1493 c_ithread_t *t = NULL;
1495 pthread_mutex_lock (&perl_threads->mutex);
1496 t = c_ithread_create (perl_threads->head->interp);
1497 pthread_mutex_unlock (&perl_threads->mutex);
1502 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1503 aTHX, perl_threads->number_of_threads);
1505 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1507 PFC_USER_DATA_FREE (data);
1510 } /* static int fc_destroy (int, void **) */
1512 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1513 notification_meta_t **meta, void **user_data)
1515 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1519 if (NULL == perl_threads)
1522 assert (NULL != data);
1525 c_ithread_t *t = NULL;
1527 pthread_mutex_lock (&perl_threads->mutex);
1528 t = c_ithread_create (perl_threads->head->interp);
1529 pthread_mutex_unlock (&perl_threads->mutex);
1534 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1535 aTHX, perl_threads->number_of_threads);
1537 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1538 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1539 notification_meta_t **, void **) */
1541 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1543 return fc_create (FC_MATCH, ci, user_data);
1544 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1546 static int pmatch_destroy (void **user_data)
1548 return fc_destroy (FC_MATCH, user_data);
1549 } /* static int pmatch_destroy (void **) */
1551 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1552 notification_meta_t **meta, void **user_data)
1554 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1555 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1556 notification_meta_t **, void **) */
1558 static match_proc_t pmatch = {
1559 pmatch_create, pmatch_destroy, pmatch_match
1562 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1564 return fc_create (FC_TARGET, ci, user_data);
1565 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1567 static int ptarget_destroy (void **user_data)
1569 return fc_destroy (FC_TARGET, user_data);
1570 } /* static int ptarget_destroy (void **) */
1572 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1573 notification_meta_t **meta, void **user_data)
1575 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1576 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1577 notification_meta_t **, void **) */
1579 static target_proc_t ptarget = {
1580 ptarget_create, ptarget_destroy, ptarget_invoke
1584 * Exported Perl API.
1588 * Collectd::plugin_register_data_set (type, dataset).
1591 * type of the dataset
1594 * dataset to be registered
1596 static XS (Collectd_plugin_register_ds)
1603 log_warn ("Using plugin_register() to register new data-sets is "
1604 "deprecated - add new entries to a custom types.db instead.");
1607 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1611 log_debug ("Collectd::plugin_register_data_set: "
1612 "type = \"%s\", dataset = \"%s\"",
1613 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1617 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1618 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1622 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1630 } /* static XS (Collectd_plugin_register_ds) */
1633 * Collectd::plugin_unregister_data_set (type).
1636 * type of the dataset
1638 static XS (Collectd_plugin_unregister_ds)
1643 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1647 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1648 SvPV_nolen (ST (0)));
1650 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1654 } /* static XS (Collectd_plugin_register_ds) */
1657 * Collectd::plugin_dispatch_values (name, values).
1660 * name of the plugin
1663 * value list to submit
1665 static XS (Collectd_plugin_dispatch_values)
1674 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1678 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1679 SvPV_nolen (ST (/* stack index = */ 0)));
1681 values = ST (/* stack index = */ 0);
1686 /* Make sure the argument is a hash reference. */
1687 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1688 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1692 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1698 } /* static XS (Collectd_plugin_dispatch_values) */
1701 * Collectd::plugin_get_interval ().
1703 static XS (Collectd_plugin_get_interval)
1707 /* make sure we don't get any unused variable warnings for 'items';
1708 * don't abort, though */
1710 log_err ("Usage: Collectd::plugin_get_interval()");
1712 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1713 } /* static XS (Collectd_plugin_get_interval) */
1715 /* Collectd::plugin_write (plugin, ds, vl).
1718 * name of the plugin to call, may be 'undef'
1721 * data-set that describes the submitted values, may be 'undef'
1724 * value-list to be written
1726 static XS (Collectd__plugin_write)
1737 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1741 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1742 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1743 SvPV_nolen (ST (2)));
1745 if (! SvOK (ST (0)))
1748 plugin = SvPV_nolen (ST (0));
1751 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1752 ds_array = (AV *)SvRV (ds);
1753 else if (! SvOK (ds))
1756 log_err ("Collectd::plugin_write: Invalid data-set.");
1761 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1762 log_err ("Collectd::plugin_write: Invalid value-list.");
1766 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1772 } /* static XS (Collectd__plugin_write) */
1775 * Collectd::_plugin_flush (plugin, timeout, identifier).
1778 * name of the plugin to flush
1781 * timeout to use when flushing the data
1784 * data-set identifier to flush
1786 static XS (Collectd__plugin_flush)
1788 char *plugin = NULL;
1795 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1800 plugin = SvPV_nolen (ST (0));
1803 timeout = (int)SvIV (ST (1));
1806 id = SvPV_nolen (ST (2));
1808 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1809 "id = \"%s\"", plugin, timeout, id);
1811 if (0 == plugin_flush (plugin, timeout, id))
1815 } /* static XS (Collectd__plugin_flush) */
1818 * Collectd::plugin_dispatch_notification (notif).
1821 * notification to dispatch
1823 static XS (Collectd_plugin_dispatch_notification)
1832 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1836 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1837 SvPV_nolen (ST (0)));
1841 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1842 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1846 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1852 } /* static XS (Collectd_plugin_dispatch_notification) */
1855 * Collectd::plugin_log (level, message).
1858 * log level (LOG_DEBUG, ... LOG_ERR)
1863 static XS (Collectd_plugin_log)
1868 log_err ("Usage: Collectd::plugin_log(level, message)");
1872 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1874 } /* static XS (Collectd_plugin_log) */
1877 * Collectd::_fc_register (type, name)
1885 static XS (Collectd__fc_register)
1895 log_err ("Usage: Collectd::_fc_register(type, name)");
1899 type = SvIV (ST (0));
1900 name = SvPV_nolen (ST (1));
1902 if (FC_MATCH == type)
1903 ret = fc_register_match (name, pmatch);
1904 else if (FC_TARGET == type)
1905 ret = fc_register_target (name, ptarget);
1911 } /* static XS (Collectd_fc_register) */
1914 * Collectd::call_by_name (...).
1916 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1918 static XS (Collectd_call_by_name)
1923 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1924 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1929 name = SvPV_nolen (tmp);
1931 if (NULL == get_cv (name, 0)) {
1932 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1937 /* simply pass on the subroutine call without touching the stack,
1938 * thus leaving any arguments and return values in place */
1940 } /* static XS (Collectd_call_by_name) */
1943 * Interface to collectd.
1946 static int perl_init (void)
1951 if (NULL == perl_threads)
1955 c_ithread_t *t = NULL;
1957 pthread_mutex_lock (&perl_threads->mutex);
1958 t = c_ithread_create (perl_threads->head->interp);
1959 pthread_mutex_unlock (&perl_threads->mutex);
1964 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1965 aTHX, perl_threads->number_of_threads);
1967 /* Lock the base thread to avoid race conditions with c_ithread_create().
1968 * See https://github.com/collectd/collectd/issues/9 and
1969 * https://github.com/collectd/collectd/issues/1706 for details.
1970 * Locking here requires additional check in perl_log() to avoid deadlock.
1972 assert (aTHX == perl_threads->head->interp);
1973 pthread_mutex_lock (&perl_threads->mutex);
1975 status = pplugin_call_all (aTHX_ PLUGIN_INIT);
1977 pthread_mutex_unlock (&perl_threads->mutex);
1980 } /* static int perl_init (void) */
1982 static int perl_read (void)
1986 if (NULL == perl_threads)
1990 c_ithread_t *t = NULL;
1992 pthread_mutex_lock (&perl_threads->mutex);
1993 t = c_ithread_create (perl_threads->head->interp);
1994 pthread_mutex_unlock (&perl_threads->mutex);
1999 /* Assert that we're not running as the base thread. Otherwise, we might
2000 * run into concurrency issues with c_ithread_create(). See
2001 * https://github.com/collectd/collectd/issues/9 for details. */
2002 assert (aTHX != perl_threads->head->interp);
2004 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
2005 aTHX, perl_threads->number_of_threads);
2006 return pplugin_call_all (aTHX_ PLUGIN_READ);
2007 } /* static int perl_read (void) */
2009 static int perl_write (const data_set_t *ds, const value_list_t *vl,
2010 user_data_t __attribute__((unused)) *user_data)
2015 if (NULL == perl_threads)
2019 c_ithread_t *t = NULL;
2021 pthread_mutex_lock (&perl_threads->mutex);
2022 t = c_ithread_create (perl_threads->head->interp);
2023 pthread_mutex_unlock (&perl_threads->mutex);
2028 /* Lock the base thread if this is not called from one of the read threads
2029 * to avoid race conditions with c_ithread_create(). See
2030 * https://github.com/collectd/collectd/issues/9 for details. */
2031 if (aTHX == perl_threads->head->interp)
2032 pthread_mutex_lock (&perl_threads->mutex);
2034 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
2035 aTHX, perl_threads->number_of_threads);
2036 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
2038 if (aTHX == perl_threads->head->interp)
2039 pthread_mutex_unlock (&perl_threads->mutex);
2042 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2044 static void perl_log (int level, const char *msg,
2045 user_data_t __attribute__((unused)) *user_data)
2050 if (NULL == perl_threads)
2054 c_ithread_t *t = NULL;
2056 pthread_mutex_lock (&perl_threads->mutex);
2057 t = c_ithread_create (perl_threads->head->interp);
2058 pthread_mutex_unlock (&perl_threads->mutex);
2063 /* Lock the base thread if this is not called from one of the read threads
2064 * to avoid race conditions with c_ithread_create(). See
2065 * https://github.com/collectd/collectd/issues/9 for details.
2066 * Additionally check, if we are called from perl interpreter.
2067 * Maybe PTHREAD_MUTEX_RECURSIVE mutex type will be more appropriate?
2070 if (aTHX == perl_threads->head->interp && !perl_threads->head->running) {
2071 pthread_mutex_lock (&perl_threads->mutex);
2075 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2078 pthread_mutex_unlock (&perl_threads->mutex);
2081 } /* static void perl_log (int, const char *) */
2083 static int perl_notify (const notification_t *notif,
2084 user_data_t __attribute__((unused)) *user_data)
2088 if (NULL == perl_threads)
2092 c_ithread_t *t = NULL;
2094 pthread_mutex_lock (&perl_threads->mutex);
2095 t = c_ithread_create (perl_threads->head->interp);
2096 pthread_mutex_unlock (&perl_threads->mutex);
2100 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2101 } /* static int perl_notify (const notification_t *) */
2103 static int perl_flush (cdtime_t timeout, const char *identifier,
2104 user_data_t __attribute__((unused)) *user_data)
2108 if (NULL == perl_threads)
2112 c_ithread_t *t = NULL;
2114 pthread_mutex_lock (&perl_threads->mutex);
2115 t = c_ithread_create (perl_threads->head->interp);
2116 pthread_mutex_unlock (&perl_threads->mutex);
2120 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2121 } /* static int perl_flush (const int) */
2123 static int perl_shutdown (void)
2125 c_ithread_t *t = NULL;
2131 plugin_unregister_complex_config ("perl");
2133 if (NULL == perl_threads)
2139 pthread_mutex_lock (&perl_threads->mutex);
2140 t = c_ithread_create (perl_threads->head->interp);
2141 pthread_mutex_unlock (&perl_threads->mutex);
2146 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2147 aTHX, perl_threads->number_of_threads);
2149 plugin_unregister_log ("perl");
2150 plugin_unregister_notification ("perl");
2151 plugin_unregister_init ("perl");
2152 plugin_unregister_read ("perl");
2153 plugin_unregister_write ("perl");
2154 plugin_unregister_flush ("perl");
2156 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2158 pthread_mutex_lock (&perl_threads->mutex);
2159 t = perl_threads->tail;
2162 struct timespec ts_wait;
2163 c_ithread_t *thr = t;
2165 /* the pointer has to be advanced before destroying
2166 * the thread as this will free the memory */
2171 /* Give some time to thread to exit from pi */
2172 WARNING ("perl shutdown: thread is running inside perl. Waiting.");
2174 ts_wait.tv_nsec = 500000;
2175 nanosleep (&ts_wait, NULL);
2178 /* This will crash collectd process later due to PERL_SYS_TERM() */
2179 //ERROR ("perl shutdown: thread hangs inside perl. "
2180 // "Skipped perl interpreter destroy.");
2183 ERROR ("perl shutdown: thread hangs inside perl. Thread killed.");
2184 pthread_kill (thr->pthread, SIGTERM);
2186 c_ithread_destroy (thr);
2189 pthread_mutex_unlock (&perl_threads->mutex);
2190 pthread_mutex_destroy (&perl_threads->mutex);
2192 sfree (perl_threads);
2194 pthread_key_delete (perl_thr_key);
2198 plugin_unregister_shutdown ("perl");
2200 } /* static void perl_shutdown (void) */
2203 * Access functions for global variables.
2205 * These functions implement the "magic" used to access
2206 * the global variables from Perl.
2209 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2211 char *pv = mg->mg_ptr;
2214 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2216 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2218 char *pv = mg->mg_ptr;
2219 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2221 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2223 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2225 log_warn ("Accessing $interval_g is deprecated (and might not "
2226 "give the desired results) - plugin_get_interval() should "
2227 "be used instead.");
2228 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2230 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2232 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2234 double nv = (double)SvNV (var);
2235 log_warn ("Accessing $interval_g is deprecated (and might not "
2236 "give the desired results) - plugin_get_interval() should "
2237 "be used instead.");
2238 interval_g = DOUBLE_TO_CDTIME_T (nv);
2240 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2242 static MGVTBL g_pv_vtbl = {
2243 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2244 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2248 static MGVTBL g_interval_vtbl = {
2249 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2250 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2255 /* bootstrap the Collectd module */
2256 static void xs_init (pTHX)
2260 char *file = __FILE__;
2266 /* enable usage of Perl modules using shared libraries */
2267 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2270 for (i = 0; NULL != api[i].f; ++i)
2271 newXS (api[i].name, api[i].f, file);
2273 stash = gv_stashpv ("Collectd", 1);
2275 /* export "constants" */
2276 for (i = 0; '\0' != constants[i].name[0]; ++i)
2277 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2279 /* export global variables
2280 * by adding "magic" to the SV's representing the globale variables
2281 * perl is able to automagically call the get/set function when
2282 * accessing any such variable (this is basically the same as using
2284 /* global strings */
2285 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2286 tmp = get_sv (g_strings[i].name, 1);
2287 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2288 g_strings[i].var, 0);
2291 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2292 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2293 /* vtbl = */ &g_interval_vtbl,
2294 /* name = */ NULL, /* namelen = */ 0);
2297 } /* static void xs_init (pTHX) */
2299 /* Initialize the global Perl interpreter. */
2300 static int init_pi (int argc, char **argv)
2304 if (NULL != perl_threads)
2307 log_info ("Initializing Perl interpreter...");
2312 for (i = 0; i < argc; ++i)
2313 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2315 #endif /* COLLECT_DEBUG */
2317 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2318 log_err ("init_pi: pthread_key_create failed");
2320 /* this must not happen - cowardly giving up if it does */
2325 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2326 * triggers a "value computed is not used" warning by gcc. */
2329 PERL_SYS_INIT3 (&argc, &argv, &environ);
2331 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2332 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2334 pthread_mutex_init (&perl_threads->mutex, NULL);
2335 /* locking the mutex should not be necessary at this point
2336 * but let's just do it for the sake of completeness */
2337 pthread_mutex_lock (&perl_threads->mutex);
2339 perl_threads->head = c_ithread_create (NULL);
2340 perl_threads->tail = perl_threads->head;
2342 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2343 log_err ("init_pi: Not enough memory.");
2347 aTHX = perl_threads->head->interp;
2348 pthread_mutex_unlock (&perl_threads->mutex);
2350 perl_construct (aTHX);
2352 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2354 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2355 SV *err = get_sv ("@", 1);
2356 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2359 perl_destruct (perl_threads->head->interp);
2360 perl_free (perl_threads->head->interp);
2361 sfree (perl_threads);
2363 pthread_key_delete (perl_thr_key);
2367 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2368 sv_setpv (get_sv ("0", 0), "collectd");
2372 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2373 plugin_register_notification ("perl", perl_notify,
2374 /* user_data = */ NULL);
2375 plugin_register_init ("perl", perl_init);
2377 plugin_register_read ("perl", perl_read);
2379 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2380 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2381 plugin_register_shutdown ("perl", perl_shutdown);
2383 } /* static int init_pi (const char **, const int) */
2386 * LoadPlugin "<Plugin>"
2388 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2390 char module_name[DATA_MAX_NAME_LEN];
2394 if ((0 != ci->children_num) || (1 != ci->values_num)
2395 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2396 log_err ("LoadPlugin expects a single string argument.");
2400 value = ci->values[0].value.string;
2402 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2403 log_err ("Invalid module name %s", value);
2407 if (0 != init_pi (perl_argc, perl_argv))
2410 assert (NULL != perl_threads);
2411 assert (NULL != perl_threads->head);
2413 aTHX = perl_threads->head->interp;
2415 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2416 load_module (PERL_LOADMOD_NOIMPORT,
2417 newSVpv (module_name, strlen (module_name)), Nullsv);
2419 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2424 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2428 if ((0 != ci->children_num) || (1 != ci->values_num)
2429 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2430 log_err ("BaseName expects a single string argument.");
2434 value = ci->values[0].value.string;
2436 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2437 sstrncpy (base_name, value, sizeof (base_name));
2439 } /* static int perl_config_basename (oconfig_item_it *) */
2442 * EnableDebugger "<Package>"|""
2444 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2448 if ((0 != ci->children_num) || (1 != ci->values_num)
2449 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2450 log_err ("EnableDebugger expects a single string argument.");
2454 if (NULL != perl_threads) {
2455 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2459 value = ci->values[0].value.string;
2461 perl_argv = (char **)realloc (perl_argv,
2462 (++perl_argc + 1) * sizeof (char *));
2464 if (NULL == perl_argv) {
2465 log_err ("perl_config: Not enough memory.");
2469 if ('\0' == value[0]) {
2470 perl_argv[perl_argc - 1] = "-d";
2473 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2474 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2475 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2478 perl_argv[perl_argc] = NULL;
2480 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2483 * IncludeDir "<Dir>"
2485 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2489 if ((0 != ci->children_num) || (1 != ci->values_num)
2490 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2491 log_err ("IncludeDir expects a single string argument.");
2495 value = ci->values[0].value.string;
2498 perl_argv = (char **)realloc (perl_argv,
2499 (++perl_argc + 1) * sizeof (char *));
2501 if (NULL == perl_argv) {
2502 log_err ("perl_config: Not enough memory.");
2506 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2507 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2508 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2510 perl_argv[perl_argc] = NULL;
2513 /* prepend the directory to @INC */
2514 av_unshift (GvAVn (PL_incgv), 1);
2515 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2518 } /* static int perl_config_includedir (oconfig_item_it *) */
2523 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2533 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2534 log_err ("LoadPlugin expects a single string argument.");
2538 plugin = ci->values[0].value.string;
2541 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2545 log_err ("Unable to convert configuration to a Perl hash value.");
2546 config = (HV *)&PL_sv_undef;
2554 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2555 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2559 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2574 } /* static int perl_config_plugin (oconfig_item_it *) */
2576 static int perl_config (oconfig_item_t *ci)
2583 for (i = 0; i < ci->children_num; ++i) {
2584 oconfig_item_t *c = ci->children + i;
2585 int current_status = 0;
2587 if (NULL != perl_threads)
2589 if ((aTHX = PERL_GET_CONTEXT) == NULL)
2593 if (0 == strcasecmp (c->key, "LoadPlugin"))
2594 current_status = perl_config_loadplugin (aTHX_ c);
2595 else if (0 == strcasecmp (c->key, "BaseName"))
2596 current_status = perl_config_basename (aTHX_ c);
2597 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2598 current_status = perl_config_enabledebugger (aTHX_ c);
2599 else if (0 == strcasecmp (c->key, "IncludeDir"))
2600 current_status = perl_config_includedir (aTHX_ c);
2601 else if (0 == strcasecmp (c->key, "Plugin"))
2602 current_status = perl_config_plugin (aTHX_ c);
2605 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2609 /* fatal error - it's up to perl_config_* to clean up */
2610 if (0 > current_status) {
2611 log_err ("Configuration failed with a fatal error - "
2612 "plugin disabled!");
2613 return current_status;
2616 status += current_status;
2619 } /* static int perl_config (oconfig_item_t *) */
2621 void module_register (void)
2624 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2626 /* default options for the Perl interpreter */
2628 perl_argv[1] = "-MCollectd";
2629 perl_argv[2] = "-e";
2631 perl_argv[4] = NULL;
2633 plugin_register_complex_config ("perl", perl_config);
2635 } /* void module_register (void) */
2637 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */