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__
45 # pragma GCC poison sprintf
50 /* Some versions of Perl define their own version of DEBUG... :-/ */
55 /* ... while we want the definition found in plugin.h. */
59 #include "filter_chain.h"
63 #if !defined(USE_ITHREADS)
64 # error "Perl does not support ithreads!"
65 #endif /* !defined(USE_ITHREADS) */
67 /* clear the Perl sub's stack frame
68 * (this should only be used inside an XSUB) */
69 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
73 #define PLUGIN_WRITE 2
74 #define PLUGIN_SHUTDOWN 3
76 #define PLUGIN_NOTIF 5
77 #define PLUGIN_FLUSH 6
79 #define PLUGIN_TYPES 7
81 #define PLUGIN_CONFIG 254
82 #define PLUGIN_DATASET 255
89 #define FC_CB_CREATE 0
90 #define FC_CB_DESTROY 1
95 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
96 #define log_info(...) INFO ("perl: " __VA_ARGS__)
97 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
98 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
100 /* this is defined in DynaLoader.a */
101 void boot_DynaLoader (PerlInterpreter *, CV *);
103 static XS (Collectd_plugin_register_ds);
104 static XS (Collectd_plugin_unregister_ds);
105 static XS (Collectd_plugin_dispatch_values);
106 static XS (Collectd_plugin_get_interval);
107 static XS (Collectd__plugin_write);
108 static XS (Collectd__plugin_flush);
109 static XS (Collectd_plugin_dispatch_notification);
110 static XS (Collectd_plugin_log);
111 static XS (Collectd__fc_register);
112 static XS (Collectd_call_by_name);
118 typedef struct c_ithread_s {
119 /* the thread's Perl interpreter */
120 PerlInterpreter *interp;
122 /* double linked list of threads */
123 struct c_ithread_s *prev;
124 struct c_ithread_s *next;
132 /* some usage stats */
133 int number_of_threads;
134 #endif /* COLLECT_DEBUG */
136 pthread_mutex_t mutex;
139 /* name / user_data for Perl matches / targets */
145 #define PFC_USER_DATA_FREE(data) \
147 sfree ((data)->name); \
148 if (NULL != (data)->user_data) \
149 sv_free ((data)->user_data); \
156 extern char **environ;
162 /* if perl_threads != NULL perl_threads->head must
163 * point to the "base" thread */
164 static c_ithread_list_t *perl_threads = NULL;
166 /* the key used to store each pthread's ithread */
167 static pthread_key_t perl_thr_key;
169 static int perl_argc = 0;
170 static char **perl_argv = NULL;
172 static char base_name[DATA_MAX_NAME_LEN] = "";
179 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
180 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
181 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
182 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
183 { "Collectd::_plugin_write", Collectd__plugin_write },
184 { "Collectd::_plugin_flush", Collectd__plugin_flush },
185 { "Collectd::plugin_dispatch_notification",
186 Collectd_plugin_dispatch_notification },
187 { "Collectd::plugin_log", Collectd_plugin_log },
188 { "Collectd::_fc_register", Collectd__fc_register },
189 { "Collectd::call_by_name", Collectd_call_by_name },
198 { "Collectd::TYPE_INIT", PLUGIN_INIT },
199 { "Collectd::TYPE_READ", PLUGIN_READ },
200 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
201 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
202 { "Collectd::TYPE_LOG", PLUGIN_LOG },
203 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
204 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
205 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
206 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
207 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
208 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
209 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
210 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
211 { "Collectd::LOG_ERR", LOG_ERR },
212 { "Collectd::LOG_WARNING", LOG_WARNING },
213 { "Collectd::LOG_NOTICE", LOG_NOTICE },
214 { "Collectd::LOG_INFO", LOG_INFO },
215 { "Collectd::LOG_DEBUG", LOG_DEBUG },
216 { "Collectd::FC_MATCH", FC_MATCH },
217 { "Collectd::FC_TARGET", FC_TARGET },
218 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
219 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
220 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
221 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
222 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
223 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
224 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
225 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
226 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
227 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
228 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
237 { "Collectd::hostname_g", hostname_g },
242 * Helper functions for data type conversion.
257 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
261 if ((NULL == hash) || (NULL == ds))
264 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
265 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
268 log_err ("hv2data_source: No DS name given.");
272 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
273 ds->type = SvIV (*tmp);
275 if ((DS_TYPE_COUNTER != ds->type)
276 && (DS_TYPE_GAUGE != ds->type)
277 && (DS_TYPE_DERIVE != ds->type)
278 && (DS_TYPE_ABSOLUTE != ds->type)) {
279 log_err ("hv2data_source: Invalid DS type.");
284 ds->type = DS_TYPE_COUNTER;
287 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
288 ds->min = SvNV (*tmp);
292 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
293 ds->max = SvNV (*tmp);
297 } /* static int hv2data_source (HV *, data_source_t *) */
299 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
301 const data_set_t *ds;
305 if ((NULL == name) || (NULL == array) || (NULL == value))
308 if (av_len (array) < len - 1)
309 len = av_len (array) + 1;
314 ds = plugin_get_ds (name);
316 log_err ("av2value: Unknown dataset \"%s\"", name);
320 if (ds->ds_num < len) {
321 log_warn ("av2value: Value length exceeds data set length.");
325 for (i = 0; i < len; ++i) {
326 SV **tmp = av_fetch (array, i, 0);
329 if (DS_TYPE_COUNTER == ds->ds[i].type)
330 value[i].counter = SvIV (*tmp);
331 else if (DS_TYPE_GAUGE == ds->ds[i].type)
332 value[i].gauge = SvNV (*tmp);
333 else if (DS_TYPE_DERIVE == ds->ds[i].type)
334 value[i].derive = SvIV (*tmp);
335 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
336 value[i].absolute = SvIV (*tmp);
343 } /* static int av2value (char *, AV *, value_t *, int) */
348 * values => [ @values ],
352 * plugin_instance => $pinstance,
353 * type_instance => $tinstance,
356 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
360 if ((NULL == hash) || (NULL == vl))
363 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
364 log_err ("hv2value_list: No type given.");
368 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
370 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
371 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
372 log_err ("hv2value_list: No valid values given.");
377 AV *array = (AV *)SvRV (*tmp);
378 int len = av_len (array) + 1;
383 vl->values = (value_t *)smalloc (len * sizeof (value_t));
384 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
387 if (-1 == vl->values_len) {
393 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
395 double t = SvNV (*tmp);
396 vl->time = DOUBLE_TO_CDTIME_T (t);
399 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
401 double t = SvNV (*tmp);
402 vl->interval = DOUBLE_TO_CDTIME_T (t);
405 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
406 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
408 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
410 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
411 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
413 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
414 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
415 sizeof (vl->plugin_instance));
417 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
418 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
419 sizeof (vl->type_instance));
421 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
423 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
427 if ((NULL == array) || (NULL == name) || (NULL == ds))
430 len = av_len (array);
433 log_err ("av2data_set: Invalid data set.");
437 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
438 ds->ds_num = len + 1;
440 for (i = 0; i <= len; ++i) {
441 SV **elem = av_fetch (array, i, 0);
444 log_err ("av2data_set: Failed to fetch data source %i.", i);
448 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
449 log_err ("av2data_set: Invalid data source.");
453 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
456 log_debug ("av2data_set: "
457 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
458 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
461 sstrncpy (ds->type, name, sizeof (ds->type));
463 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
468 * severity => $severity,
474 * plugin_instance => $instance,
475 * type_instance => $type_instance,
476 * meta => [ { name => <name>, value => <value> }, ... ]
479 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
481 notification_meta_t **m = meta;
483 int len = av_len (array);
486 for (i = 0; i <= len; ++i) {
487 SV **tmp = av_fetch (array, i, 0);
493 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
494 log_warn ("av2notification_meta: Skipping invalid "
495 "meta information.");
499 hash = (HV *)SvRV (*tmp);
501 *m = (notification_meta_t *)smalloc (sizeof (**m));
503 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
504 log_warn ("av2notification_meta: Skipping invalid "
505 "meta information.");
509 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
511 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
512 log_warn ("av2notification_meta: Skipping invalid "
513 "meta information.");
520 (*m)->nm_value.nm_double = SvNVX (*tmp);
521 (*m)->type = NM_TYPE_DOUBLE;
523 else if (SvUOK (*tmp)) {
524 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
525 (*m)->type = NM_TYPE_UNSIGNED_INT;
527 else if (SvIOK (*tmp)) {
528 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
529 (*m)->type = NM_TYPE_SIGNED_INT;
532 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
533 (*m)->type = NM_TYPE_STRING;
540 } /* static int av2notification_meta (AV *, notification_meta_t *) */
542 static int hv2notification (pTHX_ HV *hash, notification_t *n)
546 if ((NULL == hash) || (NULL == n))
549 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
550 n->severity = SvIV (*tmp);
552 n->severity = NOTIF_FAILURE;
554 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
556 double t = SvNV (*tmp);
557 n->time = DOUBLE_TO_CDTIME_T (t);
562 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
563 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
565 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
566 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
568 sstrncpy (n->host, hostname_g, sizeof (n->host));
570 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
571 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
573 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
574 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
575 sizeof (n->plugin_instance));
577 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
578 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
580 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
581 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
582 sizeof (n->type_instance));
585 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
586 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
587 log_warn ("hv2notification: Ignoring invalid meta information.");
591 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
592 plugin_notification_meta_free (n->meta);
599 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
601 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
605 if ((NULL == ds) || (NULL == array))
608 av_extend (array, ds->ds_num);
610 for (i = 0; i < ds->ds_num; ++i) {
611 HV *source = newHV ();
613 if (NULL == hv_store (source, "name", 4,
614 newSVpv (ds->ds[i].name, 0), 0))
617 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
620 if (! isnan (ds->ds[i].min))
621 if (NULL == hv_store (source, "min", 3,
622 newSVnv (ds->ds[i].min), 0))
625 if (! isnan (ds->ds[i].max))
626 if (NULL == hv_store (source, "max", 3,
627 newSVnv (ds->ds[i].max), 0))
630 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
634 } /* static int data_set2av (data_set_t *, AV *) */
636 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
643 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
646 len = vl->values_len;
648 if (ds->ds_num < len) {
649 log_warn ("value2av: Value length exceeds data set length.");
654 av_extend (values, len - 1);
656 for (i = 0; i < len; ++i) {
659 if (DS_TYPE_COUNTER == ds->ds[i].type)
660 val = newSViv (vl->values[i].counter);
661 else if (DS_TYPE_GAUGE == ds->ds[i].type)
662 val = newSVnv (vl->values[i].gauge);
663 else if (DS_TYPE_DERIVE == ds->ds[i].type)
664 val = newSViv (vl->values[i].derive);
665 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
666 val = newSViv (vl->values[i].absolute);
668 if (NULL == av_store (values, i, val)) {
674 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
679 double t = CDTIME_T_TO_DOUBLE (vl->time);
680 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
685 double t = CDTIME_T_TO_DOUBLE (vl->interval);
686 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
690 if ('\0' != vl->host[0])
691 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
694 if ('\0' != vl->plugin[0])
695 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
698 if ('\0' != vl->plugin_instance[0])
699 if (NULL == hv_store (hash, "plugin_instance", 15,
700 newSVpv (vl->plugin_instance, 0), 0))
703 if ('\0' != vl->type[0])
704 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
707 if ('\0' != vl->type_instance[0])
708 if (NULL == hv_store (hash, "type_instance", 13,
709 newSVpv (vl->type_instance, 0), 0))
712 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
714 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
724 av_extend (array, meta_num);
726 for (i = 0; NULL != meta; meta = meta->next, ++i) {
730 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
733 if (NM_TYPE_STRING == meta->type)
734 value = newSVpv (meta->nm_value.nm_string, 0);
735 else if (NM_TYPE_SIGNED_INT == meta->type)
736 value = newSViv (meta->nm_value.nm_signed_int);
737 else if (NM_TYPE_UNSIGNED_INT == meta->type)
738 value = newSVuv (meta->nm_value.nm_unsigned_int);
739 else if (NM_TYPE_DOUBLE == meta->type)
740 value = newSVnv (meta->nm_value.nm_double);
741 else if (NM_TYPE_BOOLEAN == meta->type)
742 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
746 if (NULL == hv_store (m, "value", 5, value, 0)) {
751 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
758 } /* static int notification_meta2av (notification_meta_t *, AV *) */
760 static int notification2hv (pTHX_ notification_t *n, HV *hash)
762 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
767 double t = CDTIME_T_TO_DOUBLE (n->time);
768 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
772 if ('\0' != *n->message)
773 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
776 if ('\0' != *n->host)
777 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
780 if ('\0' != *n->plugin)
781 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
784 if ('\0' != *n->plugin_instance)
785 if (NULL == hv_store (hash, "plugin_instance", 15,
786 newSVpv (n->plugin_instance, 0), 0))
789 if ('\0' != *n->type)
790 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
793 if ('\0' != *n->type_instance)
794 if (NULL == hv_store (hash, "type_instance", 13,
795 newSVpv (n->type_instance, 0), 0))
798 if (NULL != n->meta) {
800 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
801 || (NULL == hv_store (hash, "meta", 4,
802 newRV_noinc ((SV *)meta), 0))) {
809 } /* static int notification2hv (notification_t *, HV *) */
811 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
818 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
822 if (0 < ci->values_num)
823 av_extend (values, ci->values_num);
825 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
831 for (i = 0; i < ci->values_num; ++i) {
834 switch (ci->values[i].type) {
835 case OCONFIG_TYPE_STRING:
836 value = newSVpv (ci->values[i].value.string, 0);
838 case OCONFIG_TYPE_NUMBER:
839 value = newSVnv ((NV)ci->values[i].value.number);
841 case OCONFIG_TYPE_BOOLEAN:
842 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
845 log_err ("oconfig_item2hv: Invalid value type %i.",
847 value = &PL_sv_undef;
850 if (NULL == av_store (values, i, value)) {
856 /* ignoring 'parent' member which is uninteresting in this case */
859 if (0 < ci->children_num)
860 av_extend (children, ci->children_num);
862 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
868 for (i = 0; i < ci->children_num; ++i) {
869 HV *child = newHV ();
871 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
877 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
884 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
887 * Internal functions.
890 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
892 if (base_name[0] == '\0')
893 status = ssnprintf (buf, buf_len, "%s", module);
895 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
896 if ((status < 0) || ((unsigned int)status >= buf_len))
899 } /* char *get_module_name */
902 * Add a plugin's data set definition.
904 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
910 if ((NULL == name) || (NULL == dataset))
913 if (0 != av2data_set (aTHX_ dataset, name, &ds))
916 ret = plugin_register_data_set (&ds);
920 } /* static int pplugin_register_data_set (char *, SV *) */
923 * Remove a plugin's data set definition.
925 static int pplugin_unregister_data_set (char *name)
929 return plugin_unregister_data_set (name);
930 } /* static int pplugin_unregister_data_set (char *) */
933 * Submit the values to the write functions.
935 static int pplugin_dispatch_values (pTHX_ HV *values)
937 value_list_t vl = VALUE_LIST_INIT;
944 if (0 != hv2value_list (aTHX_ values, &vl))
947 ret = plugin_dispatch_values (&vl);
951 } /* static int pplugin_dispatch_values (char *, HV *) */
954 * Submit the values to a single write function.
956 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
959 value_list_t vl = VALUE_LIST_INIT;
966 if (0 != hv2value_list (aTHX_ values, &vl))
969 if ((NULL != data_set)
970 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
973 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
975 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
976 NULL == plugin ? "<any>" : plugin, ret);
978 if (NULL != data_set)
982 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
985 * Dispatch a notification.
987 static int pplugin_dispatch_notification (pTHX_ HV *notif)
996 memset (&n, 0, sizeof (n));
998 if (0 != hv2notification (aTHX_ notif, &n))
1001 ret = plugin_dispatch_notification (&n);
1002 plugin_notification_meta_free (n.meta);
1004 } /* static int pplugin_dispatch_notification (HV *) */
1007 * Call all working functions of the given type.
1009 static int pplugin_call_all (pTHX_ int type, ...)
1018 if ((type < 0) || (type >= PLUGIN_TYPES))
1021 va_start (ap, type);
1028 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1030 if (PLUGIN_WRITE == type) {
1032 * $_[0] = $plugin_type;
1047 * values => [ $v1, ... ],
1049 * host => $hostname,
1050 * plugin => $plugin,
1052 * plugin_instance => $instance,
1053 * type_instance => $type_instance
1062 ds = va_arg (ap, data_set_t *);
1063 vl = va_arg (ap, value_list_t *);
1065 if (-1 == data_set2av (aTHX_ ds, pds)) {
1068 pds = (AV *)&PL_sv_undef;
1072 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1075 pvl = (HV *)&PL_sv_undef;
1079 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1080 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1081 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1083 else if (PLUGIN_LOG == type) {
1089 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1090 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1092 else if (PLUGIN_NOTIF == type) {
1096 * severity => $severity,
1100 * plugin => $plugin,
1102 * plugin_instance => $instance,
1103 * type_instance => $type_instance
1107 HV *notif = newHV ();
1109 n = va_arg (ap, notification_t *);
1111 if (-1 == notification2hv (aTHX_ n, notif)) {
1114 notif = (HV *)&PL_sv_undef;
1118 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1120 else if (PLUGIN_FLUSH == type) {
1125 * $_[1] = $identifier;
1127 timeout = va_arg (ap, cdtime_t);
1129 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1130 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1135 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1150 } /* static int pplugin_call_all (int, ...) */
1153 * collectd's perl interpreter based thread implementation.
1155 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1158 /* must be called with perl_threads->mutex locked */
1159 static void c_ithread_destroy (c_ithread_t *ithread)
1161 dTHXa (ithread->interp);
1163 assert (NULL != perl_threads);
1165 PERL_SET_CONTEXT (aTHX);
1166 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1171 --perl_threads->number_of_threads;
1172 #endif /* COLLECT_DEBUG */
1174 perl_destruct (aTHX);
1177 if (NULL == ithread->prev)
1178 perl_threads->head = ithread->next;
1180 ithread->prev->next = ithread->next;
1182 if (NULL == ithread->next)
1183 perl_threads->tail = ithread->prev;
1185 ithread->next->prev = ithread->prev;
1189 } /* static void c_ithread_destroy (c_ithread_t *) */
1191 static void c_ithread_destructor (void *arg)
1193 c_ithread_t *ithread = (c_ithread_t *)arg;
1194 c_ithread_t *t = NULL;
1196 if (NULL == perl_threads)
1199 pthread_mutex_lock (&perl_threads->mutex);
1201 for (t = perl_threads->head; NULL != t; t = t->next)
1205 /* the ithread no longer exists */
1209 c_ithread_destroy (ithread);
1211 pthread_mutex_unlock (&perl_threads->mutex);
1213 } /* static void c_ithread_destructor (void *) */
1215 /* must be called with perl_threads->mutex locked */
1216 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1218 c_ithread_t *t = NULL;
1221 assert (NULL != perl_threads);
1223 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1224 memset (t, 0, sizeof (c_ithread_t));
1226 t->interp = (NULL == base)
1228 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1232 if ((NULL != base) && (NULL != PL_endav)) {
1233 av_clear (PL_endav);
1234 av_undef (PL_endav);
1239 ++perl_threads->number_of_threads;
1240 #endif /* COLLECT_DEBUG */
1244 if (NULL == perl_threads->tail) {
1245 perl_threads->head = t;
1249 perl_threads->tail->next = t;
1250 t->prev = perl_threads->tail;
1253 perl_threads->tail = t;
1255 pthread_setspecific (perl_thr_key, (const void *)t);
1257 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1260 * Filter chains implementation.
1263 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1270 notification_meta_t **meta = NULL;
1275 if ((type < 0) || (type >= FC_TYPES))
1278 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1281 va_start (ap, data);
1288 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1289 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1290 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1292 if (FC_CB_CREATE == cb_type) {
1295 * $_[1] = $user_data;
1298 HV *config = newHV ();
1300 ci = va_arg (ap, oconfig_item_t *);
1302 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1305 config = (HV *)&PL_sv_undef;
1309 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1311 else if (FC_CB_DESTROY == cb_type) {
1313 * $_[1] = $user_data;
1316 /* nothing to be done - the user data pointer
1317 * is pushed onto the stack later */
1319 else if (FC_CB_EXEC == cb_type) {
1324 * $_[3] = $user_data;
1332 ds = va_arg (ap, data_set_t *);
1333 vl = va_arg (ap, value_list_t *);
1334 meta = va_arg (ap, notification_meta_t **);
1336 if (0 != data_set2av (aTHX_ ds, pds)) {
1339 pds = (AV *)&PL_sv_undef;
1343 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1346 pvl = (HV *)&PL_sv_undef;
1353 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1356 pmeta = (AV *)&PL_sv_undef;
1361 pmeta = (AV *)&PL_sv_undef;
1364 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1365 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1366 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1369 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1373 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1375 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1376 assert (pmeta != NULL);
1378 plugin_notification_meta_free (*meta);
1379 av2notification_meta (aTHX_ pmeta, meta);
1386 /* the exec callbacks return a status, while
1387 * the others return a boolean value */
1388 if (FC_CB_EXEC == cb_type)
1390 else if (! SvTRUE (tmp))
1400 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1402 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1404 pfc_user_data_t *data;
1410 if (NULL == perl_threads)
1414 c_ithread_t *t = NULL;
1416 pthread_mutex_lock (&perl_threads->mutex);
1417 t = c_ithread_create (perl_threads->head->interp);
1418 pthread_mutex_unlock (&perl_threads->mutex);
1423 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1424 aTHX, perl_threads->number_of_threads);
1426 if ((1 != ci->values_num)
1427 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1428 log_warn ("A \"%s\" block expects a single string argument.",
1429 (FC_MATCH == type) ? "Match" : "Target");
1433 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1434 data->name = sstrdup (ci->values[0].value.string);
1435 data->user_data = newSV (0);
1437 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1440 PFC_USER_DATA_FREE (data);
1444 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1446 static int fc_destroy (int type, void **user_data)
1448 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1454 if ((NULL == perl_threads) || (NULL == data))
1458 c_ithread_t *t = NULL;
1460 pthread_mutex_lock (&perl_threads->mutex);
1461 t = c_ithread_create (perl_threads->head->interp);
1462 pthread_mutex_unlock (&perl_threads->mutex);
1467 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1468 aTHX, perl_threads->number_of_threads);
1470 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1472 PFC_USER_DATA_FREE (data);
1475 } /* static int fc_destroy (int, void **) */
1477 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1478 notification_meta_t **meta, void **user_data)
1480 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1484 if (NULL == perl_threads)
1487 assert (NULL != data);
1490 c_ithread_t *t = NULL;
1492 pthread_mutex_lock (&perl_threads->mutex);
1493 t = c_ithread_create (perl_threads->head->interp);
1494 pthread_mutex_unlock (&perl_threads->mutex);
1499 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1500 aTHX, perl_threads->number_of_threads);
1502 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1503 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1504 notification_meta_t **, void **) */
1506 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1508 return fc_create (FC_MATCH, ci, user_data);
1509 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1511 static int pmatch_destroy (void **user_data)
1513 return fc_destroy (FC_MATCH, user_data);
1514 } /* static int pmatch_destroy (void **) */
1516 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1517 notification_meta_t **meta, void **user_data)
1519 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1520 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1521 notification_meta_t **, void **) */
1523 static match_proc_t pmatch = {
1524 pmatch_create, pmatch_destroy, pmatch_match
1527 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1529 return fc_create (FC_TARGET, ci, user_data);
1530 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1532 static int ptarget_destroy (void **user_data)
1534 return fc_destroy (FC_TARGET, user_data);
1535 } /* static int ptarget_destroy (void **) */
1537 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1538 notification_meta_t **meta, void **user_data)
1540 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1541 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1542 notification_meta_t **, void **) */
1544 static target_proc_t ptarget = {
1545 ptarget_create, ptarget_destroy, ptarget_invoke
1549 * Exported Perl API.
1553 * Collectd::plugin_register_data_set (type, dataset).
1556 * type of the dataset
1559 * dataset to be registered
1561 static XS (Collectd_plugin_register_ds)
1568 log_warn ("Using plugin_register() to register new data-sets is "
1569 "deprecated - add new entries to a custom types.db instead.");
1572 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1576 log_debug ("Collectd::plugin_register_data_set: "
1577 "type = \"%s\", dataset = \"%s\"",
1578 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1582 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1583 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1587 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1595 } /* static XS (Collectd_plugin_register_ds) */
1598 * Collectd::plugin_unregister_data_set (type).
1601 * type of the dataset
1603 static XS (Collectd_plugin_unregister_ds)
1608 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1612 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1613 SvPV_nolen (ST (0)));
1615 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1619 } /* static XS (Collectd_plugin_register_ds) */
1622 * Collectd::plugin_dispatch_values (name, values).
1625 * name of the plugin
1628 * value list to submit
1630 static XS (Collectd_plugin_dispatch_values)
1639 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1643 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1644 SvPV_nolen (ST (/* stack index = */ 0)));
1646 values = ST (/* stack index = */ 0);
1648 /* Make sure the argument is a hash reference. */
1649 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1650 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1657 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1663 } /* static XS (Collectd_plugin_dispatch_values) */
1666 * Collectd::plugin_get_interval ().
1668 static XS (Collectd_plugin_get_interval)
1672 /* make sure we don't get any unused variable warnings for 'items';
1673 * don't abort, though */
1675 log_err ("Usage: Collectd::plugin_get_interval()");
1677 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1678 } /* static XS (Collectd_plugin_get_interval) */
1680 /* Collectd::plugin_write (plugin, ds, vl).
1683 * name of the plugin to call, may be 'undef'
1686 * data-set that describes the submitted values, may be 'undef'
1689 * value-list to be written
1691 static XS (Collectd__plugin_write)
1702 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1706 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1707 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1708 SvPV_nolen (ST (2)));
1710 if (! SvOK (ST (0)))
1713 plugin = SvPV_nolen (ST (0));
1716 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1717 ds_array = (AV *)SvRV (ds);
1718 else if (! SvOK (ds))
1721 log_err ("Collectd::plugin_write: Invalid data-set.");
1726 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1727 log_err ("Collectd::plugin_write: Invalid value-list.");
1731 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1737 } /* static XS (Collectd__plugin_write) */
1740 * Collectd::_plugin_flush (plugin, timeout, identifier).
1743 * name of the plugin to flush
1746 * timeout to use when flushing the data
1749 * data-set identifier to flush
1751 static XS (Collectd__plugin_flush)
1753 char *plugin = NULL;
1760 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1765 plugin = SvPV_nolen (ST (0));
1768 timeout = (int)SvIV (ST (1));
1771 id = SvPV_nolen (ST (2));
1773 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1774 "id = \"%s\"", plugin, timeout, id);
1776 if (0 == plugin_flush (plugin, timeout, id))
1780 } /* static XS (Collectd__plugin_flush) */
1783 * Collectd::plugin_dispatch_notification (notif).
1786 * notification to dispatch
1788 static XS (Collectd_plugin_dispatch_notification)
1797 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1801 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1802 SvPV_nolen (ST (0)));
1806 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1807 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1811 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1817 } /* static XS (Collectd_plugin_dispatch_notification) */
1820 * Collectd::plugin_log (level, message).
1823 * log level (LOG_DEBUG, ... LOG_ERR)
1828 static XS (Collectd_plugin_log)
1833 log_err ("Usage: Collectd::plugin_log(level, message)");
1837 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1839 } /* static XS (Collectd_plugin_log) */
1842 * Collectd::_fc_register (type, name)
1850 static XS (Collectd__fc_register)
1860 log_err ("Usage: Collectd::_fc_register(type, name)");
1864 type = SvIV (ST (0));
1865 name = SvPV_nolen (ST (1));
1867 if (FC_MATCH == type)
1868 ret = fc_register_match (name, pmatch);
1869 else if (FC_TARGET == type)
1870 ret = fc_register_target (name, ptarget);
1876 } /* static XS (Collectd_fc_register) */
1879 * Collectd::call_by_name (...).
1881 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1883 static XS (Collectd_call_by_name)
1888 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1889 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1894 name = SvPV_nolen (tmp);
1896 if (NULL == get_cv (name, 0)) {
1897 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1902 /* simply pass on the subroutine call without touching the stack,
1903 * thus leaving any arguments and return values in place */
1905 } /* static XS (Collectd_call_by_name) */
1908 * Interface to collectd.
1911 static int perl_init (void)
1915 if (NULL == perl_threads)
1919 c_ithread_t *t = NULL;
1921 pthread_mutex_lock (&perl_threads->mutex);
1922 t = c_ithread_create (perl_threads->head->interp);
1923 pthread_mutex_unlock (&perl_threads->mutex);
1928 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1929 aTHX, perl_threads->number_of_threads);
1930 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1931 } /* static int perl_init (void) */
1933 static int perl_read (void)
1937 if (NULL == perl_threads)
1941 c_ithread_t *t = NULL;
1943 pthread_mutex_lock (&perl_threads->mutex);
1944 t = c_ithread_create (perl_threads->head->interp);
1945 pthread_mutex_unlock (&perl_threads->mutex);
1950 /* Assert that we're not running as the base thread. Otherwise, we might
1951 * run into concurrency issues with c_ithread_create(). See
1952 * https://github.com/collectd/collectd/issues/9 for details. */
1953 assert (aTHX != perl_threads->head->interp);
1955 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1956 aTHX, perl_threads->number_of_threads);
1957 return pplugin_call_all (aTHX_ PLUGIN_READ);
1958 } /* static int perl_read (void) */
1960 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1961 user_data_t __attribute__((unused)) *user_data)
1966 if (NULL == perl_threads)
1970 c_ithread_t *t = NULL;
1972 pthread_mutex_lock (&perl_threads->mutex);
1973 t = c_ithread_create (perl_threads->head->interp);
1974 pthread_mutex_unlock (&perl_threads->mutex);
1979 /* Lock the base thread if this is not called from one of the read threads
1980 * to avoid race conditions with c_ithread_create(). See
1981 * https://github.com/collectd/collectd/issues/9 for details. */
1982 if (aTHX == perl_threads->head->interp)
1983 pthread_mutex_lock (&perl_threads->mutex);
1985 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1986 aTHX, perl_threads->number_of_threads);
1987 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1989 if (aTHX == perl_threads->head->interp)
1990 pthread_mutex_unlock (&perl_threads->mutex);
1993 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1995 static void perl_log (int level, const char *msg,
1996 user_data_t __attribute__((unused)) *user_data)
2000 if (NULL == perl_threads)
2004 c_ithread_t *t = NULL;
2006 pthread_mutex_lock (&perl_threads->mutex);
2007 t = c_ithread_create (perl_threads->head->interp);
2008 pthread_mutex_unlock (&perl_threads->mutex);
2013 /* Lock the base thread if this is not called from one of the read threads
2014 * to avoid race conditions with c_ithread_create(). See
2015 * https://github.com/collectd/collectd/issues/9 for details. */
2016 if (aTHX == perl_threads->head->interp)
2017 pthread_mutex_lock (&perl_threads->mutex);
2019 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2021 if (aTHX == perl_threads->head->interp)
2022 pthread_mutex_unlock (&perl_threads->mutex);
2025 } /* static void perl_log (int, const char *) */
2027 static int perl_notify (const notification_t *notif,
2028 user_data_t __attribute__((unused)) *user_data)
2032 if (NULL == perl_threads)
2036 c_ithread_t *t = NULL;
2038 pthread_mutex_lock (&perl_threads->mutex);
2039 t = c_ithread_create (perl_threads->head->interp);
2040 pthread_mutex_unlock (&perl_threads->mutex);
2044 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2045 } /* static int perl_notify (const notification_t *) */
2047 static int perl_flush (cdtime_t timeout, const char *identifier,
2048 user_data_t __attribute__((unused)) *user_data)
2052 if (NULL == perl_threads)
2056 c_ithread_t *t = NULL;
2058 pthread_mutex_lock (&perl_threads->mutex);
2059 t = c_ithread_create (perl_threads->head->interp);
2060 pthread_mutex_unlock (&perl_threads->mutex);
2064 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2065 } /* static int perl_flush (const int) */
2067 static int perl_shutdown (void)
2069 c_ithread_t *t = NULL;
2075 plugin_unregister_complex_config ("perl");
2077 if (NULL == perl_threads)
2081 c_ithread_t *t = NULL;
2083 pthread_mutex_lock (&perl_threads->mutex);
2084 t = c_ithread_create (perl_threads->head->interp);
2085 pthread_mutex_unlock (&perl_threads->mutex);
2090 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2091 aTHX, perl_threads->number_of_threads);
2093 plugin_unregister_log ("perl");
2094 plugin_unregister_notification ("perl");
2095 plugin_unregister_init ("perl");
2096 plugin_unregister_read ("perl");
2097 plugin_unregister_write ("perl");
2098 plugin_unregister_flush ("perl");
2100 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2102 pthread_mutex_lock (&perl_threads->mutex);
2103 t = perl_threads->tail;
2106 c_ithread_t *thr = t;
2108 /* the pointer has to be advanced before destroying
2109 * the thread as this will free the memory */
2112 c_ithread_destroy (thr);
2115 pthread_mutex_unlock (&perl_threads->mutex);
2116 pthread_mutex_destroy (&perl_threads->mutex);
2118 sfree (perl_threads);
2120 pthread_key_delete (perl_thr_key);
2124 plugin_unregister_shutdown ("perl");
2126 } /* static void perl_shutdown (void) */
2129 * Access functions for global variables.
2131 * These functions implement the "magic" used to access
2132 * the global variables from Perl.
2135 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2137 char *pv = mg->mg_ptr;
2140 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2142 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2144 char *pv = mg->mg_ptr;
2145 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2147 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2149 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2151 log_warn ("Accessing $interval_g is deprecated (and might not "
2152 "give the desired results) - plugin_get_interval() should "
2153 "be used instead.");
2154 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2156 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2158 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2160 double nv = (double)SvNV (var);
2161 log_warn ("Accessing $interval_g is deprecated (and might not "
2162 "give the desired results) - plugin_get_interval() should "
2163 "be used instead.");
2164 interval_g = DOUBLE_TO_CDTIME_T (nv);
2166 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2168 static MGVTBL g_pv_vtbl = {
2169 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2170 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2174 static MGVTBL g_interval_vtbl = {
2175 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2176 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2181 /* bootstrap the Collectd module */
2182 static void xs_init (pTHX)
2186 char *file = __FILE__;
2192 /* enable usage of Perl modules using shared libraries */
2193 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2196 for (i = 0; NULL != api[i].f; ++i)
2197 newXS (api[i].name, api[i].f, file);
2199 stash = gv_stashpv ("Collectd", 1);
2201 /* export "constants" */
2202 for (i = 0; '\0' != constants[i].name[0]; ++i)
2203 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2205 /* export global variables
2206 * by adding "magic" to the SV's representing the globale variables
2207 * perl is able to automagically call the get/set function when
2208 * accessing any such variable (this is basically the same as using
2210 /* global strings */
2211 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2212 tmp = get_sv (g_strings[i].name, 1);
2213 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2214 g_strings[i].var, 0);
2217 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2218 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2219 /* vtbl = */ &g_interval_vtbl,
2220 /* name = */ NULL, /* namelen = */ 0);
2223 } /* static void xs_init (pTHX) */
2225 /* Initialize the global Perl interpreter. */
2226 static int init_pi (int argc, char **argv)
2230 if (NULL != perl_threads)
2233 log_info ("Initializing Perl interpreter...");
2238 for (i = 0; i < argc; ++i)
2239 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2241 #endif /* COLLECT_DEBUG */
2243 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2244 log_err ("init_pi: pthread_key_create failed");
2246 /* this must not happen - cowardly giving up if it does */
2251 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2252 * triggers a "value computed is not used" warning by gcc. */
2255 PERL_SYS_INIT3 (&argc, &argv, &environ);
2257 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2258 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2260 pthread_mutex_init (&perl_threads->mutex, NULL);
2261 /* locking the mutex should not be necessary at this point
2262 * but let's just do it for the sake of completeness */
2263 pthread_mutex_lock (&perl_threads->mutex);
2265 perl_threads->head = c_ithread_create (NULL);
2266 perl_threads->tail = perl_threads->head;
2268 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2269 log_err ("init_pi: Not enough memory.");
2273 aTHX = perl_threads->head->interp;
2274 pthread_mutex_unlock (&perl_threads->mutex);
2276 perl_construct (aTHX);
2278 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2280 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2281 SV *err = get_sv ("@", 1);
2282 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2285 perl_destruct (perl_threads->head->interp);
2286 perl_free (perl_threads->head->interp);
2287 sfree (perl_threads);
2289 pthread_key_delete (perl_thr_key);
2293 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2294 sv_setpv (get_sv ("0", 0), "collectd");
2298 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2299 plugin_register_notification ("perl", perl_notify,
2300 /* user_data = */ NULL);
2301 plugin_register_init ("perl", perl_init);
2303 plugin_register_read ("perl", perl_read);
2305 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2306 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2307 plugin_register_shutdown ("perl", perl_shutdown);
2309 } /* static int init_pi (const char **, const int) */
2312 * LoadPlugin "<Plugin>"
2314 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2316 char module_name[DATA_MAX_NAME_LEN];
2320 if ((0 != ci->children_num) || (1 != ci->values_num)
2321 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2322 log_err ("LoadPlugin expects a single string argument.");
2326 value = ci->values[0].value.string;
2328 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2329 log_err ("Invalid module name %s", value);
2333 if (0 != init_pi (perl_argc, perl_argv))
2336 assert (NULL != perl_threads);
2337 assert (NULL != perl_threads->head);
2339 aTHX = perl_threads->head->interp;
2341 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2342 load_module (PERL_LOADMOD_NOIMPORT,
2343 newSVpv (module_name, strlen (module_name)), Nullsv);
2345 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2350 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2354 if ((0 != ci->children_num) || (1 != ci->values_num)
2355 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2356 log_err ("BaseName expects a single string argument.");
2360 value = ci->values[0].value.string;
2362 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2363 sstrncpy (base_name, value, sizeof (base_name));
2365 } /* static int perl_config_basename (oconfig_item_it *) */
2368 * EnableDebugger "<Package>"|""
2370 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2374 if ((0 != ci->children_num) || (1 != ci->values_num)
2375 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2376 log_err ("EnableDebugger expects a single string argument.");
2380 if (NULL != perl_threads) {
2381 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2385 value = ci->values[0].value.string;
2387 perl_argv = (char **)realloc (perl_argv,
2388 (++perl_argc + 1) * sizeof (char *));
2390 if (NULL == perl_argv) {
2391 log_err ("perl_config: Not enough memory.");
2395 if ('\0' == value[0]) {
2396 perl_argv[perl_argc - 1] = "-d";
2399 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2400 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2401 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2404 perl_argv[perl_argc] = NULL;
2406 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2409 * IncludeDir "<Dir>"
2411 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2415 if ((0 != ci->children_num) || (1 != ci->values_num)
2416 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2417 log_err ("IncludeDir expects a single string argument.");
2421 value = ci->values[0].value.string;
2424 perl_argv = (char **)realloc (perl_argv,
2425 (++perl_argc + 1) * sizeof (char *));
2427 if (NULL == perl_argv) {
2428 log_err ("perl_config: Not enough memory.");
2432 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2433 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2434 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2436 perl_argv[perl_argc] = NULL;
2439 /* prepend the directory to @INC */
2440 av_unshift (GvAVn (PL_incgv), 1);
2441 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2444 } /* static int perl_config_includedir (oconfig_item_it *) */
2449 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2459 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2460 log_err ("LoadPlugin expects a single string argument.");
2464 plugin = ci->values[0].value.string;
2467 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2471 log_err ("Unable to convert configuration to a Perl hash value.");
2472 config = (HV *)&PL_sv_undef;
2480 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2481 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2485 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2500 } /* static int perl_config_plugin (oconfig_item_it *) */
2502 static int perl_config (oconfig_item_t *ci)
2509 for (i = 0; i < ci->children_num; ++i) {
2510 oconfig_item_t *c = ci->children + i;
2511 int current_status = 0;
2513 if (NULL != perl_threads)
2514 aTHX = PERL_GET_CONTEXT;
2516 if (0 == strcasecmp (c->key, "LoadPlugin"))
2517 current_status = perl_config_loadplugin (aTHX_ c);
2518 else if (0 == strcasecmp (c->key, "BaseName"))
2519 current_status = perl_config_basename (aTHX_ c);
2520 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2521 current_status = perl_config_enabledebugger (aTHX_ c);
2522 else if (0 == strcasecmp (c->key, "IncludeDir"))
2523 current_status = perl_config_includedir (aTHX_ c);
2524 else if (0 == strcasecmp (c->key, "Plugin"))
2525 current_status = perl_config_plugin (aTHX_ c);
2528 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2532 /* fatal error - it's up to perl_config_* to clean up */
2533 if (0 > current_status) {
2534 log_err ("Configuration failed with a fatal error - "
2535 "plugin disabled!");
2536 return current_status;
2539 status += current_status;
2542 } /* static int perl_config (oconfig_item_t *) */
2544 void module_register (void)
2547 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2549 /* default options for the Perl interpreter */
2551 perl_argv[1] = "-MCollectd";
2552 perl_argv[2] = "-e";
2554 perl_argv[4] = NULL;
2556 plugin_register_complex_config ("perl", perl_config);
2558 } /* void module_register (void) */
2560 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */