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
48 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
50 #pragma GCC poison sprintf
55 /* Some versions of Perl define their own version of DEBUG... :-/ */
60 /* ... while we want the definition found in plugin.h. */
64 #include "filter_chain.h"
66 #if !defined(USE_ITHREADS)
67 #error "Perl does not support ithreads!"
68 #endif /* !defined(USE_ITHREADS) */
70 /* clear the Perl sub's stack frame
71 * (this should only be used inside an XSUB) */
72 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
76 #define PLUGIN_WRITE 2
77 #define PLUGIN_SHUTDOWN 3
79 #define PLUGIN_NOTIF 5
80 #define PLUGIN_FLUSH 6
81 #define PLUGIN_FLUSH_ALL 7 /* For collectd-5.6 only */
83 #define PLUGIN_TYPES 8
85 #define PLUGIN_CONFIG 254
86 #define PLUGIN_DATASET 255
93 #define FC_CB_CREATE 0
94 #define FC_CB_DESTROY 1
99 #define log_debug(...) DEBUG("perl: " __VA_ARGS__)
100 #define log_info(...) INFO("perl: " __VA_ARGS__)
101 #define log_warn(...) WARNING("perl: " __VA_ARGS__)
102 #define log_err(...) ERROR("perl: " __VA_ARGS__)
104 /* this is defined in DynaLoader.a */
105 void boot_DynaLoader(PerlInterpreter *, CV *);
107 static XS(Collectd_plugin_register_read);
108 static XS(Collectd_plugin_register_write);
109 static XS(Collectd_plugin_register_log);
110 static XS(Collectd_plugin_register_notification);
111 static XS(Collectd_plugin_register_flush);
112 static XS(Collectd_plugin_unregister_read);
113 static XS(Collectd_plugin_unregister_write);
114 static XS(Collectd_plugin_unregister_log);
115 static XS(Collectd_plugin_unregister_notification);
116 static XS(Collectd_plugin_unregister_flush);
117 static XS(Collectd_plugin_register_ds);
118 static XS(Collectd_plugin_unregister_ds);
119 static XS(Collectd_plugin_dispatch_values);
120 static XS(Collectd_plugin_get_interval);
121 static XS(Collectd__plugin_write);
122 static XS(Collectd__plugin_flush);
123 static XS(Collectd_plugin_dispatch_notification);
124 static XS(Collectd_plugin_log);
125 static XS(Collectd__fc_register);
126 static XS(Collectd_call_by_name);
128 static int perl_read(user_data_t *ud);
129 static int perl_write(const data_set_t *ds, const value_list_t *vl,
130 user_data_t *user_data);
131 static void perl_log(int level, const char *msg, user_data_t *user_data);
132 static int perl_notify(const notification_t *notif, user_data_t *user_data);
133 static int perl_flush(cdtime_t timeout, const char *identifier,
134 user_data_t *user_data);
140 typedef struct c_ithread_s {
141 /* the thread's Perl interpreter */
142 PerlInterpreter *interp;
143 _Bool running; /* thread is inside Perl interpreter */
147 /* double linked list of threads */
148 struct c_ithread_s *prev;
149 struct c_ithread_s *next;
157 /* some usage stats */
158 int number_of_threads;
159 #endif /* COLLECT_DEBUG */
161 pthread_mutex_t mutex;
162 pthread_mutexattr_t mutexattr;
165 /* name / user_data for Perl matches / targets */
171 #define PFC_USER_DATA_FREE(data) \
173 sfree((data)->name); \
174 if (NULL != (data)->user_data) \
175 sv_free((data)->user_data); \
182 extern char **environ;
188 static _Bool register_legacy_flush = 1;
190 /* if perl_threads != NULL perl_threads->head must
191 * point to the "base" thread */
192 static c_ithread_list_t *perl_threads = NULL;
194 /* the key used to store each pthread's ithread */
195 static pthread_key_t perl_thr_key;
197 static int perl_argc = 0;
198 static char **perl_argv = NULL;
200 static char base_name[DATA_MAX_NAME_LEN] = "";
206 {"Collectd::plugin_register_read", Collectd_plugin_register_read},
207 {"Collectd::plugin_register_write", Collectd_plugin_register_write},
208 {"Collectd::plugin_register_log", Collectd_plugin_register_log},
209 {"Collectd::plugin_register_notification",
210 Collectd_plugin_register_notification},
211 {"Collectd::plugin_register_flush", Collectd_plugin_register_flush},
212 {"Collectd::plugin_unregister_read", Collectd_plugin_unregister_read},
213 {"Collectd::plugin_unregister_write", Collectd_plugin_unregister_write},
214 {"Collectd::plugin_unregister_log", Collectd_plugin_unregister_log},
215 {"Collectd::plugin_unregister_notification",
216 Collectd_plugin_unregister_notification},
217 {"Collectd::plugin_unregister_flush", Collectd_plugin_unregister_flush},
218 {"Collectd::plugin_register_data_set", Collectd_plugin_register_ds},
219 {"Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds},
220 {"Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values},
221 {"Collectd::plugin_get_interval", Collectd_plugin_get_interval},
222 {"Collectd::_plugin_write", Collectd__plugin_write},
223 {"Collectd::_plugin_flush", Collectd__plugin_flush},
224 {"Collectd::plugin_dispatch_notification",
225 Collectd_plugin_dispatch_notification},
226 {"Collectd::plugin_log", Collectd_plugin_log},
227 {"Collectd::_fc_register", Collectd__fc_register},
228 {"Collectd::call_by_name", Collectd_call_by_name},
234 } constants[] = {{"Collectd::TYPE_INIT", PLUGIN_INIT},
235 {"Collectd::TYPE_READ", PLUGIN_READ},
236 {"Collectd::TYPE_WRITE", PLUGIN_WRITE},
237 {"Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN},
238 {"Collectd::TYPE_LOG", PLUGIN_LOG},
239 {"Collectd::TYPE_NOTIF", PLUGIN_NOTIF},
240 {"Collectd::TYPE_FLUSH", PLUGIN_FLUSH},
241 {"Collectd::TYPE_CONFIG", PLUGIN_CONFIG},
242 {"Collectd::TYPE_DATASET", PLUGIN_DATASET},
243 {"Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER},
244 {"Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE},
245 {"Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE},
246 {"Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE},
247 {"Collectd::LOG_ERR", LOG_ERR},
248 {"Collectd::LOG_WARNING", LOG_WARNING},
249 {"Collectd::LOG_NOTICE", LOG_NOTICE},
250 {"Collectd::LOG_INFO", LOG_INFO},
251 {"Collectd::LOG_DEBUG", LOG_DEBUG},
252 {"Collectd::FC_MATCH", FC_MATCH},
253 {"Collectd::FC_TARGET", FC_TARGET},
254 {"Collectd::FC_CB_CREATE", FC_CB_CREATE},
255 {"Collectd::FC_CB_DESTROY", FC_CB_DESTROY},
256 {"Collectd::FC_CB_EXEC", FC_CB_EXEC},
257 {"Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH},
258 {"Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES},
259 {"Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE},
260 {"Collectd::FC_TARGET_STOP", FC_TARGET_STOP},
261 {"Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN},
262 {"Collectd::NOTIF_FAILURE", NOTIF_FAILURE},
263 {"Collectd::NOTIF_WARNING", NOTIF_WARNING},
264 {"Collectd::NOTIF_OKAY", NOTIF_OKAY},
270 } g_strings[] = {{"Collectd::hostname_g", hostname_g}, {"", NULL}};
273 * Helper functions for data type conversion.
288 static int hv2data_source(pTHX_ HV *hash, data_source_t *ds) {
291 if ((NULL == hash) || (NULL == ds))
294 if (NULL != (tmp = hv_fetch(hash, "name", 4, 0))) {
295 sstrncpy(ds->name, SvPV_nolen(*tmp), sizeof(ds->name));
297 log_err("hv2data_source: No DS name given.");
301 if (NULL != (tmp = hv_fetch(hash, "type", 4, 0))) {
302 ds->type = SvIV(*tmp);
304 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type) &&
305 (DS_TYPE_DERIVE != ds->type) && (DS_TYPE_ABSOLUTE != ds->type)) {
306 log_err("hv2data_source: Invalid DS type.");
310 ds->type = DS_TYPE_COUNTER;
313 if (NULL != (tmp = hv_fetch(hash, "min", 3, 0)))
314 ds->min = SvNV(*tmp);
318 if (NULL != (tmp = hv_fetch(hash, "max", 3, 0)))
319 ds->max = SvNV(*tmp);
323 } /* static int hv2data_source (HV *, data_source_t *) */
325 /* av2value converts at most "len" elements from "array" to "value". Returns the
326 * number of elements converted or zero on error. */
327 static size_t av2value(pTHX_ char *name, AV *array, value_t *value,
329 const data_set_t *ds;
331 if ((NULL == name) || (NULL == array) || (NULL == value) || (array_len == 0))
334 ds = plugin_get_ds(name);
336 log_err("av2value: Unknown dataset \"%s\"", name);
340 if (array_len < ds->ds_num) {
341 log_warn("av2value: array does not contain enough elements for type "
342 "\"%s\": got %zu, want %zu",
343 name, array_len, ds->ds_num);
345 } else if (array_len > ds->ds_num) {
346 log_warn("av2value: array contains excess elements for type \"%s\": got "
348 name, array_len, ds->ds_num);
351 for (size_t i = 0; i < ds->ds_num; ++i) {
352 SV **tmp = av_fetch(array, i, 0);
355 if (DS_TYPE_COUNTER == ds->ds[i].type)
356 value[i].counter = SvIV(*tmp);
357 else if (DS_TYPE_GAUGE == ds->ds[i].type)
358 value[i].gauge = SvNV(*tmp);
359 else if (DS_TYPE_DERIVE == ds->ds[i].type)
360 value[i].derive = SvIV(*tmp);
361 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
362 value[i].absolute = SvIV(*tmp);
369 } /* static size_t av2value (char *, AV *, value_t *, size_t) */
374 * values => [ @values ],
378 * plugin_instance => $pinstance,
379 * type_instance => $tinstance,
382 static int hv2value_list(pTHX_ HV *hash, value_list_t *vl) {
385 if ((NULL == hash) || (NULL == vl))
388 if (NULL == (tmp = hv_fetch(hash, "type", 4, 0))) {
389 log_err("hv2value_list: No type given.");
393 sstrncpy(vl->type, SvPV_nolen(*tmp), sizeof(vl->type));
395 if ((NULL == (tmp = hv_fetch(hash, "values", 6, 0))) ||
396 (!(SvROK(*tmp) && (SVt_PVAV == SvTYPE(SvRV(*tmp)))))) {
397 log_err("hv2value_list: No valid values given.");
402 AV *array = (AV *)SvRV(*tmp);
403 /* av_len returns the highest index, not the actual length. */
404 size_t array_len = (size_t)(av_len(array) + 1);
408 vl->values = calloc(array_len, sizeof(*vl->values));
410 av2value(aTHX_ vl->type, (AV *)SvRV(*tmp), vl->values, array_len);
411 if (vl->values_len == 0) {
417 if (NULL != (tmp = hv_fetch(hash, "time", 4, 0))) {
418 double t = SvNV(*tmp);
419 vl->time = DOUBLE_TO_CDTIME_T(t);
422 if (NULL != (tmp = hv_fetch(hash, "interval", 8, 0))) {
423 double t = SvNV(*tmp);
424 vl->interval = DOUBLE_TO_CDTIME_T(t);
427 if (NULL != (tmp = hv_fetch(hash, "host", 4, 0)))
428 sstrncpy(vl->host, SvPV_nolen(*tmp), sizeof(vl->host));
430 sstrncpy(vl->host, hostname_g, sizeof(vl->host));
432 if (NULL != (tmp = hv_fetch(hash, "plugin", 6, 0)))
433 sstrncpy(vl->plugin, SvPV_nolen(*tmp), sizeof(vl->plugin));
435 if (NULL != (tmp = hv_fetch(hash, "plugin_instance", 15, 0)))
436 sstrncpy(vl->plugin_instance, SvPV_nolen(*tmp),
437 sizeof(vl->plugin_instance));
439 if (NULL != (tmp = hv_fetch(hash, "type_instance", 13, 0)))
440 sstrncpy(vl->type_instance, SvPV_nolen(*tmp), sizeof(vl->type_instance));
442 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
444 static int av2data_set(pTHX_ AV *array, char *name, data_set_t *ds) {
447 if ((NULL == array) || (NULL == name) || (NULL == ds))
453 log_err("av2data_set: Invalid data set.");
457 ds->ds = smalloc((len + 1) * sizeof(*ds->ds));
458 ds->ds_num = len + 1;
460 for (int i = 0; i <= len; ++i) {
461 SV **elem = av_fetch(array, i, 0);
464 log_err("av2data_set: Failed to fetch data source %i.", i);
468 if (!(SvROK(*elem) && (SVt_PVHV == SvTYPE(SvRV(*elem))))) {
469 log_err("av2data_set: Invalid data source.");
473 if (-1 == hv2data_source(aTHX_(HV *) SvRV(*elem), &ds->ds[i]))
476 log_debug("av2data_set: "
477 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
478 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
481 sstrncpy(ds->type, name, sizeof(ds->type));
483 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
488 * severity => $severity,
494 * plugin_instance => $instance,
495 * type_instance => $type_instance,
496 * meta => [ { name => <name>, value => <value> }, ... ]
499 static int av2notification_meta(pTHX_ AV *array,
500 notification_meta_t **ret_meta) {
501 notification_meta_t *tail = NULL;
503 int len = av_len(array);
505 for (int i = 0; i <= len; ++i) {
506 SV **tmp = av_fetch(array, i, 0);
511 if (!(SvROK(*tmp) && (SVt_PVHV == SvTYPE(SvRV(*tmp))))) {
512 log_warn("av2notification_meta: Skipping invalid "
513 "meta information.");
517 HV *hash = (HV *)SvRV(*tmp);
519 notification_meta_t *m = calloc(1, sizeof(*m));
523 SV **name = hv_fetch(hash, "name", strlen("name"), 0);
525 log_warn("av2notification_meta: Skipping invalid "
526 "meta information.");
530 sstrncpy(m->name, SvPV_nolen(*name), sizeof(m->name));
532 SV **value = hv_fetch(hash, "value", strlen("value"), 0);
534 log_warn("av2notification_meta: Skipping invalid "
535 "meta information.");
541 m->nm_value.nm_double = SvNVX(*value);
542 m->type = NM_TYPE_DOUBLE;
543 } else if (SvUOK(*value)) {
544 m->nm_value.nm_unsigned_int = SvUVX(*value);
545 m->type = NM_TYPE_UNSIGNED_INT;
546 } else if (SvIOK(*value)) {
547 m->nm_value.nm_signed_int = SvIVX(*value);
548 m->type = NM_TYPE_SIGNED_INT;
550 m->nm_value.nm_string = sstrdup(SvPV_nolen(*value));
551 m->type = NM_TYPE_STRING;
563 } /* static int av2notification_meta (AV *, notification_meta_t *) */
565 static int hv2notification(pTHX_ HV *hash, notification_t *n) {
568 if ((NULL == hash) || (NULL == n))
571 if (NULL != (tmp = hv_fetch(hash, "severity", 8, 0)))
572 n->severity = SvIV(*tmp);
574 n->severity = NOTIF_FAILURE;
576 if (NULL != (tmp = hv_fetch(hash, "time", 4, 0))) {
577 double t = SvNV(*tmp);
578 n->time = DOUBLE_TO_CDTIME_T(t);
582 if (NULL != (tmp = hv_fetch(hash, "message", 7, 0)))
583 sstrncpy(n->message, SvPV_nolen(*tmp), sizeof(n->message));
585 if (NULL != (tmp = hv_fetch(hash, "host", 4, 0)))
586 sstrncpy(n->host, SvPV_nolen(*tmp), sizeof(n->host));
588 sstrncpy(n->host, hostname_g, sizeof(n->host));
590 if (NULL != (tmp = hv_fetch(hash, "plugin", 6, 0)))
591 sstrncpy(n->plugin, SvPV_nolen(*tmp), sizeof(n->plugin));
593 if (NULL != (tmp = hv_fetch(hash, "plugin_instance", 15, 0)))
594 sstrncpy(n->plugin_instance, SvPV_nolen(*tmp), sizeof(n->plugin_instance));
596 if (NULL != (tmp = hv_fetch(hash, "type", 4, 0)))
597 sstrncpy(n->type, SvPV_nolen(*tmp), sizeof(n->type));
599 if (NULL != (tmp = hv_fetch(hash, "type_instance", 13, 0)))
600 sstrncpy(n->type_instance, SvPV_nolen(*tmp), sizeof(n->type_instance));
603 while (NULL != (tmp = hv_fetch(hash, "meta", 4, 0))) {
604 if (!(SvROK(*tmp) && (SVt_PVAV == SvTYPE(SvRV(*tmp))))) {
605 log_warn("hv2notification: Ignoring invalid meta information.");
609 if (0 != av2notification_meta(aTHX_(AV *) SvRV(*tmp), &n->meta)) {
610 plugin_notification_meta_free(n->meta);
617 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
619 static int data_set2av(pTHX_ data_set_t *ds, AV *array) {
620 if ((NULL == ds) || (NULL == array))
623 av_extend(array, ds->ds_num);
625 for (size_t i = 0; i < ds->ds_num; ++i) {
626 HV *source = newHV();
628 if (NULL == hv_store(source, "name", 4, newSVpv(ds->ds[i].name, 0), 0))
631 if (NULL == hv_store(source, "type", 4, newSViv(ds->ds[i].type), 0))
634 if (!isnan(ds->ds[i].min))
635 if (NULL == hv_store(source, "min", 3, newSVnv(ds->ds[i].min), 0))
638 if (!isnan(ds->ds[i].max))
639 if (NULL == hv_store(source, "max", 3, newSVnv(ds->ds[i].max), 0))
642 if (NULL == av_store(array, i, newRV_noinc((SV *)source)))
646 } /* static int data_set2av (data_set_t *, AV *) */
648 static int value_list2hv(pTHX_ value_list_t *vl, data_set_t *ds, HV *hash) {
652 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
656 /* av_extend takes the last *index* to which the array should be extended. */
657 av_extend(values, vl->values_len - 1);
659 assert(ds->ds_num == vl->values_len);
660 for (i = 0; i < vl->values_len; ++i) {
663 if (DS_TYPE_COUNTER == ds->ds[i].type)
664 val = newSViv(vl->values[i].counter);
665 else if (DS_TYPE_GAUGE == ds->ds[i].type)
666 val = newSVnv(vl->values[i].gauge);
667 else if (DS_TYPE_DERIVE == ds->ds[i].type)
668 val = newSViv(vl->values[i].derive);
669 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
670 val = newSViv(vl->values[i].absolute);
672 if (NULL == av_store(values, i, val)) {
678 if (NULL == hv_store(hash, "values", 6, newRV_noinc((SV *)values), 0))
682 double t = CDTIME_T_TO_DOUBLE(vl->time);
683 if (NULL == hv_store(hash, "time", 4, newSVnv(t), 0))
688 double t = CDTIME_T_TO_DOUBLE(vl->interval);
689 if (NULL == hv_store(hash, "interval", 8, newSVnv(t), 0))
693 if ('\0' != vl->host[0])
694 if (NULL == hv_store(hash, "host", 4, newSVpv(vl->host, 0), 0))
697 if ('\0' != vl->plugin[0])
698 if (NULL == hv_store(hash, "plugin", 6, newSVpv(vl->plugin, 0), 0))
701 if ('\0' != vl->plugin_instance[0])
702 if (NULL == hv_store(hash, "plugin_instance", 15,
703 newSVpv(vl->plugin_instance, 0), 0))
706 if ('\0' != vl->type[0])
707 if (NULL == hv_store(hash, "type", 4, newSVpv(vl->type, 0), 0))
710 if ('\0' != vl->type_instance[0])
712 hv_store(hash, "type_instance", 13, newSVpv(vl->type_instance, 0), 0))
715 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
717 static int notification_meta2av(pTHX_ notification_meta_t *meta, AV *array) {
719 for (notification_meta_t *m = meta; m != NULL; m = m->next) {
723 av_extend(array, meta_num);
725 for (int i = 0; NULL != meta; meta = meta->next, ++i) {
729 if (NULL == hv_store(m, "name", 4, newSVpv(meta->name, 0), 0))
732 if (NM_TYPE_STRING == meta->type)
733 value = newSVpv(meta->nm_value.nm_string, 0);
734 else if (NM_TYPE_SIGNED_INT == meta->type)
735 value = newSViv(meta->nm_value.nm_signed_int);
736 else if (NM_TYPE_UNSIGNED_INT == meta->type)
737 value = newSVuv(meta->nm_value.nm_unsigned_int);
738 else if (NM_TYPE_DOUBLE == meta->type)
739 value = newSVnv(meta->nm_value.nm_double);
740 else if (NM_TYPE_BOOLEAN == meta->type)
741 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
745 if (NULL == hv_store(m, "value", 5, value, 0)) {
750 if (NULL == av_store(array, i, newRV_noinc((SV *)m))) {
757 } /* static int notification_meta2av (notification_meta_t *, AV *) */
759 static int notification2hv(pTHX_ notification_t *n, HV *hash) {
760 if (NULL == hv_store(hash, "severity", 8, newSViv(n->severity), 0))
764 double t = CDTIME_T_TO_DOUBLE(n->time);
765 if (NULL == hv_store(hash, "time", 4, newSVnv(t), 0))
769 if ('\0' != *n->message)
770 if (NULL == hv_store(hash, "message", 7, newSVpv(n->message, 0), 0))
773 if ('\0' != *n->host)
774 if (NULL == hv_store(hash, "host", 4, newSVpv(n->host, 0), 0))
777 if ('\0' != *n->plugin)
778 if (NULL == hv_store(hash, "plugin", 6, newSVpv(n->plugin, 0), 0))
781 if ('\0' != *n->plugin_instance)
782 if (NULL == hv_store(hash, "plugin_instance", 15,
783 newSVpv(n->plugin_instance, 0), 0))
786 if ('\0' != *n->type)
787 if (NULL == hv_store(hash, "type", 4, newSVpv(n->type, 0), 0))
790 if ('\0' != *n->type_instance)
792 hv_store(hash, "type_instance", 13, newSVpv(n->type_instance, 0), 0))
795 if (NULL != n->meta) {
797 if ((0 != notification_meta2av(aTHX_ n->meta, meta)) ||
798 (NULL == hv_store(hash, "meta", 4, newRV_noinc((SV *)meta), 0))) {
805 } /* static int notification2hv (notification_t *, HV *) */
807 static int oconfig_item2hv(pTHX_ oconfig_item_t *ci, HV *hash) {
811 if (NULL == hv_store(hash, "key", 3, newSVpv(ci->key, 0), 0))
815 if (0 < ci->values_num)
816 av_extend(values, ci->values_num);
818 if (NULL == hv_store(hash, "values", 6, newRV_noinc((SV *)values), 0)) {
824 for (int i = 0; i < ci->values_num; ++i) {
827 switch (ci->values[i].type) {
828 case OCONFIG_TYPE_STRING:
829 value = newSVpv(ci->values[i].value.string, 0);
831 case OCONFIG_TYPE_NUMBER:
832 value = newSVnv((NV)ci->values[i].value.number);
834 case OCONFIG_TYPE_BOOLEAN:
835 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
838 log_err("oconfig_item2hv: Invalid value type %i.", ci->values[i].type);
839 value = &PL_sv_undef;
842 if (NULL == av_store(values, i, value)) {
848 /* ignoring 'parent' member which is uninteresting in this case */
851 if (0 < ci->children_num)
852 av_extend(children, ci->children_num);
854 if (NULL == hv_store(hash, "children", 8, newRV_noinc((SV *)children), 0)) {
860 for (int i = 0; i < ci->children_num; ++i) {
863 if (0 != oconfig_item2hv(aTHX_ ci->children + i, child)) {
869 if (NULL == av_store(children, i, newRV_noinc((SV *)child))) {
876 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
879 * Internal functions.
882 static char *get_module_name(char *buf, size_t buf_len, const char *module) {
884 if (base_name[0] == '\0')
885 status = ssnprintf(buf, buf_len, "%s", module);
887 status = ssnprintf(buf, buf_len, "%s::%s", base_name, module);
888 if ((status < 0) || ((unsigned int)status >= buf_len))
891 } /* char *get_module_name */
894 * Add a plugin's data set definition.
896 static int pplugin_register_data_set(pTHX_ char *name, AV *dataset) {
901 if ((NULL == name) || (NULL == dataset))
904 if (0 != av2data_set(aTHX_ dataset, name, &ds))
907 ret = plugin_register_data_set(&ds);
911 } /* static int pplugin_register_data_set (char *, SV *) */
914 * Remove a plugin's data set definition.
916 static int pplugin_unregister_data_set(char *name) {
919 return plugin_unregister_data_set(name);
920 } /* static int pplugin_unregister_data_set (char *) */
923 * Submit the values to the write functions.
925 static int pplugin_dispatch_values(pTHX_ HV *values) {
926 value_list_t vl = VALUE_LIST_INIT;
933 if (0 != hv2value_list(aTHX_ values, &vl))
936 ret = plugin_dispatch_values(&vl);
940 } /* static int pplugin_dispatch_values (char *, HV *) */
943 * Submit the values to a single write function.
945 static int pplugin_write(pTHX_ const char *plugin, AV *data_set, HV *values) {
947 value_list_t vl = VALUE_LIST_INIT;
954 if (0 != hv2value_list(aTHX_ values, &vl))
957 if ((NULL != data_set) && (0 != av2data_set(aTHX_ data_set, vl.type, &ds)))
960 ret = plugin_write(plugin, NULL == data_set ? NULL : &ds, &vl);
962 log_warn("Dispatching value to plugin \"%s\" failed with status %i.",
963 NULL == plugin ? "<any>" : plugin, ret);
965 if (NULL != data_set)
969 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
972 * Dispatch a notification.
974 static int pplugin_dispatch_notification(pTHX_ HV *notif) {
975 notification_t n = {0};
982 if (0 != hv2notification(aTHX_ notif, &n))
985 ret = plugin_dispatch_notification(&n);
986 plugin_notification_meta_free(n.meta);
988 } /* static int pplugin_dispatch_notification (HV *) */
991 * Call perl sub with thread locking flags handled.
993 static int call_pv_locked(pTHX_ const char *sub_name) {
997 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
998 if (t == NULL) /* thread destroyed */
1001 old_running = t->running;
1005 t->running = old_running;
1009 ret = call_pv(sub_name, G_SCALAR | G_EVAL);
1011 t->running = old_running;
1013 } /* static int call_pv_locked (pTHX, *sub_name) */
1016 * Call all working functions of the given type.
1018 static int pplugin_call(pTHX_ int type, ...) {
1027 if ((type < 0) || (type >= PLUGIN_TYPES))
1037 if (PLUGIN_READ == type) {
1038 subname = va_arg(ap, char *);
1039 } else if (PLUGIN_WRITE == type) {
1046 subname = va_arg(ap, char *);
1048 * $_[0] = $plugin_type;
1063 * values => [ $v1, ... ],
1065 * host => $hostname,
1066 * plugin => $plugin,
1068 * plugin_instance => $instance,
1069 * type_instance => $type_instance
1072 ds = va_arg(ap, data_set_t *);
1073 vl = va_arg(ap, value_list_t *);
1075 if (-1 == data_set2av(aTHX_ ds, pds)) {
1078 pds = (AV *)&PL_sv_undef;
1082 if (-1 == value_list2hv(aTHX_ vl, ds, pvl)) {
1085 pvl = (HV *)&PL_sv_undef;
1089 XPUSHs(sv_2mortal(newSVpv(ds->type, 0)));
1090 XPUSHs(sv_2mortal(newRV_noinc((SV *)pds)));
1091 XPUSHs(sv_2mortal(newRV_noinc((SV *)pvl)));
1092 } else if (PLUGIN_LOG == type) {
1093 subname = va_arg(ap, char *);
1099 XPUSHs(sv_2mortal(newSViv(va_arg(ap, int))));
1100 XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
1101 } else if (PLUGIN_NOTIF == type) {
1103 HV *notif = newHV();
1105 subname = va_arg(ap, char *);
1109 * severity => $severity,
1113 * plugin => $plugin,
1115 * plugin_instance => $instance,
1116 * type_instance => $type_instance
1119 n = va_arg(ap, notification_t *);
1121 if (-1 == notification2hv(aTHX_ n, notif)) {
1124 notif = (HV *)&PL_sv_undef;
1128 XPUSHs(sv_2mortal(newRV_noinc((SV *)notif)));
1129 } else if (PLUGIN_FLUSH == type) {
1131 subname = va_arg(ap, char *);
1134 * $_[1] = $identifier;
1136 timeout = va_arg(ap, cdtime_t);
1138 XPUSHs(sv_2mortal(newSVnv(CDTIME_T_TO_DOUBLE(timeout))));
1139 XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
1140 } else if (PLUGIN_FLUSH_ALL == type) {
1142 subname = "Collectd::plugin_call_all";
1145 * $_[1] = $identifier;
1147 timeout = va_arg(ap, cdtime_t);
1149 XPUSHs(sv_2mortal(newSViv((IV)PLUGIN_FLUSH)));
1150 XPUSHs(sv_2mortal(newSVnv(CDTIME_T_TO_DOUBLE(timeout))));
1151 XPUSHs(sv_2mortal(newSVpv(va_arg(ap, char *), 0)));
1152 } else if (PLUGIN_INIT == type) {
1153 subname = "Collectd::plugin_call_all";
1154 XPUSHs(sv_2mortal(newSViv((IV)type)));
1155 } else if (PLUGIN_SHUTDOWN == type) {
1156 subname = "Collectd::plugin_call_all";
1157 XPUSHs(sv_2mortal(newSViv((IV)type)));
1158 } else { /* Unknown type. Run 'plugin_call_all' and make compiler happy */
1159 subname = "Collectd::plugin_call_all";
1160 XPUSHs(sv_2mortal(newSViv((IV)type)));
1165 retvals = call_pv_locked(aTHX_ subname);
1168 if (SvTRUE(ERRSV)) {
1169 if (PLUGIN_LOG != type)
1170 ERROR("perl: %s error: %s", subname, SvPV_nolen(ERRSV));
1172 } else if (0 < retvals) {
1184 } /* static int pplugin_call (int, ...) */
1187 * collectd's Perl interpreter based thread implementation.
1189 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1192 /* must be called with perl_threads->mutex locked */
1193 static void c_ithread_destroy(c_ithread_t *ithread) {
1194 dTHXa(ithread->interp);
1196 assert(NULL != perl_threads);
1198 PERL_SET_CONTEXT(aTHX);
1199 /* Mark as running to avoid deadlock:
1200 c_ithread_destroy -> log_debug -> perl_log()
1202 ithread->running = 1;
1203 log_debug("Shutting down Perl interpreter %p...", aTHX);
1208 --perl_threads->number_of_threads;
1209 #endif /* COLLECT_DEBUG */
1211 perl_destruct(aTHX);
1214 if (NULL == ithread->prev)
1215 perl_threads->head = ithread->next;
1217 ithread->prev->next = ithread->next;
1219 if (NULL == ithread->next)
1220 perl_threads->tail = ithread->prev;
1222 ithread->next->prev = ithread->prev;
1226 } /* static void c_ithread_destroy (c_ithread_t *) */
1228 static void c_ithread_destructor(void *arg) {
1229 c_ithread_t *ithread = (c_ithread_t *)arg;
1230 c_ithread_t *t = NULL;
1232 if (NULL == perl_threads)
1235 pthread_mutex_lock(&perl_threads->mutex);
1237 for (t = perl_threads->head; NULL != t; t = t->next)
1241 /* the ithread no longer exists */
1243 pthread_mutex_unlock(&perl_threads->mutex);
1247 c_ithread_destroy(ithread);
1249 pthread_mutex_unlock(&perl_threads->mutex);
1251 } /* static void c_ithread_destructor (void *) */
1253 /* must be called with perl_threads->mutex locked */
1254 static c_ithread_t *c_ithread_create(PerlInterpreter *base) {
1255 c_ithread_t *t = NULL;
1258 assert(NULL != perl_threads);
1260 t = smalloc(sizeof(*t));
1261 memset(t, 0, sizeof(c_ithread_t));
1263 t->interp = (NULL == base) ? NULL : perl_clone(base, CLONEf_KEEP_PTR_TABLE);
1267 if ((NULL != base) && (NULL != PL_endav)) {
1274 ++perl_threads->number_of_threads;
1275 #endif /* COLLECT_DEBUG */
1279 if (NULL == perl_threads->tail) {
1280 perl_threads->head = t;
1283 perl_threads->tail->next = t;
1284 t->prev = perl_threads->tail;
1287 t->pthread = pthread_self();
1290 perl_threads->tail = t;
1292 pthread_setspecific(perl_thr_key, (const void *)t);
1294 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1297 * Filter chains implementation.
1300 static int fc_call(pTHX_ int type, int cb_type, pfc_user_data_t *data, ...) {
1306 notification_meta_t **meta = NULL;
1311 if ((type < 0) || (type >= FC_TYPES))
1314 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1324 XPUSHs(sv_2mortal(newSViv((IV)type)));
1325 XPUSHs(sv_2mortal(newSVpv(data->name, 0)));
1326 XPUSHs(sv_2mortal(newSViv((IV)cb_type)));
1328 if (FC_CB_CREATE == cb_type) {
1331 * $_[1] = $user_data;
1334 HV *config = newHV();
1336 ci = va_arg(ap, oconfig_item_t *);
1338 if (0 != oconfig_item2hv(aTHX_ ci, config)) {
1341 config = (HV *)&PL_sv_undef;
1345 XPUSHs(sv_2mortal(newRV_noinc((SV *)config)));
1346 } else if (FC_CB_DESTROY == cb_type) {
1348 * $_[1] = $user_data;
1351 /* nothing to be done - the user data pointer
1352 * is pushed onto the stack later */
1353 } else if (FC_CB_EXEC == cb_type) {
1358 * $_[3] = $user_data;
1366 ds = va_arg(ap, data_set_t *);
1367 vl = va_arg(ap, value_list_t *);
1368 meta = va_arg(ap, notification_meta_t **);
1370 if (0 != data_set2av(aTHX_ ds, pds)) {
1373 pds = (AV *)&PL_sv_undef;
1377 if (0 != value_list2hv(aTHX_ vl, ds, pvl)) {
1380 pvl = (HV *)&PL_sv_undef;
1387 if (0 != notification_meta2av(aTHX_ * meta, pmeta)) {
1390 pmeta = (AV *)&PL_sv_undef;
1394 pmeta = (AV *)&PL_sv_undef;
1397 XPUSHs(sv_2mortal(newRV_noinc((SV *)pds)));
1398 XPUSHs(sv_2mortal(newRV_noinc((SV *)pvl)));
1399 XPUSHs(sv_2mortal(newRV_noinc((SV *)pmeta)));
1402 XPUSHs(sv_2mortal(newRV_inc(data->user_data)));
1406 retvals = call_pv_locked(aTHX_ "Collectd::fc_call");
1408 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1409 assert(pmeta != NULL);
1411 plugin_notification_meta_free(*meta);
1412 av2notification_meta(aTHX_ pmeta, meta);
1416 if (SvTRUE(ERRSV)) {
1417 ERROR("perl: Collectd::fc_call error: %s", SvPV_nolen(ERRSV));
1419 } else if (0 < retvals) {
1422 /* the exec callbacks return a status, while
1423 * the others return a boolean value */
1424 if (FC_CB_EXEC == cb_type)
1426 else if (!SvTRUE(tmp))
1436 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1438 static int fc_create(int type, const oconfig_item_t *ci, void **user_data) {
1439 pfc_user_data_t *data;
1445 if (NULL == perl_threads)
1449 c_ithread_t *t = NULL;
1451 pthread_mutex_lock(&perl_threads->mutex);
1452 t = c_ithread_create(perl_threads->head->interp);
1453 pthread_mutex_unlock(&perl_threads->mutex);
1458 log_debug("fc_create: c_ithread: interp = %p (active threads: %i)", aTHX,
1459 perl_threads->number_of_threads);
1461 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1462 log_warn("A \"%s\" block expects a single string argument.",
1463 (FC_MATCH == type) ? "Match" : "Target");
1467 data = smalloc(sizeof(*data));
1468 data->name = sstrdup(ci->values[0].value.string);
1469 data->user_data = newSV(0);
1471 ret = fc_call(aTHX_ type, FC_CB_CREATE, data, ci);
1474 PFC_USER_DATA_FREE(data);
1478 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1480 static int fc_destroy(int type, void **user_data) {
1481 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1487 if ((NULL == perl_threads) || (NULL == data))
1491 c_ithread_t *t = NULL;
1493 pthread_mutex_lock(&perl_threads->mutex);
1494 t = c_ithread_create(perl_threads->head->interp);
1495 pthread_mutex_unlock(&perl_threads->mutex);
1500 log_debug("fc_destroy: c_ithread: interp = %p (active threads: %i)", aTHX,
1501 perl_threads->number_of_threads);
1503 ret = fc_call(aTHX_ type, FC_CB_DESTROY, data);
1505 PFC_USER_DATA_FREE(data);
1508 } /* static int fc_destroy (int, void **) */
1510 static int fc_exec(int type, const data_set_t *ds, const value_list_t *vl,
1511 notification_meta_t **meta, void **user_data) {
1512 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1516 if (NULL == perl_threads)
1519 assert(NULL != data);
1522 c_ithread_t *t = NULL;
1524 pthread_mutex_lock(&perl_threads->mutex);
1525 t = c_ithread_create(perl_threads->head->interp);
1526 pthread_mutex_unlock(&perl_threads->mutex);
1531 log_debug("fc_exec: c_ithread: interp = %p (active threads: %i)", aTHX,
1532 perl_threads->number_of_threads);
1534 return fc_call(aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1535 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1536 notification_meta_t **, void **) */
1538 static int pmatch_create(const oconfig_item_t *ci, void **user_data) {
1539 return fc_create(FC_MATCH, ci, user_data);
1540 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1542 static int pmatch_destroy(void **user_data) {
1543 return fc_destroy(FC_MATCH, user_data);
1544 } /* static int pmatch_destroy (void **) */
1546 static int pmatch_match(const data_set_t *ds, const value_list_t *vl,
1547 notification_meta_t **meta, void **user_data) {
1548 return fc_exec(FC_MATCH, ds, vl, meta, user_data);
1549 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1550 notification_meta_t **, void **) */
1552 static match_proc_t pmatch = {pmatch_create, pmatch_destroy, pmatch_match};
1554 static int ptarget_create(const oconfig_item_t *ci, void **user_data) {
1555 return fc_create(FC_TARGET, ci, user_data);
1556 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1558 static int ptarget_destroy(void **user_data) {
1559 return fc_destroy(FC_TARGET, user_data);
1560 } /* static int ptarget_destroy (void **) */
1562 static int ptarget_invoke(const data_set_t *ds, value_list_t *vl,
1563 notification_meta_t **meta, void **user_data) {
1564 return fc_exec(FC_TARGET, ds, vl, meta, user_data);
1565 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1566 notification_meta_t **, void **) */
1568 static target_proc_t ptarget = {ptarget_create, ptarget_destroy,
1572 * Exported Perl API.
1575 static void _plugin_register_generic_userdata(pTHX, int type,
1578 user_data_t userdata;
1584 log_err("Usage: Collectd::plugin_register_%s(pluginname, subname)", desc);
1589 log_err("Collectd::plugin_register_%s(pluginname, subname): "
1590 "Invalid pluginname",
1595 log_err("Collectd::plugin_register_%s(pluginname, subname): "
1601 /* Use pluginname as-is to allow flush a single perl plugin */
1602 pluginname = SvPV_nolen(ST(0));
1604 log_debug("Collectd::plugin_register_%s: "
1605 "plugin = \"%s\", sub = \"%s\"",
1606 desc, pluginname, SvPV_nolen(ST(1)));
1608 memset(&userdata, 0, sizeof(userdata));
1609 userdata.data = strdup(SvPV_nolen(ST(1)));
1610 userdata.free_func = free;
1612 if (PLUGIN_READ == type) {
1613 ret = plugin_register_complex_read(
1615 pluginname, perl_read, plugin_get_interval(), /* Default interval */
1617 } else if (PLUGIN_WRITE == type) {
1618 ret = plugin_register_write(pluginname, perl_write, &userdata);
1619 } else if (PLUGIN_LOG == type) {
1620 ret = plugin_register_log(pluginname, perl_log, &userdata);
1621 } else if (PLUGIN_NOTIF == type) {
1622 ret = plugin_register_notification(pluginname, perl_notify, &userdata);
1623 } else if (PLUGIN_FLUSH == type) {
1624 if (1 == register_legacy_flush) { /* For collectd-5.7 only, #1731 */
1625 register_legacy_flush = 0;
1626 ret = plugin_register_flush("perl", perl_flush, /* user_data = */ NULL);
1630 ret = plugin_register_flush(pluginname, perl_flush, &userdata);
1638 free(userdata.data);
1641 } /* static void _plugin_register_generic_userdata ( ... ) */
1644 * Collectd::plugin_register_TYPE (pluginname, subname).
1647 * name of the perl plugin
1650 * name of the plugin's subroutine that does the work
1653 static XS(Collectd_plugin_register_read) {
1654 return _plugin_register_generic_userdata(aTHX, PLUGIN_READ, "read");
1657 static XS(Collectd_plugin_register_write) {
1658 return _plugin_register_generic_userdata(aTHX, PLUGIN_WRITE, "write");
1661 static XS(Collectd_plugin_register_log) {
1662 return _plugin_register_generic_userdata(aTHX, PLUGIN_LOG, "log");
1665 static XS(Collectd_plugin_register_notification) {
1666 return _plugin_register_generic_userdata(aTHX, PLUGIN_NOTIF, "notification");
1669 static XS(Collectd_plugin_register_flush) {
1670 return _plugin_register_generic_userdata(aTHX, PLUGIN_FLUSH, "flush");
1673 typedef int perl_unregister_function_t(const char *name);
1675 static void _plugin_unregister_generic(pTHX, perl_unregister_function_t *unreg,
1680 log_err("Usage: Collectd::plugin_unregister_%s(pluginname)", desc);
1685 log_err("Collectd::plugin_unregister_%s(pluginname): "
1686 "Invalid pluginname",
1691 log_debug("Collectd::plugin_unregister_%s: plugin = \"%s\"", desc,
1694 unreg(SvPV_nolen(ST(0)));
1699 } /* static void _plugin_unregister_generic ( ... ) */
1702 * Collectd::plugin_unregister_TYPE (pluginname).
1705 * type of callback to be unregistered: read, write, log, notification, flush
1708 * name of the perl plugin
1711 static XS(Collectd_plugin_unregister_read) {
1712 return _plugin_unregister_generic(aTHX, plugin_unregister_read, "read");
1715 static XS(Collectd_plugin_unregister_write) {
1716 return _plugin_unregister_generic(aTHX, plugin_unregister_write, "write");
1719 static XS(Collectd_plugin_unregister_log) {
1720 return _plugin_unregister_generic(aTHX, plugin_unregister_log, "log");
1723 static XS(Collectd_plugin_unregister_notification) {
1724 return _plugin_unregister_generic(aTHX, plugin_unregister_notification,
1728 static XS(Collectd_plugin_unregister_flush) {
1729 return _plugin_unregister_generic(aTHX, plugin_unregister_flush, "flush");
1733 * Collectd::plugin_register_data_set (type, dataset).
1736 * type of the dataset
1739 * dataset to be registered
1741 static XS(Collectd_plugin_register_ds) {
1747 log_warn("Using plugin_register() to register new data-sets is "
1748 "deprecated - add new entries to a custom types.db instead.");
1751 log_err("Usage: Collectd::plugin_register_data_set(type, dataset)");
1755 log_debug("Collectd::plugin_register_data_set: "
1756 "type = \"%s\", dataset = \"%s\"",
1757 SvPV_nolen(ST(0)), SvPV_nolen(ST(1)));
1761 if (SvROK(data) && (SVt_PVAV == SvTYPE(SvRV(data)))) {
1762 ret = pplugin_register_data_set(aTHX_ SvPV_nolen(ST(0)), (AV *)SvRV(data));
1764 log_err("Collectd::plugin_register_data_set: Invalid data.");
1772 } /* static XS (Collectd_plugin_register_ds) */
1775 * Collectd::plugin_unregister_data_set (type).
1778 * type of the dataset
1780 static XS(Collectd_plugin_unregister_ds) {
1784 log_err("Usage: Collectd::plugin_unregister_data_set(type)");
1788 log_debug("Collectd::plugin_unregister_data_set: type = \"%s\"",
1791 if (0 == pplugin_unregister_data_set(SvPV_nolen(ST(0))))
1795 } /* static XS (Collectd_plugin_register_ds) */
1798 * Collectd::plugin_dispatch_values (name, values).
1801 * name of the plugin
1804 * value list to submit
1806 static XS(Collectd_plugin_dispatch_values) {
1814 log_err("Usage: Collectd::plugin_dispatch_values(values)");
1818 log_debug("Collectd::plugin_dispatch_values: values=\"%s\"",
1819 SvPV_nolen(ST(/* stack index = */ 0)));
1821 values = ST(/* stack index = */ 0);
1826 /* Make sure the argument is a hash reference. */
1827 if (!(SvROK(values) && (SVt_PVHV == SvTYPE(SvRV(values))))) {
1828 log_err("Collectd::plugin_dispatch_values: Invalid values.");
1832 ret = pplugin_dispatch_values(aTHX_(HV *) SvRV(values));
1838 } /* static XS (Collectd_plugin_dispatch_values) */
1841 * Collectd::plugin_get_interval ().
1843 static XS(Collectd_plugin_get_interval) {
1846 /* make sure we don't get any unused variable warnings for 'items';
1847 * don't abort, though */
1849 log_err("Usage: Collectd::plugin_get_interval()");
1851 XSRETURN_NV((NV)CDTIME_T_TO_DOUBLE(plugin_get_interval()));
1852 } /* static XS (Collectd_plugin_get_interval) */
1854 /* Collectd::plugin_write (plugin, ds, vl).
1857 * name of the plugin to call, may be 'undef'
1860 * data-set that describes the submitted values, may be 'undef'
1863 * value-list to be written
1865 static XS(Collectd__plugin_write) {
1875 log_err("Usage: Collectd::plugin_write(plugin, ds, vl)");
1879 log_debug("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1880 SvPV_nolen(ST(0)), SvOK(ST(1)) ? SvPV_nolen(ST(1)) : "",
1886 plugin = SvPV_nolen(ST(0));
1889 if (SvROK(ds) && (SVt_PVAV == SvTYPE(SvRV(ds))))
1890 ds_array = (AV *)SvRV(ds);
1894 log_err("Collectd::plugin_write: Invalid data-set.");
1899 if (!(SvROK(vl) && (SVt_PVHV == SvTYPE(SvRV(vl))))) {
1900 log_err("Collectd::plugin_write: Invalid value-list.");
1904 ret = pplugin_write(aTHX_ plugin, ds_array, (HV *)SvRV(vl));
1910 } /* static XS (Collectd__plugin_write) */
1913 * Collectd::_plugin_flush (plugin, timeout, identifier).
1916 * name of the plugin to flush
1919 * timeout to use when flushing the data
1922 * data-set identifier to flush
1924 static XS(Collectd__plugin_flush) {
1925 char *plugin = NULL;
1932 log_err("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1937 plugin = SvPV_nolen(ST(0));
1940 timeout = (int)SvIV(ST(1));
1943 id = SvPV_nolen(ST(2));
1945 log_debug("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1947 plugin, timeout, id);
1949 if (0 == plugin_flush(plugin, timeout, id))
1953 } /* static XS (Collectd__plugin_flush) */
1956 * Collectd::plugin_dispatch_notification (notif).
1959 * notification to dispatch
1961 static XS(Collectd_plugin_dispatch_notification) {
1969 log_err("Usage: Collectd::plugin_dispatch_notification(notif)");
1973 log_debug("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1978 if (!(SvROK(notif) && (SVt_PVHV == SvTYPE(SvRV(notif))))) {
1979 log_err("Collectd::plugin_dispatch_notification: Invalid notif.");
1983 ret = pplugin_dispatch_notification(aTHX_(HV *) SvRV(notif));
1989 } /* static XS (Collectd_plugin_dispatch_notification) */
1992 * Collectd::plugin_log (level, message).
1995 * log level (LOG_DEBUG, ... LOG_ERR)
2000 static XS(Collectd_plugin_log) {
2004 log_err("Usage: Collectd::plugin_log(level, message)");
2008 plugin_log(SvIV(ST(0)), "%s", SvPV_nolen(ST(1)));
2010 } /* static XS (Collectd_plugin_log) */
2013 * Collectd::_fc_register (type, name)
2021 static XS(Collectd__fc_register) {
2030 log_err("Usage: Collectd::_fc_register(type, name)");
2035 name = SvPV_nolen(ST(1));
2037 if (FC_MATCH == type)
2038 ret = fc_register_match(name, pmatch);
2039 else if (FC_TARGET == type)
2040 ret = fc_register_target(name, ptarget);
2046 } /* static XS (Collectd_fc_register) */
2049 * Collectd::call_by_name (...).
2051 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
2053 static XS(Collectd_call_by_name) {
2057 if (NULL == (tmp = get_sv("Collectd::cb_name", 0))) {
2058 sv_setpv(get_sv("@", 1), "cb_name has not been set");
2063 name = SvPV_nolen(tmp);
2065 if (NULL == get_cv(name, 0)) {
2066 sv_setpvf(get_sv("@", 1), "unknown callback \"%s\"", name);
2071 /* simply pass on the subroutine call without touching the stack,
2072 * thus leaving any arguments and return values in place */
2074 } /* static XS (Collectd_call_by_name) */
2077 * Interface to collectd.
2080 static int perl_init(void) {
2084 if (NULL == perl_threads)
2088 c_ithread_t *t = NULL;
2090 pthread_mutex_lock(&perl_threads->mutex);
2091 t = c_ithread_create(perl_threads->head->interp);
2092 pthread_mutex_unlock(&perl_threads->mutex);
2097 log_debug("perl_init: c_ithread: interp = %p (active threads: %i)", aTHX,
2098 perl_threads->number_of_threads);
2100 /* Lock the base thread to avoid race conditions with c_ithread_create().
2101 * See https://github.com/collectd/collectd/issues/9 and
2102 * https://github.com/collectd/collectd/issues/1706 for details.
2104 assert(aTHX == perl_threads->head->interp);
2105 pthread_mutex_lock(&perl_threads->mutex);
2107 status = pplugin_call(aTHX_ PLUGIN_INIT);
2109 pthread_mutex_unlock(&perl_threads->mutex);
2112 } /* static int perl_init (void) */
2114 static int perl_read(user_data_t *user_data) {
2117 if (NULL == perl_threads)
2121 c_ithread_t *t = NULL;
2123 pthread_mutex_lock(&perl_threads->mutex);
2124 t = c_ithread_create(perl_threads->head->interp);
2125 pthread_mutex_unlock(&perl_threads->mutex);
2130 /* Assert that we're not running as the base thread. Otherwise, we might
2131 * run into concurrency issues with c_ithread_create(). See
2132 * https://github.com/collectd/collectd/issues/9 for details. */
2133 assert(aTHX != perl_threads->head->interp);
2135 log_debug("perl_read: c_ithread: interp = %p (active threads: %i)", aTHX,
2136 perl_threads->number_of_threads);
2138 return pplugin_call(aTHX_ PLUGIN_READ, user_data->data);
2139 } /* static int perl_read (user_data_t *user_data) */
2141 static int perl_write(const data_set_t *ds, const value_list_t *vl,
2142 user_data_t *user_data) {
2146 if (NULL == perl_threads)
2150 c_ithread_t *t = NULL;
2152 pthread_mutex_lock(&perl_threads->mutex);
2153 t = c_ithread_create(perl_threads->head->interp);
2154 pthread_mutex_unlock(&perl_threads->mutex);
2159 /* Lock the base thread if this is not called from one of the read threads
2160 * to avoid race conditions with c_ithread_create(). See
2161 * https://github.com/collectd/collectd/issues/9 for details. */
2162 if (aTHX == perl_threads->head->interp)
2163 pthread_mutex_lock(&perl_threads->mutex);
2165 log_debug("perl_write: c_ithread: interp = %p (active threads: %i)", aTHX,
2166 perl_threads->number_of_threads);
2167 status = pplugin_call(aTHX_ PLUGIN_WRITE, user_data->data, ds, vl);
2169 if (aTHX == perl_threads->head->interp)
2170 pthread_mutex_unlock(&perl_threads->mutex);
2173 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2175 static void perl_log(int level, const char *msg, user_data_t *user_data) {
2178 if (NULL == perl_threads)
2182 c_ithread_t *t = NULL;
2184 pthread_mutex_lock(&perl_threads->mutex);
2185 t = c_ithread_create(perl_threads->head->interp);
2186 pthread_mutex_unlock(&perl_threads->mutex);
2191 /* Lock the base thread if this is not called from one of the read threads
2192 * to avoid race conditions with c_ithread_create(). See
2193 * https://github.com/collectd/collectd/issues/9 for details.
2196 if (aTHX == perl_threads->head->interp)
2197 pthread_mutex_lock(&perl_threads->mutex);
2199 pplugin_call(aTHX_ PLUGIN_LOG, user_data->data, level, msg);
2201 if (aTHX == perl_threads->head->interp)
2202 pthread_mutex_unlock(&perl_threads->mutex);
2205 } /* static void perl_log (int, const char *) */
2207 static int perl_notify(const notification_t *notif, user_data_t *user_data) {
2210 if (NULL == perl_threads)
2214 c_ithread_t *t = NULL;
2216 pthread_mutex_lock(&perl_threads->mutex);
2217 t = c_ithread_create(perl_threads->head->interp);
2218 pthread_mutex_unlock(&perl_threads->mutex);
2222 return pplugin_call(aTHX_ PLUGIN_NOTIF, user_data->data, notif);
2223 } /* static int perl_notify (const notification_t *) */
2225 static int perl_flush(cdtime_t timeout, const char *identifier,
2226 user_data_t *user_data) {
2229 if (NULL == perl_threads)
2233 c_ithread_t *t = NULL;
2235 pthread_mutex_lock(&perl_threads->mutex);
2236 t = c_ithread_create(perl_threads->head->interp);
2237 pthread_mutex_unlock(&perl_threads->mutex);
2242 /* For collectd-5.6 only, #1731 */
2243 if (user_data == NULL || user_data->data == NULL)
2244 return pplugin_call(aTHX_ PLUGIN_FLUSH_ALL, timeout, identifier);
2246 return pplugin_call(aTHX_ PLUGIN_FLUSH, user_data->data, timeout, identifier);
2247 } /* static int perl_flush (const int) */
2249 static int perl_shutdown(void) {
2255 plugin_unregister_complex_config("perl");
2256 plugin_unregister_read_group("perl");
2258 if (NULL == perl_threads)
2262 pthread_mutex_lock(&perl_threads->mutex);
2263 t = c_ithread_create(perl_threads->head->interp);
2264 pthread_mutex_unlock(&perl_threads->mutex);
2269 log_debug("perl_shutdown: c_ithread: interp = %p (active threads: %i)", aTHX,
2270 perl_threads->number_of_threads);
2272 plugin_unregister_init("perl");
2273 plugin_unregister_flush("perl"); /* For collectd-5.6 only, #1731 */
2275 ret = pplugin_call(aTHX_ PLUGIN_SHUTDOWN);
2277 pthread_mutex_lock(&perl_threads->mutex);
2278 t = perl_threads->tail;
2281 struct timespec ts_wait;
2282 c_ithread_t *thr = t;
2284 /* the pointer has to be advanced before destroying
2285 * the thread as this will free the memory */
2290 /* Give some time to thread to exit from Perl interpreter */
2291 WARNING("perl shutdown: Thread is running inside Perl. Waiting.");
2293 ts_wait.tv_nsec = 500000;
2294 nanosleep(&ts_wait, NULL);
2297 pthread_kill(thr->pthread, SIGTERM);
2298 ERROR("perl shutdown: Thread hangs inside Perl. Thread killed.");
2300 c_ithread_destroy(thr);
2303 pthread_mutex_unlock(&perl_threads->mutex);
2304 pthread_mutex_destroy(&perl_threads->mutex);
2305 pthread_mutexattr_destroy(&perl_threads->mutexattr);
2307 sfree(perl_threads);
2309 pthread_key_delete(perl_thr_key);
2313 plugin_unregister_shutdown("perl");
2315 } /* static void perl_shutdown (void) */
2318 * Access functions for global variables.
2320 * These functions implement the "magic" used to access
2321 * the global variables from Perl.
2324 static int g_pv_get(pTHX_ SV *var, MAGIC *mg) {
2325 char *pv = mg->mg_ptr;
2328 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2330 static int g_pv_set(pTHX_ SV *var, MAGIC *mg) {
2331 char *pv = mg->mg_ptr;
2332 sstrncpy(pv, SvPV_nolen(var), DATA_MAX_NAME_LEN);
2334 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2336 static int g_interval_get(pTHX_ SV *var, MAGIC *mg) {
2337 log_warn("Accessing $interval_g is deprecated (and might not "
2338 "give the desired results) - plugin_get_interval() should "
2339 "be used instead.");
2340 sv_setnv(var, CDTIME_T_TO_DOUBLE(interval_g));
2342 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2344 static int g_interval_set(pTHX_ SV *var, MAGIC *mg) {
2345 double nv = (double)SvNV(var);
2346 log_warn("Accessing $interval_g is deprecated (and might not "
2347 "give the desired results) - plugin_get_interval() should "
2348 "be used instead.");
2349 interval_g = DOUBLE_TO_CDTIME_T(nv);
2351 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2353 static MGVTBL g_pv_vtbl = {g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2354 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2359 static MGVTBL g_interval_vtbl = {g_interval_get, g_interval_set, NULL, NULL,
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 */
2394 for (int i = 0; '\0' != g_strings[i].name[0]; ++i) {
2395 tmp = get_sv(g_strings[i].name, 1);
2396 sv_magicext(tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl, g_strings[i].var, 0);
2399 tmp = get_sv("Collectd::interval_g", /* create = */ 1);
2400 sv_magicext(tmp, NULL, /* how = */ PERL_MAGIC_ext,
2401 /* vtbl = */ &g_interval_vtbl,
2402 /* name = */ NULL, /* namelen = */ 0);
2405 } /* static void xs_init (pTHX) */
2407 /* Initialize the global Perl interpreter. */
2408 static int init_pi(int argc, char **argv) {
2411 if (NULL != perl_threads)
2414 log_info("Initializing Perl interpreter...");
2417 for (int i = 0; i < argc; ++i)
2418 log_debug("argv[%i] = \"%s\"", i, argv[i]);
2420 #endif /* COLLECT_DEBUG */
2422 if (0 != pthread_key_create(&perl_thr_key, c_ithread_destructor)) {
2423 log_err("init_pi: pthread_key_create failed");
2425 /* this must not happen - cowardly giving up if it does */
2430 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2431 * triggers a "value computed is not used" warning by gcc. */
2434 PERL_SYS_INIT3(&argc, &argv, &environ);
2436 perl_threads = smalloc(sizeof(*perl_threads));
2437 memset(perl_threads, 0, sizeof(c_ithread_list_t));
2439 pthread_mutexattr_init(&perl_threads->mutexattr);
2440 pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE);
2441 pthread_mutex_init(&perl_threads->mutex, &perl_threads->mutexattr);
2442 /* locking the mutex should not be necessary at this point
2443 * but let's just do it for the sake of completeness */
2444 pthread_mutex_lock(&perl_threads->mutex);
2446 perl_threads->head = c_ithread_create(NULL);
2447 perl_threads->tail = perl_threads->head;
2449 if (NULL == (perl_threads->head->interp = perl_alloc())) {
2450 log_err("init_pi: Not enough memory.");
2454 aTHX = perl_threads->head->interp;
2455 pthread_mutex_unlock(&perl_threads->mutex);
2457 perl_construct(aTHX);
2459 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2461 if (0 != perl_parse(aTHX_ xs_init, argc, argv, NULL)) {
2462 SV *err = get_sv("@", 1);
2463 log_err("init_pi: Unable to bootstrap Collectd: %s", SvPV_nolen(err));
2465 perl_destruct(perl_threads->head->interp);
2466 perl_free(perl_threads->head->interp);
2467 sfree(perl_threads);
2469 pthread_key_delete(perl_thr_key);
2473 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2474 sv_setpv(get_sv("0", 0), "collectd");
2478 plugin_register_init("perl", perl_init);
2479 plugin_register_shutdown("perl", perl_shutdown);
2481 } /* static int init_pi (const char **, const int) */
2484 * LoadPlugin "<Plugin>"
2486 static int perl_config_loadplugin(pTHX_ oconfig_item_t *ci) {
2487 char module_name[DATA_MAX_NAME_LEN];
2491 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2492 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2493 log_err("LoadPlugin expects a single string argument.");
2497 value = ci->values[0].value.string;
2499 if (NULL == get_module_name(module_name, sizeof(module_name), value)) {
2500 log_err("Invalid module name %s", value);
2504 if (0 != init_pi(perl_argc, perl_argv))
2507 assert(NULL != perl_threads);
2508 assert(NULL != perl_threads->head);
2510 aTHX = perl_threads->head->interp;
2512 log_debug("perl_config: Loading Perl plugin \"%s\"", value);
2513 load_module(PERL_LOADMOD_NOIMPORT, newSVpv(module_name, strlen(module_name)),
2516 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2521 static int perl_config_basename(pTHX_ oconfig_item_t *ci) {
2524 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2525 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2526 log_err("BaseName expects a single string argument.");
2530 value = ci->values[0].value.string;
2532 log_debug("perl_config: Setting plugin basename to \"%s\"", value);
2533 sstrncpy(base_name, value, sizeof(base_name));
2535 } /* static int perl_config_basename (oconfig_item_it *) */
2538 * EnableDebugger "<Package>"|""
2540 static int perl_config_enabledebugger(pTHX_ oconfig_item_t *ci) {
2543 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2544 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2545 log_err("EnableDebugger expects a single string argument.");
2549 if (NULL != perl_threads) {
2550 log_warn("EnableDebugger has no effects if used after LoadPlugin.");
2554 value = ci->values[0].value.string;
2556 perl_argv = realloc(perl_argv, (++perl_argc + 1) * sizeof(char *));
2558 if (NULL == perl_argv) {
2559 log_err("perl_config: Not enough memory.");
2563 if ('\0' == value[0]) {
2564 perl_argv[perl_argc - 1] = "-d";
2566 perl_argv[perl_argc - 1] = smalloc(strlen(value) + 4);
2567 sstrncpy(perl_argv[perl_argc - 1], "-d:", 4);
2568 sstrncpy(perl_argv[perl_argc - 1] + 3, value, strlen(value) + 1);
2571 perl_argv[perl_argc] = NULL;
2573 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2576 * IncludeDir "<Dir>"
2578 static int perl_config_includedir(pTHX_ oconfig_item_t *ci) {
2581 if ((0 != ci->children_num) || (1 != ci->values_num) ||
2582 (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2583 log_err("IncludeDir expects a single string argument.");
2587 value = ci->values[0].value.string;
2590 perl_argv = realloc(perl_argv, (++perl_argc + 1) * sizeof(char *));
2592 if (NULL == perl_argv) {
2593 log_err("perl_config: Not enough memory.");
2597 perl_argv[perl_argc - 1] = smalloc(strlen(value) + 3);
2598 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2599 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen(value) + 1);
2601 perl_argv[perl_argc] = NULL;
2603 /* prepend the directory to @INC */
2604 av_unshift(GvAVn(PL_incgv), 1);
2605 av_store(GvAVn(PL_incgv), 0, newSVpv(value, strlen(value)));
2608 } /* static int perl_config_includedir (oconfig_item_it *) */
2613 static int perl_config_plugin(pTHX_ oconfig_item_t *ci) {
2620 if (NULL == perl_threads) {
2621 log_err("A `Plugin' block was encountered but no plugin was loaded yet. "
2622 "Put the appropriate `LoadPlugin' option in front of it.");
2628 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2629 log_err("LoadPlugin expects a single string argument.");
2633 plugin = ci->values[0].value.string;
2636 if (0 != oconfig_item2hv(aTHX_ ci, config)) {
2640 log_err("Unable to convert configuration to a Perl hash value.");
2641 config = (HV *)&PL_sv_undef;
2649 XPUSHs(sv_2mortal(newSVpv(plugin, 0)));
2650 XPUSHs(sv_2mortal(newRV_noinc((SV *)config)));
2654 retvals = call_pv("Collectd::_plugin_dispatch_config", G_SCALAR);
2668 } /* static int perl_config_plugin (oconfig_item_it *) */
2670 static int perl_config(oconfig_item_t *ci) {
2675 for (int i = 0; i < ci->children_num; ++i) {
2676 oconfig_item_t *c = ci->children + i;
2677 int current_status = 0;
2679 if (NULL != perl_threads) {
2680 if ((aTHX = PERL_GET_CONTEXT) == NULL)
2684 if (0 == strcasecmp(c->key, "LoadPlugin"))
2685 current_status = perl_config_loadplugin(aTHX_ c);
2686 else if (0 == strcasecmp(c->key, "BaseName"))
2687 current_status = perl_config_basename(aTHX_ c);
2688 else if (0 == strcasecmp(c->key, "EnableDebugger"))
2689 current_status = perl_config_enabledebugger(aTHX_ c);
2690 else if (0 == strcasecmp(c->key, "IncludeDir"))
2691 current_status = perl_config_includedir(aTHX_ c);
2692 else if (0 == strcasecmp(c->key, "Plugin"))
2693 current_status = perl_config_plugin(aTHX_ c);
2694 else if (0 == strcasecmp(c->key, "RegisterLegacyFlush"))
2695 cf_util_get_boolean(c, ®ister_legacy_flush);
2697 log_warn("Ignoring unknown config key \"%s\".", c->key);
2701 /* fatal error - it's up to perl_config_* to clean up */
2702 if (0 > current_status) {
2703 log_err("Configuration failed with a fatal error - "
2704 "plugin disabled!");
2705 return current_status;
2708 status += current_status;
2711 } /* static int perl_config (oconfig_item_t *) */
2713 void module_register(void) {
2715 perl_argv = smalloc((perl_argc + 1) * sizeof(*perl_argv));
2717 /* default options for the Perl interpreter */
2719 perl_argv[1] = "-MCollectd";
2720 perl_argv[2] = "-e";
2722 perl_argv[4] = NULL;
2724 plugin_register_complex_config("perl", perl_config);
2726 } /* void module_register (void) */
2728 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */