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 perl sub with thread locking flags handled.
1011 static int call_pv_locked (pTHX_ const char* sub_name)
1016 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1017 if (t == NULL) /* thread destroyed */
1020 old_running = t->running;
1024 t->running = old_running;
1028 ret = call_pv (sub_name, G_SCALAR);
1030 t->running = old_running;
1032 } /* static int call_pv_locked (pTHX, *sub_name) */
1035 * Call all working functions of the given type.
1037 static int pplugin_call_all (pTHX_ int type, ...)
1046 if ((type < 0) || (type >= PLUGIN_TYPES))
1049 va_start (ap, type);
1056 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1058 if (PLUGIN_WRITE == type) {
1060 * $_[0] = $plugin_type;
1075 * values => [ $v1, ... ],
1077 * host => $hostname,
1078 * plugin => $plugin,
1080 * plugin_instance => $instance,
1081 * type_instance => $type_instance
1090 ds = va_arg (ap, data_set_t *);
1091 vl = va_arg (ap, value_list_t *);
1093 if (-1 == data_set2av (aTHX_ ds, pds)) {
1096 pds = (AV *)&PL_sv_undef;
1100 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1103 pvl = (HV *)&PL_sv_undef;
1107 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1108 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1109 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1111 else if (PLUGIN_LOG == type) {
1117 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1118 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1120 else if (PLUGIN_NOTIF == type) {
1124 * severity => $severity,
1128 * plugin => $plugin,
1130 * plugin_instance => $instance,
1131 * type_instance => $type_instance
1135 HV *notif = newHV ();
1137 n = va_arg (ap, notification_t *);
1139 if (-1 == notification2hv (aTHX_ n, notif)) {
1142 notif = (HV *)&PL_sv_undef;
1146 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1148 else if (PLUGIN_FLUSH == type) {
1153 * $_[1] = $identifier;
1155 timeout = va_arg (ap, cdtime_t);
1157 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1158 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1163 retvals = call_pv_locked (aTHX_ "Collectd::plugin_call_all");
1178 } /* static int pplugin_call_all (int, ...) */
1181 * collectd's perl interpreter based thread implementation.
1183 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1186 /* must be called with perl_threads->mutex locked */
1187 static void c_ithread_destroy (c_ithread_t *ithread)
1189 dTHXa (ithread->interp);
1191 assert (NULL != perl_threads);
1193 PERL_SET_CONTEXT (aTHX);
1194 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1199 --perl_threads->number_of_threads;
1200 #endif /* COLLECT_DEBUG */
1202 perl_destruct (aTHX);
1205 if (NULL == ithread->prev)
1206 perl_threads->head = ithread->next;
1208 ithread->prev->next = ithread->next;
1210 if (NULL == ithread->next)
1211 perl_threads->tail = ithread->prev;
1213 ithread->next->prev = ithread->prev;
1217 } /* static void c_ithread_destroy (c_ithread_t *) */
1219 static void c_ithread_destructor (void *arg)
1221 c_ithread_t *ithread = (c_ithread_t *)arg;
1222 c_ithread_t *t = NULL;
1224 if (NULL == perl_threads)
1227 pthread_mutex_lock (&perl_threads->mutex);
1229 for (t = perl_threads->head; NULL != t; t = t->next)
1233 /* the ithread no longer exists */
1236 pthread_mutex_unlock (&perl_threads->mutex);
1240 c_ithread_destroy (ithread);
1242 pthread_mutex_unlock (&perl_threads->mutex);
1244 } /* static void c_ithread_destructor (void *) */
1246 /* must be called with perl_threads->mutex locked */
1247 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1249 c_ithread_t *t = NULL;
1252 assert (NULL != perl_threads);
1254 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1255 memset (t, 0, sizeof (c_ithread_t));
1257 t->interp = (NULL == base)
1259 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1263 if ((NULL != base) && (NULL != PL_endav)) {
1264 av_clear (PL_endav);
1265 av_undef (PL_endav);
1270 ++perl_threads->number_of_threads;
1271 #endif /* COLLECT_DEBUG */
1275 if (NULL == perl_threads->tail) {
1276 perl_threads->head = t;
1280 perl_threads->tail->next = t;
1281 t->prev = perl_threads->tail;
1284 t->pthread = pthread_self();
1287 perl_threads->tail = t;
1289 pthread_setspecific (perl_thr_key, (const void *)t);
1291 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1294 * Filter chains implementation.
1297 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1304 notification_meta_t **meta = NULL;
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_locked (aTHX_ "Collectd::fc_call");
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))
1434 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1436 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1438 pfc_user_data_t *data;
1444 if (NULL == perl_threads)
1448 c_ithread_t *t = NULL;
1450 pthread_mutex_lock (&perl_threads->mutex);
1451 t = c_ithread_create (perl_threads->head->interp);
1452 pthread_mutex_unlock (&perl_threads->mutex);
1457 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1458 aTHX, perl_threads->number_of_threads);
1460 if ((1 != ci->values_num)
1461 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1462 log_warn ("A \"%s\" block expects a single string argument.",
1463 (FC_MATCH == type) ? "Match" : "Target");
1467 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1468 data->name = sstrdup (ci->values[0].value.string);
1469 data->user_data = newSV (0);
1471 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1474 PFC_USER_DATA_FREE (data);
1478 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1480 static int fc_destroy (int type, void **user_data)
1482 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1488 if ((NULL == perl_threads) || (NULL == data))
1492 c_ithread_t *t = NULL;
1494 pthread_mutex_lock (&perl_threads->mutex);
1495 t = c_ithread_create (perl_threads->head->interp);
1496 pthread_mutex_unlock (&perl_threads->mutex);
1501 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1502 aTHX, perl_threads->number_of_threads);
1504 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1506 PFC_USER_DATA_FREE (data);
1509 } /* static int fc_destroy (int, void **) */
1511 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1512 notification_meta_t **meta, void **user_data)
1514 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1518 if (NULL == perl_threads)
1521 assert (NULL != data);
1524 c_ithread_t *t = NULL;
1526 pthread_mutex_lock (&perl_threads->mutex);
1527 t = c_ithread_create (perl_threads->head->interp);
1528 pthread_mutex_unlock (&perl_threads->mutex);
1533 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1534 aTHX, perl_threads->number_of_threads);
1536 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1537 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1538 notification_meta_t **, void **) */
1540 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1542 return fc_create (FC_MATCH, ci, user_data);
1543 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1545 static int pmatch_destroy (void **user_data)
1547 return fc_destroy (FC_MATCH, user_data);
1548 } /* static int pmatch_destroy (void **) */
1550 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1551 notification_meta_t **meta, void **user_data)
1553 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1554 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1555 notification_meta_t **, void **) */
1557 static match_proc_t pmatch = {
1558 pmatch_create, pmatch_destroy, pmatch_match
1561 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1563 return fc_create (FC_TARGET, ci, user_data);
1564 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1566 static int ptarget_destroy (void **user_data)
1568 return fc_destroy (FC_TARGET, user_data);
1569 } /* static int ptarget_destroy (void **) */
1571 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1572 notification_meta_t **meta, void **user_data)
1574 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1575 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1576 notification_meta_t **, void **) */
1578 static target_proc_t ptarget = {
1579 ptarget_create, ptarget_destroy, ptarget_invoke
1583 * Exported Perl API.
1587 * Collectd::plugin_register_data_set (type, dataset).
1590 * type of the dataset
1593 * dataset to be registered
1595 static XS (Collectd_plugin_register_ds)
1602 log_warn ("Using plugin_register() to register new data-sets is "
1603 "deprecated - add new entries to a custom types.db instead.");
1606 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1610 log_debug ("Collectd::plugin_register_data_set: "
1611 "type = \"%s\", dataset = \"%s\"",
1612 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1616 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1617 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1621 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1629 } /* static XS (Collectd_plugin_register_ds) */
1632 * Collectd::plugin_unregister_data_set (type).
1635 * type of the dataset
1637 static XS (Collectd_plugin_unregister_ds)
1642 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1646 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1647 SvPV_nolen (ST (0)));
1649 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1653 } /* static XS (Collectd_plugin_register_ds) */
1656 * Collectd::plugin_dispatch_values (name, values).
1659 * name of the plugin
1662 * value list to submit
1664 static XS (Collectd_plugin_dispatch_values)
1673 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1677 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1678 SvPV_nolen (ST (/* stack index = */ 0)));
1680 values = ST (/* stack index = */ 0);
1685 /* Make sure the argument is a hash reference. */
1686 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1687 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1691 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1697 } /* static XS (Collectd_plugin_dispatch_values) */
1700 * Collectd::plugin_get_interval ().
1702 static XS (Collectd_plugin_get_interval)
1706 /* make sure we don't get any unused variable warnings for 'items';
1707 * don't abort, though */
1709 log_err ("Usage: Collectd::plugin_get_interval()");
1711 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1712 } /* static XS (Collectd_plugin_get_interval) */
1714 /* Collectd::plugin_write (plugin, ds, vl).
1717 * name of the plugin to call, may be 'undef'
1720 * data-set that describes the submitted values, may be 'undef'
1723 * value-list to be written
1725 static XS (Collectd__plugin_write)
1736 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1740 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1741 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1742 SvPV_nolen (ST (2)));
1744 if (! SvOK (ST (0)))
1747 plugin = SvPV_nolen (ST (0));
1750 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1751 ds_array = (AV *)SvRV (ds);
1752 else if (! SvOK (ds))
1755 log_err ("Collectd::plugin_write: Invalid data-set.");
1760 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1761 log_err ("Collectd::plugin_write: Invalid value-list.");
1765 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1771 } /* static XS (Collectd__plugin_write) */
1774 * Collectd::_plugin_flush (plugin, timeout, identifier).
1777 * name of the plugin to flush
1780 * timeout to use when flushing the data
1783 * data-set identifier to flush
1785 static XS (Collectd__plugin_flush)
1787 char *plugin = NULL;
1794 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1799 plugin = SvPV_nolen (ST (0));
1802 timeout = (int)SvIV (ST (1));
1805 id = SvPV_nolen (ST (2));
1807 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1808 "id = \"%s\"", plugin, timeout, id);
1810 if (0 == plugin_flush (plugin, timeout, id))
1814 } /* static XS (Collectd__plugin_flush) */
1817 * Collectd::plugin_dispatch_notification (notif).
1820 * notification to dispatch
1822 static XS (Collectd_plugin_dispatch_notification)
1831 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1835 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1836 SvPV_nolen (ST (0)));
1840 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1841 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1845 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1851 } /* static XS (Collectd_plugin_dispatch_notification) */
1854 * Collectd::plugin_log (level, message).
1857 * log level (LOG_DEBUG, ... LOG_ERR)
1862 static XS (Collectd_plugin_log)
1867 log_err ("Usage: Collectd::plugin_log(level, message)");
1871 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1873 } /* static XS (Collectd_plugin_log) */
1876 * Collectd::_fc_register (type, name)
1884 static XS (Collectd__fc_register)
1894 log_err ("Usage: Collectd::_fc_register(type, name)");
1898 type = SvIV (ST (0));
1899 name = SvPV_nolen (ST (1));
1901 if (FC_MATCH == type)
1902 ret = fc_register_match (name, pmatch);
1903 else if (FC_TARGET == type)
1904 ret = fc_register_target (name, ptarget);
1910 } /* static XS (Collectd_fc_register) */
1913 * Collectd::call_by_name (...).
1915 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1917 static XS (Collectd_call_by_name)
1922 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1923 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1928 name = SvPV_nolen (tmp);
1930 if (NULL == get_cv (name, 0)) {
1931 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1936 /* simply pass on the subroutine call without touching the stack,
1937 * thus leaving any arguments and return values in place */
1939 } /* static XS (Collectd_call_by_name) */
1942 * Interface to collectd.
1945 static int perl_init (void)
1950 if (NULL == perl_threads)
1954 c_ithread_t *t = NULL;
1956 pthread_mutex_lock (&perl_threads->mutex);
1957 t = c_ithread_create (perl_threads->head->interp);
1958 pthread_mutex_unlock (&perl_threads->mutex);
1963 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1964 aTHX, perl_threads->number_of_threads);
1966 /* Lock the base thread to avoid race conditions with c_ithread_create().
1967 * See https://github.com/collectd/collectd/issues/9 and
1968 * https://github.com/collectd/collectd/issues/1706 for details.
1970 assert (aTHX == perl_threads->head->interp);
1971 pthread_mutex_lock (&perl_threads->mutex);
1973 status = pplugin_call_all (aTHX_ PLUGIN_INIT);
1975 pthread_mutex_unlock (&perl_threads->mutex);
1978 } /* static int perl_init (void) */
1980 static int perl_read (void)
1984 if (NULL == perl_threads)
1988 c_ithread_t *t = NULL;
1990 pthread_mutex_lock (&perl_threads->mutex);
1991 t = c_ithread_create (perl_threads->head->interp);
1992 pthread_mutex_unlock (&perl_threads->mutex);
1997 /* Assert that we're not running as the base thread. Otherwise, we might
1998 * run into concurrency issues with c_ithread_create(). See
1999 * https://github.com/collectd/collectd/issues/9 for details. */
2000 assert (aTHX != perl_threads->head->interp);
2002 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
2003 aTHX, perl_threads->number_of_threads);
2004 return pplugin_call_all (aTHX_ PLUGIN_READ);
2005 } /* static int perl_read (void) */
2007 static int perl_write (const data_set_t *ds, const value_list_t *vl,
2008 user_data_t __attribute__((unused)) *user_data)
2013 if (NULL == perl_threads)
2017 c_ithread_t *t = NULL;
2019 pthread_mutex_lock (&perl_threads->mutex);
2020 t = c_ithread_create (perl_threads->head->interp);
2021 pthread_mutex_unlock (&perl_threads->mutex);
2026 /* Lock the base thread if this is not called from one of the read threads
2027 * to avoid race conditions with c_ithread_create(). See
2028 * https://github.com/collectd/collectd/issues/9 for details. */
2029 if (aTHX == perl_threads->head->interp)
2030 pthread_mutex_lock (&perl_threads->mutex);
2032 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
2033 aTHX, perl_threads->number_of_threads);
2034 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
2036 if (aTHX == perl_threads->head->interp)
2037 pthread_mutex_unlock (&perl_threads->mutex);
2040 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2042 static void perl_log (int level, const char *msg,
2043 user_data_t __attribute__((unused)) *user_data)
2047 if (NULL == perl_threads)
2051 c_ithread_t *t = NULL;
2053 pthread_mutex_lock (&perl_threads->mutex);
2054 t = c_ithread_create (perl_threads->head->interp);
2055 pthread_mutex_unlock (&perl_threads->mutex);
2060 /* Lock the base thread if this is not called from one of the read threads
2061 * to avoid race conditions with c_ithread_create(). See
2062 * https://github.com/collectd/collectd/issues/9 for details.
2065 if (aTHX == perl_threads->head->interp)
2066 pthread_mutex_lock (&perl_threads->mutex);
2068 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2070 if (aTHX == perl_threads->head->interp)
2071 pthread_mutex_unlock (&perl_threads->mutex);
2074 } /* static void perl_log (int, const char *) */
2076 static int perl_notify (const notification_t *notif,
2077 user_data_t __attribute__((unused)) *user_data)
2081 if (NULL == perl_threads)
2085 c_ithread_t *t = NULL;
2087 pthread_mutex_lock (&perl_threads->mutex);
2088 t = c_ithread_create (perl_threads->head->interp);
2089 pthread_mutex_unlock (&perl_threads->mutex);
2093 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2094 } /* static int perl_notify (const notification_t *) */
2096 static int perl_flush (cdtime_t timeout, const char *identifier,
2097 user_data_t __attribute__((unused)) *user_data)
2101 if (NULL == perl_threads)
2105 c_ithread_t *t = NULL;
2107 pthread_mutex_lock (&perl_threads->mutex);
2108 t = c_ithread_create (perl_threads->head->interp);
2109 pthread_mutex_unlock (&perl_threads->mutex);
2113 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2114 } /* static int perl_flush (const int) */
2116 static int perl_shutdown (void)
2118 c_ithread_t *t = NULL;
2124 plugin_unregister_complex_config ("perl");
2126 if (NULL == perl_threads)
2132 pthread_mutex_lock (&perl_threads->mutex);
2133 t = c_ithread_create (perl_threads->head->interp);
2134 pthread_mutex_unlock (&perl_threads->mutex);
2139 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2140 aTHX, perl_threads->number_of_threads);
2142 plugin_unregister_log ("perl");
2143 plugin_unregister_notification ("perl");
2144 plugin_unregister_init ("perl");
2145 plugin_unregister_read ("perl");
2146 plugin_unregister_write ("perl");
2147 plugin_unregister_flush ("perl");
2149 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2151 pthread_mutex_lock (&perl_threads->mutex);
2152 t = perl_threads->tail;
2155 struct timespec ts_wait;
2156 c_ithread_t *thr = t;
2158 /* the pointer has to be advanced before destroying
2159 * the thread as this will free the memory */
2164 /* Give some time to thread to exit from pi */
2165 WARNING ("perl shutdown: thread is running inside perl. Waiting.");
2167 ts_wait.tv_nsec = 500000;
2168 nanosleep (&ts_wait, NULL);
2171 ERROR ("perl shutdown: thread hangs inside perl. Thread killed.");
2172 pthread_kill (thr->pthread, SIGTERM);
2174 c_ithread_destroy (thr);
2177 pthread_mutex_unlock (&perl_threads->mutex);
2178 pthread_mutex_destroy (&perl_threads->mutex);
2179 pthread_mutexattr_destroy (&perl_threads->mutexattr);
2181 sfree (perl_threads);
2183 pthread_key_delete (perl_thr_key);
2187 plugin_unregister_shutdown ("perl");
2189 } /* static void perl_shutdown (void) */
2192 * Access functions for global variables.
2194 * These functions implement the "magic" used to access
2195 * the global variables from Perl.
2198 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2200 char *pv = mg->mg_ptr;
2203 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2205 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2207 char *pv = mg->mg_ptr;
2208 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2210 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2212 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2214 log_warn ("Accessing $interval_g is deprecated (and might not "
2215 "give the desired results) - plugin_get_interval() should "
2216 "be used instead.");
2217 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2219 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2221 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2223 double nv = (double)SvNV (var);
2224 log_warn ("Accessing $interval_g is deprecated (and might not "
2225 "give the desired results) - plugin_get_interval() should "
2226 "be used instead.");
2227 interval_g = DOUBLE_TO_CDTIME_T (nv);
2229 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2231 static MGVTBL g_pv_vtbl = {
2232 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2233 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2237 static MGVTBL g_interval_vtbl = {
2238 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2239 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2244 /* bootstrap the Collectd module */
2245 static void xs_init (pTHX)
2249 char *file = __FILE__;
2255 /* enable usage of Perl modules using shared libraries */
2256 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2259 for (i = 0; NULL != api[i].f; ++i)
2260 newXS (api[i].name, api[i].f, file);
2262 stash = gv_stashpv ("Collectd", 1);
2264 /* export "constants" */
2265 for (i = 0; '\0' != constants[i].name[0]; ++i)
2266 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2268 /* export global variables
2269 * by adding "magic" to the SV's representing the globale variables
2270 * perl is able to automagically call the get/set function when
2271 * accessing any such variable (this is basically the same as using
2273 /* global strings */
2274 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2275 tmp = get_sv (g_strings[i].name, 1);
2276 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2277 g_strings[i].var, 0);
2280 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2281 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2282 /* vtbl = */ &g_interval_vtbl,
2283 /* name = */ NULL, /* namelen = */ 0);
2286 } /* static void xs_init (pTHX) */
2288 /* Initialize the global Perl interpreter. */
2289 static int init_pi (int argc, char **argv)
2293 if (NULL != perl_threads)
2296 log_info ("Initializing Perl interpreter...");
2301 for (i = 0; i < argc; ++i)
2302 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2304 #endif /* COLLECT_DEBUG */
2306 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2307 log_err ("init_pi: pthread_key_create failed");
2309 /* this must not happen - cowardly giving up if it does */
2314 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2315 * triggers a "value computed is not used" warning by gcc. */
2318 PERL_SYS_INIT3 (&argc, &argv, &environ);
2320 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2321 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2323 pthread_mutexattr_init(&perl_threads->mutexattr);
2324 pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE);
2325 pthread_mutex_init (&perl_threads->mutex, &perl_threads->mutexattr);
2326 /* locking the mutex should not be necessary at this point
2327 * but let's just do it for the sake of completeness */
2328 pthread_mutex_lock (&perl_threads->mutex);
2330 perl_threads->head = c_ithread_create (NULL);
2331 perl_threads->tail = perl_threads->head;
2333 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2334 log_err ("init_pi: Not enough memory.");
2338 aTHX = perl_threads->head->interp;
2339 pthread_mutex_unlock (&perl_threads->mutex);
2341 perl_construct (aTHX);
2343 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2345 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2346 SV *err = get_sv ("@", 1);
2347 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2350 perl_destruct (perl_threads->head->interp);
2351 perl_free (perl_threads->head->interp);
2352 sfree (perl_threads);
2354 pthread_key_delete (perl_thr_key);
2358 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2359 sv_setpv (get_sv ("0", 0), "collectd");
2363 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2364 plugin_register_notification ("perl", perl_notify,
2365 /* user_data = */ NULL);
2366 plugin_register_init ("perl", perl_init);
2368 plugin_register_read ("perl", perl_read);
2370 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2371 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2372 plugin_register_shutdown ("perl", perl_shutdown);
2374 } /* static int init_pi (const char **, const int) */
2377 * LoadPlugin "<Plugin>"
2379 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2381 char module_name[DATA_MAX_NAME_LEN];
2385 if ((0 != ci->children_num) || (1 != ci->values_num)
2386 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2387 log_err ("LoadPlugin expects a single string argument.");
2391 value = ci->values[0].value.string;
2393 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2394 log_err ("Invalid module name %s", value);
2398 if (0 != init_pi (perl_argc, perl_argv))
2401 assert (NULL != perl_threads);
2402 assert (NULL != perl_threads->head);
2404 aTHX = perl_threads->head->interp;
2406 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2407 load_module (PERL_LOADMOD_NOIMPORT,
2408 newSVpv (module_name, strlen (module_name)), Nullsv);
2410 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2415 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2419 if ((0 != ci->children_num) || (1 != ci->values_num)
2420 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2421 log_err ("BaseName expects a single string argument.");
2425 value = ci->values[0].value.string;
2427 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2428 sstrncpy (base_name, value, sizeof (base_name));
2430 } /* static int perl_config_basename (oconfig_item_it *) */
2433 * EnableDebugger "<Package>"|""
2435 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2439 if ((0 != ci->children_num) || (1 != ci->values_num)
2440 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2441 log_err ("EnableDebugger expects a single string argument.");
2445 if (NULL != perl_threads) {
2446 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2450 value = ci->values[0].value.string;
2452 perl_argv = (char **)realloc (perl_argv,
2453 (++perl_argc + 1) * sizeof (char *));
2455 if (NULL == perl_argv) {
2456 log_err ("perl_config: Not enough memory.");
2460 if ('\0' == value[0]) {
2461 perl_argv[perl_argc - 1] = "-d";
2464 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2465 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2466 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2469 perl_argv[perl_argc] = NULL;
2471 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2474 * IncludeDir "<Dir>"
2476 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2480 if ((0 != ci->children_num) || (1 != ci->values_num)
2481 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2482 log_err ("IncludeDir expects a single string argument.");
2486 value = ci->values[0].value.string;
2489 perl_argv = (char **)realloc (perl_argv,
2490 (++perl_argc + 1) * sizeof (char *));
2492 if (NULL == perl_argv) {
2493 log_err ("perl_config: Not enough memory.");
2497 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2498 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2499 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2501 perl_argv[perl_argc] = NULL;
2504 /* prepend the directory to @INC */
2505 av_unshift (GvAVn (PL_incgv), 1);
2506 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2509 } /* static int perl_config_includedir (oconfig_item_it *) */
2514 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2524 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2525 log_err ("LoadPlugin expects a single string argument.");
2529 plugin = ci->values[0].value.string;
2532 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2536 log_err ("Unable to convert configuration to a Perl hash value.");
2537 config = (HV *)&PL_sv_undef;
2545 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2546 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2550 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2565 } /* static int perl_config_plugin (oconfig_item_it *) */
2567 static int perl_config (oconfig_item_t *ci)
2574 for (i = 0; i < ci->children_num; ++i) {
2575 oconfig_item_t *c = ci->children + i;
2576 int current_status = 0;
2578 if (NULL != perl_threads)
2580 if ((aTHX = PERL_GET_CONTEXT) == NULL)
2584 if (0 == strcasecmp (c->key, "LoadPlugin"))
2585 current_status = perl_config_loadplugin (aTHX_ c);
2586 else if (0 == strcasecmp (c->key, "BaseName"))
2587 current_status = perl_config_basename (aTHX_ c);
2588 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2589 current_status = perl_config_enabledebugger (aTHX_ c);
2590 else if (0 == strcasecmp (c->key, "IncludeDir"))
2591 current_status = perl_config_includedir (aTHX_ c);
2592 else if (0 == strcasecmp (c->key, "Plugin"))
2593 current_status = perl_config_plugin (aTHX_ c);
2596 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2600 /* fatal error - it's up to perl_config_* to clean up */
2601 if (0 > current_status) {
2602 log_err ("Configuration failed with a fatal error - "
2603 "plugin disabled!");
2604 return current_status;
2607 status += current_status;
2610 } /* static int perl_config (oconfig_item_t *) */
2612 void module_register (void)
2615 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2617 /* default options for the Perl interpreter */
2619 perl_argv[1] = "-MCollectd";
2620 perl_argv[2] = "-e";
2622 perl_argv[4] = NULL;
2624 plugin_register_complex_config ("perl", perl_config);
2626 } /* void module_register (void) */
2628 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */