2 * collectd - src/perl.c
3 * Copyright (C) 2007 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
32 #include "configfile.h"
39 /* Some versions of Perl define their own version of DEBUG... :-/ */
44 /* ... while we want the definition found in plugin.h. */
50 #if !defined(USE_ITHREADS)
51 # error "Perl does not support ithreads!"
52 #endif /* !defined(USE_ITHREADS) */
54 /* clear the Perl sub's stack frame
55 * (this should only be used inside an XSUB) */
56 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
60 #define PLUGIN_WRITE 2
61 #define PLUGIN_SHUTDOWN 3
64 #define PLUGIN_TYPES 5
66 #define PLUGIN_DATASET 255
68 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
69 #define log_info(...) INFO ("perl: " __VA_ARGS__)
70 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
71 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
73 /* this is defined in DynaLoader.a */
74 void boot_DynaLoader (PerlInterpreter *, CV *);
76 static XS (Collectd_plugin_register_ds);
77 static XS (Collectd_plugin_unregister_ds);
78 static XS (Collectd_plugin_dispatch_values);
79 static XS (Collectd_plugin_log);
80 static XS (Collectd_call_by_name);
86 typedef struct c_ithread_s {
87 /* the thread's Perl interpreter */
88 PerlInterpreter *interp;
90 /* double linked list of threads */
91 struct c_ithread_s *prev;
92 struct c_ithread_s *next;
100 /* some usage stats */
101 int number_of_threads;
102 #endif /* COLLECT_DEBUG */
104 pthread_mutex_t mutex;
111 /* if perl_threads != NULL perl_threads->head must
112 * point to the "base" thread */
113 static c_ithread_list_t *perl_threads = NULL;
115 static int perl_argc = 0;
116 static char **perl_argv = NULL;
118 static char base_name[DATA_MAX_NAME_LEN] = "";
125 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
126 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
127 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
128 { "Collectd::plugin_log", Collectd_plugin_log },
129 { "Collectd::call_by_name", Collectd_call_by_name },
138 { "Collectd::TYPE_INIT", PLUGIN_INIT },
139 { "Collectd::TYPE_READ", PLUGIN_READ },
140 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
141 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
142 { "Collectd::TYPE_LOG", PLUGIN_LOG },
143 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
144 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
145 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
146 { "Collectd::LOG_ERR", LOG_ERR },
147 { "Collectd::LOG_WARNING", LOG_WARNING },
148 { "Collectd::LOG_NOTICE", LOG_NOTICE },
149 { "Collectd::LOG_INFO", LOG_INFO },
150 { "Collectd::LOG_DEBUG", LOG_DEBUG },
155 * Helper functions for data type conversion.
170 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
174 if ((NULL == hash) || (NULL == ds))
177 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
178 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
179 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
182 log_err ("hv2data_source: No DS name given.");
186 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
187 ds->type = SvIV (*tmp);
189 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
190 log_err ("hv2data_source: Invalid DS type.");
195 ds->type = DS_TYPE_COUNTER;
198 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
199 ds->min = SvNV (*tmp);
203 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
204 ds->max = SvNV (*tmp);
208 } /* static data_source_t *hv2data_source (HV *) */
210 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
212 const data_set_t *ds;
216 if ((NULL == name) || (NULL == array) || (NULL == value))
219 if (av_len (array) < len - 1)
220 len = av_len (array) + 1;
225 ds = plugin_get_ds (name);
227 log_err ("av2value: Unknown dataset \"%s\"", name);
231 if (ds->ds_num < len) {
232 log_warn ("av2value: Value length exceeds data set length.");
236 for (i = 0; i < len; ++i) {
237 SV **tmp = av_fetch (array, i, 0);
240 if (DS_TYPE_COUNTER == ds->ds[i].type)
241 value[i].counter = SvIV (*tmp);
243 value[i].gauge = SvNV (*tmp);
250 } /* static int av2value (char *, AV *, value_t *, int) */
252 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
256 if ((NULL == ds) || (NULL == array))
259 av_extend (array, ds->ds_num);
261 for (i = 0; i < ds->ds_num; ++i) {
262 HV *source = newHV ();
264 if (NULL == hv_store (source, "name", 4,
265 newSVpv (ds->ds[i].name, 0), 0))
268 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
271 if (! isnan (ds->ds[i].min))
272 if (NULL == hv_store (source, "min", 3,
273 newSVnv (ds->ds[i].min), 0))
276 if (! isnan (ds->ds[i].max))
277 if (NULL == hv_store (source, "max", 3,
278 newSVnv (ds->ds[i].max), 0))
281 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
285 } /* static int data_set2av (data_set_t *, AV *) */
287 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
294 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
297 len = vl->values_len;
299 if (ds->ds_num < len) {
300 log_warn ("value2av: Value length exceeds data set length.");
305 av_extend (values, len - 1);
307 for (i = 0; i < len; ++i) {
310 if (DS_TYPE_COUNTER == ds->ds[i].type)
311 val = newSViv (vl->values[i].counter);
313 val = newSVnv (vl->values[i].gauge);
315 if (NULL == av_store (values, i, val)) {
321 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
325 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
328 if ('\0' != vl->host[0])
329 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
332 if ('\0' != vl->plugin[0])
333 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
336 if ('\0' != vl->plugin_instance[0])
337 if (NULL == hv_store (hash, "plugin_instance", 15,
338 newSVpv (vl->plugin_instance, 0), 0))
341 if ('\0' != vl->type_instance[0])
342 if (NULL == hv_store (hash, "type_instance", 13,
343 newSVpv (vl->type_instance, 0), 0))
346 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
349 * Internal functions.
352 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
354 if (base_name[0] == '\0')
355 status = snprintf (buf, buf_len, "%s", module);
357 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
358 if ((status < 0) || (status >= buf_len))
360 buf[buf_len - 1] = '\0';
362 } /* char *get_module_name */
365 * Add a plugin's data set definition.
367 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
372 data_source_t *ds = NULL;
373 data_set_t *set = NULL;
375 if ((NULL == name) || (NULL == dataset))
378 len = av_len (dataset);
383 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
384 set = (data_set_t *)smalloc (sizeof (data_set_t));
386 for (i = 0; i <= len; ++i) {
387 SV **elem = av_fetch (dataset, i, 0);
392 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
393 log_err ("pplugin_register_data_set: Invalid data source.");
397 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
400 log_debug ("pplugin_register_data_set: "
401 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
402 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
405 strncpy (set->type, name, DATA_MAX_NAME_LEN);
406 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
408 set->ds_num = len + 1;
410 return plugin_register_data_set (set);
411 } /* static int pplugin_register_data_set (char *, SV *) */
414 * Remove a plugin's data set definition.
416 static int pplugin_unregister_data_set (char *name)
420 return plugin_unregister_data_set (name);
421 } /* static int pplugin_unregister_data_set (char *) */
424 * Submit the values to the write functions.
428 * values => [ @values ],
432 * plugin_instance => $pinstance,
433 * type_instance => $tinstance,
436 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
438 value_list_t list = VALUE_LIST_INIT;
445 if ((NULL == name) || (NULL == values))
448 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
449 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
450 log_err ("pplugin_dispatch_values: No valid values given.");
455 AV *array = (AV *)SvRV (*tmp);
456 int len = av_len (array) + 1;
461 val = (value_t *)smalloc (len * sizeof (value_t));
463 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
466 if (-1 == list.values_len) {
472 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
473 list.time = (time_t)SvIV (*tmp);
476 list.time = time (NULL);
479 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
480 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
481 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
484 strcpy (list.host, hostname_g);
487 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
488 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
489 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
492 if (NULL != (tmp = hv_fetch (values,
493 "plugin_instance", 15, 0))) {
494 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
495 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
498 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
499 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
500 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
503 ret = plugin_dispatch_values (name, &list);
507 } /* static int pplugin_dispatch_values (char *, HV *) */
510 * Call all working functions of the given type.
512 static int pplugin_call_all (pTHX_ int type, ...)
521 if ((type < 0) || (type >= PLUGIN_TYPES))
531 XPUSHs (sv_2mortal (newSViv ((IV)type)));
533 if (PLUGIN_WRITE == type) {
535 * $_[0] = $plugin_type;
550 * values => [ $v1, ... ],
554 * plugin_instance => $instance,
555 * type_instance => $type_instance
564 ds = va_arg (ap, data_set_t *);
565 vl = va_arg (ap, value_list_t *);
567 if (-1 == data_set2av (aTHX_ ds, pds))
570 if (-1 == value_list2hv (aTHX_ vl, ds, pvl))
573 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
574 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
575 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
577 else if (PLUGIN_LOG == type) {
583 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
584 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
589 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
604 } /* static int pplugin_call_all (int, ...) */
611 * Collectd::plugin_register_data_set (type, dataset).
614 * type of the dataset
617 * dataset to be registered
619 static XS (Collectd_plugin_register_ds)
627 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
631 log_debug ("Collectd::plugin_register_data_set: "
632 "type = \"%s\", dataset = \"%s\"",
633 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
637 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
638 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
642 log_err ("Collectd::plugin_register_data_set: Invalid data.");
650 } /* static XS (Collectd_plugin_register_ds) */
653 * Collectd::plugin_unregister_data_set (type).
656 * type of the dataset
658 static XS (Collectd_plugin_unregister_ds)
663 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
667 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
668 SvPV_nolen (ST (0)));
670 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
674 } /* static XS (Collectd_plugin_register_ds) */
677 * Collectd::plugin_dispatch_values (name, values).
683 * value list to submit
685 static XS (Collectd_plugin_dispatch_values)
694 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
698 log_debug ("Collectd::plugin_dispatch_values: "
699 "name = \"%s\", values=\"%s\"",
700 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
704 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
705 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
709 if ((NULL == ST (0)) || (NULL == values))
712 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
713 (HV *)SvRV (values));
719 } /* static XS (Collectd_plugin_dispatch_values) */
722 * Collectd::plugin_log (level, message).
725 * log level (LOG_DEBUG, ... LOG_ERR)
730 static XS (Collectd_plugin_log)
735 log_err ("Usage: Collectd::plugin_log(level, message)");
739 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
741 } /* static XS (Collectd_plugin_log) */
744 * Collectd::call_by_name (...).
746 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
748 static XS (Collectd_call_by_name)
753 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
754 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
759 name = SvPV_nolen (tmp);
761 if (NULL == get_cv (name, 0)) {
762 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
767 /* simply pass on the subroutine call without touching the stack,
768 * thus leaving any arguments and return values in place */
770 } /* static XS (Collectd_call_by_name) */
773 * collectd's perl interpreter based thread implementation.
775 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
778 /* must be called with perl_threads->mutex locked */
779 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
781 c_ithread_t *t = NULL;
783 assert (NULL != perl_threads);
785 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
786 memset (t, 0, sizeof (c_ithread_t));
788 t->interp = (NULL == base)
790 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
793 ++perl_threads->number_of_threads;
794 #endif /* COLLECT_DEBUG */
798 if (NULL == perl_threads->tail) {
799 perl_threads->head = t;
803 perl_threads->tail->next = t;
804 t->prev = perl_threads->tail;
807 perl_threads->tail = t;
809 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
812 * Interface to collectd.
815 static int perl_init (void)
819 if (NULL == perl_threads)
823 c_ithread_t *t = NULL;
825 pthread_mutex_lock (&perl_threads->mutex);
826 t = c_ithread_create (perl_threads->head->interp);
827 pthread_mutex_unlock (&perl_threads->mutex);
832 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
833 aTHX, perl_threads->number_of_threads);
834 return pplugin_call_all (aTHX_ PLUGIN_INIT);
835 } /* static int perl_init (void) */
837 static int perl_read (void)
841 if (NULL == perl_threads)
845 c_ithread_t *t = NULL;
847 pthread_mutex_lock (&perl_threads->mutex);
848 t = c_ithread_create (perl_threads->head->interp);
849 pthread_mutex_unlock (&perl_threads->mutex);
854 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
855 aTHX, perl_threads->number_of_threads);
856 return pplugin_call_all (aTHX_ PLUGIN_READ);
857 } /* static int perl_read (void) */
859 static int perl_write (const data_set_t *ds, const value_list_t *vl)
863 if (NULL == perl_threads)
867 c_ithread_t *t = NULL;
869 pthread_mutex_lock (&perl_threads->mutex);
870 t = c_ithread_create (perl_threads->head->interp);
871 pthread_mutex_unlock (&perl_threads->mutex);
876 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
877 aTHX, perl_threads->number_of_threads);
878 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
879 } /* static int perl_write (const data_set_t *, const value_list_t *) */
881 static void perl_log (int level, const char *msg)
885 if (NULL == perl_threads)
889 c_ithread_t *t = NULL;
891 pthread_mutex_lock (&perl_threads->mutex);
892 t = c_ithread_create (perl_threads->head->interp);
893 pthread_mutex_unlock (&perl_threads->mutex);
898 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
900 } /* static void perl_log (int, const char *) */
902 static int perl_shutdown (void)
904 c_ithread_t *t = NULL;
910 plugin_unregister_complex_config ("perl");
912 if (NULL == perl_threads)
916 c_ithread_t *t = NULL;
918 pthread_mutex_lock (&perl_threads->mutex);
919 t = c_ithread_create (perl_threads->head->interp);
920 pthread_mutex_unlock (&perl_threads->mutex);
925 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
926 aTHX, perl_threads->number_of_threads);
928 plugin_unregister_log ("perl");
929 plugin_unregister_init ("perl");
930 plugin_unregister_read ("perl");
931 plugin_unregister_write ("perl");
933 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
935 pthread_mutex_lock (&perl_threads->mutex);
936 t = perl_threads->tail;
940 PERL_SET_CONTEXT (aTHX);
944 #endif /* COLLECT_DEBUG */
946 perl_destruct (aTHX);
954 pthread_mutex_unlock (&perl_threads->mutex);
956 sfree (perl_threads);
960 plugin_unregister_shutdown ("perl");
962 } /* static void perl_shutdown (void) */
964 /* bootstrap the Collectd module */
965 static void xs_init (pTHX)
968 char *file = __FILE__;
974 /* enable usage of Perl modules using shared libraries */
975 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
978 for (i = 0; NULL != api[i].f; ++i)
979 newXS (api[i].name, api[i].f, file);
981 stash = gv_stashpv ("Collectd", 1);
983 /* export "constants" */
984 for (i = 0; '\0' != constants[i].name[0]; ++i)
985 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
987 } /* static void xs_init (pTHX) */
989 /* Initialize the global Perl interpreter. */
990 static int init_pi (int argc, char **argv)
994 if (NULL != perl_threads)
997 log_info ("Initializing Perl interpreter...");
1002 for (i = 0; i < argc; ++i)
1003 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1005 #endif /* COLLECT_DEBUG */
1007 PERL_SYS_INIT3 (&argc, &argv, &environ);
1009 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1010 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1012 pthread_mutex_init (&perl_threads->mutex, NULL);
1013 /* locking the mutex should not be necessary at this point
1014 * but let's just do it for the sake of completeness */
1015 pthread_mutex_lock (&perl_threads->mutex);
1017 perl_threads->head = c_ithread_create (NULL);
1018 perl_threads->tail = perl_threads->head;
1020 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1021 log_err ("module_register: Not enough memory.");
1025 aTHX = perl_threads->head->interp;
1026 pthread_mutex_unlock (&perl_threads->mutex);
1028 perl_construct (aTHX);
1030 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1032 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1033 log_err ("module_register: Unable to bootstrap Collectd.");
1037 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1038 sv_setpv (get_sv ("0", 0), "collectd");
1042 plugin_register_log ("perl", perl_log);
1043 plugin_register_init ("perl", perl_init);
1045 plugin_register_read ("perl", perl_read);
1047 plugin_register_write ("perl", perl_write);
1048 plugin_register_shutdown ("perl", perl_shutdown);
1050 } /* static int init_pi (const char **, const int) */
1053 * LoadPlugin "<Plugin>"
1055 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1057 char module_name[DATA_MAX_NAME_LEN];
1061 if ((0 != ci->children_num) || (1 != ci->values_num)
1062 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1065 value = ci->values[0].value.string;
1067 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1068 log_err ("Invalid module name %s", value);
1072 init_pi (perl_argc, perl_argv);
1073 assert (NULL != perl_threads);
1074 assert (NULL != perl_threads->head);
1076 aTHX = perl_threads->head->interp;
1078 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1079 load_module (PERL_LOADMOD_NOIMPORT,
1080 newSVpv (module_name, strlen (module_name)), Nullsv);
1082 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1087 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1091 if ((0 != ci->children_num) || (1 != ci->values_num)
1092 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1095 value = ci->values[0].value.string;
1097 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1098 strncpy (base_name, value, sizeof (base_name));
1099 base_name[sizeof (base_name) - 1] = '\0';
1101 } /* static int perl_config_basename (oconfig_item_it *) */
1104 * EnableDebugger "<Package>"|""
1106 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1110 if ((0 != ci->children_num) || (1 != ci->values_num)
1111 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1114 value = ci->values[0].value.string;
1116 perl_argv = (char **)realloc (perl_argv,
1117 (++perl_argc + 1) * sizeof (char *));
1119 if (NULL == perl_argv) {
1120 log_err ("perl_config: Not enough memory.");
1124 if ('\0' == value[0]) {
1125 perl_argv[perl_argc - 1] = "-d";
1128 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1129 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1130 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1133 perl_argv[perl_argc] = NULL;
1135 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1138 * IncludeDir "<Dir>"
1140 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1144 if ((0 != ci->children_num) || (1 != ci->values_num)
1145 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1148 value = ci->values[0].value.string;
1151 perl_argv = (char **)realloc (perl_argv,
1152 (++perl_argc + 1) * sizeof (char *));
1154 if (NULL == perl_argv) {
1155 log_err ("perl_config: Not enough memory.");
1159 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1160 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1161 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1163 perl_argv[perl_argc] = NULL;
1166 /* prepend the directory to @INC */
1167 av_unshift (GvAVn (PL_incgv), 1);
1168 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1171 } /* static int perl_config_includedir (oconfig_item_it *) */
1173 static int perl_config (oconfig_item_t *ci)
1179 /* dTHX does not get any valid values in case Perl
1180 * has not been initialized */
1181 if (NULL == perl_threads)
1184 for (i = 0; i < ci->children_num; ++i) {
1185 oconfig_item_t *c = ci->children + i;
1187 if (0 == strcasecmp (c->key, "LoadPlugin"))
1188 perl_config_loadplugin (aTHX_ c);
1189 else if (0 == strcasecmp (c->key, "BaseName"))
1190 perl_config_basename (aTHX_ c);
1191 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1192 perl_config_enabledebugger (aTHX_ c);
1193 else if (0 == strcasecmp (c->key, "IncludeDir"))
1194 perl_config_includedir (aTHX_ c);
1196 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1199 } /* static int perl_config (oconfig_item_t *) */
1201 void module_register (void)
1204 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1206 /* default options for the Perl interpreter */
1208 perl_argv[1] = "-MCollectd";
1209 perl_argv[2] = "-e";
1211 perl_argv[4] = NULL;
1213 plugin_register_complex_config ("perl", perl_config);
1215 } /* void module_register (void) */
1217 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */