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] = "";
207 { "Collectd::plugin_register_read", Collectd_plugin_register_read },
208 { "Collectd::plugin_register_write", Collectd_plugin_register_write },
209 { "Collectd::plugin_register_log", Collectd_plugin_register_log },
210 { "Collectd::plugin_register_notification",
211 Collectd_plugin_register_notification },
212 { "Collectd::plugin_register_flush", Collectd_plugin_register_flush },
213 { "Collectd::plugin_unregister_read", Collectd_plugin_unregister_read },
214 { "Collectd::plugin_unregister_write", Collectd_plugin_unregister_write },
215 { "Collectd::plugin_unregister_log", Collectd_plugin_unregister_log },
216 { "Collectd::plugin_unregister_notification",
217 Collectd_plugin_unregister_notification },
218 { "Collectd::plugin_unregister_flush", Collectd_plugin_unregister_flush },
219 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
220 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
221 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
222 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
223 { "Collectd::_plugin_write", Collectd__plugin_write },
224 { "Collectd::_plugin_flush", Collectd__plugin_flush },
225 { "Collectd::plugin_dispatch_notification",
226 Collectd_plugin_dispatch_notification },
227 { "Collectd::plugin_log", Collectd_plugin_log },
228 { "Collectd::_fc_register", Collectd__fc_register },
229 { "Collectd::call_by_name", Collectd_call_by_name },
238 { "Collectd::TYPE_INIT", PLUGIN_INIT },
239 { "Collectd::TYPE_READ", PLUGIN_READ },
240 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
241 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
242 { "Collectd::TYPE_LOG", PLUGIN_LOG },
243 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
244 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
245 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
246 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
247 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
248 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
249 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
250 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
251 { "Collectd::LOG_ERR", LOG_ERR },
252 { "Collectd::LOG_WARNING", LOG_WARNING },
253 { "Collectd::LOG_NOTICE", LOG_NOTICE },
254 { "Collectd::LOG_INFO", LOG_INFO },
255 { "Collectd::LOG_DEBUG", LOG_DEBUG },
256 { "Collectd::FC_MATCH", FC_MATCH },
257 { "Collectd::FC_TARGET", FC_TARGET },
258 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
259 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
260 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
261 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
262 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
263 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
264 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
265 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
266 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
267 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
268 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
277 { "Collectd::hostname_g", hostname_g },
282 * Helper functions for data type conversion.
297 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
301 if ((NULL == hash) || (NULL == ds))
304 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
305 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
308 log_err ("hv2data_source: No DS name given.");
312 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
313 ds->type = SvIV (*tmp);
315 if ((DS_TYPE_COUNTER != ds->type)
316 && (DS_TYPE_GAUGE != ds->type)
317 && (DS_TYPE_DERIVE != ds->type)
318 && (DS_TYPE_ABSOLUTE != ds->type)) {
319 log_err ("hv2data_source: Invalid DS type.");
324 ds->type = DS_TYPE_COUNTER;
327 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
328 ds->min = SvNV (*tmp);
332 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
333 ds->max = SvNV (*tmp);
337 } /* static int hv2data_source (HV *, data_source_t *) */
339 /* av2value converts at most "len" elements from "array" to "value". Returns the
340 * number of elements converted or zero on error. */
341 static size_t av2value (pTHX_ char *name, AV *array, value_t *value, size_t array_len)
343 const data_set_t *ds;
345 if ((NULL == name) || (NULL == array) || (NULL == value) || (array_len == 0))
348 ds = plugin_get_ds (name);
350 log_err ("av2value: Unknown dataset \"%s\"", name);
354 if (array_len < ds->ds_num) {
355 log_warn ("av2value: array does not contain enough elements for type \"%s\": got %zu, want %zu",
356 name, array_len, ds->ds_num);
358 } else if (array_len > ds->ds_num) {
359 log_warn ("av2value: array contains excess elements for type \"%s\": got %zu, want %zu",
360 name, array_len, ds->ds_num);
363 for (size_t i = 0; i < ds->ds_num; ++i) {
364 SV **tmp = av_fetch (array, i, 0);
367 if (DS_TYPE_COUNTER == ds->ds[i].type)
368 value[i].counter = SvIV (*tmp);
369 else if (DS_TYPE_GAUGE == ds->ds[i].type)
370 value[i].gauge = SvNV (*tmp);
371 else if (DS_TYPE_DERIVE == ds->ds[i].type)
372 value[i].derive = SvIV (*tmp);
373 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
374 value[i].absolute = SvIV (*tmp);
382 } /* static size_t av2value (char *, AV *, value_t *, size_t) */
387 * values => [ @values ],
391 * plugin_instance => $pinstance,
392 * type_instance => $tinstance,
395 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
399 if ((NULL == hash) || (NULL == vl))
402 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
403 log_err ("hv2value_list: No type given.");
407 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
409 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
410 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
411 log_err ("hv2value_list: No valid values given.");
416 AV *array = (AV *)SvRV (*tmp);
417 /* av_len returns the highest index, not the actual length. */
418 size_t array_len = (size_t) (av_len (array) + 1);
422 vl->values = calloc (array_len, sizeof (*vl->values));
423 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp), vl->values, array_len);
424 if (vl->values_len == 0) {
430 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
432 double t = SvNV (*tmp);
433 vl->time = DOUBLE_TO_CDTIME_T (t);
436 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
438 double t = SvNV (*tmp);
439 vl->interval = DOUBLE_TO_CDTIME_T (t);
442 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
443 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
445 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
447 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
448 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
450 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
451 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
452 sizeof (vl->plugin_instance));
454 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
455 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
456 sizeof (vl->type_instance));
458 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
460 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
464 if ((NULL == array) || (NULL == name) || (NULL == ds))
467 len = av_len (array);
470 log_err ("av2data_set: Invalid data set.");
474 ds->ds = smalloc ((len + 1) * sizeof (*ds->ds));
475 ds->ds_num = len + 1;
477 for (int i = 0; i <= len; ++i) {
478 SV **elem = av_fetch (array, i, 0);
481 log_err ("av2data_set: Failed to fetch data source %i.", i);
485 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
486 log_err ("av2data_set: Invalid data source.");
490 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
493 log_debug ("av2data_set: "
494 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
495 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
498 sstrncpy (ds->type, name, sizeof (ds->type));
500 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
505 * severity => $severity,
511 * plugin_instance => $instance,
512 * type_instance => $type_instance,
513 * meta => [ { name => <name>, value => <value> }, ... ]
516 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
518 notification_meta_t **m = meta;
520 int len = av_len (array);
522 for (int i = 0; i <= len; ++i) {
523 SV **tmp = av_fetch (array, i, 0);
529 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
530 log_warn ("av2notification_meta: Skipping invalid "
531 "meta information.");
535 hash = (HV *)SvRV (*tmp);
537 *m = smalloc (sizeof (**m));
539 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
540 log_warn ("av2notification_meta: Skipping invalid "
541 "meta information.");
545 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
547 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
548 log_warn ("av2notification_meta: Skipping invalid "
549 "meta information.");
555 (*m)->nm_value.nm_double = SvNVX (*tmp);
556 (*m)->type = NM_TYPE_DOUBLE;
558 else if (SvUOK (*tmp)) {
559 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
560 (*m)->type = NM_TYPE_UNSIGNED_INT;
562 else if (SvIOK (*tmp)) {
563 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
564 (*m)->type = NM_TYPE_SIGNED_INT;
567 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
568 (*m)->type = NM_TYPE_STRING;
575 } /* static int av2notification_meta (AV *, notification_meta_t *) */
577 static int hv2notification (pTHX_ HV *hash, notification_t *n)
581 if ((NULL == hash) || (NULL == n))
584 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
585 n->severity = SvIV (*tmp);
587 n->severity = NOTIF_FAILURE;
589 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
591 double t = SvNV (*tmp);
592 n->time = DOUBLE_TO_CDTIME_T (t);
597 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
598 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
600 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
601 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
603 sstrncpy (n->host, hostname_g, sizeof (n->host));
605 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
606 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
608 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
609 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
610 sizeof (n->plugin_instance));
612 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
613 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
615 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
616 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
617 sizeof (n->type_instance));
620 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
621 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
622 log_warn ("hv2notification: Ignoring invalid meta information.");
626 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
627 plugin_notification_meta_free (n->meta);
634 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
636 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
638 if ((NULL == ds) || (NULL == array))
641 av_extend (array, ds->ds_num);
643 for (size_t i = 0; i < ds->ds_num; ++i) {
644 HV *source = newHV ();
646 if (NULL == hv_store (source, "name", 4,
647 newSVpv (ds->ds[i].name, 0), 0))
650 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
653 if (! isnan (ds->ds[i].min))
654 if (NULL == hv_store (source, "min", 3,
655 newSVnv (ds->ds[i].min), 0))
658 if (! isnan (ds->ds[i].max))
659 if (NULL == hv_store (source, "max", 3,
660 newSVnv (ds->ds[i].max), 0))
663 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
667 } /* static int data_set2av (data_set_t *, AV *) */
669 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
674 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
678 /* av_extend takes the last *index* to which the array should be extended. */
679 av_extend (values, vl->values_len - 1);
681 assert (ds->ds_num == vl->values_len);
682 for (i = 0; i < vl->values_len; ++i) {
685 if (DS_TYPE_COUNTER == ds->ds[i].type)
686 val = newSViv (vl->values[i].counter);
687 else if (DS_TYPE_GAUGE == ds->ds[i].type)
688 val = newSVnv (vl->values[i].gauge);
689 else if (DS_TYPE_DERIVE == ds->ds[i].type)
690 val = newSViv (vl->values[i].derive);
691 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
692 val = newSViv (vl->values[i].absolute);
694 if (NULL == av_store (values, i, val)) {
700 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
705 double t = CDTIME_T_TO_DOUBLE (vl->time);
706 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
711 double t = CDTIME_T_TO_DOUBLE (vl->interval);
712 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
716 if ('\0' != vl->host[0])
717 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
720 if ('\0' != vl->plugin[0])
721 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
724 if ('\0' != vl->plugin_instance[0])
725 if (NULL == hv_store (hash, "plugin_instance", 15,
726 newSVpv (vl->plugin_instance, 0), 0))
729 if ('\0' != vl->type[0])
730 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
733 if ('\0' != vl->type_instance[0])
734 if (NULL == hv_store (hash, "type_instance", 13,
735 newSVpv (vl->type_instance, 0), 0))
738 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
740 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
749 av_extend (array, meta_num);
751 for (int i = 0; NULL != meta; meta = meta->next, ++i) {
755 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
758 if (NM_TYPE_STRING == meta->type)
759 value = newSVpv (meta->nm_value.nm_string, 0);
760 else if (NM_TYPE_SIGNED_INT == meta->type)
761 value = newSViv (meta->nm_value.nm_signed_int);
762 else if (NM_TYPE_UNSIGNED_INT == meta->type)
763 value = newSVuv (meta->nm_value.nm_unsigned_int);
764 else if (NM_TYPE_DOUBLE == meta->type)
765 value = newSVnv (meta->nm_value.nm_double);
766 else if (NM_TYPE_BOOLEAN == meta->type)
767 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
771 if (NULL == hv_store (m, "value", 5, value, 0)) {
776 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
783 } /* static int notification_meta2av (notification_meta_t *, AV *) */
785 static int notification2hv (pTHX_ notification_t *n, HV *hash)
787 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
792 double t = CDTIME_T_TO_DOUBLE (n->time);
793 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
797 if ('\0' != *n->message)
798 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
801 if ('\0' != *n->host)
802 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
805 if ('\0' != *n->plugin)
806 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
809 if ('\0' != *n->plugin_instance)
810 if (NULL == hv_store (hash, "plugin_instance", 15,
811 newSVpv (n->plugin_instance, 0), 0))
814 if ('\0' != *n->type)
815 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
818 if ('\0' != *n->type_instance)
819 if (NULL == hv_store (hash, "type_instance", 13,
820 newSVpv (n->type_instance, 0), 0))
823 if (NULL != n->meta) {
825 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
826 || (NULL == hv_store (hash, "meta", 4,
827 newRV_noinc ((SV *)meta), 0))) {
834 } /* static int notification2hv (notification_t *, HV *) */
836 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
841 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
845 if (0 < ci->values_num)
846 av_extend (values, ci->values_num);
848 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
854 for (int i = 0; i < ci->values_num; ++i) {
857 switch (ci->values[i].type) {
858 case OCONFIG_TYPE_STRING:
859 value = newSVpv (ci->values[i].value.string, 0);
861 case OCONFIG_TYPE_NUMBER:
862 value = newSVnv ((NV)ci->values[i].value.number);
864 case OCONFIG_TYPE_BOOLEAN:
865 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
868 log_err ("oconfig_item2hv: Invalid value type %i.",
870 value = &PL_sv_undef;
873 if (NULL == av_store (values, i, value)) {
879 /* ignoring 'parent' member which is uninteresting in this case */
882 if (0 < ci->children_num)
883 av_extend (children, ci->children_num);
885 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
891 for (int i = 0; i < ci->children_num; ++i) {
892 HV *child = newHV ();
894 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
900 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
907 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
910 * Internal functions.
913 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
915 if (base_name[0] == '\0')
916 status = ssnprintf (buf, buf_len, "%s", module);
918 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
919 if ((status < 0) || ((unsigned int)status >= buf_len))
922 } /* char *get_module_name */
925 * Add a plugin's data set definition.
927 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
933 if ((NULL == name) || (NULL == dataset))
936 if (0 != av2data_set (aTHX_ dataset, name, &ds))
939 ret = plugin_register_data_set (&ds);
943 } /* static int pplugin_register_data_set (char *, SV *) */
946 * Remove a plugin's data set definition.
948 static int pplugin_unregister_data_set (char *name)
952 return plugin_unregister_data_set (name);
953 } /* static int pplugin_unregister_data_set (char *) */
956 * Submit the values to the write functions.
958 static int pplugin_dispatch_values (pTHX_ HV *values)
960 value_list_t vl = VALUE_LIST_INIT;
967 if (0 != hv2value_list (aTHX_ values, &vl))
970 ret = plugin_dispatch_values (&vl);
974 } /* static int pplugin_dispatch_values (char *, HV *) */
977 * Submit the values to a single write function.
979 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
982 value_list_t vl = VALUE_LIST_INIT;
989 if (0 != hv2value_list (aTHX_ values, &vl))
992 if ((NULL != data_set)
993 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
996 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
998 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
999 NULL == plugin ? "<any>" : plugin, ret);
1001 if (NULL != data_set)
1005 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
1008 * Dispatch a notification.
1010 static int pplugin_dispatch_notification (pTHX_ HV *notif)
1012 notification_t n = { 0 };
1019 if (0 != hv2notification (aTHX_ notif, &n))
1022 ret = plugin_dispatch_notification (&n);
1023 plugin_notification_meta_free (n.meta);
1025 } /* static int pplugin_dispatch_notification (HV *) */
1028 * Call perl sub with thread locking flags handled.
1030 static int call_pv_locked (pTHX_ const char* sub_name)
1035 c_ithread_t *t = (c_ithread_t *)pthread_getspecific(perl_thr_key);
1036 if (t == NULL) /* thread destroyed */
1039 old_running = t->running;
1043 t->running = old_running;
1047 ret = call_pv (sub_name, G_SCALAR|G_EVAL);
1049 t->running = old_running;
1051 } /* static int call_pv_locked (pTHX, *sub_name) */
1054 * Call all working functions of the given type.
1056 static int pplugin_call (pTHX_ int type, ...)
1066 if ((type < 0) || (type >= PLUGIN_TYPES))
1069 va_start (ap, type);
1076 if (PLUGIN_READ == type) {
1077 subname = va_arg(ap, char *);
1079 else if (PLUGIN_WRITE == type) {
1086 subname = va_arg(ap, char *);
1088 * $_[0] = $plugin_type;
1103 * values => [ $v1, ... ],
1105 * host => $hostname,
1106 * plugin => $plugin,
1108 * plugin_instance => $instance,
1109 * type_instance => $type_instance
1112 ds = va_arg (ap, data_set_t *);
1113 vl = va_arg (ap, value_list_t *);
1115 if (-1 == data_set2av (aTHX_ ds, pds)) {
1118 pds = (AV *)&PL_sv_undef;
1122 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1125 pvl = (HV *)&PL_sv_undef;
1129 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1130 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1131 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1133 else if (PLUGIN_LOG == type) {
1134 subname = va_arg(ap, char *);
1140 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1141 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1143 else if (PLUGIN_NOTIF == type) {
1145 HV *notif = newHV ();
1147 subname = va_arg(ap, char *);
1151 * severity => $severity,
1155 * plugin => $plugin,
1157 * plugin_instance => $instance,
1158 * type_instance => $type_instance
1161 n = va_arg (ap, notification_t *);
1163 if (-1 == notification2hv (aTHX_ n, notif)) {
1166 notif = (HV *)&PL_sv_undef;
1170 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1172 else if (PLUGIN_FLUSH == type) {
1174 subname = va_arg(ap, char *);
1177 * $_[1] = $identifier;
1179 timeout = va_arg (ap, cdtime_t);
1181 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1182 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1184 else if (PLUGIN_FLUSH_ALL == type) {
1186 subname = "Collectd::plugin_call_all";
1189 * $_[1] = $identifier;
1191 timeout = va_arg (ap, cdtime_t);
1193 XPUSHs (sv_2mortal (newSViv ((IV)PLUGIN_FLUSH)));
1194 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1195 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1197 else if (PLUGIN_INIT == type) {
1198 subname = "Collectd::plugin_call_all";
1199 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1201 else if (PLUGIN_SHUTDOWN == type) {
1202 subname = "Collectd::plugin_call_all";
1203 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1205 else { /* Unknown type. Run 'plugin_call_all' and make compiler happy */
1206 subname = "Collectd::plugin_call_all";
1207 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1212 retvals = call_pv_locked (aTHX_ subname);
1215 if (SvTRUE(ERRSV)) {
1216 if (PLUGIN_LOG != type)
1217 ERROR ("perl: %s error: %s", subname, SvPV_nolen(ERRSV));
1220 else if (0 < retvals) {
1232 } /* static int pplugin_call (int, ...) */
1235 * collectd's Perl interpreter based thread implementation.
1237 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1240 /* must be called with perl_threads->mutex locked */
1241 static void c_ithread_destroy (c_ithread_t *ithread)
1243 dTHXa (ithread->interp);
1245 assert (NULL != perl_threads);
1247 PERL_SET_CONTEXT (aTHX);
1248 /* Mark as running to avoid deadlock:
1249 c_ithread_destroy -> log_debug -> perl_log()
1251 ithread->running = 1;
1252 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1257 --perl_threads->number_of_threads;
1258 #endif /* COLLECT_DEBUG */
1260 perl_destruct (aTHX);
1263 if (NULL == ithread->prev)
1264 perl_threads->head = ithread->next;
1266 ithread->prev->next = ithread->next;
1268 if (NULL == ithread->next)
1269 perl_threads->tail = ithread->prev;
1271 ithread->next->prev = ithread->prev;
1275 } /* static void c_ithread_destroy (c_ithread_t *) */
1277 static void c_ithread_destructor (void *arg)
1279 c_ithread_t *ithread = (c_ithread_t *)arg;
1280 c_ithread_t *t = NULL;
1282 if (NULL == perl_threads)
1285 pthread_mutex_lock (&perl_threads->mutex);
1287 for (t = perl_threads->head; NULL != t; t = t->next)
1291 /* the ithread no longer exists */
1294 pthread_mutex_unlock (&perl_threads->mutex);
1298 c_ithread_destroy (ithread);
1300 pthread_mutex_unlock (&perl_threads->mutex);
1302 } /* static void c_ithread_destructor (void *) */
1304 /* must be called with perl_threads->mutex locked */
1305 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1307 c_ithread_t *t = NULL;
1310 assert (NULL != perl_threads);
1312 t = smalloc (sizeof (*t));
1313 memset (t, 0, sizeof (c_ithread_t));
1315 t->interp = (NULL == base)
1317 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1321 if ((NULL != base) && (NULL != PL_endav)) {
1322 av_clear (PL_endav);
1323 av_undef (PL_endav);
1328 ++perl_threads->number_of_threads;
1329 #endif /* COLLECT_DEBUG */
1333 if (NULL == perl_threads->tail) {
1334 perl_threads->head = t;
1338 perl_threads->tail->next = t;
1339 t->prev = perl_threads->tail;
1342 t->pthread = pthread_self();
1345 perl_threads->tail = t;
1347 pthread_setspecific (perl_thr_key, (const void *)t);
1349 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1352 * Filter chains implementation.
1355 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1362 notification_meta_t **meta = NULL;
1367 if ((type < 0) || (type >= FC_TYPES))
1370 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1373 va_start (ap, data);
1380 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1381 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1382 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1384 if (FC_CB_CREATE == cb_type) {
1387 * $_[1] = $user_data;
1390 HV *config = newHV ();
1392 ci = va_arg (ap, oconfig_item_t *);
1394 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1397 config = (HV *)&PL_sv_undef;
1401 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1403 else if (FC_CB_DESTROY == cb_type) {
1405 * $_[1] = $user_data;
1408 /* nothing to be done - the user data pointer
1409 * is pushed onto the stack later */
1411 else if (FC_CB_EXEC == cb_type) {
1416 * $_[3] = $user_data;
1424 ds = va_arg (ap, data_set_t *);
1425 vl = va_arg (ap, value_list_t *);
1426 meta = va_arg (ap, notification_meta_t **);
1428 if (0 != data_set2av (aTHX_ ds, pds)) {
1431 pds = (AV *)&PL_sv_undef;
1435 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1438 pvl = (HV *)&PL_sv_undef;
1445 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1448 pmeta = (AV *)&PL_sv_undef;
1453 pmeta = (AV *)&PL_sv_undef;
1456 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1457 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1458 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1461 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1465 retvals = call_pv_locked (aTHX_ "Collectd::fc_call");
1467 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1468 assert (pmeta != NULL);
1470 plugin_notification_meta_free (*meta);
1471 av2notification_meta (aTHX_ pmeta, meta);
1475 if (SvTRUE(ERRSV)) {
1476 ERROR ("perl: Collectd::fc_call error: %s", SvPV_nolen(ERRSV));
1479 else if (0 < retvals) {
1482 /* the exec callbacks return a status, while
1483 * the others return a boolean value */
1484 if (FC_CB_EXEC == cb_type)
1486 else if (! SvTRUE (tmp))
1496 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1498 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1500 pfc_user_data_t *data;
1506 if (NULL == perl_threads)
1510 c_ithread_t *t = NULL;
1512 pthread_mutex_lock (&perl_threads->mutex);
1513 t = c_ithread_create (perl_threads->head->interp);
1514 pthread_mutex_unlock (&perl_threads->mutex);
1519 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1520 aTHX, perl_threads->number_of_threads);
1522 if ((1 != ci->values_num)
1523 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1524 log_warn ("A \"%s\" block expects a single string argument.",
1525 (FC_MATCH == type) ? "Match" : "Target");
1529 data = smalloc (sizeof (*data));
1530 data->name = sstrdup (ci->values[0].value.string);
1531 data->user_data = newSV (0);
1533 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1536 PFC_USER_DATA_FREE (data);
1540 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1542 static int fc_destroy (int type, void **user_data)
1544 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1550 if ((NULL == perl_threads) || (NULL == data))
1554 c_ithread_t *t = NULL;
1556 pthread_mutex_lock (&perl_threads->mutex);
1557 t = c_ithread_create (perl_threads->head->interp);
1558 pthread_mutex_unlock (&perl_threads->mutex);
1563 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1564 aTHX, perl_threads->number_of_threads);
1566 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1568 PFC_USER_DATA_FREE (data);
1571 } /* static int fc_destroy (int, void **) */
1573 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1574 notification_meta_t **meta, void **user_data)
1576 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1580 if (NULL == perl_threads)
1583 assert (NULL != data);
1586 c_ithread_t *t = NULL;
1588 pthread_mutex_lock (&perl_threads->mutex);
1589 t = c_ithread_create (perl_threads->head->interp);
1590 pthread_mutex_unlock (&perl_threads->mutex);
1595 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1596 aTHX, perl_threads->number_of_threads);
1598 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1599 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1600 notification_meta_t **, void **) */
1602 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1604 return fc_create (FC_MATCH, ci, user_data);
1605 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1607 static int pmatch_destroy (void **user_data)
1609 return fc_destroy (FC_MATCH, user_data);
1610 } /* static int pmatch_destroy (void **) */
1612 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1613 notification_meta_t **meta, void **user_data)
1615 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1616 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1617 notification_meta_t **, void **) */
1619 static match_proc_t pmatch = {
1620 pmatch_create, pmatch_destroy, pmatch_match
1623 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1625 return fc_create (FC_TARGET, ci, user_data);
1626 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1628 static int ptarget_destroy (void **user_data)
1630 return fc_destroy (FC_TARGET, user_data);
1631 } /* static int ptarget_destroy (void **) */
1633 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1634 notification_meta_t **meta, void **user_data)
1636 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1637 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1638 notification_meta_t **, void **) */
1640 static target_proc_t ptarget = {
1641 ptarget_create, ptarget_destroy, ptarget_invoke
1645 * Exported Perl API.
1648 static void _plugin_register_generic_userdata (pTHX, int type, const char *desc)
1651 user_data_t userdata;
1657 log_err ("Usage: Collectd::plugin_register_%s(pluginname, subname)",
1662 if (! SvOK (ST (0))) {
1663 log_err ("Collectd::plugin_register_%s(pluginname, subname): "
1664 "Invalid pluginname", desc);
1667 if (! SvOK (ST (1))) {
1668 log_err ("Collectd::plugin_register_%s(pluginname, subname): "
1669 "Invalid subname", desc);
1673 /* Use pluginname as-is to allow flush a single perl plugin */
1674 pluginname = SvPV_nolen (ST (0));
1676 log_debug ("Collectd::plugin_register_%s: "
1677 "plugin = \"%s\", sub = \"%s\"",
1678 desc, pluginname, SvPV_nolen (ST (1)));
1680 memset(&userdata, 0, sizeof(userdata));
1681 userdata.data = strdup(SvPV_nolen (ST (1)));
1682 userdata.free_func = free;
1684 if (PLUGIN_READ == type) {
1685 ret = plugin_register_complex_read(
1689 plugin_get_interval(), /* Default interval */
1692 else if (PLUGIN_WRITE == type) {
1693 ret = plugin_register_write(pluginname, perl_write, &userdata);
1695 else if (PLUGIN_LOG == type) {
1696 ret = plugin_register_log(pluginname, perl_log, &userdata);
1698 else if (PLUGIN_NOTIF == type) {
1699 ret = plugin_register_notification(pluginname, perl_notify, &userdata);
1701 else if (PLUGIN_FLUSH == type) {
1702 if (1 == register_legacy_flush) { /* For collectd-5.7 only, #1731 */
1703 register_legacy_flush = 0;
1704 ret = plugin_register_flush("perl", perl_flush, /* user_data = */ NULL);
1708 ret = plugin_register_flush(pluginname, perl_flush, &userdata);
1717 free (userdata.data);
1720 } /* static void _plugin_register_generic_userdata ( ... ) */
1723 * Collectd::plugin_register_TYPE (pluginname, subname).
1726 * name of the perl plugin
1729 * name of the plugin's subroutine that does the work
1732 static XS (Collectd_plugin_register_read) {
1733 return _plugin_register_generic_userdata(aTHX, PLUGIN_READ, "read");
1736 static XS (Collectd_plugin_register_write) {
1737 return _plugin_register_generic_userdata(aTHX, PLUGIN_WRITE, "write");
1740 static XS (Collectd_plugin_register_log) {
1741 return _plugin_register_generic_userdata(aTHX, PLUGIN_LOG, "log");
1744 static XS (Collectd_plugin_register_notification) {
1745 return _plugin_register_generic_userdata(aTHX, PLUGIN_NOTIF, "notification");
1748 static XS (Collectd_plugin_register_flush) {
1749 return _plugin_register_generic_userdata(aTHX, PLUGIN_FLUSH, "flush");
1752 typedef int perl_unregister_function_t(const char *name);
1754 static void _plugin_unregister_generic (pTHX,
1755 perl_unregister_function_t *unreg, const char *desc)
1760 log_err ("Usage: Collectd::plugin_unregister_%s(pluginname)", desc);
1764 if (! SvOK (ST (0))) {
1765 log_err ("Collectd::plugin_unregister_%s(pluginname): "
1766 "Invalid pluginname", desc);
1770 log_debug ("Collectd::plugin_unregister_%s: plugin = \"%s\"",
1771 desc, SvPV_nolen (ST (0)));
1773 unreg(SvPV_nolen (ST (0)));
1778 } /* static void _plugin_unregister_generic ( ... ) */
1781 * Collectd::plugin_unregister_TYPE (pluginname).
1784 * type of callback to be unregistered: read, write, log, notification, flush
1787 * name of the perl plugin
1790 static XS (Collectd_plugin_unregister_read) {
1791 return _plugin_unregister_generic(aTHX,
1792 plugin_unregister_read, "read");
1795 static XS (Collectd_plugin_unregister_write) {
1796 return _plugin_unregister_generic(aTHX,
1797 plugin_unregister_write, "write");
1800 static XS (Collectd_plugin_unregister_log) {
1801 return _plugin_unregister_generic(aTHX,
1802 plugin_unregister_log, "log");
1805 static XS (Collectd_plugin_unregister_notification) {
1806 return _plugin_unregister_generic(aTHX,
1807 plugin_unregister_notification, "notification");
1810 static XS (Collectd_plugin_unregister_flush) {
1811 return _plugin_unregister_generic(aTHX,
1812 plugin_unregister_flush, "flush");
1816 * Collectd::plugin_register_data_set (type, dataset).
1819 * type of the dataset
1822 * dataset to be registered
1824 static XS (Collectd_plugin_register_ds)
1831 log_warn ("Using plugin_register() to register new data-sets is "
1832 "deprecated - add new entries to a custom types.db instead.");
1835 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1839 log_debug ("Collectd::plugin_register_data_set: "
1840 "type = \"%s\", dataset = \"%s\"",
1841 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1845 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1846 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1850 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1858 } /* static XS (Collectd_plugin_register_ds) */
1861 * Collectd::plugin_unregister_data_set (type).
1864 * type of the dataset
1866 static XS (Collectd_plugin_unregister_ds)
1871 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1875 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1876 SvPV_nolen (ST (0)));
1878 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1882 } /* static XS (Collectd_plugin_register_ds) */
1885 * Collectd::plugin_dispatch_values (name, values).
1888 * name of the plugin
1891 * value list to submit
1893 static XS (Collectd_plugin_dispatch_values)
1902 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1906 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1907 SvPV_nolen (ST (/* stack index = */ 0)));
1909 values = ST (/* stack index = */ 0);
1914 /* Make sure the argument is a hash reference. */
1915 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1916 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1920 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1926 } /* static XS (Collectd_plugin_dispatch_values) */
1929 * Collectd::plugin_get_interval ().
1931 static XS (Collectd_plugin_get_interval)
1935 /* make sure we don't get any unused variable warnings for 'items';
1936 * don't abort, though */
1938 log_err ("Usage: Collectd::plugin_get_interval()");
1940 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1941 } /* static XS (Collectd_plugin_get_interval) */
1943 /* Collectd::plugin_write (plugin, ds, vl).
1946 * name of the plugin to call, may be 'undef'
1949 * data-set that describes the submitted values, may be 'undef'
1952 * value-list to be written
1954 static XS (Collectd__plugin_write)
1965 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1969 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1970 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1971 SvPV_nolen (ST (2)));
1973 if (! SvOK (ST (0)))
1976 plugin = SvPV_nolen (ST (0));
1979 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1980 ds_array = (AV *)SvRV (ds);
1981 else if (! SvOK (ds))
1984 log_err ("Collectd::plugin_write: Invalid data-set.");
1989 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1990 log_err ("Collectd::plugin_write: Invalid value-list.");
1994 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
2000 } /* static XS (Collectd__plugin_write) */
2003 * Collectd::_plugin_flush (plugin, timeout, identifier).
2006 * name of the plugin to flush
2009 * timeout to use when flushing the data
2012 * data-set identifier to flush
2014 static XS (Collectd__plugin_flush)
2016 char *plugin = NULL;
2023 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
2028 plugin = SvPV_nolen (ST (0));
2031 timeout = (int)SvIV (ST (1));
2034 id = SvPV_nolen (ST (2));
2036 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
2037 "id = \"%s\"", plugin, timeout, id);
2039 if (0 == plugin_flush (plugin, timeout, id))
2043 } /* static XS (Collectd__plugin_flush) */
2046 * Collectd::plugin_dispatch_notification (notif).
2049 * notification to dispatch
2051 static XS (Collectd_plugin_dispatch_notification)
2060 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
2064 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
2065 SvPV_nolen (ST (0)));
2069 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
2070 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
2074 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
2080 } /* static XS (Collectd_plugin_dispatch_notification) */
2083 * Collectd::plugin_log (level, message).
2086 * log level (LOG_DEBUG, ... LOG_ERR)
2091 static XS (Collectd_plugin_log)
2096 log_err ("Usage: Collectd::plugin_log(level, message)");
2100 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
2102 } /* static XS (Collectd_plugin_log) */
2105 * Collectd::_fc_register (type, name)
2113 static XS (Collectd__fc_register)
2123 log_err ("Usage: Collectd::_fc_register(type, name)");
2127 type = SvIV (ST (0));
2128 name = SvPV_nolen (ST (1));
2130 if (FC_MATCH == type)
2131 ret = fc_register_match (name, pmatch);
2132 else if (FC_TARGET == type)
2133 ret = fc_register_target (name, ptarget);
2139 } /* static XS (Collectd_fc_register) */
2142 * Collectd::call_by_name (...).
2144 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
2146 static XS (Collectd_call_by_name)
2151 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
2152 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
2157 name = SvPV_nolen (tmp);
2159 if (NULL == get_cv (name, 0)) {
2160 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
2165 /* simply pass on the subroutine call without touching the stack,
2166 * thus leaving any arguments and return values in place */
2168 } /* static XS (Collectd_call_by_name) */
2171 * Interface to collectd.
2174 static int perl_init (void)
2179 if (NULL == perl_threads)
2183 c_ithread_t *t = NULL;
2185 pthread_mutex_lock (&perl_threads->mutex);
2186 t = c_ithread_create (perl_threads->head->interp);
2187 pthread_mutex_unlock (&perl_threads->mutex);
2192 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
2193 aTHX, perl_threads->number_of_threads);
2195 /* Lock the base thread to avoid race conditions with c_ithread_create().
2196 * See https://github.com/collectd/collectd/issues/9 and
2197 * https://github.com/collectd/collectd/issues/1706 for details.
2199 assert (aTHX == perl_threads->head->interp);
2200 pthread_mutex_lock (&perl_threads->mutex);
2202 status = pplugin_call (aTHX_ PLUGIN_INIT);
2204 pthread_mutex_unlock (&perl_threads->mutex);
2207 } /* static int perl_init (void) */
2209 static int perl_read (user_data_t *user_data)
2213 if (NULL == perl_threads)
2217 c_ithread_t *t = NULL;
2219 pthread_mutex_lock (&perl_threads->mutex);
2220 t = c_ithread_create (perl_threads->head->interp);
2221 pthread_mutex_unlock (&perl_threads->mutex);
2226 /* Assert that we're not running as the base thread. Otherwise, we might
2227 * run into concurrency issues with c_ithread_create(). See
2228 * https://github.com/collectd/collectd/issues/9 for details. */
2229 assert (aTHX != perl_threads->head->interp);
2231 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
2232 aTHX, perl_threads->number_of_threads);
2234 return pplugin_call (aTHX_ PLUGIN_READ, user_data->data);
2235 } /* static int perl_read (user_data_t *user_data) */
2237 static int perl_write (const data_set_t *ds, const value_list_t *vl,
2238 user_data_t *user_data)
2243 if (NULL == perl_threads)
2247 c_ithread_t *t = NULL;
2249 pthread_mutex_lock (&perl_threads->mutex);
2250 t = c_ithread_create (perl_threads->head->interp);
2251 pthread_mutex_unlock (&perl_threads->mutex);
2256 /* Lock the base thread if this is not called from one of the read threads
2257 * to avoid race conditions with c_ithread_create(). See
2258 * https://github.com/collectd/collectd/issues/9 for details. */
2259 if (aTHX == perl_threads->head->interp)
2260 pthread_mutex_lock (&perl_threads->mutex);
2262 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
2263 aTHX, perl_threads->number_of_threads);
2264 status = pplugin_call (aTHX_ PLUGIN_WRITE, user_data->data, ds, vl);
2266 if (aTHX == perl_threads->head->interp)
2267 pthread_mutex_unlock (&perl_threads->mutex);
2270 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2272 static void perl_log (int level, const char *msg,
2273 user_data_t *user_data)
2277 if (NULL == perl_threads)
2281 c_ithread_t *t = NULL;
2283 pthread_mutex_lock (&perl_threads->mutex);
2284 t = c_ithread_create (perl_threads->head->interp);
2285 pthread_mutex_unlock (&perl_threads->mutex);
2290 /* Lock the base thread if this is not called from one of the read threads
2291 * to avoid race conditions with c_ithread_create(). See
2292 * https://github.com/collectd/collectd/issues/9 for details.
2295 if (aTHX == perl_threads->head->interp)
2296 pthread_mutex_lock (&perl_threads->mutex);
2298 pplugin_call (aTHX_ PLUGIN_LOG, user_data->data, level, msg);
2300 if (aTHX == perl_threads->head->interp)
2301 pthread_mutex_unlock (&perl_threads->mutex);
2304 } /* static void perl_log (int, const char *) */
2306 static int perl_notify (const notification_t *notif, user_data_t *user_data)
2310 if (NULL == perl_threads)
2314 c_ithread_t *t = NULL;
2316 pthread_mutex_lock (&perl_threads->mutex);
2317 t = c_ithread_create (perl_threads->head->interp);
2318 pthread_mutex_unlock (&perl_threads->mutex);
2322 return pplugin_call (aTHX_ PLUGIN_NOTIF, user_data->data, notif);
2323 } /* static int perl_notify (const notification_t *) */
2325 static int perl_flush (cdtime_t timeout, const char *identifier,
2326 user_data_t *user_data)
2330 if (NULL == perl_threads)
2334 c_ithread_t *t = NULL;
2336 pthread_mutex_lock (&perl_threads->mutex);
2337 t = c_ithread_create (perl_threads->head->interp);
2338 pthread_mutex_unlock (&perl_threads->mutex);
2343 /* For collectd-5.6 only, #1731 */
2344 if (user_data == NULL || user_data->data == NULL)
2345 return pplugin_call (aTHX_ PLUGIN_FLUSH_ALL, timeout, identifier);
2347 return pplugin_call (aTHX_ PLUGIN_FLUSH, user_data->data, timeout, identifier);
2348 } /* static int perl_flush (const int) */
2350 static int perl_shutdown (void)
2357 plugin_unregister_complex_config ("perl");
2358 plugin_unregister_read_group ("perl");
2360 if (NULL == perl_threads)
2364 pthread_mutex_lock (&perl_threads->mutex);
2365 t = c_ithread_create (perl_threads->head->interp);
2366 pthread_mutex_unlock (&perl_threads->mutex);
2371 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2372 aTHX, perl_threads->number_of_threads);
2374 plugin_unregister_init ("perl");
2375 plugin_unregister_flush ("perl"); /* For collectd-5.6 only, #1731 */
2377 ret = pplugin_call (aTHX_ PLUGIN_SHUTDOWN);
2379 pthread_mutex_lock (&perl_threads->mutex);
2380 t = perl_threads->tail;
2383 struct timespec ts_wait;
2384 c_ithread_t *thr = t;
2386 /* the pointer has to be advanced before destroying
2387 * the thread as this will free the memory */
2392 /* Give some time to thread to exit from Perl interpreter */
2393 WARNING ("perl shutdown: Thread is running inside Perl. Waiting.");
2395 ts_wait.tv_nsec = 500000;
2396 nanosleep (&ts_wait, NULL);
2399 pthread_kill (thr->pthread, SIGTERM);
2400 ERROR ("perl shutdown: Thread hangs inside Perl. Thread killed.");
2402 c_ithread_destroy (thr);
2405 pthread_mutex_unlock (&perl_threads->mutex);
2406 pthread_mutex_destroy (&perl_threads->mutex);
2407 pthread_mutexattr_destroy (&perl_threads->mutexattr);
2409 sfree (perl_threads);
2411 pthread_key_delete (perl_thr_key);
2415 plugin_unregister_shutdown ("perl");
2417 } /* static void perl_shutdown (void) */
2420 * Access functions for global variables.
2422 * These functions implement the "magic" used to access
2423 * the global variables from Perl.
2426 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2428 char *pv = mg->mg_ptr;
2431 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2433 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2435 char *pv = mg->mg_ptr;
2436 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2438 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2440 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2442 log_warn ("Accessing $interval_g is deprecated (and might not "
2443 "give the desired results) - plugin_get_interval() should "
2444 "be used instead.");
2445 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2447 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2449 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2451 double nv = (double)SvNV (var);
2452 log_warn ("Accessing $interval_g is deprecated (and might not "
2453 "give the desired results) - plugin_get_interval() should "
2454 "be used instead.");
2455 interval_g = DOUBLE_TO_CDTIME_T (nv);
2457 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2459 static MGVTBL g_pv_vtbl = {
2460 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2461 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2465 static MGVTBL g_interval_vtbl = {
2466 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2467 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2472 /* bootstrap the Collectd module */
2473 static void xs_init (pTHX)
2477 char *file = __FILE__;
2481 /* enable usage of Perl modules using shared libraries */
2482 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2485 for (int i = 0; NULL != api[i].f; ++i)
2486 newXS (api[i].name, api[i].f, file);
2488 stash = gv_stashpv ("Collectd", 1);
2490 /* export "constants" */
2491 for (int i = 0; '\0' != constants[i].name[0]; ++i)
2492 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2494 /* export global variables
2495 * by adding "magic" to the SV's representing the globale variables
2496 * perl is able to automagically call the get/set function when
2497 * accessing any such variable (this is basically the same as using
2499 /* global strings */
2500 for (int i = 0; '\0' != g_strings[i].name[0]; ++i) {
2501 tmp = get_sv (g_strings[i].name, 1);
2502 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2503 g_strings[i].var, 0);
2506 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2507 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2508 /* vtbl = */ &g_interval_vtbl,
2509 /* name = */ NULL, /* namelen = */ 0);
2512 } /* static void xs_init (pTHX) */
2514 /* Initialize the global Perl interpreter. */
2515 static int init_pi (int argc, char **argv)
2519 if (NULL != perl_threads)
2522 log_info ("Initializing Perl interpreter...");
2525 for (int i = 0; i < argc; ++i)
2526 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2528 #endif /* COLLECT_DEBUG */
2530 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2531 log_err ("init_pi: pthread_key_create failed");
2533 /* this must not happen - cowardly giving up if it does */
2538 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2539 * triggers a "value computed is not used" warning by gcc. */
2542 PERL_SYS_INIT3 (&argc, &argv, &environ);
2544 perl_threads = smalloc (sizeof (*perl_threads));
2545 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2547 pthread_mutexattr_init(&perl_threads->mutexattr);
2548 pthread_mutexattr_settype(&perl_threads->mutexattr, PTHREAD_MUTEX_RECURSIVE);
2549 pthread_mutex_init (&perl_threads->mutex, &perl_threads->mutexattr);
2550 /* locking the mutex should not be necessary at this point
2551 * but let's just do it for the sake of completeness */
2552 pthread_mutex_lock (&perl_threads->mutex);
2554 perl_threads->head = c_ithread_create (NULL);
2555 perl_threads->tail = perl_threads->head;
2557 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2558 log_err ("init_pi: Not enough memory.");
2562 aTHX = perl_threads->head->interp;
2563 pthread_mutex_unlock (&perl_threads->mutex);
2565 perl_construct (aTHX);
2567 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2569 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2570 SV *err = get_sv ("@", 1);
2571 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2574 perl_destruct (perl_threads->head->interp);
2575 perl_free (perl_threads->head->interp);
2576 sfree (perl_threads);
2578 pthread_key_delete (perl_thr_key);
2582 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2583 sv_setpv (get_sv ("0", 0), "collectd");
2587 plugin_register_init ("perl", perl_init);
2588 plugin_register_shutdown ("perl", perl_shutdown);
2590 } /* static int init_pi (const char **, const int) */
2593 * LoadPlugin "<Plugin>"
2595 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2597 char module_name[DATA_MAX_NAME_LEN];
2601 if ((0 != ci->children_num) || (1 != ci->values_num)
2602 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2603 log_err ("LoadPlugin expects a single string argument.");
2607 value = ci->values[0].value.string;
2609 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2610 log_err ("Invalid module name %s", value);
2614 if (0 != init_pi (perl_argc, perl_argv))
2617 assert (NULL != perl_threads);
2618 assert (NULL != perl_threads->head);
2620 aTHX = perl_threads->head->interp;
2622 log_debug ("perl_config: Loading Perl plugin \"%s\"", value);
2623 load_module (PERL_LOADMOD_NOIMPORT,
2624 newSVpv (module_name, strlen (module_name)), Nullsv);
2626 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2631 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2635 if ((0 != ci->children_num) || (1 != ci->values_num)
2636 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2637 log_err ("BaseName expects a single string argument.");
2641 value = ci->values[0].value.string;
2643 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2644 sstrncpy (base_name, value, sizeof (base_name));
2646 } /* static int perl_config_basename (oconfig_item_it *) */
2649 * EnableDebugger "<Package>"|""
2651 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2655 if ((0 != ci->children_num) || (1 != ci->values_num)
2656 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2657 log_err ("EnableDebugger expects a single string argument.");
2661 if (NULL != perl_threads) {
2662 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2666 value = ci->values[0].value.string;
2668 perl_argv = realloc (perl_argv,
2669 (++perl_argc + 1) * sizeof (char *));
2671 if (NULL == perl_argv) {
2672 log_err ("perl_config: Not enough memory.");
2676 if ('\0' == value[0]) {
2677 perl_argv[perl_argc - 1] = "-d";
2680 perl_argv[perl_argc - 1] = smalloc (strlen (value) + 4);
2681 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2682 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2685 perl_argv[perl_argc] = NULL;
2687 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2690 * IncludeDir "<Dir>"
2692 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2696 if ((0 != ci->children_num) || (1 != ci->values_num)
2697 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2698 log_err ("IncludeDir expects a single string argument.");
2702 value = ci->values[0].value.string;
2705 perl_argv = realloc (perl_argv,
2706 (++perl_argc + 1) * sizeof (char *));
2708 if (NULL == perl_argv) {
2709 log_err ("perl_config: Not enough memory.");
2713 perl_argv[perl_argc - 1] = smalloc (strlen (value) + 3);
2714 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2715 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2717 perl_argv[perl_argc] = NULL;
2720 /* prepend the directory to @INC */
2721 av_unshift (GvAVn (PL_incgv), 1);
2722 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2725 } /* static int perl_config_includedir (oconfig_item_it *) */
2730 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2740 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2741 log_err ("LoadPlugin expects a single string argument.");
2745 plugin = ci->values[0].value.string;
2748 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2752 log_err ("Unable to convert configuration to a Perl hash value.");
2753 config = (HV *)&PL_sv_undef;
2761 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2762 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2766 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2781 } /* static int perl_config_plugin (oconfig_item_it *) */
2783 static int perl_config (oconfig_item_t *ci)
2789 for (int i = 0; i < ci->children_num; ++i) {
2790 oconfig_item_t *c = ci->children + i;
2791 int current_status = 0;
2793 if (NULL != perl_threads)
2795 if ((aTHX = PERL_GET_CONTEXT) == NULL)
2799 if (0 == strcasecmp (c->key, "LoadPlugin"))
2800 current_status = perl_config_loadplugin (aTHX_ c);
2801 else if (0 == strcasecmp (c->key, "BaseName"))
2802 current_status = perl_config_basename (aTHX_ c);
2803 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2804 current_status = perl_config_enabledebugger (aTHX_ c);
2805 else if (0 == strcasecmp (c->key, "IncludeDir"))
2806 current_status = perl_config_includedir (aTHX_ c);
2807 else if (0 == strcasecmp (c->key, "Plugin"))
2808 current_status = perl_config_plugin (aTHX_ c);
2809 else if (0 == strcasecmp (c->key, "RegisterLegacyFlush"))
2810 cf_util_get_boolean (c, ®ister_legacy_flush);
2813 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2817 /* fatal error - it's up to perl_config_* to clean up */
2818 if (0 > current_status) {
2819 log_err ("Configuration failed with a fatal error - "
2820 "plugin disabled!");
2821 return current_status;
2824 status += current_status;
2827 } /* static int perl_config (oconfig_item_t *) */
2829 void module_register (void)
2832 perl_argv = smalloc ((perl_argc + 1) * sizeof (*perl_argv));
2834 /* default options for the Perl interpreter */
2836 perl_argv[1] = "-MCollectd";
2837 perl_argv[2] = "-e";
2839 perl_argv[4] = NULL;
2841 plugin_register_complex_config ("perl", perl_config);
2843 } /* void module_register (void) */
2845 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */