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) */
56 #define PLUGIN_WRITE 2
57 #define PLUGIN_SHUTDOWN 3
60 #define PLUGIN_TYPES 5
62 #define PLUGIN_DATASET 255
64 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
65 #define log_info(...) INFO ("perl: " __VA_ARGS__)
66 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
67 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
69 /* this is defined in DynaLoader.a */
70 void boot_DynaLoader (PerlInterpreter *, CV *);
72 static XS (Collectd_plugin_register_ds);
73 static XS (Collectd_plugin_unregister_ds);
74 static XS (Collectd_plugin_dispatch_values);
75 static XS (Collectd_plugin_log);
81 typedef struct c_ithread_s {
82 /* the thread's Perl interpreter */
83 PerlInterpreter *interp;
85 /* double linked list of threads */
86 struct c_ithread_s *prev;
87 struct c_ithread_s *next;
95 /* some usage stats */
96 int number_of_threads;
97 #endif /* COLLECT_DEBUG */
99 pthread_mutex_t mutex;
106 /* if perl_threads != NULL perl_threads->head must
107 * point to the "base" thread */
108 static c_ithread_list_t *perl_threads = NULL;
110 static int perl_argc = 0;
111 static char **perl_argv = NULL;
113 static char base_name[DATA_MAX_NAME_LEN] = "";
120 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
121 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
122 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
123 { "Collectd::plugin_log", Collectd_plugin_log },
132 { "Collectd::TYPE_INIT", PLUGIN_INIT },
133 { "Collectd::TYPE_READ", PLUGIN_READ },
134 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
135 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
136 { "Collectd::TYPE_LOG", PLUGIN_LOG },
137 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
138 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
139 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
140 { "Collectd::LOG_ERR", LOG_ERR },
141 { "Collectd::LOG_WARNING", LOG_WARNING },
142 { "Collectd::LOG_NOTICE", LOG_NOTICE },
143 { "Collectd::LOG_INFO", LOG_INFO },
144 { "Collectd::LOG_DEBUG", LOG_DEBUG },
149 * Helper functions for data type conversion.
164 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
168 if ((NULL == hash) || (NULL == ds))
171 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
172 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
173 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
176 log_err ("hv2data_source: No DS name given.");
180 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
181 ds->type = SvIV (*tmp);
183 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
184 log_err ("hv2data_source: Invalid DS type.");
189 ds->type = DS_TYPE_COUNTER;
192 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
193 ds->min = SvNV (*tmp);
197 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
198 ds->max = SvNV (*tmp);
202 } /* static data_source_t *hv2data_source (HV *) */
204 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
206 const data_set_t *ds;
210 if ((NULL == name) || (NULL == array) || (NULL == value))
213 if (av_len (array) < len - 1)
214 len = av_len (array) + 1;
219 ds = plugin_get_ds (name);
221 log_err ("av2value: Unknown dataset \"%s\"", name);
225 if (ds->ds_num < len) {
226 log_warn ("av2value: Value length exceeds data set length.");
230 for (i = 0; i < len; ++i) {
231 SV **tmp = av_fetch (array, i, 0);
234 if (DS_TYPE_COUNTER == ds->ds[i].type)
235 value[i].counter = SvIV (*tmp);
237 value[i].gauge = SvNV (*tmp);
244 } /* static int av2value (char *, AV *, value_t *, int) */
246 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
250 if ((NULL == ds) || (NULL == array))
253 av_extend (array, ds->ds_num);
255 for (i = 0; i < ds->ds_num; ++i) {
256 HV *source = newHV ();
258 if (NULL == hv_store (source, "name", 4,
259 newSVpv (ds->ds[i].name, 0), 0))
262 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
265 if (! isnan (ds->ds[i].min))
266 if (NULL == hv_store (source, "min", 3,
267 newSVnv (ds->ds[i].min), 0))
270 if (! isnan (ds->ds[i].max))
271 if (NULL == hv_store (source, "max", 3,
272 newSVnv (ds->ds[i].max), 0))
275 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
279 } /* static int data_set2av (data_set_t *, AV *) */
281 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
288 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
291 len = vl->values_len;
293 if (ds->ds_num < len) {
294 log_warn ("value2av: Value length exceeds data set length.");
299 av_extend (values, len - 1);
301 for (i = 0; i < len; ++i) {
304 if (DS_TYPE_COUNTER == ds->ds[i].type)
305 val = newSViv (vl->values[i].counter);
307 val = newSVnv (vl->values[i].gauge);
309 if (NULL == av_store (values, i, val)) {
315 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
319 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
322 if ('\0' != vl->host[0])
323 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
326 if ('\0' != vl->plugin[0])
327 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
330 if ('\0' != vl->plugin_instance[0])
331 if (NULL == hv_store (hash, "plugin_instance", 15,
332 newSVpv (vl->plugin_instance, 0), 0))
335 if ('\0' != vl->type_instance[0])
336 if (NULL == hv_store (hash, "type_instance", 13,
337 newSVpv (vl->type_instance, 0), 0))
340 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
343 * Internal functions.
346 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
348 if (base_name[0] == '\0')
349 status = snprintf (buf, buf_len, "%s", module);
351 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
352 if ((status < 0) || (status >= buf_len))
354 buf[buf_len - 1] = '\0';
356 } /* char *get_module_name */
359 * Add a plugin's data set definition.
361 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
366 data_source_t *ds = NULL;
367 data_set_t *set = NULL;
369 if ((NULL == name) || (NULL == dataset))
372 len = av_len (dataset);
377 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
378 set = (data_set_t *)smalloc (sizeof (data_set_t));
380 for (i = 0; i <= len; ++i) {
381 SV **elem = av_fetch (dataset, i, 0);
386 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
387 log_err ("pplugin_register_data_set: Invalid data source.");
391 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
394 log_debug ("pplugin_register_data_set: "
395 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
396 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
399 strncpy (set->type, name, DATA_MAX_NAME_LEN);
400 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
402 set->ds_num = len + 1;
404 return plugin_register_data_set (set);
405 } /* static int pplugin_register_data_set (char *, SV *) */
408 * Remove a plugin's data set definition.
410 static int pplugin_unregister_data_set (char *name)
414 return plugin_unregister_data_set (name);
415 } /* static int pplugin_unregister_data_set (char *) */
418 * Submit the values to the write functions.
422 * values => [ @values ],
426 * plugin_instance => $pinstance,
427 * type_instance => $tinstance,
430 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
432 value_list_t list = VALUE_LIST_INIT;
439 if ((NULL == name) || (NULL == values))
442 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
443 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
444 log_err ("pplugin_dispatch_values: No valid values given.");
449 AV *array = (AV *)SvRV (*tmp);
450 int len = av_len (array) + 1;
455 val = (value_t *)smalloc (len * sizeof (value_t));
457 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
460 if (-1 == list.values_len) {
466 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
467 list.time = (time_t)SvIV (*tmp);
470 list.time = time (NULL);
473 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
474 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
475 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
478 strcpy (list.host, hostname_g);
481 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
482 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
483 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
486 if (NULL != (tmp = hv_fetch (values,
487 "plugin_instance", 15, 0))) {
488 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
489 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
492 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
493 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
494 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
497 ret = plugin_dispatch_values (name, &list);
501 } /* static int pplugin_dispatch_values (char *, HV *) */
504 * Call all working functions of the given type.
506 static int pplugin_call_all (pTHX_ int type, ...)
515 if ((type < 0) || (type >= PLUGIN_TYPES))
525 XPUSHs (sv_2mortal (newSViv ((IV)type)));
527 if (PLUGIN_WRITE == type) {
529 * $_[0] = $plugin_type;
544 * values => [ $v1, ... ],
548 * plugin_instance => $instance,
549 * type_instance => $type_instance
558 ds = va_arg (ap, data_set_t *);
559 vl = va_arg (ap, value_list_t *);
561 if (-1 == data_set2av (aTHX_ ds, pds))
564 if (-1 == value_list2hv (aTHX_ vl, ds, pvl))
567 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
568 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
569 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
571 else if (PLUGIN_LOG == type) {
577 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
578 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
583 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
598 } /* static int pplugin_call_all (int, ...) */
605 * Collectd::plugin_register_data_set (type, dataset).
608 * type of the dataset
611 * dataset to be registered
613 static XS (Collectd_plugin_register_ds)
621 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
625 log_debug ("Collectd::plugin_register_data_set: "
626 "type = \"%s\", dataset = \"%s\"",
627 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
631 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
632 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
636 log_err ("Collectd::plugin_register_data_set: Invalid data.");
644 } /* static XS (Collectd_plugin_register_ds) */
647 * Collectd::plugin_unregister_data_set (type).
650 * type of the dataset
652 static XS (Collectd_plugin_unregister_ds)
657 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
661 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
662 SvPV_nolen (ST (0)));
664 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
668 } /* static XS (Collectd_plugin_register_ds) */
671 * Collectd::plugin_dispatch_values (name, values).
677 * value list to submit
679 static XS (Collectd_plugin_dispatch_values)
688 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
692 log_debug ("Collectd::plugin_dispatch_values: "
693 "name = \"%s\", values=\"%s\"",
694 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
698 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
699 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
703 if ((NULL == ST (0)) || (NULL == values))
706 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
707 (HV *)SvRV (values));
713 } /* static XS (Collectd_plugin_dispatch_values) */
716 * Collectd::plugin_log (level, message).
719 * log level (LOG_DEBUG, ... LOG_ERR)
724 static XS (Collectd_plugin_log)
729 log_err ("Usage: Collectd::plugin_log(level, message)");
733 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
735 } /* static XS (Collectd_plugin_log) */
738 * collectd's perl interpreter based thread implementation.
740 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
743 /* must be called with perl_threads->mutex locked */
744 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
746 c_ithread_t *t = NULL;
748 assert (NULL != perl_threads);
750 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
751 memset (t, 0, sizeof (c_ithread_t));
753 t->interp = (NULL == base)
755 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
758 ++perl_threads->number_of_threads;
759 #endif /* COLLECT_DEBUG */
763 if (NULL == perl_threads->tail) {
764 perl_threads->head = t;
768 perl_threads->tail->next = t;
769 t->prev = perl_threads->tail;
772 perl_threads->tail = t;
774 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
777 * Interface to collectd.
780 static int perl_init (void)
784 if (NULL == perl_threads)
788 c_ithread_t *t = NULL;
790 pthread_mutex_lock (&perl_threads->mutex);
791 t = c_ithread_create (perl_threads->head->interp);
792 pthread_mutex_unlock (&perl_threads->mutex);
797 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)\n",
798 aTHX, perl_threads->number_of_threads);
799 return pplugin_call_all (aTHX_ PLUGIN_INIT);
800 } /* static int perl_init (void) */
802 static int perl_read (void)
806 if (NULL == perl_threads)
810 c_ithread_t *t = NULL;
812 pthread_mutex_lock (&perl_threads->mutex);
813 t = c_ithread_create (perl_threads->head->interp);
814 pthread_mutex_unlock (&perl_threads->mutex);
819 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)\n",
820 aTHX, perl_threads->number_of_threads);
821 return pplugin_call_all (aTHX_ PLUGIN_READ);
822 } /* static int perl_read (void) */
824 static int perl_write (const data_set_t *ds, const value_list_t *vl)
828 if (NULL == perl_threads)
832 c_ithread_t *t = NULL;
834 pthread_mutex_lock (&perl_threads->mutex);
835 t = c_ithread_create (perl_threads->head->interp);
836 pthread_mutex_unlock (&perl_threads->mutex);
841 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)\n",
842 aTHX, perl_threads->number_of_threads);
843 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
844 } /* static int perl_write (const data_set_t *, const value_list_t *) */
846 static void perl_log (int level, const char *msg)
850 if (NULL == perl_threads)
854 c_ithread_t *t = NULL;
856 pthread_mutex_lock (&perl_threads->mutex);
857 t = c_ithread_create (perl_threads->head->interp);
858 pthread_mutex_unlock (&perl_threads->mutex);
863 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
865 } /* static void perl_log (int, const char *) */
867 static int perl_shutdown (void)
869 c_ithread_t *t = NULL;
875 plugin_unregister_complex_config ("perl");
877 if (NULL == perl_threads)
881 c_ithread_t *t = NULL;
883 pthread_mutex_lock (&perl_threads->mutex);
884 t = c_ithread_create (perl_threads->head->interp);
885 pthread_mutex_unlock (&perl_threads->mutex);
890 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)\n",
891 aTHX, perl_threads->number_of_threads);
893 plugin_unregister_log ("perl");
894 plugin_unregister_init ("perl");
895 plugin_unregister_read ("perl");
896 plugin_unregister_write ("perl");
898 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
900 pthread_mutex_lock (&perl_threads->mutex);
901 t = perl_threads->tail;
905 PERL_SET_CONTEXT (aTHX);
909 #endif /* COLLECT_DEBUG */
911 perl_destruct (aTHX);
919 pthread_mutex_unlock (&perl_threads->mutex);
921 sfree (perl_threads);
925 plugin_unregister_shutdown ("perl");
927 } /* static void perl_shutdown (void) */
929 /* bootstrap the Collectd module */
930 static void xs_init (pTHX)
933 char *file = __FILE__;
939 /* enable usage of Perl modules using shared libraries */
940 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
943 for (i = 0; NULL != api[i].f; ++i)
944 newXS (api[i].name, api[i].f, file);
946 stash = gv_stashpv ("Collectd", 1);
948 /* export "constants" */
949 for (i = 0; '\0' != constants[i].name[0]; ++i)
950 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
952 } /* static void xs_init (pTHX) */
954 /* Initialize the global Perl interpreter. */
955 static int init_pi (int argc, char **argv)
959 if (NULL != perl_threads)
962 log_info ("Initializing Perl interpreter...");
967 for (i = 0; i < argc; ++i)
968 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
970 #endif /* COLLECT_DEBUG */
972 PERL_SYS_INIT3 (&argc, &argv, &environ);
974 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
975 memset (perl_threads, 0, sizeof (c_ithread_list_t));
977 pthread_mutex_init (&perl_threads->mutex, NULL);
978 /* locking the mutex should not be necessary at this point
979 * but let's just do it for the sake of completeness */
980 pthread_mutex_lock (&perl_threads->mutex);
982 perl_threads->head = c_ithread_create (NULL);
983 perl_threads->tail = perl_threads->head;
985 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
986 log_err ("module_register: Not enough memory.");
990 aTHX = perl_threads->head->interp;
991 pthread_mutex_unlock (&perl_threads->mutex);
993 perl_construct (aTHX);
995 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
997 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
998 log_err ("module_register: Unable to bootstrap Collectd.");
1002 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1003 sv_setpv (get_sv ("0", 0), "collectd");
1007 plugin_register_log ("perl", perl_log);
1008 plugin_register_init ("perl", perl_init);
1010 plugin_register_read ("perl", perl_read);
1012 plugin_register_write ("perl", perl_write);
1013 plugin_register_shutdown ("perl", perl_shutdown);
1015 } /* static int init_pi (const char **, const int) */
1018 * LoadPlugin "<Plugin>"
1020 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1022 char module_name[DATA_MAX_NAME_LEN];
1026 if ((0 != ci->children_num) || (1 != ci->values_num)
1027 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1030 value = ci->values[0].value.string;
1032 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1033 log_err ("Invalid module name %s", value);
1037 init_pi (perl_argc, perl_argv);
1038 assert (NULL != perl_threads);
1039 assert (NULL != perl_threads->head);
1041 aTHX = perl_threads->head->interp;
1043 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1044 load_module (PERL_LOADMOD_NOIMPORT,
1045 newSVpv (module_name, strlen (module_name)), Nullsv);
1047 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1052 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1056 if ((0 != ci->children_num) || (1 != ci->values_num)
1057 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1060 value = ci->values[0].value.string;
1062 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1063 strncpy (base_name, value, sizeof (base_name));
1064 base_name[sizeof (base_name) - 1] = '\0';
1066 } /* static int perl_config_basename (oconfig_item_it *) */
1069 * EnableDebugger "<Package>"|""
1071 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1075 if ((0 != ci->children_num) || (1 != ci->values_num)
1076 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1079 value = ci->values[0].value.string;
1081 perl_argv = (char **)realloc (perl_argv,
1082 (++perl_argc + 1) * sizeof (char *));
1084 if (NULL == perl_argv) {
1085 log_err ("perl_config: Not enough memory.");
1089 if ('\0' == value[0]) {
1090 perl_argv[perl_argc - 1] = "-d";
1093 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1094 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1095 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1098 perl_argv[perl_argc] = NULL;
1100 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1103 * IncludeDir "<Dir>"
1105 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1109 if ((0 != ci->children_num) || (1 != ci->values_num)
1110 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1113 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 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1125 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1126 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1128 perl_argv[perl_argc] = NULL;
1131 /* prepend the directory to @INC */
1132 av_unshift (GvAVn (PL_incgv), 1);
1133 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1136 } /* static int perl_config_includedir (oconfig_item_it *) */
1138 static int perl_config (oconfig_item_t *ci)
1144 /* dTHX does not get any valid values in case Perl
1145 * has not been initialized */
1146 if (NULL == perl_threads)
1149 for (i = 0; i < ci->children_num; ++i) {
1150 oconfig_item_t *c = ci->children + i;
1152 if (0 == strcasecmp (c->key, "LoadPlugin"))
1153 perl_config_loadplugin (aTHX_ c);
1154 else if (0 == strcasecmp (c->key, "BaseName"))
1155 perl_config_basename (aTHX_ c);
1156 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1157 perl_config_enabledebugger (aTHX_ c);
1158 else if (0 == strcasecmp (c->key, "IncludeDir"))
1159 perl_config_includedir (aTHX_ c);
1161 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1164 } /* static int perl_config (oconfig_item_t *) */
1166 void module_register (void)
1169 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1171 /* default options for the Perl interpreter */
1173 perl_argv[1] = "-MCollectd";
1174 perl_argv[2] = "-e";
1176 perl_argv[4] = NULL;
1178 plugin_register_complex_config ("perl", perl_config);
1180 } /* void module_register (void) */
1182 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */