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;
139 pthread_mutexattr_t mutexattr;
142 /* name / user_data for Perl matches / targets */
148 #define PFC_USER_DATA_FREE(data) \
150 sfree ((data)->name); \
151 if (NULL != (data)->user_data) \
152 sv_free ((data)->user_data); \
159 extern char **environ;
165 /* if perl_threads != NULL perl_threads->head must
166 * point to the "base" thread */
167 static c_ithread_list_t *perl_threads = NULL;
169 /* the key used to store each pthread's ithread */
170 static pthread_key_t perl_thr_key;
172 static int perl_argc = 0;
173 static char **perl_argv = NULL;
175 static char base_name[DATA_MAX_NAME_LEN] = "";
182 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
183 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
184 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
185 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
186 { "Collectd::_plugin_write", Collectd__plugin_write },
187 { "Collectd::_plugin_flush", Collectd__plugin_flush },
188 { "Collectd::plugin_dispatch_notification",
189 Collectd_plugin_dispatch_notification },
190 { "Collectd::plugin_log", Collectd_plugin_log },
191 { "Collectd::_fc_register", Collectd__fc_register },
192 { "Collectd::call_by_name", Collectd_call_by_name },
201 { "Collectd::TYPE_INIT", PLUGIN_INIT },
202 { "Collectd::TYPE_READ", PLUGIN_READ },
203 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
204 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
205 { "Collectd::TYPE_LOG", PLUGIN_LOG },
206 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
207 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
208 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
209 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
210 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
211 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
212 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
213 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
214 { "Collectd::LOG_ERR", LOG_ERR },
215 { "Collectd::LOG_WARNING", LOG_WARNING },
216 { "Collectd::LOG_NOTICE", LOG_NOTICE },
217 { "Collectd::LOG_INFO", LOG_INFO },
218 { "Collectd::LOG_DEBUG", LOG_DEBUG },
219 { "Collectd::FC_MATCH", FC_MATCH },
220 { "Collectd::FC_TARGET", FC_TARGET },
221 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
222 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
223 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
224 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
225 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
226 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
227 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
228 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
229 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
230 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
231 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
240 { "Collectd::hostname_g", hostname_g },
245 * Helper functions for data type conversion.
260 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
264 if ((NULL == hash) || (NULL == ds))
267 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
268 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
271 log_err ("hv2data_source: No DS name given.");
275 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
276 ds->type = SvIV (*tmp);
278 if ((DS_TYPE_COUNTER != ds->type)
279 && (DS_TYPE_GAUGE != ds->type)
280 && (DS_TYPE_DERIVE != ds->type)
281 && (DS_TYPE_ABSOLUTE != ds->type)) {
282 log_err ("hv2data_source: Invalid DS type.");
287 ds->type = DS_TYPE_COUNTER;
290 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
291 ds->min = SvNV (*tmp);
295 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
296 ds->max = SvNV (*tmp);
300 } /* static int hv2data_source (HV *, data_source_t *) */
302 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
304 const data_set_t *ds;
308 if ((NULL == name) || (NULL == array) || (NULL == value))
311 if (av_len (array) < len - 1)
312 len = av_len (array) + 1;
317 ds = plugin_get_ds (name);
319 log_err ("av2value: Unknown dataset \"%s\"", name);
323 if (ds->ds_num < len) {
324 log_warn ("av2value: Value length exceeds data set length.");
328 for (i = 0; i < len; ++i) {
329 SV **tmp = av_fetch (array, i, 0);
332 if (DS_TYPE_COUNTER == ds->ds[i].type)
333 value[i].counter = SvIV (*tmp);
334 else if (DS_TYPE_GAUGE == ds->ds[i].type)
335 value[i].gauge = SvNV (*tmp);
336 else if (DS_TYPE_DERIVE == ds->ds[i].type)
337 value[i].derive = SvIV (*tmp);
338 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
339 value[i].absolute = SvIV (*tmp);
346 } /* static int av2value (char *, AV *, value_t *, int) */
351 * values => [ @values ],
355 * plugin_instance => $pinstance,
356 * type_instance => $tinstance,
359 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
363 if ((NULL == hash) || (NULL == vl))
366 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
367 log_err ("hv2value_list: No type given.");
371 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
373 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
374 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
375 log_err ("hv2value_list: No valid values given.");
380 AV *array = (AV *)SvRV (*tmp);
381 int len = av_len (array) + 1;
386 vl->values = (value_t *)smalloc (len * sizeof (value_t));
387 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
390 if (-1 == vl->values_len) {
396 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
398 double t = SvNV (*tmp);
399 vl->time = DOUBLE_TO_CDTIME_T (t);
402 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
404 double t = SvNV (*tmp);
405 vl->interval = DOUBLE_TO_CDTIME_T (t);
408 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
409 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
411 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
413 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
414 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
416 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
417 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
418 sizeof (vl->plugin_instance));
420 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
421 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
422 sizeof (vl->type_instance));
424 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
426 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
430 if ((NULL == array) || (NULL == name) || (NULL == ds))
433 len = av_len (array);
436 log_err ("av2data_set: Invalid data set.");
440 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
441 ds->ds_num = len + 1;
443 for (i = 0; i <= len; ++i) {
444 SV **elem = av_fetch (array, i, 0);
447 log_err ("av2data_set: Failed to fetch data source %i.", i);
451 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
452 log_err ("av2data_set: Invalid data source.");
456 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
459 log_debug ("av2data_set: "
460 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
461 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
464 sstrncpy (ds->type, name, sizeof (ds->type));
466 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
471 * severity => $severity,
477 * plugin_instance => $instance,
478 * type_instance => $type_instance,
479 * meta => [ { name => <name>, value => <value> }, ... ]
482 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
484 notification_meta_t **m = meta;
486 int len = av_len (array);
489 for (i = 0; i <= len; ++i) {
490 SV **tmp = av_fetch (array, i, 0);
496 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
497 log_warn ("av2notification_meta: Skipping invalid "
498 "meta information.");
502 hash = (HV *)SvRV (*tmp);
504 *m = (notification_meta_t *)smalloc (sizeof (**m));
506 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
507 log_warn ("av2notification_meta: Skipping invalid "
508 "meta information.");
512 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
514 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
515 log_warn ("av2notification_meta: Skipping invalid "
516 "meta information.");
522 (*m)->nm_value.nm_double = SvNVX (*tmp);
523 (*m)->type = NM_TYPE_DOUBLE;
525 else if (SvUOK (*tmp)) {
526 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
527 (*m)->type = NM_TYPE_UNSIGNED_INT;
529 else if (SvIOK (*tmp)) {
530 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
531 (*m)->type = NM_TYPE_SIGNED_INT;
534 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
535 (*m)->type = NM_TYPE_STRING;
542 } /* static int av2notification_meta (AV *, notification_meta_t *) */
544 static int hv2notification (pTHX_ HV *hash, notification_t *n)
548 if ((NULL == hash) || (NULL == n))
551 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
552 n->severity = SvIV (*tmp);
554 n->severity = NOTIF_FAILURE;
556 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
558 double t = SvNV (*tmp);
559 n->time = DOUBLE_TO_CDTIME_T (t);
564 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
565 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
567 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
568 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
570 sstrncpy (n->host, hostname_g, sizeof (n->host));
572 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
573 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
575 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
576 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
577 sizeof (n->plugin_instance));
579 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
580 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
582 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
583 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
584 sizeof (n->type_instance));
587 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
588 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
589 log_warn ("hv2notification: Ignoring invalid meta information.");
593 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
594 plugin_notification_meta_free (n->meta);
601 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
603 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
607 if ((NULL == ds) || (NULL == array))
610 av_extend (array, ds->ds_num);
612 for (i = 0; i < ds->ds_num; ++i) {
613 HV *source = newHV ();
615 if (NULL == hv_store (source, "name", 4,
616 newSVpv (ds->ds[i].name, 0), 0))
619 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
622 if (! isnan (ds->ds[i].min))
623 if (NULL == hv_store (source, "min", 3,
624 newSVnv (ds->ds[i].min), 0))
627 if (! isnan (ds->ds[i].max))
628 if (NULL == hv_store (source, "max", 3,
629 newSVnv (ds->ds[i].max), 0))
632 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
636 } /* static int data_set2av (data_set_t *, AV *) */
638 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
645 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
648 len = vl->values_len;
650 if (ds->ds_num < len) {
651 log_warn ("value2av: Value length exceeds data set length.");
656 av_extend (values, len - 1);
658 for (i = 0; i < len; ++i) {
661 if (DS_TYPE_COUNTER == ds->ds[i].type)
662 val = newSViv (vl->values[i].counter);
663 else if (DS_TYPE_GAUGE == ds->ds[i].type)
664 val = newSVnv (vl->values[i].gauge);
665 else if (DS_TYPE_DERIVE == ds->ds[i].type)
666 val = newSViv (vl->values[i].derive);
667 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
668 val = newSViv (vl->values[i].absolute);
670 if (NULL == av_store (values, i, val)) {
676 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
681 double t = CDTIME_T_TO_DOUBLE (vl->time);
682 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
687 double t = CDTIME_T_TO_DOUBLE (vl->interval);
688 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
692 if ('\0' != vl->host[0])
693 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
696 if ('\0' != vl->plugin[0])
697 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
700 if ('\0' != vl->plugin_instance[0])
701 if (NULL == hv_store (hash, "plugin_instance", 15,
702 newSVpv (vl->plugin_instance, 0), 0))
705 if ('\0' != vl->type[0])
706 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
709 if ('\0' != vl->type_instance[0])
710 if (NULL == hv_store (hash, "type_instance", 13,
711 newSVpv (vl->type_instance, 0), 0))
714 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
716 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
726 av_extend (array, meta_num);
728 for (i = 0; NULL != meta; meta = meta->next, ++i) {
732 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
735 if (NM_TYPE_STRING == meta->type)
736 value = newSVpv (meta->nm_value.nm_string, 0);
737 else if (NM_TYPE_SIGNED_INT == meta->type)
738 value = newSViv (meta->nm_value.nm_signed_int);
739 else if (NM_TYPE_UNSIGNED_INT == meta->type)
740 value = newSVuv (meta->nm_value.nm_unsigned_int);
741 else if (NM_TYPE_DOUBLE == meta->type)
742 value = newSVnv (meta->nm_value.nm_double);
743 else if (NM_TYPE_BOOLEAN == meta->type)
744 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
748 if (NULL == hv_store (m, "value", 5, value, 0)) {
753 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
760 } /* static int notification_meta2av (notification_meta_t *, AV *) */
762 static int notification2hv (pTHX_ notification_t *n, HV *hash)
764 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
769 double t = CDTIME_T_TO_DOUBLE (n->time);
770 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
774 if ('\0' != *n->message)
775 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
778 if ('\0' != *n->host)
779 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
782 if ('\0' != *n->plugin)
783 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
786 if ('\0' != *n->plugin_instance)
787 if (NULL == hv_store (hash, "plugin_instance", 15,
788 newSVpv (n->plugin_instance, 0), 0))
791 if ('\0' != *n->type)
792 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
795 if ('\0' != *n->type_instance)
796 if (NULL == hv_store (hash, "type_instance", 13,
797 newSVpv (n->type_instance, 0), 0))
800 if (NULL != n->meta) {
802 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
803 || (NULL == hv_store (hash, "meta", 4,
804 newRV_noinc ((SV *)meta), 0))) {
811 } /* static int notification2hv (notification_t *, HV *) */
813 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
820 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
824 if (0 < ci->values_num)
825 av_extend (values, ci->values_num);
827 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
833 for (i = 0; i < ci->values_num; ++i) {
836 switch (ci->values[i].type) {
837 case OCONFIG_TYPE_STRING:
838 value = newSVpv (ci->values[i].value.string, 0);
840 case OCONFIG_TYPE_NUMBER:
841 value = newSVnv ((NV)ci->values[i].value.number);
843 case OCONFIG_TYPE_BOOLEAN:
844 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
847 log_err ("oconfig_item2hv: Invalid value type %i.",
849 value = &PL_sv_undef;
852 if (NULL == av_store (values, i, value)) {
858 /* ignoring 'parent' member which is uninteresting in this case */
861 if (0 < ci->children_num)
862 av_extend (children, ci->children_num);
864 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
870 for (i = 0; i < ci->children_num; ++i) {
871 HV *child = newHV ();
873 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
879 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
886 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
889 * Internal functions.
892 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
894 if (base_name[0] == '\0')
895 status = ssnprintf (buf, buf_len, "%s", module);
897 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
898 if ((status < 0) || ((unsigned int)status >= buf_len))
901 } /* char *get_module_name */
904 * Add a plugin's data set definition.
906 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
912 if ((NULL == name) || (NULL == dataset))
915 if (0 != av2data_set (aTHX_ dataset, name, &ds))
918 ret = plugin_register_data_set (&ds);
922 } /* static int pplugin_register_data_set (char *, SV *) */
925 * Remove a plugin's data set definition.
927 static int pplugin_unregister_data_set (char *name)
931 return plugin_unregister_data_set (name);
932 } /* static int pplugin_unregister_data_set (char *) */
935 * Submit the values to the write functions.
937 static int pplugin_dispatch_values (pTHX_ HV *values)
939 value_list_t vl = VALUE_LIST_INIT;
946 if (0 != hv2value_list (aTHX_ values, &vl))
949 ret = plugin_dispatch_values (&vl);
953 } /* static int pplugin_dispatch_values (char *, HV *) */
956 * Submit the values to a single write function.
958 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
961 value_list_t vl = VALUE_LIST_INIT;
968 if (0 != hv2value_list (aTHX_ values, &vl))
971 if ((NULL != data_set)
972 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
975 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
977 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
978 NULL == plugin ? "<any>" : plugin, ret);
980 if (NULL != data_set)
984 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
987 * Dispatch a notification.
989 static int pplugin_dispatch_notification (pTHX_ HV *notif)
998 memset (&n, 0, sizeof (n));
1000 if (0 != hv2notification (aTHX_ notif, &n))
1003 ret = plugin_dispatch_notification (&n);
1004 plugin_notification_meta_free (n.meta);
1006 } /* static int pplugin_dispatch_notification (HV *) */
1009 * Call all working functions of the given type.
1011 static int pplugin_call_all (pTHX_ int type, ...)
1021 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1022 if (t == NULL) /* thread destroyed ( c_ithread_destroy*() -> log_debug() ) */
1025 old_running = t->running;
1029 t->running = old_running;
1033 if ((type < 0) || (type >= PLUGIN_TYPES))
1036 va_start (ap, type);
1043 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1045 if (PLUGIN_WRITE == type) {
1047 * $_[0] = $plugin_type;
1062 * values => [ $v1, ... ],
1064 * host => $hostname,
1065 * plugin => $plugin,
1067 * plugin_instance => $instance,
1068 * type_instance => $type_instance
1077 ds = va_arg (ap, data_set_t *);
1078 vl = va_arg (ap, value_list_t *);
1080 if (-1 == data_set2av (aTHX_ ds, pds)) {
1083 pds = (AV *)&PL_sv_undef;
1087 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1090 pvl = (HV *)&PL_sv_undef;
1094 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1095 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1096 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1098 else if (PLUGIN_LOG == type) {
1104 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1105 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1107 else if (PLUGIN_NOTIF == type) {
1111 * severity => $severity,
1115 * plugin => $plugin,
1117 * plugin_instance => $instance,
1118 * type_instance => $type_instance
1122 HV *notif = newHV ();
1124 n = va_arg (ap, notification_t *);
1126 if (-1 == notification2hv (aTHX_ n, notif)) {
1129 notif = (HV *)&PL_sv_undef;
1133 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1135 else if (PLUGIN_FLUSH == type) {
1140 * $_[1] = $identifier;
1142 timeout = va_arg (ap, cdtime_t);
1144 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1145 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1150 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1163 t->running = old_running;
1166 } /* static int pplugin_call_all (int, ...) */
1169 * collectd's perl interpreter based thread implementation.
1171 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1174 /* must be called with perl_threads->mutex locked */
1175 static void c_ithread_destroy (c_ithread_t *ithread)
1177 dTHXa (ithread->interp);
1179 assert (NULL != perl_threads);
1181 PERL_SET_CONTEXT (aTHX);
1182 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1187 --perl_threads->number_of_threads;
1188 #endif /* COLLECT_DEBUG */
1190 perl_destruct (aTHX);
1193 if (NULL == ithread->prev)
1194 perl_threads->head = ithread->next;
1196 ithread->prev->next = ithread->next;
1198 if (NULL == ithread->next)
1199 perl_threads->tail = ithread->prev;
1201 ithread->next->prev = ithread->prev;
1205 } /* static void c_ithread_destroy (c_ithread_t *) */
1207 static void c_ithread_destructor (void *arg)
1209 c_ithread_t *ithread = (c_ithread_t *)arg;
1210 c_ithread_t *t = NULL;
1212 if (NULL == perl_threads)
1215 pthread_mutex_lock (&perl_threads->mutex);
1217 for (t = perl_threads->head; NULL != t; t = t->next)
1221 /* the ithread no longer exists */
1224 pthread_mutex_unlock (&perl_threads->mutex);
1228 c_ithread_destroy (ithread);
1230 pthread_mutex_unlock (&perl_threads->mutex);
1232 } /* static void c_ithread_destructor (void *) */
1234 /* must be called with perl_threads->mutex locked */
1235 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1237 c_ithread_t *t = NULL;
1240 assert (NULL != perl_threads);
1242 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1243 memset (t, 0, sizeof (c_ithread_t));
1245 t->interp = (NULL == base)
1247 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1251 if ((NULL != base) && (NULL != PL_endav)) {
1252 av_clear (PL_endav);
1253 av_undef (PL_endav);
1258 ++perl_threads->number_of_threads;
1259 #endif /* COLLECT_DEBUG */
1263 if (NULL == perl_threads->tail) {
1264 perl_threads->head = t;
1268 perl_threads->tail->next = t;
1269 t->prev = perl_threads->tail;
1272 t->pthread = pthread_self();
1275 perl_threads->tail = t;
1277 pthread_setspecific (perl_thr_key, (const void *)t);
1279 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1282 * Filter chains implementation.
1285 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1293 notification_meta_t **meta = NULL;
1298 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1299 if (t == NULL) /* thread destroyed */
1302 old_running = t->running;
1306 t->running = old_running;
1310 if ((type < 0) || (type >= FC_TYPES))
1313 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1316 va_start (ap, data);
1323 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1324 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1325 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1327 if (FC_CB_CREATE == cb_type) {
1330 * $_[1] = $user_data;
1333 HV *config = newHV ();
1335 ci = va_arg (ap, oconfig_item_t *);
1337 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1340 config = (HV *)&PL_sv_undef;
1344 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1346 else if (FC_CB_DESTROY == cb_type) {
1348 * $_[1] = $user_data;
1351 /* nothing to be done - the user data pointer
1352 * is pushed onto the stack later */
1354 else if (FC_CB_EXEC == cb_type) {
1359 * $_[3] = $user_data;
1367 ds = va_arg (ap, data_set_t *);
1368 vl = va_arg (ap, value_list_t *);
1369 meta = va_arg (ap, notification_meta_t **);
1371 if (0 != data_set2av (aTHX_ ds, pds)) {
1374 pds = (AV *)&PL_sv_undef;
1378 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1381 pvl = (HV *)&PL_sv_undef;
1388 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1391 pmeta = (AV *)&PL_sv_undef;
1396 pmeta = (AV *)&PL_sv_undef;
1399 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1400 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1401 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1404 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1408 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1410 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1411 assert (pmeta != NULL);
1413 plugin_notification_meta_free (*meta);
1414 av2notification_meta (aTHX_ pmeta, meta);
1421 /* the exec callbacks return a status, while
1422 * the others return a boolean value */
1423 if (FC_CB_EXEC == cb_type)
1425 else if (! SvTRUE (tmp))
1433 t->running = old_running;
1436 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1438 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1440 pfc_user_data_t *data;
1446 if (NULL == perl_threads)
1450 c_ithread_t *t = NULL;
1452 pthread_mutex_lock (&perl_threads->mutex);
1453 t = c_ithread_create (perl_threads->head->interp);
1454 pthread_mutex_unlock (&perl_threads->mutex);
1459 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1460 aTHX, perl_threads->number_of_threads);
1462 if ((1 != ci->values_num)
1463 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1464 log_warn ("A \"%s\" block expects a single string argument.",
1465 (FC_MATCH == type) ? "Match" : "Target");
1469 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1470 data->name = sstrdup (ci->values[0].value.string);
1471 data->user_data = newSV (0);
1473 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1476 PFC_USER_DATA_FREE (data);
1480 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1482 static int fc_destroy (int type, void **user_data)
1484 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1490 if ((NULL == perl_threads) || (NULL == data))
1494 c_ithread_t *t = NULL;
1496 pthread_mutex_lock (&perl_threads->mutex);
1497 t = c_ithread_create (perl_threads->head->interp);
1498 pthread_mutex_unlock (&perl_threads->mutex);
1503 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1504 aTHX, perl_threads->number_of_threads);
1506 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1508 PFC_USER_DATA_FREE (data);
1511 } /* static int fc_destroy (int, void **) */
1513 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1514 notification_meta_t **meta, void **user_data)
1516 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1520 if (NULL == perl_threads)
1523 assert (NULL != data);
1526 c_ithread_t *t = NULL;
1528 pthread_mutex_lock (&perl_threads->mutex);
1529 t = c_ithread_create (perl_threads->head->interp);
1530 pthread_mutex_unlock (&perl_threads->mutex);
1535 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1536 aTHX, perl_threads->number_of_threads);
1538 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1539 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1540 notification_meta_t **, void **) */
1542 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1544 return fc_create (FC_MATCH, ci, user_data);
1545 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1547 static int pmatch_destroy (void **user_data)
1549 return fc_destroy (FC_MATCH, user_data);
1550 } /* static int pmatch_destroy (void **) */
1552 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1553 notification_meta_t **meta, void **user_data)
1555 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1556 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1557 notification_meta_t **, void **) */
1559 static match_proc_t pmatch = {
1560 pmatch_create, pmatch_destroy, pmatch_match
1563 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1565 return fc_create (FC_TARGET, ci, user_data);
1566 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1568 static int ptarget_destroy (void **user_data)
1570 return fc_destroy (FC_TARGET, user_data);
1571 } /* static int ptarget_destroy (void **) */
1573 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1574 notification_meta_t **meta, void **user_data)
1576 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1577 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1578 notification_meta_t **, void **) */
1580 static target_proc_t ptarget = {
1581 ptarget_create, ptarget_destroy, ptarget_invoke
1585 * Exported Perl API.
1589 * Collectd::plugin_register_data_set (type, dataset).
1592 * type of the dataset
1595 * dataset to be registered
1597 static XS (Collectd_plugin_register_ds)
1604 log_warn ("Using plugin_register() to register new data-sets is "
1605 "deprecated - add new entries to a custom types.db instead.");
1608 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1612 log_debug ("Collectd::plugin_register_data_set: "
1613 "type = \"%s\", dataset = \"%s\"",
1614 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1618 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1619 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1623 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1631 } /* static XS (Collectd_plugin_register_ds) */
1634 * Collectd::plugin_unregister_data_set (type).
1637 * type of the dataset
1639 static XS (Collectd_plugin_unregister_ds)
1644 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1648 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1649 SvPV_nolen (ST (0)));
1651 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1655 } /* static XS (Collectd_plugin_register_ds) */
1658 * Collectd::plugin_dispatch_values (name, values).
1661 * name of the plugin
1664 * value list to submit
1666 static XS (Collectd_plugin_dispatch_values)
1675 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1679 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1680 SvPV_nolen (ST (/* stack index = */ 0)));
1682 values = ST (/* stack index = */ 0);
1687 /* Make sure the argument is a hash reference. */
1688 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1689 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1693 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1699 } /* static XS (Collectd_plugin_dispatch_values) */
1702 * Collectd::plugin_get_interval ().
1704 static XS (Collectd_plugin_get_interval)
1708 /* make sure we don't get any unused variable warnings for 'items';
1709 * don't abort, though */
1711 log_err ("Usage: Collectd::plugin_get_interval()");
1713 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1714 } /* static XS (Collectd_plugin_get_interval) */
1716 /* Collectd::plugin_write (plugin, ds, vl).
1719 * name of the plugin to call, may be 'undef'
1722 * data-set that describes the submitted values, may be 'undef'
1725 * value-list to be written
1727 static XS (Collectd__plugin_write)
1738 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1742 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1743 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1744 SvPV_nolen (ST (2)));
1746 if (! SvOK (ST (0)))
1749 plugin = SvPV_nolen (ST (0));
1752 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1753 ds_array = (AV *)SvRV (ds);
1754 else if (! SvOK (ds))
1757 log_err ("Collectd::plugin_write: Invalid data-set.");
1762 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1763 log_err ("Collectd::plugin_write: Invalid value-list.");
1767 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1773 } /* static XS (Collectd__plugin_write) */
1776 * Collectd::_plugin_flush (plugin, timeout, identifier).
1779 * name of the plugin to flush
1782 * timeout to use when flushing the data
1785 * data-set identifier to flush
1787 static XS (Collectd__plugin_flush)
1789 char *plugin = NULL;
1796 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1801 plugin = SvPV_nolen (ST (0));
1804 timeout = (int)SvIV (ST (1));
1807 id = SvPV_nolen (ST (2));
1809 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1810 "id = \"%s\"", plugin, timeout, id);
1812 if (0 == plugin_flush (plugin, timeout, id))
1816 } /* static XS (Collectd__plugin_flush) */
1819 * Collectd::plugin_dispatch_notification (notif).
1822 * notification to dispatch
1824 static XS (Collectd_plugin_dispatch_notification)
1833 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1837 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1838 SvPV_nolen (ST (0)));
1842 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1843 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1847 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1853 } /* static XS (Collectd_plugin_dispatch_notification) */
1856 * Collectd::plugin_log (level, message).
1859 * log level (LOG_DEBUG, ... LOG_ERR)
1864 static XS (Collectd_plugin_log)
1869 log_err ("Usage: Collectd::plugin_log(level, message)");
1873 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1875 } /* static XS (Collectd_plugin_log) */
1878 * Collectd::_fc_register (type, name)
1886 static XS (Collectd__fc_register)
1896 log_err ("Usage: Collectd::_fc_register(type, name)");
1900 type = SvIV (ST (0));
1901 name = SvPV_nolen (ST (1));
1903 if (FC_MATCH == type)
1904 ret = fc_register_match (name, pmatch);
1905 else if (FC_TARGET == type)
1906 ret = fc_register_target (name, ptarget);
1912 } /* static XS (Collectd_fc_register) */
1915 * Collectd::call_by_name (...).
1917 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1919 static XS (Collectd_call_by_name)
1924 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1925 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1930 name = SvPV_nolen (tmp);
1932 if (NULL == get_cv (name, 0)) {
1933 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1938 /* simply pass on the subroutine call without touching the stack,
1939 * thus leaving any arguments and return values in place */
1941 } /* static XS (Collectd_call_by_name) */
1944 * Interface to collectd.
1947 static int perl_init (void)
1952 if (NULL == perl_threads)
1956 c_ithread_t *t = NULL;
1958 pthread_mutex_lock (&perl_threads->mutex);
1959 t = c_ithread_create (perl_threads->head->interp);
1960 pthread_mutex_unlock (&perl_threads->mutex);
1965 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1966 aTHX, perl_threads->number_of_threads);
1968 /* Lock the base thread to avoid race conditions with c_ithread_create().
1969 * See https://github.com/collectd/collectd/issues/9 and
1970 * https://github.com/collectd/collectd/issues/1706 for details.
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)
2049 if (NULL == perl_threads)
2053 c_ithread_t *t = NULL;
2055 pthread_mutex_lock (&perl_threads->mutex);
2056 t = c_ithread_create (perl_threads->head->interp);
2057 pthread_mutex_unlock (&perl_threads->mutex);
2062 /* Lock the base thread if this is not called from one of the read threads
2063 * to avoid race conditions with c_ithread_create(). See
2064 * https://github.com/collectd/collectd/issues/9 for details.
2067 if (aTHX == perl_threads->head->interp)
2068 pthread_mutex_lock (&perl_threads->mutex);
2070 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2072 if (aTHX == perl_threads->head->interp)
2073 pthread_mutex_unlock (&perl_threads->mutex);
2076 } /* static void perl_log (int, const char *) */
2078 static int perl_notify (const notification_t *notif,
2079 user_data_t __attribute__((unused)) *user_data)
2083 if (NULL == perl_threads)
2087 c_ithread_t *t = NULL;
2089 pthread_mutex_lock (&perl_threads->mutex);
2090 t = c_ithread_create (perl_threads->head->interp);
2091 pthread_mutex_unlock (&perl_threads->mutex);
2095 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2096 } /* static int perl_notify (const notification_t *) */
2098 static int perl_flush (cdtime_t timeout, const char *identifier,
2099 user_data_t __attribute__((unused)) *user_data)
2103 if (NULL == perl_threads)
2107 c_ithread_t *t = NULL;
2109 pthread_mutex_lock (&perl_threads->mutex);
2110 t = c_ithread_create (perl_threads->head->interp);
2111 pthread_mutex_unlock (&perl_threads->mutex);
2115 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2116 } /* static int perl_flush (const int) */
2118 static int perl_shutdown (void)
2120 c_ithread_t *t = NULL;
2126 plugin_unregister_complex_config ("perl");
2128 if (NULL == perl_threads)
2134 pthread_mutex_lock (&perl_threads->mutex);
2135 t = c_ithread_create (perl_threads->head->interp);
2136 pthread_mutex_unlock (&perl_threads->mutex);
2141 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2142 aTHX, perl_threads->number_of_threads);
2144 plugin_unregister_log ("perl");
2145 plugin_unregister_notification ("perl");
2146 plugin_unregister_init ("perl");
2147 plugin_unregister_read ("perl");
2148 plugin_unregister_write ("perl");
2149 plugin_unregister_flush ("perl");
2151 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2153 pthread_mutex_lock (&perl_threads->mutex);
2154 t = perl_threads->tail;
2157 struct timespec ts_wait;
2158 c_ithread_t *thr = t;
2160 /* the pointer has to be advanced before destroying
2161 * the thread as this will free the memory */
2166 /* Give some time to thread to exit from pi */
2167 WARNING ("perl shutdown: thread is running inside perl. Waiting.");
2169 ts_wait.tv_nsec = 500000;
2170 nanosleep (&ts_wait, NULL);
2173 /* This will crash collectd process later due to PERL_SYS_TERM() */
2174 //ERROR ("perl shutdown: thread hangs inside perl. "
2175 // "Skipped perl interpreter destroy.");
2178 ERROR ("perl shutdown: thread hangs inside perl. Thread killed.");
2179 pthread_kill (thr->pthread, SIGTERM);
2181 c_ithread_destroy (thr);
2184 pthread_mutex_unlock (&perl_threads->mutex);
2185 pthread_mutex_destroy (&perl_threads->mutex);
2186 pthread_mutexattr_destroy (&perl_threads->mutexattr);
2188 sfree (perl_threads);
2190 pthread_key_delete (perl_thr_key);
2194 plugin_unregister_shutdown ("perl");
2196 } /* static void perl_shutdown (void) */
2199 * Access functions for global variables.
2201 * These functions implement the "magic" used to access
2202 * the global variables from Perl.
2205 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2207 char *pv = mg->mg_ptr;
2210 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2212 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2214 char *pv = mg->mg_ptr;
2215 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2217 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2219 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2221 log_warn ("Accessing $interval_g is deprecated (and might not "
2222 "give the desired results) - plugin_get_interval() should "
2223 "be used instead.");
2224 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2226 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2228 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2230 double nv = (double)SvNV (var);
2231 log_warn ("Accessing $interval_g is deprecated (and might not "
2232 "give the desired results) - plugin_get_interval() should "
2233 "be used instead.");
2234 interval_g = DOUBLE_TO_CDTIME_T (nv);
2236 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2238 static MGVTBL g_pv_vtbl = {
2239 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2240 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2244 static MGVTBL g_interval_vtbl = {
2245 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2246 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2251 /* bootstrap the Collectd module */
2252 static void xs_init (pTHX)
2256 char *file = __FILE__;
2262 /* enable usage of Perl modules using shared libraries */
2263 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2266 for (i = 0; NULL != api[i].f; ++i)
2267 newXS (api[i].name, api[i].f, file);
2269 stash = gv_stashpv ("Collectd", 1);
2271 /* export "constants" */
2272 for (i = 0; '\0' != constants[i].name[0]; ++i)
2273 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2275 /* export global variables
2276 * by adding "magic" to the SV's representing the globale variables
2277 * perl is able to automagically call the get/set function when
2278 * accessing any such variable (this is basically the same as using
2280 /* global strings */
2281 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2282 tmp = get_sv (g_strings[i].name, 1);
2283 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2284 g_strings[i].var, 0);
2287 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2288 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2289 /* vtbl = */ &g_interval_vtbl,
2290 /* name = */ NULL, /* namelen = */ 0);
2293 } /* static void xs_init (pTHX) */
2295 /* Initialize the global Perl interpreter. */
2296 static int init_pi (int argc, char **argv)
2300 if (NULL != perl_threads)
2303 log_info ("Initializing Perl interpreter...");
2308 for (i = 0; i < argc; ++i)
2309 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2311 #endif /* COLLECT_DEBUG */
2313 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2314 log_err ("init_pi: pthread_key_create failed");
2316 /* this must not happen - cowardly giving up if it does */
2321 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2322 * triggers a "value computed is not used" warning by gcc. */
2325 PERL_SYS_INIT3 (&argc, &argv, &environ);
2327 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2328 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2330 pthread_mutexattr_init(&perl_threads->mutexattr);
2331 pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE);
2332 pthread_mutex_init (&perl_threads->mutex, &perl_threads->mutexattr);
2333 /* locking the mutex should not be necessary at this point
2334 * but let's just do it for the sake of completeness */
2335 pthread_mutex_lock (&perl_threads->mutex);
2337 perl_threads->head = c_ithread_create (NULL);
2338 perl_threads->tail = perl_threads->head;
2340 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2341 log_err ("init_pi: Not enough memory.");
2345 aTHX = perl_threads->head->interp;
2346 pthread_mutex_unlock (&perl_threads->mutex);
2348 perl_construct (aTHX);
2350 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2352 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2353 SV *err = get_sv ("@", 1);
2354 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2357 perl_destruct (perl_threads->head->interp);
2358 perl_free (perl_threads->head->interp);
2359 sfree (perl_threads);
2361 pthread_key_delete (perl_thr_key);
2365 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2366 sv_setpv (get_sv ("0", 0), "collectd");
2370 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2371 plugin_register_notification ("perl", perl_notify,
2372 /* user_data = */ NULL);
2373 plugin_register_init ("perl", perl_init);
2375 plugin_register_read ("perl", perl_read);
2377 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2378 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2379 plugin_register_shutdown ("perl", perl_shutdown);
2381 } /* static int init_pi (const char **, const int) */
2384 * LoadPlugin "<Plugin>"
2386 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2388 char module_name[DATA_MAX_NAME_LEN];
2392 if ((0 != ci->children_num) || (1 != ci->values_num)
2393 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2394 log_err ("LoadPlugin expects a single string argument.");
2398 value = ci->values[0].value.string;
2400 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2401 log_err ("Invalid module name %s", value);
2405 if (0 != init_pi (perl_argc, perl_argv))
2408 assert (NULL != perl_threads);
2409 assert (NULL != perl_threads->head);
2411 aTHX = perl_threads->head->interp;
2413 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2414 load_module (PERL_LOADMOD_NOIMPORT,
2415 newSVpv (module_name, strlen (module_name)), Nullsv);
2417 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2422 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2426 if ((0 != ci->children_num) || (1 != ci->values_num)
2427 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2428 log_err ("BaseName expects a single string argument.");
2432 value = ci->values[0].value.string;
2434 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2435 sstrncpy (base_name, value, sizeof (base_name));
2437 } /* static int perl_config_basename (oconfig_item_it *) */
2440 * EnableDebugger "<Package>"|""
2442 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2446 if ((0 != ci->children_num) || (1 != ci->values_num)
2447 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2448 log_err ("EnableDebugger expects a single string argument.");
2452 if (NULL != perl_threads) {
2453 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2457 value = ci->values[0].value.string;
2459 perl_argv = (char **)realloc (perl_argv,
2460 (++perl_argc + 1) * sizeof (char *));
2462 if (NULL == perl_argv) {
2463 log_err ("perl_config: Not enough memory.");
2467 if ('\0' == value[0]) {
2468 perl_argv[perl_argc - 1] = "-d";
2471 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2472 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2473 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2476 perl_argv[perl_argc] = NULL;
2478 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2481 * IncludeDir "<Dir>"
2483 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2487 if ((0 != ci->children_num) || (1 != ci->values_num)
2488 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2489 log_err ("IncludeDir expects a single string argument.");
2493 value = ci->values[0].value.string;
2496 perl_argv = (char **)realloc (perl_argv,
2497 (++perl_argc + 1) * sizeof (char *));
2499 if (NULL == perl_argv) {
2500 log_err ("perl_config: Not enough memory.");
2504 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2505 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2506 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2508 perl_argv[perl_argc] = NULL;
2511 /* prepend the directory to @INC */
2512 av_unshift (GvAVn (PL_incgv), 1);
2513 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2516 } /* static int perl_config_includedir (oconfig_item_it *) */
2521 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2531 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2532 log_err ("LoadPlugin expects a single string argument.");
2536 plugin = ci->values[0].value.string;
2539 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2543 log_err ("Unable to convert configuration to a Perl hash value.");
2544 config = (HV *)&PL_sv_undef;
2552 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2553 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2557 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2572 } /* static int perl_config_plugin (oconfig_item_it *) */
2574 static int perl_config (oconfig_item_t *ci)
2581 for (i = 0; i < ci->children_num; ++i) {
2582 oconfig_item_t *c = ci->children + i;
2583 int current_status = 0;
2585 if (NULL != perl_threads)
2587 if ((aTHX = PERL_GET_CONTEXT) == NULL)
2591 if (0 == strcasecmp (c->key, "LoadPlugin"))
2592 current_status = perl_config_loadplugin (aTHX_ c);
2593 else if (0 == strcasecmp (c->key, "BaseName"))
2594 current_status = perl_config_basename (aTHX_ c);
2595 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2596 current_status = perl_config_enabledebugger (aTHX_ c);
2597 else if (0 == strcasecmp (c->key, "IncludeDir"))
2598 current_status = perl_config_includedir (aTHX_ c);
2599 else if (0 == strcasecmp (c->key, "Plugin"))
2600 current_status = perl_config_plugin (aTHX_ c);
2603 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2607 /* fatal error - it's up to perl_config_* to clean up */
2608 if (0 > current_status) {
2609 log_err ("Configuration failed with a fatal error - "
2610 "plugin disabled!");
2611 return current_status;
2614 status += current_status;
2617 } /* static int perl_config (oconfig_item_t *) */
2619 void module_register (void)
2622 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2624 /* default options for the Perl interpreter */
2626 perl_argv[1] = "-MCollectd";
2627 perl_argv[2] = "-e";
2629 perl_argv[4] = NULL;
2631 plugin_register_complex_config ("perl", perl_config);
2633 } /* void module_register (void) */
2635 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */