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;
94 pthread_mutex_t mutex;
101 /* if perl_threads != NULL perl_threads->head must
102 * point to the "base" thread */
103 static c_ithread_list_t *perl_threads = NULL;
105 static int perl_argc = 0;
106 static char **perl_argv = NULL;
108 static char base_name[DATA_MAX_NAME_LEN] = "";
115 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
116 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
117 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
118 { "Collectd::plugin_log", Collectd_plugin_log },
127 { "Collectd::TYPE_INIT", PLUGIN_INIT },
128 { "Collectd::TYPE_READ", PLUGIN_READ },
129 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
130 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
131 { "Collectd::TYPE_LOG", PLUGIN_LOG },
132 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
133 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
134 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
135 { "Collectd::LOG_ERR", LOG_ERR },
136 { "Collectd::LOG_WARNING", LOG_WARNING },
137 { "Collectd::LOG_NOTICE", LOG_NOTICE },
138 { "Collectd::LOG_INFO", LOG_INFO },
139 { "Collectd::LOG_DEBUG", LOG_DEBUG },
144 * Helper functions for data type conversion.
159 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
163 if ((NULL == hash) || (NULL == ds))
166 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
167 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
168 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
171 log_err ("hv2data_source: No DS name given.");
175 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
176 ds->type = SvIV (*tmp);
178 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
179 log_err ("hv2data_source: Invalid DS type.");
184 ds->type = DS_TYPE_COUNTER;
187 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
188 ds->min = SvNV (*tmp);
192 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
193 ds->max = SvNV (*tmp);
197 } /* static data_source_t *hv2data_source (HV *) */
199 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
201 const data_set_t *ds;
205 if ((NULL == name) || (NULL == array) || (NULL == value))
208 if (av_len (array) < len - 1)
209 len = av_len (array) + 1;
214 ds = plugin_get_ds (name);
216 log_err ("av2value: Unknown dataset \"%s\"", name);
220 if (ds->ds_num < len) {
221 log_warn ("av2value: Value length exceeds data set length.");
225 for (i = 0; i < len; ++i) {
226 SV **tmp = av_fetch (array, i, 0);
229 if (DS_TYPE_COUNTER == ds->ds[i].type)
230 value[i].counter = SvIV (*tmp);
232 value[i].gauge = SvNV (*tmp);
239 } /* static int av2value (char *, AV *, value_t *, int) */
241 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
245 if ((NULL == ds) || (NULL == array))
248 av_extend (array, ds->ds_num);
250 for (i = 0; i < ds->ds_num; ++i) {
251 HV *source = newHV ();
253 if (NULL == hv_store (source, "name", 4,
254 newSVpv (ds->ds[i].name, 0), 0))
257 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
260 if (! isnan (ds->ds[i].min))
261 if (NULL == hv_store (source, "min", 3,
262 newSVnv (ds->ds[i].min), 0))
265 if (! isnan (ds->ds[i].max))
266 if (NULL == hv_store (source, "max", 3,
267 newSVnv (ds->ds[i].max), 0))
270 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
274 } /* static int data_set2av (data_set_t *, AV *) */
276 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
283 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
286 len = vl->values_len;
288 if (ds->ds_num < len) {
289 log_warn ("value2av: Value length exceeds data set length.");
294 av_extend (values, len - 1);
296 for (i = 0; i < len; ++i) {
299 if (DS_TYPE_COUNTER == ds->ds[i].type)
300 val = newSViv (vl->values[i].counter);
302 val = newSVnv (vl->values[i].gauge);
304 if (NULL == av_store (values, i, val)) {
310 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
314 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
317 if ('\0' != vl->host[0])
318 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
321 if ('\0' != vl->plugin[0])
322 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
325 if ('\0' != vl->plugin_instance[0])
326 if (NULL == hv_store (hash, "plugin_instance", 15,
327 newSVpv (vl->plugin_instance, 0), 0))
330 if ('\0' != vl->type_instance[0])
331 if (NULL == hv_store (hash, "type_instance", 13,
332 newSVpv (vl->type_instance, 0), 0))
335 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
338 * Internal functions.
341 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
343 if (base_name[0] == '\0')
344 status = snprintf (buf, buf_len, "%s", module);
346 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
347 if ((status < 0) || (status >= buf_len))
349 buf[buf_len - 1] = '\0';
351 } /* char *get_module_name */
354 * Add a plugin's data set definition.
356 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
361 data_source_t *ds = NULL;
362 data_set_t *set = NULL;
364 if ((NULL == name) || (NULL == dataset))
367 len = av_len (dataset);
372 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
373 set = (data_set_t *)smalloc (sizeof (data_set_t));
375 for (i = 0; i <= len; ++i) {
376 SV **elem = av_fetch (dataset, i, 0);
381 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
382 log_err ("pplugin_register_data_set: Invalid data source.");
386 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
389 log_debug ("pplugin_register_data_set: "
390 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
391 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
394 strncpy (set->type, name, DATA_MAX_NAME_LEN);
395 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
397 set->ds_num = len + 1;
399 return plugin_register_data_set (set);
400 } /* static int pplugin_register_data_set (char *, SV *) */
403 * Remove a plugin's data set definition.
405 static int pplugin_unregister_data_set (char *name)
409 return plugin_unregister_data_set (name);
410 } /* static int pplugin_unregister_data_set (char *) */
413 * Submit the values to the write functions.
417 * values => [ @values ],
421 * plugin_instance => $pinstance,
422 * type_instance => $tinstance,
425 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
427 value_list_t list = VALUE_LIST_INIT;
434 if ((NULL == name) || (NULL == values))
437 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
438 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
439 log_err ("pplugin_dispatch_values: No valid values given.");
444 AV *array = (AV *)SvRV (*tmp);
445 int len = av_len (array) + 1;
450 val = (value_t *)smalloc (len * sizeof (value_t));
452 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
455 if (-1 == list.values_len) {
461 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
462 list.time = (time_t)SvIV (*tmp);
465 list.time = time (NULL);
468 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
469 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
470 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
473 strcpy (list.host, hostname_g);
476 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
477 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
478 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
481 if (NULL != (tmp = hv_fetch (values,
482 "plugin_instance", 15, 0))) {
483 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
484 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
487 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
488 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
489 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
492 ret = plugin_dispatch_values (name, &list);
496 } /* static int pplugin_dispatch_values (char *, HV *) */
499 * Call all working functions of the given type.
501 static int pplugin_call_all (pTHX_ int type, ...)
510 if ((type < 0) || (type >= PLUGIN_TYPES))
520 XPUSHs (sv_2mortal (newSViv ((IV)type)));
522 if (PLUGIN_WRITE == type) {
524 * $_[0] = $plugin_type;
539 * values => [ $v1, ... ],
543 * plugin_instance => $instance,
544 * type_instance => $type_instance
553 ds = va_arg (ap, data_set_t *);
554 vl = va_arg (ap, value_list_t *);
556 if (-1 == data_set2av (aTHX_ ds, pds))
559 if (-1 == value_list2hv (aTHX_ vl, ds, pvl))
562 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
563 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
564 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
566 else if (PLUGIN_LOG == type) {
572 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
573 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
578 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
593 } /* static int pplugin_call_all (int, ...) */
600 * Collectd::plugin_register_data_set (type, dataset).
603 * type of the dataset
606 * dataset to be registered
608 static XS (Collectd_plugin_register_ds)
616 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
620 log_debug ("Collectd::plugin_register_data_set: "
621 "type = \"%s\", dataset = \"%s\"",
622 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
626 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
627 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
631 log_err ("Collectd::plugin_register_data_set: Invalid data.");
639 } /* static XS (Collectd_plugin_register_ds) */
642 * Collectd::plugin_unregister_data_set (type).
645 * type of the dataset
647 static XS (Collectd_plugin_unregister_ds)
652 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
656 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
657 SvPV_nolen (ST (0)));
659 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
663 } /* static XS (Collectd_plugin_register_ds) */
666 * Collectd::plugin_dispatch_values (name, values).
672 * value list to submit
674 static XS (Collectd_plugin_dispatch_values)
683 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
687 log_debug ("Collectd::plugin_dispatch_values: "
688 "name = \"%s\", values=\"%s\"",
689 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
693 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
694 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
698 if ((NULL == ST (0)) || (NULL == values))
701 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
702 (HV *)SvRV (values));
708 } /* static XS (Collectd_plugin_dispatch_values) */
711 * Collectd::plugin_log (level, message).
714 * log level (LOG_DEBUG, ... LOG_ERR)
719 static XS (Collectd_plugin_log)
724 log_err ("Usage: Collectd::plugin_log(level, message)");
728 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
730 } /* static XS (Collectd_plugin_log) */
733 * collectd's perl interpreter based thread implementation.
735 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
738 /* must be called with perl_threads->mutex locked */
739 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
741 c_ithread_t *t = NULL;
743 assert (NULL != perl_threads);
745 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
746 memset (t, 0, sizeof (c_ithread_t));
748 t->interp = (NULL == base)
750 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
754 if (NULL == perl_threads->tail) {
755 perl_threads->head = t;
759 perl_threads->tail->next = t;
760 t->prev = perl_threads->tail;
763 perl_threads->tail = t;
765 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
768 * Interface to collectd.
771 static int perl_init (void)
775 if (NULL == perl_threads)
779 c_ithread_t *t = NULL;
781 pthread_mutex_lock (&perl_threads->mutex);
782 t = c_ithread_create (perl_threads->head->interp);
783 pthread_mutex_unlock (&perl_threads->mutex);
787 return pplugin_call_all (aTHX_ PLUGIN_INIT);
788 } /* static int perl_init (void) */
790 static int perl_read (void)
794 if (NULL == perl_threads)
798 c_ithread_t *t = NULL;
800 pthread_mutex_lock (&perl_threads->mutex);
801 t = c_ithread_create (perl_threads->head->interp);
802 pthread_mutex_unlock (&perl_threads->mutex);
806 return pplugin_call_all (aTHX_ PLUGIN_READ);
807 } /* static int perl_read (void) */
809 static int perl_write (const data_set_t *ds, const value_list_t *vl)
813 if (NULL == perl_threads)
817 c_ithread_t *t = NULL;
819 pthread_mutex_lock (&perl_threads->mutex);
820 t = c_ithread_create (perl_threads->head->interp);
821 pthread_mutex_unlock (&perl_threads->mutex);
825 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
826 } /* static int perl_write (const data_set_t *, const value_list_t *) */
828 static void perl_log (int level, const char *msg)
832 if (NULL == perl_threads)
836 c_ithread_t *t = NULL;
838 pthread_mutex_lock (&perl_threads->mutex);
839 t = c_ithread_create (perl_threads->head->interp);
840 pthread_mutex_unlock (&perl_threads->mutex);
845 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
847 } /* static void perl_log (int, const char *) */
849 static int perl_shutdown (void)
851 c_ithread_t *t = NULL;
857 plugin_unregister_complex_config ("perl");
859 if (NULL == perl_threads)
863 c_ithread_t *t = NULL;
865 pthread_mutex_lock (&perl_threads->mutex);
866 t = c_ithread_create (perl_threads->head->interp);
867 pthread_mutex_unlock (&perl_threads->mutex);
872 plugin_unregister_log ("perl");
873 plugin_unregister_init ("perl");
874 plugin_unregister_read ("perl");
875 plugin_unregister_write ("perl");
877 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
879 pthread_mutex_lock (&perl_threads->mutex);
880 t = perl_threads->tail;
884 PERL_SET_CONTEXT (aTHX);
888 #endif /* COLLECT_DEBUG */
890 perl_destruct (aTHX);
898 pthread_mutex_unlock (&perl_threads->mutex);
900 sfree (perl_threads);
904 plugin_unregister_shutdown ("perl");
906 } /* static void perl_shutdown (void) */
908 /* bootstrap the Collectd module */
909 static void xs_init (pTHX)
912 char *file = __FILE__;
918 /* enable usage of Perl modules using shared libraries */
919 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
922 for (i = 0; NULL != api[i].f; ++i)
923 newXS (api[i].name, api[i].f, file);
925 stash = gv_stashpv ("Collectd", 1);
927 /* export "constants" */
928 for (i = 0; '\0' != constants[i].name[0]; ++i)
929 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
931 } /* static void xs_init (pTHX) */
933 /* Initialize the global Perl interpreter. */
934 static int init_pi (int argc, char **argv)
938 if (NULL != perl_threads)
941 log_info ("Initializing Perl interpreter...");
946 for (i = 0; i < argc; ++i)
947 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
949 #endif /* COLLECT_DEBUG */
951 PERL_SYS_INIT3 (&argc, &argv, &environ);
953 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
954 memset (perl_threads, 0, sizeof (c_ithread_list_t));
956 pthread_mutex_init (&perl_threads->mutex, NULL);
957 /* locking the mutex should not be necessary at this point
958 * but let's just do it for the sake of completeness */
959 pthread_mutex_lock (&perl_threads->mutex);
961 perl_threads->head = c_ithread_create (NULL);
962 perl_threads->tail = perl_threads->head;
964 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
965 log_err ("module_register: Not enough memory.");
969 aTHX = perl_threads->head->interp;
970 pthread_mutex_unlock (&perl_threads->mutex);
972 perl_construct (aTHX);
974 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
976 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
977 log_err ("module_register: Unable to bootstrap Collectd.");
981 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
982 sv_setpv (get_sv ("0", 0), "collectd");
986 plugin_register_log ("perl", perl_log);
987 plugin_register_init ("perl", perl_init);
989 plugin_register_read ("perl", perl_read);
991 plugin_register_write ("perl", perl_write);
992 plugin_register_shutdown ("perl", perl_shutdown);
994 } /* static int init_pi (const char **, const int) */
997 * LoadPlugin "<Plugin>"
999 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1001 char module_name[DATA_MAX_NAME_LEN];
1005 if ((0 != ci->children_num) || (1 != ci->values_num)
1006 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1009 value = ci->values[0].value.string;
1011 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1012 log_err ("Invalid module name %s", value);
1016 init_pi (perl_argc, perl_argv);
1017 assert (NULL != perl_threads);
1018 assert (NULL != perl_threads->head);
1020 aTHX = perl_threads->head->interp;
1022 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1023 load_module (PERL_LOADMOD_NOIMPORT,
1024 newSVpv (module_name, strlen (module_name)), Nullsv);
1026 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1031 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1035 if ((0 != ci->children_num) || (1 != ci->values_num)
1036 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1039 value = ci->values[0].value.string;
1041 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1042 strncpy (base_name, value, sizeof (base_name));
1043 base_name[sizeof (base_name) - 1] = '\0';
1045 } /* static int perl_config_basename (oconfig_item_it *) */
1048 * EnableDebugger "<Package>"|""
1050 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1054 if ((0 != ci->children_num) || (1 != ci->values_num)
1055 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1058 value = ci->values[0].value.string;
1060 perl_argv = (char **)realloc (perl_argv,
1061 (++perl_argc + 1) * sizeof (char *));
1063 if (NULL == perl_argv) {
1064 log_err ("perl_config: Not enough memory.");
1068 if ('\0' == value[0]) {
1069 perl_argv[perl_argc - 1] = "-d";
1072 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1073 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1074 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1077 perl_argv[perl_argc] = NULL;
1079 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1082 * IncludeDir "<Dir>"
1084 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1088 if ((0 != ci->children_num) || (1 != ci->values_num)
1089 || (OCONFIG_TYPE_STRING != ci->values[0].type))
1092 value = ci->values[0].value.string;
1095 perl_argv = (char **)realloc (perl_argv,
1096 (++perl_argc + 1) * sizeof (char *));
1098 if (NULL == perl_argv) {
1099 log_err ("perl_config: Not enough memory.");
1103 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1104 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1105 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1107 perl_argv[perl_argc] = NULL;
1110 /* prepend the directory to @INC */
1111 av_unshift (GvAVn (PL_incgv), 1);
1112 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1115 } /* static int perl_config_includedir (oconfig_item_it *) */
1117 static int perl_config (oconfig_item_t *ci)
1123 /* dTHX does not get any valid values in case Perl
1124 * has not been initialized */
1125 if (NULL == perl_threads)
1128 for (i = 0; i < ci->children_num; ++i) {
1129 oconfig_item_t *c = ci->children + i;
1131 if (0 == strcasecmp (c->key, "LoadPlugin"))
1132 perl_config_loadplugin (aTHX_ c);
1133 else if (0 == strcasecmp (c->key, "BaseName"))
1134 perl_config_basename (aTHX_ c);
1135 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1136 perl_config_enabledebugger (aTHX_ c);
1137 else if (0 == strcasecmp (c->key, "IncludeDir"))
1138 perl_config_includedir (aTHX_ c);
1140 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1143 } /* static int perl_config (oconfig_item_t *) */
1145 void module_register (void)
1148 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1150 /* default options for the Perl interpreter */
1152 perl_argv[1] = "-MCollectd";
1153 perl_argv[2] = "-e";
1155 perl_argv[4] = NULL;
1157 plugin_register_complex_config ("perl", perl_config);
1159 } /* void module_register (void) */
1161 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */