2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 Sebastian Harl
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Sebastian Harl <sh at tokkee.org>
25 * Pavel Rochnyak <pavel2000 ngs.ru>
29 * This plugin embeds a Perl interpreter into collectd and provides an
30 * interface for collectd plugins written in perl.
33 /* do not automatically get the thread specific Perl interpreter */
34 #define PERL_NO_GET_CONTEXT
36 #define DONT_POISON_SPRINTF_YET 1
39 #undef DONT_POISON_SPRINTF_YET
46 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
48 #pragma GCC poison sprintf
53 /* Some versions of Perl define their own version of DEBUG... :-/ */
58 /* ... while we want the definition found in plugin.h. */
62 #include "filter_chain.h"
64 #if !defined(USE_ITHREADS)
65 #error "Perl does not support ithreads!"
66 #endif /* !defined(USE_ITHREADS) */
68 /* clear the Perl sub's stack frame
69 * (this should only be used inside an XSUB) */
70 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
74 #define PLUGIN_WRITE 2
75 #define PLUGIN_SHUTDOWN 3
77 #define PLUGIN_NOTIF 5
78 #define PLUGIN_FLUSH 6
79 #define PLUGIN_FLUSH_ALL 7 /* For collectd-5.6 only */
81 #define PLUGIN_TYPES 8
83 #define PLUGIN_CONFIG 254
84 #define PLUGIN_DATASET 255
91 #define FC_CB_CREATE 0
92 #define FC_CB_DESTROY 1
97 #define log_debug(...) DEBUG("perl: " __VA_ARGS__)
98 #define log_info(...) INFO("perl: " __VA_ARGS__)
99 #define log_warn(...) WARNING("perl: " __VA_ARGS__)
100 #define log_err(...) ERROR("perl: " __VA_ARGS__)
102 /* this is defined in DynaLoader.a */
103 void boot_DynaLoader(PerlInterpreter *, CV *);
105 static XS(Collectd_plugin_register_read);
106 static XS(Collectd_plugin_register_write);
107 static XS(Collectd_plugin_register_log);
108 static XS(Collectd_plugin_register_notification);
109 static XS(Collectd_plugin_register_flush);
110 static XS(Collectd_plugin_unregister_read);
111 static XS(Collectd_plugin_unregister_write);
112 static XS(Collectd_plugin_unregister_log);
113 static XS(Collectd_plugin_unregister_notification);
114 static XS(Collectd_plugin_unregister_flush);
115 static XS(Collectd_plugin_register_ds);
116 static XS(Collectd_plugin_unregister_ds);
117 static XS(Collectd_plugin_dispatch_values);
118 static XS(Collectd_plugin_get_interval);
119 static XS(Collectd__plugin_write);
120 static XS(Collectd__plugin_flush);
121 static XS(Collectd_plugin_dispatch_notification);
122 static XS(Collectd_plugin_log);
123 static XS(Collectd__fc_register);
124 static XS(Collectd_call_by_name);
126 static int perl_read(user_data_t *ud);
127 static int perl_write(const data_set_t *ds, const value_list_t *vl,
128 user_data_t *user_data);
129 static void perl_log(int level, const char *msg, user_data_t *user_data);
130 static int perl_notify(const notification_t *notif, user_data_t *user_data);
131 static int perl_flush(cdtime_t timeout, const char *identifier,
132 user_data_t *user_data);
138 typedef struct c_ithread_s {
139 /* the thread's Perl interpreter */
140 PerlInterpreter *interp;
141 bool running; /* thread is inside Perl interpreter */
145 /* double linked list of threads */
146 struct c_ithread_s *prev;
147 struct c_ithread_s *next;
155 /* some usage stats */
156 int number_of_threads;
157 #endif /* COLLECT_DEBUG */
159 pthread_mutex_t mutex;
160 pthread_mutexattr_t mutexattr;
163 /* name / user_data for Perl matches / targets */
169 #define PFC_USER_DATA_FREE(data) \
171 sfree((data)->name); \
172 if (NULL != (data)->user_data) \
173 sv_free((data)->user_data); \
180 extern char **environ;
186 static bool register_legacy_flush = true;
188 /* if perl_threads != NULL perl_threads->head must
189 * point to the "base" thread */
190 static c_ithread_list_t *perl_threads;
192 /* the key used to store each pthread's ithread */
193 static pthread_key_t perl_thr_key;
195 static int perl_argc;
196 static char **perl_argv;
198 static char base_name[DATA_MAX_NAME_LEN] = "";
204 {"Collectd::plugin_register_read", Collectd_plugin_register_read},
205 {"Collectd::plugin_register_write", Collectd_plugin_register_write},
206 {"Collectd::plugin_register_log", Collectd_plugin_register_log},
207 {"Collectd::plugin_register_notification",
208 Collectd_plugin_register_notification},
209 {"Collectd::plugin_register_flush", Collectd_plugin_register_flush},
210 {"Collectd::plugin_unregister_read", Collectd_plugin_unregister_read},
211 {"Collectd::plugin_unregister_write", Collectd_plugin_unregister_write},
212 {"Collectd::plugin_unregister_log", Collectd_plugin_unregister_log},
213 {"Collectd::plugin_unregister_notification",
214 Collectd_plugin_unregister_notification},
215 {"Collectd::plugin_unregister_flush", Collectd_plugin_unregister_flush},
216 {"Collectd::plugin_register_data_set", Collectd_plugin_register_ds},
217 {"Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds},
218 {"Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values},
219 {"Collectd::plugin_get_interval", Collectd_plugin_get_interval},
220 {"Collectd::_plugin_write", Collectd__plugin_write},
221 {"Collectd::_plugin_flush", Collectd__plugin_flush},
222 {"Collectd::plugin_dispatch_notification",
223 Collectd_plugin_dispatch_notification},
224 {"Collectd::plugin_log", Collectd_plugin_log},
225 {"Collectd::_fc_register", Collectd__fc_register},
226 {"Collectd::call_by_name", Collectd_call_by_name},
232 } constants[] = {{"Collectd::TYPE_INIT", PLUGIN_INIT},
233 {"Collectd::TYPE_READ", PLUGIN_READ},
234 {"Collectd::TYPE_WRITE", PLUGIN_WRITE},
235 {"Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN},
236 {"Collectd::TYPE_LOG", PLUGIN_LOG},
237 {"Collectd::TYPE_NOTIF", PLUGIN_NOTIF},
238 {"Collectd::TYPE_FLUSH", PLUGIN_FLUSH},
239 {"Collectd::TYPE_CONFIG", PLUGIN_CONFIG},
240 {"Collectd::TYPE_DATASET", PLUGIN_DATASET},
241 {"Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER},
242 {"Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE},
243 {"Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE},
244 {"Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE},
245 {"Collectd::LOG_ERR", LOG_ERR},
246 {"Collectd::LOG_WARNING", LOG_WARNING},
247 {"Collectd::LOG_NOTICE", LOG_NOTICE},
248 {"Collectd::LOG_INFO", LOG_INFO},
249 {"Collectd::LOG_DEBUG", LOG_DEBUG},
250 {"Collectd::FC_MATCH", FC_MATCH},
251 {"Collectd::FC_TARGET", FC_TARGET},
252 {"Collectd::FC_CB_CREATE", FC_CB_CREATE},
253 {"Collectd::FC_CB_DESTROY", FC_CB_DESTROY},
254 {"Collectd::FC_CB_EXEC", FC_CB_EXEC},
255 {"Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH},
256 {"Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES},
257 {"Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE},
258 {"Collectd::FC_TARGET_STOP", FC_TARGET_STOP},
259 {"Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN},
260 {"Collectd::NOTIF_FAILURE", NOTIF_FAILURE},
261 {"Collectd::NOTIF_WARNING", NOTIF_WARNING},
262 {"Collectd::NOTIF_OKAY", NOTIF_OKAY},
265 * Helper functions for data type conversion.
280 static int hv2data_source(pTHX_ HV *hash, data_source_t *ds) {
283 if ((NULL == hash) || (NULL == ds))
286 if (NULL != (tmp = hv_fetch(hash, "name", 4, 0))) {
287 sstrncpy(ds->name, SvPV_nolen(*tmp), sizeof(ds->name));
289 log_err("hv2data_source: No DS name given.");
293 if (NULL != (tmp = hv_fetch(hash, "type", 4, 0))) {
294 ds->type = SvIV(*tmp);
296 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type) &&
297 (DS_TYPE_DERIVE != ds->type) && (DS_TYPE_ABSOLUTE != ds->type)) {
298 log_err("hv2data_source: Invalid DS type.");
302 ds->type = DS_TYPE_COUNTER;
305 if (NULL != (tmp = hv_fetch(hash, "min", 3, 0)))
306 ds->min = SvNV(*tmp);
310 if (NULL != (tmp = hv_fetch(hash, "max", 3, 0)))
311 ds->max = SvNV(*tmp);
315 } /* static int hv2data_source (HV *, data_source_t *) */
317 /* av2value converts at most "len" elements from "array" to "value". Returns the
318 * number of elements converted or zero on error. */
319 static size_t av2value(pTHX_ char *name, AV *array, value_t *value,
321 const data_set_t *ds;
323 if ((NULL == name) || (NULL == array) || (NULL == value) || (array_len == 0))
326 ds = plugin_get_ds(name);
328 log_err("av2value: Unknown dataset \"%s\"", name);
332 if (array_len < ds->ds_num) {
333 log_warn("av2value: array does not contain enough elements for type "
334 "\"%s\": got %" PRIsz ", want %" PRIsz,
335 name, array_len, ds->ds_num);
337 } else if (array_len > ds->ds_num) {
338 log_warn("av2value: array contains excess elements for type \"%s\": got "
339 "%" PRIsz ", want %" PRIsz,
340 name, array_len, ds->ds_num);
343 for (size_t i = 0; i < ds->ds_num; ++i) {
344 SV **tmp = av_fetch(array, i, 0);
347 if (DS_TYPE_COUNTER == ds->ds[i].type)
348 value[i].counter = SvIV(*tmp);
349 else if (DS_TYPE_GAUGE == ds->ds[i].type)
350 value[i].gauge = SvNV(*tmp);
351 else if (DS_TYPE_DERIVE == ds->ds[i].type)
352 value[i].derive = SvIV(*tmp);
353 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
354 value[i].absolute = SvIV(*tmp);
361 } /* static size_t av2value (char *, AV *, value_t *, size_t) */
366 * values => [ @values ],
370 * plugin_instance => $pinstance,
371 * type_instance => $tinstance,
374 static int hv2value_list(pTHX_ HV *hash, value_list_t *vl) {
377 if ((NULL == hash) || (NULL == vl))
380 if (NULL == (tmp = hv_fetch(hash, "type", 4, 0))) {
381 log_err("hv2value_list: No type given.");
385 sstrncpy(vl->type, SvPV_nolen(*tmp), sizeof(vl->type));
387 if ((NULL == (tmp = hv_fetch(hash, "values", 6, 0))) ||
388 (!(SvROK(*tmp) && (SVt_PVAV == SvTYPE(SvRV(*tmp)))))) {
389 log_err("hv2value_list: No valid values given.");
394 AV *array = (AV *)SvRV(*tmp);
395 /* av_len returns the highest index, not the actual length. */
396 size_t array_len = (size_t)(av_len(array) + 1);
400 vl->values = calloc(array_len, sizeof(*vl->values));
402 av2value(aTHX_ vl->type, (AV *)SvRV(*tmp), vl->values, array_len);
403 if (vl->values_len == 0) {
409 if (NULL != (tmp = hv_fetch(hash, "time", 4, 0))) {
410 double t = SvNV(*tmp);
411 vl->time = DOUBLE_TO_CDTIME_T(t);
414 if (NULL != (tmp = hv_fetch(hash, "interval", 8, 0))) {
415 double t = SvNV(*tmp);
416 vl->interval = DOUBLE_TO_CDTIME_T(t);
419 if (NULL != (tmp = hv_fetch(hash, "host", 4, 0)))
420 sstrncpy(vl->host, SvPV_nolen(*tmp), sizeof(vl->host));
422 if (NULL != (tmp = hv_fetch(hash, "plugin", 6, 0)))
423 sstrncpy(vl->plugin, SvPV_nolen(*tmp), sizeof(vl->plugin));
425 if (NULL != (tmp = hv_fetch(hash, "plugin_instance", 15, 0)))
426 sstrncpy(vl->plugin_instance, SvPV_nolen(*tmp),
427 sizeof(vl->plugin_instance));
429 if (NULL != (tmp = hv_fetch(hash, "type_instance", 13, 0)))
430 sstrncpy(vl->type_instance, SvPV_nolen(*tmp), sizeof(vl->type_instance));
432 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
434 static int av2data_set(pTHX_ AV *array, char *name, data_set_t *ds) {
437 if ((NULL == array) || (NULL == name) || (NULL == ds))
443 log_err("av2data_set: Invalid data set.");
447 ds->ds = smalloc((len + 1) * sizeof(*ds->ds));
448 ds->ds_num = len + 1;
450 for (int i = 0; i <= len; ++i) {
451 SV **elem = av_fetch(array, i, 0);
454 log_err("av2data_set: Failed to fetch data source %i.", i);
458 if (!(SvROK(*elem) && (SVt_PVHV == SvTYPE(SvRV(*elem))))) {
459 log_err("av2data_set: Invalid data source.");
463 if (-1 == hv2data_source(aTHX_(HV *) SvRV(*elem), &ds->ds[i]))
466 log_debug("av2data_set: "
467 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
468 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
471 sstrncpy(ds->type, name, sizeof(ds->type));
473 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
478 * severity => $severity,
484 * plugin_instance => $instance,
485 * type_instance => $type_instance,
486 * meta => [ { name => <name>, value => <value> }, ... ]
489 static int av2notification_meta(pTHX_ AV *array,
490 notification_meta_t **ret_meta) {
491 notification_meta_t *tail = NULL;
493 int len = av_len(array);
495 for (int i = 0; i <= len; ++i) {
496 SV **tmp = av_fetch(array, i, 0);
501 if (!(SvROK(*tmp) && (SVt_PVHV == SvTYPE(SvRV(*tmp))))) {
502 log_warn("av2notification_meta: Skipping invalid "
503 "meta information.");
507 HV *hash = (HV *)SvRV(*tmp);
509 notification_meta_t *m = calloc(1, sizeof(*m));
513 SV **name = hv_fetch(hash, "name", strlen("name"), 0);
515 log_warn("av2notification_meta: Skipping invalid "
516 "meta information.");
520 sstrncpy(m->name, SvPV_nolen(*name), sizeof(m->name));
522 SV **value = hv_fetch(hash, "value", strlen("value"), 0);
524 log_warn("av2notification_meta: Skipping invalid "
525 "meta information.");
531 m->nm_value.nm_double = SvNVX(*value);
532 m->type = NM_TYPE_DOUBLE;
533 } else if (SvUOK(*value)) {
534 m->nm_value.nm_unsigned_int = SvUVX(*value);
535 m->type = NM_TYPE_UNSIGNED_INT;
536 } else if (SvIOK(*value)) {
537 m->nm_value.nm_signed_int = SvIVX(*value);
538 m->type = NM_TYPE_SIGNED_INT;
540 m->nm_value.nm_string = sstrdup(SvPV_nolen(*value));
541 m->type = NM_TYPE_STRING;
553 } /* static int av2notification_meta (AV *, notification_meta_t *) */
555 static int hv2notification(pTHX_ HV *hash, notification_t *n) {
558 if ((NULL == hash) || (NULL == n))
561 if (NULL != (tmp = hv_fetch(hash, "severity", 8, 0)))
562 n->severity = SvIV(*tmp);
564 n->severity = NOTIF_FAILURE;
566 if (NULL != (tmp = hv_fetch(hash, "time", 4, 0))) {
567 double t = SvNV(*tmp);
568 n->time = DOUBLE_TO_CDTIME_T(t);
572 if (NULL != (tmp = hv_fetch(hash, "message", 7, 0)))
573 sstrncpy(n->message, SvPV_nolen(*tmp), sizeof(n->message));
575 if (NULL != (tmp = hv_fetch(hash, "host", 4, 0)))
576 sstrncpy(n->host, SvPV_nolen(*tmp), sizeof(n->host));
578 sstrncpy(n->host, hostname_g, sizeof(n->host));
580 if (NULL != (tmp = hv_fetch(hash, "plugin", 6, 0)))
581 sstrncpy(n->plugin, SvPV_nolen(*tmp), sizeof(n->plugin));
583 if (NULL != (tmp = hv_fetch(hash, "plugin_instance", 15, 0)))
584 sstrncpy(n->plugin_instance, SvPV_nolen(*tmp), sizeof(n->plugin_instance));
586 if (NULL != (tmp = hv_fetch(hash, "type", 4, 0)))
587 sstrncpy(n->type, SvPV_nolen(*tmp), sizeof(n->type));
589 if (NULL != (tmp = hv_fetch(hash, "type_instance", 13, 0)))
590 sstrncpy(n->type_instance, SvPV_nolen(*tmp), sizeof(n->type_instance));
593 while (NULL != (tmp = hv_fetch(hash, "meta", 4, 0))) {
594 if (!(SvROK(*tmp) && (SVt_PVAV == SvTYPE(SvRV(*tmp))))) {
595 log_warn("hv2notification: Ignoring invalid meta information.");
599 if (0 != av2notification_meta(aTHX_(AV *) SvRV(*tmp), &n->meta)) {
600 plugin_notification_meta_free(n->meta);
607 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
609 static int data_set2av(pTHX_ data_set_t *ds, AV *array) {
610 if ((NULL == ds) || (NULL == array))
613 av_extend(array, ds->ds_num);
615 for (size_t i = 0; i < ds->ds_num; ++i) {
616 HV *source = newHV();
618 if (NULL == hv_store(source, "name", 4, newSVpv(ds->ds[i].name, 0), 0))
621 if (NULL == hv_store(source, "type", 4, newSViv(ds->ds[i].type), 0))
624 if (!isnan(ds->ds[i].min))
625 if (NULL == hv_store(source, "min", 3, newSVnv(ds->ds[i].min), 0))
628 if (!isnan(ds->ds[i].max))
629 if (NULL == hv_store(source, "max", 3, 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) {
642 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
646 /* av_extend takes the last *index* to which the array should be extended. */
647 av_extend(values, vl->values_len - 1);
649 assert(ds->ds_num == vl->values_len);
650 for (i = 0; i < vl->values_len; ++i) {
653 if (DS_TYPE_COUNTER == ds->ds[i].type)
654 val = newSViv(vl->values[i].counter);
655 else if (DS_TYPE_GAUGE == ds->ds[i].type)
656 val = newSVnv(vl->values[i].gauge);
657 else if (DS_TYPE_DERIVE == ds->ds[i].type)
658 val = newSViv(vl->values[i].derive);
659 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
660 val = newSViv(vl->values[i].absolute);
662 if (NULL == av_store(values, i, val)) {
668 if (NULL == hv_store(hash, "values", 6, newRV_noinc((SV *)values), 0))
672 double t = CDTIME_T_TO_DOUBLE(vl->time);
673 if (NULL == hv_store(hash, "time", 4, newSVnv(t), 0))
678 double t = CDTIME_T_TO_DOUBLE(vl->interval);
679 if (NULL == hv_store(hash, "interval", 8, newSVnv(t), 0))
683 if ('\0' != vl->host[0])
684 if (NULL == hv_store(hash, "host", 4, newSVpv(vl->host, 0), 0))
687 if ('\0' != vl->plugin[0])
688 if (NULL == hv_store(hash, "plugin", 6, newSVpv(vl->plugin, 0), 0))
691 if ('\0' != vl->plugin_instance[0])
692 if (NULL == hv_store(hash, "plugin_instance", 15,
693 newSVpv(vl->plugin_instance, 0), 0))
696 if ('\0' != vl->type[0])
697 if (NULL == hv_store(hash, "type", 4, newSVpv(vl->type, 0), 0))
700 if ('\0' != vl->type_instance[0])
702 hv_store(hash, "type_instance", 13, newSVpv(vl->type_instance, 0), 0))
705 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
707 static int notification_meta2av(pTHX_ notification_meta_t *meta, AV *array) {
709 for (notification_meta_t *m = meta; m != NULL; m = m->next) {
713 av_extend(array, meta_num);
715 for (int i = 0; NULL != meta; meta = meta->next, ++i) {
719 if (NULL == hv_store(m, "name", 4, newSVpv(meta->name, 0), 0))
722 if (NM_TYPE_STRING == meta->type)
723 value = newSVpv(meta->nm_value.nm_string, 0);
724 else if (NM_TYPE_SIGNED_INT == meta->type)
725 value = newSViv(meta->nm_value.nm_signed_int);
726 else if (NM_TYPE_UNSIGNED_INT == meta->type)
727 value = newSVuv(meta->nm_value.nm_unsigned_int);
728 else if (NM_TYPE_DOUBLE == meta->type)
729 value = newSVnv(meta->nm_value.nm_double);
730 else if (NM_TYPE_BOOLEAN == meta->type)
731 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
735 if (NULL == hv_store(m, "value", 5, value, 0)) {
740 if (NULL == av_store(array, i, newRV_noinc((SV *)m))) {
747 } /* static int notification_meta2av (notification_meta_t *, AV *) */
749 static int notification2hv(pTHX_ notification_t *n, HV *hash) {
750 if (NULL == hv_store(hash, "severity", 8, newSViv(n->severity), 0))
754 double t = CDTIME_T_TO_DOUBLE(n->time);
755 if (NULL == hv_store(hash, "time", 4, newSVnv(t), 0))
759 if ('\0' != *n->message)
760 if (NULL == hv_store(hash, "message", 7, newSVpv(n->message, 0), 0))
763 if ('\0' != *n->host)
764 if (NULL == hv_store(hash, "host", 4, newSVpv(n->host, 0), 0))
767 if ('\0' != *n->plugin)
768 if (NULL == hv_store(hash, "plugin", 6, newSVpv(n->plugin, 0), 0))
771 if ('\0' != *n->plugin_instance)
772 if (NULL == hv_store(hash, "plugin_instance", 15,
773 newSVpv(n->plugin_instance, 0), 0))
776 if ('\0' != *n->type)
777 if (NULL == hv_store(hash, "type", 4, newSVpv(n->type, 0), 0))
780 if ('\0' != *n->type_instance)
782 hv_store(hash, "type_instance", 13, newSVpv(n->type_instance, 0), 0))
785 if (NULL != n->meta) {
787 if ((0 != notification_meta2av(aTHX_ n->meta, meta)) ||
788 (NULL == hv_store(hash, "meta", 4, newRV_noinc((SV *)meta), 0))) {
795 } /* static int notification2hv (notification_t *, HV *) */
797 static int oconfig_item2hv(pTHX_ oconfig_item_t *ci, HV *hash) {
801 if (NULL == hv_store(hash, "key", 3, newSVpv(ci->key, 0), 0))
805 if (0 < ci->values_num)
806 av_extend(values, ci->values_num);
808 if (NULL == hv_store(hash, "values", 6, newRV_noinc((SV *)values), 0)) {
814 for (int i = 0; i < ci->values_num; ++i) {
817 switch (ci->values[i].type) {
818 case OCONFIG_TYPE_STRING:
819 value = newSVpv(ci->values[i].value.string, 0);
821 case OCONFIG_TYPE_NUMBER:
822 value = newSVnv((NV)ci->values[i].value.number);
824 case OCONFIG_TYPE_BOOLEAN:
825 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
828 log_err("oconfig_item2hv: Invalid value type %i.", ci->values[i].type);
829 value = &PL_sv_undef;
832 if (NULL == av_store(values, i, value)) {
838 /* ignoring 'parent' member which is uninteresting in this case */
841 if (0 < ci->children_num)
842 av_extend(children, ci->children_num);
844 if (NULL == hv_store(hash, "children", 8, newRV_noinc((SV *)children), 0)) {
850 for (int i = 0; i < ci->children_num; ++i) {
853 if (0 != oconfig_item2hv(aTHX_ ci->children + i, child)) {
859 if (NULL == av_store(children, i, newRV_noinc((SV *)child))) {
866 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
869 * Internal functions.
872 static char *get_module_name(char *buf, size_t buf_len, const char *module) {
874 if (base_name[0] == '\0')
875 status = snprintf(buf, buf_len, "%s", module);
877 status = snprintf(buf, buf_len, "%s::%s", base_name, module);
878 if ((status < 0) || ((unsigned int)status >= buf_len))
881 } /* char *get_module_name */
884 * Add a plugin's data set definition.
886 static int pplugin_register_data_set(pTHX_ char *name, AV *dataset) {
891 if ((NULL == name) || (NULL == dataset))
894 if (0 != av2data_set(aTHX_ dataset, name, &ds))
897 ret = plugin_register_data_set(&ds);
901 } /* static int pplugin_register_data_set (char *, SV *) */
904 * Remove a plugin's data set definition.
906 static int pplugin_unregister_data_set(char *name) {
909 return plugin_unregister_data_set(name);
910 } /* static int pplugin_unregister_data_set (char *) */
913 * Submit the values to the write functions.
915 static int pplugin_dispatch_values(pTHX_ HV *values) {
916 value_list_t vl = VALUE_LIST_INIT;
923 if (0 != hv2value_list(aTHX_ values, &vl))
926 ret = plugin_dispatch_values(&vl);
930 } /* static int pplugin_dispatch_values (char *, HV *) */
933 * Submit the values to a single write function.
935 static int pplugin_write(pTHX_ const char *plugin, AV *data_set, HV *values) {
937 value_list_t vl = VALUE_LIST_INIT;
944 if (0 != hv2value_list(aTHX_ values, &vl))
947 if ((NULL != data_set) && (0 != av2data_set(aTHX_ data_set, vl.type, &ds)))
950 ret = plugin_write(plugin, NULL == data_set ? NULL : &ds, &vl);
952 log_warn("Dispatching value to plugin \"%s\" failed with status %i.",
953 NULL == plugin ? "<any>" : plugin, ret);
955 if (NULL != data_set)
959 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
962 * Dispatch a notification.
964 static int pplugin_dispatch_notification(pTHX_ HV *notif) {
965 notification_t n = {0};
972 if (0 != hv2notification(aTHX_ notif, &n))
975 ret = plugin_dispatch_notification(&n);
976 plugin_notification_meta_free(n.meta);
978 } /* static int pplugin_dispatch_notification (HV *) */
981 * Call perl sub with thread locking flags handled.
983 static int call_pv_locked(pTHX_ const char *sub_name) {
987 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
988 if (t == NULL) /* thread destroyed */
991 old_running = t->running;
995 t->running = old_running;
999 ret = call_pv(sub_name, G_SCALAR | G_EVAL);
1001 t->running = old_running;
1003 } /* static int call_pv_locked (pTHX, *sub_name) */
1006 * Call all working functions of the given type.
1008 static int pplugin_call(pTHX_ int type, ...) {
1017 if ((type < 0) || (type >= PLUGIN_TYPES))
1027 if (PLUGIN_READ == type) {
1028 subname = va_arg(ap, char *);
1029 } else if (PLUGIN_WRITE == type) {
1036 subname = va_arg(ap, char *);
1038 * $_[0] = $plugin_type;
1053 * values => [ $v1, ... ],
1055 * host => $hostname,
1056 * plugin => $plugin,
1058 * plugin_instance => $instance,
1059 * 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)));
1082 } else if (PLUGIN_LOG == type) {
1083 subname = va_arg(ap, char *);
1089 XPUSHs(sv_2mortal(newSViv(va_arg(ap, int))));
1090 XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
1091 } else if (PLUGIN_NOTIF == type) {
1093 HV *notif = newHV();
1095 subname = va_arg(ap, char *);
1099 * severity => $severity,
1103 * plugin => $plugin,
1105 * plugin_instance => $instance,
1106 * type_instance => $type_instance
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)));
1119 } else if (PLUGIN_FLUSH == type) {
1121 subname = va_arg(ap, char *);
1124 * $_[1] = $identifier;
1126 timeout = va_arg(ap, cdtime_t);
1128 XPUSHs(sv_2mortal(newSVnv(CDTIME_T_TO_DOUBLE(timeout))));
1129 XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
1130 } else if (PLUGIN_FLUSH_ALL == type) {
1132 subname = "Collectd::plugin_call_all";
1135 * $_[1] = $identifier;
1137 timeout = va_arg(ap, cdtime_t);
1139 XPUSHs(sv_2mortal(newSViv((IV)PLUGIN_FLUSH)));
1140 XPUSHs(sv_2mortal(newSVnv(CDTIME_T_TO_DOUBLE(timeout))));
1141 XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
1142 } else if (PLUGIN_INIT == type) {
1143 subname = "Collectd::plugin_call_all";
1144 XPUSHs(sv_2mortal(newSViv((IV)type)));
1145 } else if (PLUGIN_SHUTDOWN == type) {
1146 subname = "Collectd::plugin_call_all";
1147 XPUSHs(sv_2mortal(newSViv((IV)type)));
1148 } else { /* Unknown type. Run 'plugin_call_all' and make compiler happy */
1149 subname = "Collectd::plugin_call_all";
1150 XPUSHs(sv_2mortal(newSViv((IV)type)));
1155 retvals = call_pv_locked(aTHX_ subname);
1158 if (SvTRUE(ERRSV)) {
1159 if (PLUGIN_LOG != type)
1160 ERROR("perl: %s error: %s", subname, SvPV_nolen(ERRSV));
1162 } else if (0 < retvals) {
1174 } /* static int pplugin_call (int, ...) */
1177 * collectd's Perl interpreter based thread implementation.
1179 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1182 /* must be called with perl_threads->mutex locked */
1183 static void c_ithread_destroy(c_ithread_t *ithread) {
1184 dTHXa(ithread->interp);
1186 assert(NULL != perl_threads);
1188 PERL_SET_CONTEXT(aTHX);
1189 /* Mark as running to avoid deadlock:
1190 c_ithread_destroy -> log_debug -> perl_log()
1192 ithread->running = true;
1193 log_debug("Shutting down Perl interpreter %p...", aTHX);
1198 --perl_threads->number_of_threads;
1199 #endif /* COLLECT_DEBUG */
1201 perl_destruct(aTHX);
1204 if (NULL == ithread->prev)
1205 perl_threads->head = ithread->next;
1207 ithread->prev->next = ithread->next;
1209 if (NULL == ithread->next)
1210 perl_threads->tail = ithread->prev;
1212 ithread->next->prev = ithread->prev;
1216 } /* static void c_ithread_destroy (c_ithread_t *) */
1218 static void c_ithread_destructor(void *arg) {
1219 c_ithread_t *ithread = (c_ithread_t *)arg;
1220 c_ithread_t *t = NULL;
1222 if (NULL == perl_threads)
1225 pthread_mutex_lock(&perl_threads->mutex);
1227 for (t = perl_threads->head; NULL != t; t = t->next)
1231 /* the ithread no longer exists */
1233 pthread_mutex_unlock(&perl_threads->mutex);
1237 c_ithread_destroy(ithread);
1239 pthread_mutex_unlock(&perl_threads->mutex);
1241 } /* static void c_ithread_destructor (void *) */
1243 /* must be called with perl_threads->mutex locked */
1244 static c_ithread_t *c_ithread_create(PerlInterpreter *base) {
1245 c_ithread_t *t = NULL;
1248 assert(NULL != perl_threads);
1250 t = smalloc(sizeof(*t));
1251 memset(t, 0, sizeof(c_ithread_t));
1253 t->interp = (NULL == base) ? NULL : perl_clone(base, CLONEf_KEEP_PTR_TABLE);
1257 if ((NULL != base) && (NULL != PL_endav)) {
1264 ++perl_threads->number_of_threads;
1265 #endif /* COLLECT_DEBUG */
1269 if (NULL == perl_threads->tail) {
1270 perl_threads->head = t;
1273 perl_threads->tail->next = t;
1274 t->prev = perl_threads->tail;
1277 t->pthread = pthread_self();
1279 t->shutdown = false;
1280 perl_threads->tail = t;
1282 pthread_setspecific(perl_thr_key, (const void *)t);
1284 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1287 * Filter chains implementation.
1290 static int fc_call(pTHX_ int type, int cb_type, pfc_user_data_t *data, ...) {
1296 notification_meta_t **meta = NULL;
1301 if ((type < 0) || (type >= FC_TYPES))
1304 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1314 XPUSHs(sv_2mortal(newSViv((IV)type)));
1315 XPUSHs(sv_2mortal(newSVpv(data->name, 0)));
1316 XPUSHs(sv_2mortal(newSViv((IV)cb_type)));
1318 if (FC_CB_CREATE == cb_type) {
1321 * $_[1] = $user_data;
1324 HV *config = newHV();
1326 ci = va_arg(ap, oconfig_item_t *);
1328 if (0 != oconfig_item2hv(aTHX_ ci, config)) {
1331 config = (HV *)&PL_sv_undef;
1335 XPUSHs(sv_2mortal(newRV_noinc((SV *)config)));
1336 } else if (FC_CB_DESTROY == cb_type) {
1338 * $_[1] = $user_data;
1341 /* nothing to be done - the user data pointer
1342 * is pushed onto the stack later */
1343 } else if (FC_CB_EXEC == cb_type) {
1348 * $_[3] = $user_data;
1356 ds = va_arg(ap, data_set_t *);
1357 vl = va_arg(ap, value_list_t *);
1358 meta = va_arg(ap, notification_meta_t **);
1360 if (0 != data_set2av(aTHX_ ds, pds)) {
1363 pds = (AV *)&PL_sv_undef;
1367 if (0 != value_list2hv(aTHX_ vl, ds, pvl)) {
1370 pvl = (HV *)&PL_sv_undef;
1377 if (0 != notification_meta2av(aTHX_ * meta, pmeta)) {
1380 pmeta = (AV *)&PL_sv_undef;
1384 pmeta = (AV *)&PL_sv_undef;
1387 XPUSHs(sv_2mortal(newRV_noinc((SV *)pds)));
1388 XPUSHs(sv_2mortal(newRV_noinc((SV *)pvl)));
1389 XPUSHs(sv_2mortal(newRV_noinc((SV *)pmeta)));
1392 XPUSHs(sv_2mortal(newRV_inc(data->user_data)));
1396 retvals = call_pv_locked(aTHX_ "Collectd::fc_call");
1398 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1399 assert(pmeta != NULL);
1401 plugin_notification_meta_free(*meta);
1402 av2notification_meta(aTHX_ pmeta, meta);
1406 if (SvTRUE(ERRSV)) {
1407 ERROR("perl: Collectd::fc_call error: %s", SvPV_nolen(ERRSV));
1409 } else if (0 < retvals) {
1412 /* the exec callbacks return a status, while
1413 * the others return a boolean value */
1414 if (FC_CB_EXEC == cb_type)
1416 else if (!SvTRUE(tmp))
1426 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1428 static int fc_create(int type, const oconfig_item_t *ci, void **user_data) {
1429 pfc_user_data_t *data;
1435 if (NULL == perl_threads)
1439 c_ithread_t *t = NULL;
1441 pthread_mutex_lock(&perl_threads->mutex);
1442 t = c_ithread_create(perl_threads->head->interp);
1443 pthread_mutex_unlock(&perl_threads->mutex);
1448 log_debug("fc_create: c_ithread: interp = %p (active threads: %i)", aTHX,
1449 perl_threads->number_of_threads);
1451 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1452 log_warn("A \"%s\" block expects a single string argument.",
1453 (FC_MATCH == type) ? "Match" : "Target");
1457 data = smalloc(sizeof(*data));
1458 data->name = sstrdup(ci->values[0].value.string);
1459 data->user_data = newSV(0);
1461 ret = fc_call(aTHX_ type, FC_CB_CREATE, data, ci);
1464 PFC_USER_DATA_FREE(data);
1468 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1470 static int fc_destroy(int type, void **user_data) {
1471 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1477 if ((NULL == perl_threads) || (NULL == data))
1481 c_ithread_t *t = NULL;
1483 pthread_mutex_lock(&perl_threads->mutex);
1484 t = c_ithread_create(perl_threads->head->interp);
1485 pthread_mutex_unlock(&perl_threads->mutex);
1490 log_debug("fc_destroy: c_ithread: interp = %p (active threads: %i)", aTHX,
1491 perl_threads->number_of_threads);
1493 ret = fc_call(aTHX_ type, FC_CB_DESTROY, data);
1495 PFC_USER_DATA_FREE(data);
1498 } /* static int fc_destroy (int, void **) */
1500 static int fc_exec(int type, const data_set_t *ds, const value_list_t *vl,
1501 notification_meta_t **meta, void **user_data) {
1502 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1506 if (NULL == perl_threads)
1509 assert(NULL != data);
1512 c_ithread_t *t = NULL;
1514 pthread_mutex_lock(&perl_threads->mutex);
1515 t = c_ithread_create(perl_threads->head->interp);
1516 pthread_mutex_unlock(&perl_threads->mutex);
1521 log_debug("fc_exec: c_ithread: interp = %p (active threads: %i)", aTHX,
1522 perl_threads->number_of_threads);
1524 return fc_call(aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1525 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1526 notification_meta_t **, void **) */
1528 static int pmatch_create(const oconfig_item_t *ci, void **user_data) {
1529 return fc_create(FC_MATCH, ci, user_data);
1530 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1532 static int pmatch_destroy(void **user_data) {
1533 return fc_destroy(FC_MATCH, user_data);
1534 } /* static int pmatch_destroy (void **) */
1536 static int pmatch_match(const data_set_t *ds, const value_list_t *vl,
1537 notification_meta_t **meta, void **user_data) {
1538 return fc_exec(FC_MATCH, ds, vl, meta, user_data);
1539 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1540 notification_meta_t **, void **) */
1542 static match_proc_t pmatch = {pmatch_create, pmatch_destroy, pmatch_match};
1544 static int ptarget_create(const oconfig_item_t *ci, void **user_data) {
1545 return fc_create(FC_TARGET, ci, user_data);
1546 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1548 static int ptarget_destroy(void **user_data) {
1549 return fc_destroy(FC_TARGET, user_data);
1550 } /* static int ptarget_destroy (void **) */
1552 static int ptarget_invoke(const data_set_t *ds, value_list_t *vl,
1553 notification_meta_t **meta, void **user_data) {
1554 return fc_exec(FC_TARGET, ds, vl, meta, user_data);
1555 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1556 notification_meta_t **, void **) */
1558 static target_proc_t ptarget = {ptarget_create, ptarget_destroy,
1562 * Exported Perl API.
1565 static void _plugin_register_generic_userdata(pTHX, int type,
1568 user_data_t userdata;
1574 log_err("Usage: Collectd::plugin_register_%s(pluginname, subname)", desc);
1579 log_err("Collectd::plugin_register_%s(pluginname, subname): "
1580 "Invalid pluginname",
1585 log_err("Collectd::plugin_register_%s(pluginname, subname): "
1591 /* Use pluginname as-is to allow flush a single perl plugin */
1592 pluginname = SvPV_nolen(ST(0));
1594 log_debug("Collectd::plugin_register_%s: "
1595 "plugin = \"%s\", sub = \"%s\"",
1596 desc, pluginname, SvPV_nolen(ST(1)));
1598 memset(&userdata, 0, sizeof(userdata));
1599 userdata.data = strdup(SvPV_nolen(ST(1)));
1600 userdata.free_func = free;
1602 if (PLUGIN_READ == type) {
1603 ret = plugin_register_complex_read(
1605 pluginname, perl_read, plugin_get_interval(), /* Default interval */
1607 } else if (PLUGIN_WRITE == type) {
1608 ret = plugin_register_write(pluginname, perl_write, &userdata);
1609 } else if (PLUGIN_LOG == type) {
1610 ret = plugin_register_log(pluginname, perl_log, &userdata);
1611 } else if (PLUGIN_NOTIF == type) {
1612 ret = plugin_register_notification(pluginname, perl_notify, &userdata);
1613 } else if (PLUGIN_FLUSH == type) {
1614 if (1 == register_legacy_flush) { /* For collectd-5.7 only, #1731 */
1615 register_legacy_flush = 0;
1616 ret = plugin_register_flush("perl", perl_flush, /* user_data = */ NULL);
1620 ret = plugin_register_flush(pluginname, perl_flush, &userdata);
1622 free(userdata.data);
1632 } /* static void _plugin_register_generic_userdata ( ... ) */
1635 * Collectd::plugin_register_TYPE (pluginname, subname).
1638 * name of the perl plugin
1641 * name of the plugin's subroutine that does the work
1644 static XS(Collectd_plugin_register_read) {
1645 _plugin_register_generic_userdata(aTHX, PLUGIN_READ, "read");
1648 static XS(Collectd_plugin_register_write) {
1649 _plugin_register_generic_userdata(aTHX, PLUGIN_WRITE, "write");
1652 static XS(Collectd_plugin_register_log) {
1653 _plugin_register_generic_userdata(aTHX, PLUGIN_LOG, "log");
1656 static XS(Collectd_plugin_register_notification) {
1657 _plugin_register_generic_userdata(aTHX, PLUGIN_NOTIF, "notification");
1660 static XS(Collectd_plugin_register_flush) {
1661 _plugin_register_generic_userdata(aTHX, PLUGIN_FLUSH, "flush");
1664 typedef int perl_unregister_function_t(const char *name);
1666 static void _plugin_unregister_generic(pTHX, perl_unregister_function_t *unreg,
1671 log_err("Usage: Collectd::plugin_unregister_%s(pluginname)", desc);
1676 log_err("Collectd::plugin_unregister_%s(pluginname): "
1677 "Invalid pluginname",
1682 log_debug("Collectd::plugin_unregister_%s: plugin = \"%s\"", desc,
1685 unreg(SvPV_nolen(ST(0)));
1688 } /* static void _plugin_unregister_generic ( ... ) */
1691 * Collectd::plugin_unregister_TYPE (pluginname).
1694 * type of callback to be unregistered: read, write, log, notification, flush
1697 * name of the perl plugin
1700 static XS(Collectd_plugin_unregister_read) {
1701 _plugin_unregister_generic(aTHX, plugin_unregister_read, "read");
1704 static XS(Collectd_plugin_unregister_write) {
1705 _plugin_unregister_generic(aTHX, plugin_unregister_write, "write");
1708 static XS(Collectd_plugin_unregister_log) {
1709 _plugin_unregister_generic(aTHX, plugin_unregister_log, "log");
1712 static XS(Collectd_plugin_unregister_notification) {
1713 _plugin_unregister_generic(aTHX, plugin_unregister_notification,
1717 static XS(Collectd_plugin_unregister_flush) {
1718 _plugin_unregister_generic(aTHX, plugin_unregister_flush, "flush");
1722 * Collectd::plugin_register_data_set (type, dataset).
1725 * type of the dataset
1728 * dataset to be registered
1730 static XS(Collectd_plugin_register_ds) {
1736 log_warn("Using plugin_register() to register new data-sets is "
1737 "deprecated - add new entries to a custom types.db instead.");
1740 log_err("Usage: Collectd::plugin_register_data_set(type, dataset)");
1744 log_debug("Collectd::plugin_register_data_set: "
1745 "type = \"%s\", dataset = \"%s\"",
1746 SvPV_nolen(ST(0)), SvPV_nolen(ST(1)));
1750 if (SvROK(data) && (SVt_PVAV == SvTYPE(SvRV(data)))) {
1751 ret = pplugin_register_data_set(aTHX_ SvPV_nolen(ST(0)), (AV *)SvRV(data));
1753 log_err("Collectd::plugin_register_data_set: Invalid data.");
1761 } /* static XS (Collectd_plugin_register_ds) */
1764 * Collectd::plugin_unregister_data_set (type).
1767 * type of the dataset
1769 static XS(Collectd_plugin_unregister_ds) {
1773 log_err("Usage: Collectd::plugin_unregister_data_set(type)");
1777 log_debug("Collectd::plugin_unregister_data_set: type = \"%s\"",
1780 if (0 == pplugin_unregister_data_set(SvPV_nolen(ST(0))))
1784 } /* static XS (Collectd_plugin_register_ds) */
1787 * Collectd::plugin_dispatch_values (name, values).
1790 * name of the plugin
1793 * value list to submit
1795 static XS(Collectd_plugin_dispatch_values) {
1803 log_err("Usage: Collectd::plugin_dispatch_values(values)");
1807 log_debug("Collectd::plugin_dispatch_values: values=\"%s\"",
1808 SvPV_nolen(ST(/* stack index = */ 0)));
1810 values = ST(/* stack index = */ 0);
1815 /* Make sure the argument is a hash reference. */
1816 if (!(SvROK(values) && (SVt_PVHV == SvTYPE(SvRV(values))))) {
1817 log_err("Collectd::plugin_dispatch_values: Invalid values.");
1821 ret = pplugin_dispatch_values(aTHX_(HV *) SvRV(values));
1827 } /* static XS (Collectd_plugin_dispatch_values) */
1830 * Collectd::plugin_get_interval ().
1832 static XS(Collectd_plugin_get_interval) {
1835 /* make sure we don't get any unused variable warnings for 'items';
1836 * don't abort, though */
1838 log_err("Usage: Collectd::plugin_get_interval()");
1840 XSRETURN_NV((NV)CDTIME_T_TO_DOUBLE(plugin_get_interval()));
1841 } /* static XS (Collectd_plugin_get_interval) */
1843 /* Collectd::plugin_write (plugin, ds, vl).
1846 * name of the plugin to call, may be 'undef'
1849 * data-set that describes the submitted values, may be 'undef'
1852 * value-list to be written
1854 static XS(Collectd__plugin_write) {
1864 log_err("Usage: Collectd::plugin_write(plugin, ds, vl)");
1868 log_debug("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1869 SvPV_nolen(ST(0)), SvOK(ST(1)) ? SvPV_nolen(ST(1)) : "",
1875 plugin = SvPV_nolen(ST(0));
1878 if (SvROK(ds) && (SVt_PVAV == SvTYPE(SvRV(ds))))
1879 ds_array = (AV *)SvRV(ds);
1883 log_err("Collectd::plugin_write: Invalid data-set.");
1888 if (!(SvROK(vl) && (SVt_PVHV == SvTYPE(SvRV(vl))))) {
1889 log_err("Collectd::plugin_write: Invalid value-list.");
1893 ret = pplugin_write(aTHX_ plugin, ds_array, (HV *)SvRV(vl));
1899 } /* static XS (Collectd__plugin_write) */
1902 * Collectd::_plugin_flush (plugin, timeout, identifier).
1905 * name of the plugin to flush
1908 * timeout to use when flushing the data
1911 * data-set identifier to flush
1913 static XS(Collectd__plugin_flush) {
1914 char *plugin = NULL;
1921 log_err("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1926 plugin = SvPV_nolen(ST(0));
1929 timeout = (int)SvIV(ST(1));
1932 id = SvPV_nolen(ST(2));
1934 log_debug("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1936 plugin, timeout, id);
1938 if (0 == plugin_flush(plugin, timeout, id))
1942 } /* static XS (Collectd__plugin_flush) */
1945 * Collectd::plugin_dispatch_notification (notif).
1948 * notification to dispatch
1950 static XS(Collectd_plugin_dispatch_notification) {
1958 log_err("Usage: Collectd::plugin_dispatch_notification(notif)");
1962 log_debug("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1967 if (!(SvROK(notif) && (SVt_PVHV == SvTYPE(SvRV(notif))))) {
1968 log_err("Collectd::plugin_dispatch_notification: Invalid notif.");
1972 ret = pplugin_dispatch_notification(aTHX_(HV *) SvRV(notif));
1978 } /* static XS (Collectd_plugin_dispatch_notification) */
1981 * Collectd::plugin_log (level, message).
1984 * log level (LOG_DEBUG, ... LOG_ERR)
1989 static XS(Collectd_plugin_log) {
1993 log_err("Usage: Collectd::plugin_log(level, message)");
1997 plugin_log(SvIV(ST(0)), "%s", SvPV_nolen(ST(1)));
1999 } /* static XS (Collectd_plugin_log) */
2002 * Collectd::_fc_register (type, name)
2010 static XS(Collectd__fc_register) {
2019 log_err("Usage: Collectd::_fc_register(type, name)");
2024 name = SvPV_nolen(ST(1));
2026 if (FC_MATCH == type)
2027 ret = fc_register_match(name, pmatch);
2028 else if (FC_TARGET == type)
2029 ret = fc_register_target(name, ptarget);
2035 } /* static XS (Collectd_fc_register) */
2038 * Collectd::call_by_name (...).
2040 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
2042 static XS(Collectd_call_by_name) {
2046 if (NULL == (tmp = get_sv("Collectd::cb_name", 0))) {
2047 sv_setpv(get_sv("@", 1), "cb_name has not been set");
2052 name = SvPV_nolen(tmp);
2054 if (NULL == get_cv(name, 0)) {
2055 sv_setpvf(get_sv("@", 1), "unknown callback \"%s\"", name);
2060 /* simply pass on the subroutine call without touching the stack,
2061 * thus leaving any arguments and return values in place */
2063 } /* static XS (Collectd_call_by_name) */
2066 * Interface to collectd.
2069 static int perl_init(void) {
2073 if (NULL == perl_threads)
2077 c_ithread_t *t = NULL;
2079 pthread_mutex_lock(&perl_threads->mutex);
2080 t = c_ithread_create(perl_threads->head->interp);
2081 pthread_mutex_unlock(&perl_threads->mutex);
2086 log_debug("perl_init: c_ithread: interp = %p (active threads: %i)", aTHX,
2087 perl_threads->number_of_threads);
2089 /* Lock the base thread to avoid race conditions with c_ithread_create().
2090 * See https://github.com/collectd/collectd/issues/9 and
2091 * https://github.com/collectd/collectd/issues/1706 for details.
2093 assert(aTHX == perl_threads->head->interp);
2094 pthread_mutex_lock(&perl_threads->mutex);
2096 status = pplugin_call(aTHX_ PLUGIN_INIT);
2098 pthread_mutex_unlock(&perl_threads->mutex);
2101 } /* static int perl_init (void) */
2103 static int perl_read(user_data_t *user_data) {
2106 if (NULL == perl_threads)
2110 c_ithread_t *t = NULL;
2112 pthread_mutex_lock(&perl_threads->mutex);
2113 t = c_ithread_create(perl_threads->head->interp);
2114 pthread_mutex_unlock(&perl_threads->mutex);
2119 /* Assert that we're not running as the base thread. Otherwise, we might
2120 * run into concurrency issues with c_ithread_create(). See
2121 * https://github.com/collectd/collectd/issues/9 for details. */
2122 assert(aTHX != perl_threads->head->interp);
2124 log_debug("perl_read: c_ithread: interp = %p (active threads: %i)", aTHX,
2125 perl_threads->number_of_threads);
2127 return pplugin_call(aTHX_ PLUGIN_READ, user_data->data);
2128 } /* static int perl_read (user_data_t *user_data) */
2130 static int perl_write(const data_set_t *ds, const value_list_t *vl,
2131 user_data_t *user_data) {
2135 if (NULL == perl_threads)
2139 c_ithread_t *t = NULL;
2141 pthread_mutex_lock(&perl_threads->mutex);
2142 t = c_ithread_create(perl_threads->head->interp);
2143 pthread_mutex_unlock(&perl_threads->mutex);
2148 /* Lock the base thread if this is not called from one of the read threads
2149 * to avoid race conditions with c_ithread_create(). See
2150 * https://github.com/collectd/collectd/issues/9 for details. */
2151 if (aTHX == perl_threads->head->interp)
2152 pthread_mutex_lock(&perl_threads->mutex);
2154 log_debug("perl_write: c_ithread: interp = %p (active threads: %i)", aTHX,
2155 perl_threads->number_of_threads);
2156 status = pplugin_call(aTHX_ PLUGIN_WRITE, user_data->data, ds, vl);
2158 if (aTHX == perl_threads->head->interp)
2159 pthread_mutex_unlock(&perl_threads->mutex);
2162 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2164 static void perl_log(int level, const char *msg, user_data_t *user_data) {
2167 if (NULL == perl_threads)
2171 c_ithread_t *t = NULL;
2173 pthread_mutex_lock(&perl_threads->mutex);
2174 t = c_ithread_create(perl_threads->head->interp);
2175 pthread_mutex_unlock(&perl_threads->mutex);
2180 /* Lock the base thread if this is not called from one of the read threads
2181 * to avoid race conditions with c_ithread_create(). See
2182 * https://github.com/collectd/collectd/issues/9 for details.
2185 if (aTHX == perl_threads->head->interp)
2186 pthread_mutex_lock(&perl_threads->mutex);
2188 pplugin_call(aTHX_ PLUGIN_LOG, user_data->data, level, msg);
2190 if (aTHX == perl_threads->head->interp)
2191 pthread_mutex_unlock(&perl_threads->mutex);
2194 } /* static void perl_log (int, const char *) */
2196 static int perl_notify(const notification_t *notif, user_data_t *user_data) {
2199 if (NULL == perl_threads)
2203 c_ithread_t *t = NULL;
2205 pthread_mutex_lock(&perl_threads->mutex);
2206 t = c_ithread_create(perl_threads->head->interp);
2207 pthread_mutex_unlock(&perl_threads->mutex);
2211 return pplugin_call(aTHX_ PLUGIN_NOTIF, user_data->data, notif);
2212 } /* static int perl_notify (const notification_t *) */
2214 static int perl_flush(cdtime_t timeout, const char *identifier,
2215 user_data_t *user_data) {
2218 if (NULL == perl_threads)
2222 c_ithread_t *t = NULL;
2224 pthread_mutex_lock(&perl_threads->mutex);
2225 t = c_ithread_create(perl_threads->head->interp);
2226 pthread_mutex_unlock(&perl_threads->mutex);
2231 /* For collectd-5.6 only, #1731 */
2232 if (user_data == NULL || user_data->data == NULL)
2233 return pplugin_call(aTHX_ PLUGIN_FLUSH_ALL, timeout, identifier);
2235 return pplugin_call(aTHX_ PLUGIN_FLUSH, user_data->data, timeout, identifier);
2236 } /* static int perl_flush (const int) */
2238 static int perl_shutdown(void) {
2244 plugin_unregister_complex_config("perl");
2245 plugin_unregister_read_group("perl");
2247 if (NULL == perl_threads)
2251 pthread_mutex_lock(&perl_threads->mutex);
2252 t = c_ithread_create(perl_threads->head->interp);
2253 pthread_mutex_unlock(&perl_threads->mutex);
2258 log_debug("perl_shutdown: c_ithread: interp = %p (active threads: %i)", aTHX,
2259 perl_threads->number_of_threads);
2261 plugin_unregister_init("perl");
2262 plugin_unregister_flush("perl"); /* For collectd-5.6 only, #1731 */
2264 ret = pplugin_call(aTHX_ PLUGIN_SHUTDOWN);
2266 pthread_mutex_lock(&perl_threads->mutex);
2267 t = perl_threads->tail;
2270 struct timespec ts_wait;
2271 c_ithread_t *thr = t;
2273 /* the pointer has to be advanced before destroying
2274 * the thread as this will free the memory */
2277 thr->shutdown = true;
2279 /* Give some time to thread to exit from Perl interpreter */
2280 WARNING("perl shutdown: Thread is running inside Perl. Waiting.");
2282 ts_wait.tv_nsec = 500000;
2283 nanosleep(&ts_wait, NULL);
2286 pthread_kill(thr->pthread, SIGTERM);
2287 ERROR("perl shutdown: Thread hangs inside Perl. Thread killed.");
2289 c_ithread_destroy(thr);
2292 pthread_mutex_unlock(&perl_threads->mutex);
2293 pthread_mutex_destroy(&perl_threads->mutex);
2294 pthread_mutexattr_destroy(&perl_threads->mutexattr);
2296 sfree(perl_threads);
2298 pthread_key_delete(perl_thr_key);
2302 plugin_unregister_shutdown("perl");
2304 } /* static void perl_shutdown (void) */
2307 * Access functions for global variables.
2309 * These functions implement the "magic" used to access
2310 * the global variables from Perl.
2313 static int g_pv_get(pTHX_ SV *var, MAGIC *mg) {
2314 char *pv = mg->mg_ptr;
2317 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2319 static int g_pv_set(pTHX_ SV *var, MAGIC *mg) {
2320 char *pv = mg->mg_ptr;
2321 sstrncpy(pv, SvPV_nolen(var), DATA_MAX_NAME_LEN);
2323 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2325 static int g_interval_get(pTHX_ SV *var, MAGIC *mg) {
2326 log_warn("Accessing $interval_g is deprecated (and might not "
2327 "give the desired results) - plugin_get_interval() should "
2328 "be used instead.");
2329 sv_setnv(var, CDTIME_T_TO_DOUBLE(interval_g));
2331 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2333 static int g_interval_set(pTHX_ SV *var, MAGIC *mg) {
2334 double nv = (double)SvNV(var);
2335 log_warn("Accessing $interval_g is deprecated (and might not "
2336 "give the desired results) - plugin_get_interval() should "
2337 "be used instead.");
2338 interval_g = DOUBLE_TO_CDTIME_T(nv);
2340 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2342 static MGVTBL g_pv_vtbl = {g_pv_get,
2349 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2354 static MGVTBL g_interval_vtbl = {g_interval_get,
2361 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2367 /* bootstrap the Collectd module */
2368 static void xs_init(pTHX) {
2371 char *file = __FILE__;
2375 /* enable usage of Perl modules using shared libraries */
2376 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2379 for (int i = 0; NULL != api[i].f; ++i)
2380 newXS(api[i].name, api[i].f, file);
2382 stash = gv_stashpv("Collectd", 1);
2384 /* export "constants" */
2385 for (int i = 0; '\0' != constants[i].name[0]; ++i)
2386 newCONSTSUB(stash, constants[i].name, newSViv(constants[i].value));
2388 /* export global variables
2389 * by adding "magic" to the SV's representing the globale variables
2390 * perl is able to automagically call the get/set function when
2391 * accessing any such variable (this is basically the same as using
2393 /* global strings */
2397 } g_strings[] = {{"Collectd::hostname_g", hostname_g}, {"", NULL}};
2399 for (int i = 0; '\0' != g_strings[i].name[0]; ++i) {
2400 tmp = get_sv(g_strings[i].name, 1);
2401 sv_magicext(tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl, g_strings[i].var, 0);
2404 tmp = get_sv("Collectd::interval_g", /* create = */ 1);
2405 sv_magicext(tmp, NULL, /* how = */ PERL_MAGIC_ext,
2406 /* vtbl = */ &g_interval_vtbl,
2407 /* name = */ NULL, /* namelen = */ 0);
2410 } /* static void xs_init (pTHX) */
2412 /* Initialize the global Perl interpreter. */
2413 static int init_pi(int argc, char **argv) {
2416 if (NULL != perl_threads)
2419 log_info("Initializing Perl interpreter...");
2422 for (int i = 0; i < argc; ++i)
2423 log_debug("argv[%i] = \"%s\"", i, argv[i]);
2425 #endif /* COLLECT_DEBUG */
2427 if (0 != pthread_key_create(&perl_thr_key, c_ithread_destructor)) {
2428 log_err("init_pi: pthread_key_create failed");
2430 /* this must not happen - cowardly giving up if it does */
2435 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2436 * triggers a "value computed is not used" warning by gcc. */
2439 PERL_SYS_INIT3(&argc, &argv, &environ);
2441 perl_threads = smalloc(sizeof(*perl_threads));
2442 memset(perl_threads, 0, sizeof(c_ithread_list_t));
2444 pthread_mutexattr_init(&perl_threads->mutexattr);
2445 pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE);
2446 pthread_mutex_init(&perl_threads->mutex, &perl_threads->mutexattr);
2447 /* locking the mutex should not be necessary at this point
2448 * but let's just do it for the sake of completeness */
2449 pthread_mutex_lock(&perl_threads->mutex);
2451 perl_threads->head = c_ithread_create(NULL);
2452 perl_threads->tail = perl_threads->head;
2454 if (NULL == (perl_threads->head->interp = perl_alloc())) {
2455 log_err("init_pi: Not enough memory.");
2459 aTHX = perl_threads->head->interp;
2460 pthread_mutex_unlock(&perl_threads->mutex);
2462 perl_construct(aTHX);
2464 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2466 if (0 != perl_parse(aTHX_ xs_init, argc, argv, NULL)) {
2467 SV *err = get_sv("@", 1);
2468 log_err("init_pi: Unable to bootstrap Collectd: %s", SvPV_nolen(err));
2470 perl_destruct(perl_threads->head->interp);
2471 perl_free(perl_threads->head->interp);
2472 sfree(perl_threads);
2474 pthread_key_delete(perl_thr_key);
2478 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2479 sv_setpv(get_sv("0", 0), "collectd");
2483 plugin_register_init("perl", perl_init);
2484 plugin_register_shutdown("perl", perl_shutdown);
2486 } /* static int init_pi (const char **, const int) */
2489 * LoadPlugin "<Plugin>"
2491 static int perl_config_loadplugin(pTHX_ oconfig_item_t *ci) {
2492 char module_name[DATA_MAX_NAME_LEN];
2496 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2497 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2498 log_err("LoadPlugin expects a single string argument.");
2502 value = ci->values[0].value.string;
2504 if (NULL == get_module_name(module_name, sizeof(module_name), value)) {
2505 log_err("Invalid module name %s", value);
2509 if (0 != init_pi(perl_argc, perl_argv))
2512 assert(NULL != perl_threads);
2513 assert(NULL != perl_threads->head);
2515 aTHX = perl_threads->head->interp;
2517 log_debug("perl_config: Loading Perl plugin \"%s\"", value);
2518 load_module(PERL_LOADMOD_NOIMPORT, newSVpv(module_name, strlen(module_name)),
2521 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2526 static int perl_config_basename(pTHX_ oconfig_item_t *ci) {
2529 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2530 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2531 log_err("BaseName expects a single string argument.");
2535 value = ci->values[0].value.string;
2537 log_debug("perl_config: Setting plugin basename to \"%s\"", value);
2538 sstrncpy(base_name, value, sizeof(base_name));
2540 } /* static int perl_config_basename (oconfig_item_it *) */
2543 * EnableDebugger "<Package>"|""
2545 static int perl_config_enabledebugger(pTHX_ oconfig_item_t *ci) {
2548 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2549 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2550 log_err("EnableDebugger expects a single string argument.");
2554 if (NULL != perl_threads) {
2555 log_warn("EnableDebugger has no effects if used after LoadPlugin.");
2559 value = ci->values[0].value.string;
2561 perl_argv = realloc(perl_argv, (++perl_argc + 1) * sizeof(char *));
2563 if (NULL == perl_argv) {
2564 log_err("perl_config: Not enough memory.");
2568 if ('\0' == value[0]) {
2569 perl_argv[perl_argc - 1] = "-d";
2571 perl_argv[perl_argc - 1] = smalloc(strlen(value) + 4);
2572 sstrncpy(perl_argv[perl_argc - 1], "-d:", 4);
2573 sstrncpy(perl_argv[perl_argc - 1] + 3, value, strlen(value) + 1);
2576 perl_argv[perl_argc] = NULL;
2578 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2581 * IncludeDir "<Dir>"
2583 static int perl_config_includedir(pTHX_ oconfig_item_t *ci) {
2586 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2587 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2588 log_err("IncludeDir expects a single string argument.");
2592 value = ci->values[0].value.string;
2595 perl_argv = realloc(perl_argv, (++perl_argc + 1) * sizeof(char *));
2597 if (NULL == perl_argv) {
2598 log_err("perl_config: Not enough memory.");
2602 perl_argv[perl_argc - 1] = smalloc(strlen(value) + 3);
2603 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2604 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen(value) + 1);
2606 perl_argv[perl_argc] = NULL;
2608 /* prepend the directory to @INC */
2609 av_unshift(GvAVn(PL_incgv), 1);
2610 av_store(GvAVn(PL_incgv), 0, newSVpv(value, strlen(value)));
2613 } /* static int perl_config_includedir (oconfig_item_it *) */
2618 static int perl_config_plugin(pTHX_ oconfig_item_t *ci) {
2625 if (NULL == perl_threads) {
2626 log_err("A `Plugin' block was encountered but no plugin was loaded yet. "
2627 "Put the appropriate `LoadPlugin' option in front of it.");
2633 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2634 log_err("LoadPlugin expects a single string argument.");
2638 plugin = ci->values[0].value.string;
2641 if (0 != oconfig_item2hv(aTHX_ ci, config)) {
2645 log_err("Unable to convert configuration to a Perl hash value.");
2646 config = (HV *)&PL_sv_undef;
2654 XPUSHs(sv_2mortal(newSVpv(plugin, 0)));
2655 XPUSHs(sv_2mortal(newRV_noinc((SV *)config)));
2659 retvals = call_pv("Collectd::_plugin_dispatch_config", G_SCALAR);
2673 } /* static int perl_config_plugin (oconfig_item_it *) */
2675 static int perl_config(oconfig_item_t *ci) {
2680 for (int i = 0; i < ci->children_num; ++i) {
2681 oconfig_item_t *c = ci->children + i;
2682 int current_status = 0;
2684 if (NULL != perl_threads) {
2685 if ((aTHX = PERL_GET_CONTEXT) == NULL)
2689 if (0 == strcasecmp(c->key, "LoadPlugin"))
2690 current_status = perl_config_loadplugin(aTHX_ c);
2691 else if (0 == strcasecmp(c->key, "BaseName"))
2692 current_status = perl_config_basename(aTHX_ c);
2693 else if (0 == strcasecmp(c->key, "EnableDebugger"))
2694 current_status = perl_config_enabledebugger(aTHX_ c);
2695 else if (0 == strcasecmp(c->key, "IncludeDir"))
2696 current_status = perl_config_includedir(aTHX_ c);
2697 else if (0 == strcasecmp(c->key, "Plugin"))
2698 current_status = perl_config_plugin(aTHX_ c);
2699 else if (0 == strcasecmp(c->key, "RegisterLegacyFlush"))
2700 cf_util_get_boolean(c, ®ister_legacy_flush);
2702 log_warn("Ignoring unknown config key \"%s\".", c->key);
2706 /* fatal error - it's up to perl_config_* to clean up */
2707 if (0 > current_status) {
2708 log_err("Configuration failed with a fatal error - "
2709 "plugin disabled!");
2710 return current_status;
2713 status += current_status;
2716 } /* static int perl_config (oconfig_item_t *) */
2718 void module_register(void) {
2720 perl_argv = smalloc((perl_argc + 1) * sizeof(*perl_argv));
2722 /* default options for the Perl interpreter */
2724 perl_argv[1] = "-MCollectd";
2725 perl_argv[2] = "-e";
2727 perl_argv[4] = NULL;
2729 plugin_register_complex_config("perl", perl_config);
2731 } /* void module_register (void) */