1 /**
2 * collectd - src/perl.c
3 * Copyright (C) 2007, 2008 Sebastian Harl
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
25 */
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
30 #include "collectd.h"
32 #include "configfile.h"
34 #include <EXTERN.h>
35 #include <perl.h>
37 #include <XSUB.h>
39 /* Some versions of Perl define their own version of DEBUG... :-/ */
40 #ifdef DEBUG
41 # undef DEBUG
42 #endif /* DEBUG */
44 /* ... while we want the definition found in plugin.h. */
45 #include "plugin.h"
46 #include "common.h"
48 #include <pthread.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
58 #define PLUGIN_INIT 0
59 #define PLUGIN_READ 1
60 #define PLUGIN_WRITE 2
61 #define PLUGIN_SHUTDOWN 3
62 #define PLUGIN_LOG 4
63 #define PLUGIN_NOTIF 5
65 #define PLUGIN_TYPES 6
67 #define PLUGIN_DATASET 255
69 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
70 #define log_info(...) INFO ("perl: " __VA_ARGS__)
71 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
72 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
74 /* this is defined in DynaLoader.a */
75 void boot_DynaLoader (PerlInterpreter *, CV *);
77 static XS (Collectd_plugin_register_ds);
78 static XS (Collectd_plugin_unregister_ds);
79 static XS (Collectd_plugin_dispatch_values);
80 static XS (Collectd_plugin_dispatch_notification);
81 static XS (Collectd_plugin_log);
82 static XS (Collectd_call_by_name);
84 /*
85 * private data types
86 */
88 typedef struct c_ithread_s {
89 /* the thread's Perl interpreter */
90 PerlInterpreter *interp;
92 /* double linked list of threads */
93 struct c_ithread_s *prev;
94 struct c_ithread_s *next;
95 } c_ithread_t;
97 typedef struct {
98 c_ithread_t *head;
99 c_ithread_t *tail;
101 #if COLLECT_DEBUG
102 /* some usage stats */
103 int number_of_threads;
104 #endif /* COLLECT_DEBUG */
106 pthread_mutex_t mutex;
107 } c_ithread_list_t;
109 /*
110 * private variables
111 */
113 /* if perl_threads != NULL perl_threads->head must
114 * point to the "base" thread */
115 static c_ithread_list_t *perl_threads = NULL;
117 /* the key used to store each pthread's ithread */
118 static pthread_key_t perl_thr_key;
120 static int perl_argc = 0;
121 static char **perl_argv = NULL;
123 static char base_name[DATA_MAX_NAME_LEN] = "";
125 static struct {
126 char name[64];
127 XS ((*f));
128 } api[] =
129 {
130 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
131 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
132 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
133 { "Collectd::plugin_dispatch_notification",
134 Collectd_plugin_dispatch_notification },
135 { "Collectd::plugin_log", Collectd_plugin_log },
136 { "Collectd::call_by_name", Collectd_call_by_name },
137 { "", NULL }
138 };
140 struct {
141 char name[64];
142 int value;
143 } constants[] =
144 {
145 { "Collectd::TYPE_INIT", PLUGIN_INIT },
146 { "Collectd::TYPE_READ", PLUGIN_READ },
147 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
148 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
149 { "Collectd::TYPE_LOG", PLUGIN_LOG },
150 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
151 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
152 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
153 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
154 { "Collectd::LOG_ERR", LOG_ERR },
155 { "Collectd::LOG_WARNING", LOG_WARNING },
156 { "Collectd::LOG_NOTICE", LOG_NOTICE },
157 { "Collectd::LOG_INFO", LOG_INFO },
158 { "Collectd::LOG_DEBUG", LOG_DEBUG },
159 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
160 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
161 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
162 { "", 0 }
163 };
165 struct {
166 char name[64];
167 char *var;
168 } g_strings[] =
169 {
170 { "Collectd::hostname_g", hostname_g },
171 { "", NULL }
172 };
174 struct {
175 char name[64];
176 int *var;
177 } g_integers[] =
178 {
179 { "Collectd::interval_g", &interval_g },
180 { "", NULL }
181 };
183 /*
184 * Helper functions for data type conversion.
185 */
187 /*
188 * data source:
189 * [
190 * {
191 * name => $ds_name,
192 * type => $ds_type,
193 * min => $ds_min,
194 * max => $ds_max
195 * },
196 * ...
197 * ]
198 */
199 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
200 {
201 SV **tmp = NULL;
203 if ((NULL == hash) || (NULL == ds))
204 return -1;
206 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
207 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
208 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
209 }
210 else {
211 log_err ("hv2data_source: No DS name given.");
212 return -1;
213 }
215 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
216 ds->type = SvIV (*tmp);
218 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
219 log_err ("hv2data_source: Invalid DS type.");
220 return -1;
221 }
222 }
223 else {
224 ds->type = DS_TYPE_COUNTER;
225 }
227 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
228 ds->min = SvNV (*tmp);
229 else
230 ds->min = NAN;
232 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
233 ds->max = SvNV (*tmp);
234 else
235 ds->max = NAN;
236 return 0;
237 } /* static data_source_t *hv2data_source (HV *) */
239 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
240 {
241 const data_set_t *ds;
243 int i = 0;
245 if ((NULL == name) || (NULL == array) || (NULL == value))
246 return -1;
248 if (av_len (array) < len - 1)
249 len = av_len (array) + 1;
251 if (0 >= len)
252 return -1;
254 ds = plugin_get_ds (name);
255 if (NULL == ds) {
256 log_err ("av2value: Unknown dataset \"%s\"", name);
257 return -1;
258 }
260 if (ds->ds_num < len) {
261 log_warn ("av2value: Value length exceeds data set length.");
262 len = ds->ds_num;
263 }
265 for (i = 0; i < len; ++i) {
266 SV **tmp = av_fetch (array, i, 0);
268 if (NULL != tmp) {
269 if (DS_TYPE_COUNTER == ds->ds[i].type)
270 value[i].counter = SvIV (*tmp);
271 else
272 value[i].gauge = SvNV (*tmp);
273 }
274 else {
275 return -1;
276 }
277 }
278 return len;
279 } /* static int av2value (char *, AV *, value_t *, int) */
281 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
282 {
283 int i = 0;
285 if ((NULL == ds) || (NULL == array))
286 return -1;
288 av_extend (array, ds->ds_num);
290 for (i = 0; i < ds->ds_num; ++i) {
291 HV *source = newHV ();
293 if (NULL == hv_store (source, "name", 4,
294 newSVpv (ds->ds[i].name, 0), 0))
295 return -1;
297 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
298 return -1;
300 if (! isnan (ds->ds[i].min))
301 if (NULL == hv_store (source, "min", 3,
302 newSVnv (ds->ds[i].min), 0))
303 return -1;
305 if (! isnan (ds->ds[i].max))
306 if (NULL == hv_store (source, "max", 3,
307 newSVnv (ds->ds[i].max), 0))
308 return -1;
310 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
311 return -1;
312 }
313 return 0;
314 } /* static int data_set2av (data_set_t *, AV *) */
316 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
317 {
318 AV *values = NULL;
320 int i = 0;
321 int len = 0;
323 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
324 return -1;
326 len = vl->values_len;
328 if (ds->ds_num < len) {
329 log_warn ("value2av: Value length exceeds data set length.");
330 len = ds->ds_num;
331 }
333 values = newAV ();
334 av_extend (values, len - 1);
336 for (i = 0; i < len; ++i) {
337 SV *val = NULL;
339 if (DS_TYPE_COUNTER == ds->ds[i].type)
340 val = newSViv (vl->values[i].counter);
341 else
342 val = newSVnv (vl->values[i].gauge);
344 if (NULL == av_store (values, i, val)) {
345 av_undef (values);
346 return -1;
347 }
348 }
350 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
351 return -1;
353 if (0 != vl->time)
354 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
355 return -1;
357 if ('\0' != vl->host[0])
358 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
359 return -1;
361 if ('\0' != vl->plugin[0])
362 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
363 return -1;
365 if ('\0' != vl->plugin_instance[0])
366 if (NULL == hv_store (hash, "plugin_instance", 15,
367 newSVpv (vl->plugin_instance, 0), 0))
368 return -1;
370 if ('\0' != vl->type[0])
371 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
372 return -1;
374 if ('\0' != vl->type_instance[0])
375 if (NULL == hv_store (hash, "type_instance", 13,
376 newSVpv (vl->type_instance, 0), 0))
377 return -1;
378 return 0;
379 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
381 static int notification2hv (pTHX_ notification_t *n, HV *hash)
382 {
383 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
384 return -1;
386 if (0 != n->time)
387 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
388 return -1;
390 if ('\0' != *n->message)
391 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
392 return -1;
394 if ('\0' != *n->host)
395 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
396 return -1;
398 if ('\0' != *n->plugin)
399 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
400 return -1;
402 if ('\0' != *n->plugin_instance)
403 if (NULL == hv_store (hash, "plugin_instance", 15,
404 newSVpv (n->plugin_instance, 0), 0))
405 return -1;
407 if ('\0' != *n->type)
408 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
409 return -1;
411 if ('\0' != *n->type_instance)
412 if (NULL == hv_store (hash, "type_instance", 13,
413 newSVpv (n->type_instance, 0), 0))
414 return -1;
415 return 0;
416 } /* static int notification2hv (notification_t *, HV *) */
418 /*
419 * Internal functions.
420 */
422 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
423 int status = 0;
424 if (base_name[0] == '\0')
425 status = snprintf (buf, buf_len, "%s", module);
426 else
427 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
428 if ((status < 0) || ((unsigned int)status >= buf_len))
429 return (NULL);
430 buf[buf_len - 1] = '\0';
431 return (buf);
432 } /* char *get_module_name */
434 /*
435 * Add a plugin's data set definition.
436 */
437 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
438 {
439 int len = -1;
440 int ret = 0;
441 int i = 0;
443 data_source_t *ds = NULL;
444 data_set_t *set = NULL;
446 if ((NULL == name) || (NULL == dataset))
447 return -1;
449 len = av_len (dataset);
451 if (-1 == len)
452 return -1;
454 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
455 set = (data_set_t *)smalloc (sizeof (data_set_t));
457 for (i = 0; i <= len; ++i) {
458 SV **elem = av_fetch (dataset, i, 0);
460 if (NULL == elem)
461 return -1;
463 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
464 log_err ("pplugin_register_data_set: Invalid data source.");
465 return -1;
466 }
468 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
469 return -1;
471 log_debug ("pplugin_register_data_set: "
472 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
473 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
474 }
476 strncpy (set->type, name, DATA_MAX_NAME_LEN);
477 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
479 set->ds_num = len + 1;
480 set->ds = ds;
482 ret = plugin_register_data_set (set);
484 free (ds);
485 free (set);
486 return ret;
487 } /* static int pplugin_register_data_set (char *, SV *) */
489 /*
490 * Remove a plugin's data set definition.
491 */
492 static int pplugin_unregister_data_set (char *name)
493 {
494 if (NULL == name)
495 return 0;
496 return plugin_unregister_data_set (name);
497 } /* static int pplugin_unregister_data_set (char *) */
499 /*
500 * Submit the values to the write functions.
501 *
502 * value list:
503 * {
504 * values => [ @values ],
505 * time => $time,
506 * host => $host,
507 * plugin => $plugin,
508 * plugin_instance => $pinstance,
509 * type_instance => $tinstance,
510 * }
511 */
512 static int pplugin_dispatch_values (pTHX_ HV *values)
513 {
514 value_list_t list = VALUE_LIST_INIT;
515 value_t *val = NULL;
517 SV **tmp = NULL;
519 int ret = 0;
521 if (NULL == values)
522 return -1;
524 if (NULL == (tmp = hv_fetch (values, "type", 4, 0))) {
525 log_err ("pplugin_dispatch_values: No type given.");
526 return -1;
527 }
529 strncpy (list.type, SvPV_nolen (*tmp), sizeof (list.type));
530 list.type[DATA_MAX_NAME_LEN - 1] = '\0';
532 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
533 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
534 log_err ("pplugin_dispatch_values: No valid values given.");
535 return -1;
536 }
538 {
539 AV *array = (AV *)SvRV (*tmp);
540 int len = av_len (array) + 1;
542 if (len <= 0)
543 return -1;
545 val = (value_t *)smalloc (len * sizeof (value_t));
547 list.values_len = av2value (aTHX_ list.type, (AV *)SvRV (*tmp),
548 val, len);
549 list.values = val;
551 if (-1 == list.values_len) {
552 sfree (val);
553 return -1;
554 }
555 }
557 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
558 list.time = (time_t)SvIV (*tmp);
559 }
560 else {
561 list.time = time (NULL);
562 }
564 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
565 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
566 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
567 }
568 else {
569 strcpy (list.host, hostname_g);
570 }
572 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
573 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
574 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
575 }
577 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
578 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
579 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
580 }
582 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
583 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
584 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
585 }
587 ret = plugin_dispatch_values (&list);
589 sfree (val);
590 return ret;
591 } /* static int pplugin_dispatch_values (char *, HV *) */
593 /*
594 * Dispatch a notification.
595 *
596 * notification:
597 * {
598 * severity => $severity,
599 * time => $time,
600 * message => $msg,
601 * host => $host,
602 * plugin => $plugin,
603 * type => $type,
604 * plugin_instance => $instance,
605 * type_instance => $type_instance
606 * }
607 */
608 static int pplugin_dispatch_notification (pTHX_ HV *notif)
609 {
610 notification_t n;
612 SV **tmp = NULL;
614 if (NULL == notif)
615 return -1;
617 memset (&n, 0, sizeof (n));
619 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
620 n.severity = SvIV (*tmp);
621 else
622 n.severity = NOTIF_FAILURE;
624 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
625 n.time = (time_t)SvIV (*tmp);
626 else
627 n.time = time (NULL);
629 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
630 strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
631 n.message[sizeof (n.message) - 1] = '\0';
633 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
634 strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
635 else
636 strncpy (n.host, hostname_g, sizeof (n.host));
637 n.host[sizeof (n.host) - 1] = '\0';
639 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
640 strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
641 n.plugin[sizeof (n.plugin) - 1] = '\0';
643 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
644 strncpy (n.plugin_instance, SvPV_nolen (*tmp),
645 sizeof (n.plugin_instance));
646 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
648 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
649 strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
650 n.type[sizeof (n.type) - 1] = '\0';
652 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
653 strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
654 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
655 return plugin_dispatch_notification (&n);
656 } /* static int pplugin_dispatch_notification (HV *) */
658 /*
659 * Call all working functions of the given type.
660 */
661 static int pplugin_call_all (pTHX_ int type, ...)
662 {
663 int retvals = 0;
665 va_list ap;
666 int ret = 0;
668 dSP;
670 if ((type < 0) || (type >= PLUGIN_TYPES))
671 return -1;
673 va_start (ap, type);
675 ENTER;
676 SAVETMPS;
678 PUSHMARK (SP);
680 XPUSHs (sv_2mortal (newSViv ((IV)type)));
682 if (PLUGIN_WRITE == type) {
683 /*
684 * $_[0] = $plugin_type;
685 *
686 * $_[1] =
687 * [
688 * {
689 * name => $ds_name,
690 * type => $ds_type,
691 * min => $ds_min,
692 * max => $ds_max
693 * },
694 * ...
695 * ];
696 *
697 * $_[2] =
698 * {
699 * values => [ $v1, ... ],
700 * time => $time,
701 * host => $hostname,
702 * plugin => $plugin,
703 * type => $type,
704 * plugin_instance => $instance,
705 * type_instance => $type_instance
706 * };
707 */
708 data_set_t *ds;
709 value_list_t *vl;
711 AV *pds = newAV ();
712 HV *pvl = newHV ();
714 ds = va_arg (ap, data_set_t *);
715 vl = va_arg (ap, value_list_t *);
717 if (-1 == data_set2av (aTHX_ ds, pds)) {
718 av_clear (pds);
719 av_undef (pds);
720 pds = Nullav;
721 ret = -1;
722 }
724 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
725 hv_clear (pvl);
726 hv_undef (pvl);
727 pvl = Nullhv;
728 ret = -1;
729 }
731 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
732 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
733 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
734 }
735 else if (PLUGIN_LOG == type) {
736 /*
737 * $_[0] = $level;
738 *
739 * $_[1] = $message;
740 */
741 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
742 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
743 }
744 else if (PLUGIN_NOTIF == type) {
745 /*
746 * $_[0] =
747 * {
748 * severity => $severity,
749 * time => $time,
750 * message => $msg,
751 * host => $host,
752 * plugin => $plugin,
753 * type => $type,
754 * plugin_instance => $instance,
755 * type_instance => $type_instance
756 * };
757 */
758 notification_t *n;
759 HV *notif = newHV ();
761 n = va_arg (ap, notification_t *);
763 if (-1 == notification2hv (aTHX_ n, notif)) {
764 hv_clear (notif);
765 hv_undef (notif);
766 notif = Nullhv;
767 ret = -1;
768 }
770 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
771 }
773 PUTBACK;
775 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
777 SPAGAIN;
778 if (0 < retvals) {
779 SV *tmp = POPs;
780 if (! SvTRUE (tmp))
781 ret = -1;
782 }
784 PUTBACK;
785 FREETMPS;
786 LEAVE;
788 va_end (ap);
789 return ret;
790 } /* static int pplugin_call_all (int, ...) */
792 /*
793 * Exported Perl API.
794 */
796 /*
797 * Collectd::plugin_register_data_set (type, dataset).
798 *
799 * type:
800 * type of the dataset
801 *
802 * dataset:
803 * dataset to be registered
804 */
805 static XS (Collectd_plugin_register_ds)
806 {
807 SV *data = NULL;
808 int ret = 0;
810 dXSARGS;
812 if (2 != items) {
813 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
814 XSRETURN_EMPTY;
815 }
817 log_debug ("Collectd::plugin_register_data_set: "
818 "type = \"%s\", dataset = \"%s\"",
819 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
821 data = ST (1);
823 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
824 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
825 (AV *)SvRV (data));
826 }
827 else {
828 log_err ("Collectd::plugin_register_data_set: Invalid data.");
829 XSRETURN_EMPTY;
830 }
832 if (0 == ret)
833 XSRETURN_YES;
834 else
835 XSRETURN_EMPTY;
836 } /* static XS (Collectd_plugin_register_ds) */
838 /*
839 * Collectd::plugin_unregister_data_set (type).
840 *
841 * type:
842 * type of the dataset
843 */
844 static XS (Collectd_plugin_unregister_ds)
845 {
846 dXSARGS;
848 if (1 != items) {
849 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
850 XSRETURN_EMPTY;
851 }
853 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
854 SvPV_nolen (ST (0)));
856 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
857 XSRETURN_YES;
858 else
859 XSRETURN_EMPTY;
860 } /* static XS (Collectd_plugin_register_ds) */
862 /*
863 * Collectd::plugin_dispatch_values (name, values).
864 *
865 * name:
866 * name of the plugin
867 *
868 * values:
869 * value list to submit
870 */
871 static XS (Collectd_plugin_dispatch_values)
872 {
873 SV *values = NULL;
874 int values_idx = 0;
876 int ret = 0;
878 dXSARGS;
880 if (2 == items) {
881 log_warn ("Collectd::plugin_dispatch_values with two arguments "
882 "is deprecated - pass the type through values->{type}.");
883 values_idx = 1;
884 }
885 else if (1 != items) {
886 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
887 XSRETURN_EMPTY;
888 }
890 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
891 SvPV_nolen (ST (values_idx)));
893 values = ST (values_idx);
895 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
896 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
897 XSRETURN_EMPTY;
898 }
900 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
901 XSRETURN_EMPTY;
903 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
904 newSVsv (ST (0)), 0))) {
905 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
906 XSRETURN_EMPTY;
907 }
909 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
911 if (0 == ret)
912 XSRETURN_YES;
913 else
914 XSRETURN_EMPTY;
915 } /* static XS (Collectd_plugin_dispatch_values) */
917 /*
918 * Collectd::plugin_dispatch_notification (notif).
919 *
920 * notif:
921 * notification to dispatch
922 */
923 static XS (Collectd_plugin_dispatch_notification)
924 {
925 SV *notif = NULL;
927 int ret = 0;
929 dXSARGS;
931 if (1 != items) {
932 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
933 XSRETURN_EMPTY;
934 }
936 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
937 SvPV_nolen (ST (0)));
939 notif = ST (0);
941 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
942 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
943 XSRETURN_EMPTY;
944 }
946 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
948 if (0 == ret)
949 XSRETURN_YES;
950 else
951 XSRETURN_EMPTY;
952 } /* static XS (Collectd_plugin_dispatch_notification) */
954 /*
955 * Collectd::plugin_log (level, message).
956 *
957 * level:
958 * log level (LOG_DEBUG, ... LOG_ERR)
959 *
960 * message:
961 * log message
962 */
963 static XS (Collectd_plugin_log)
964 {
965 dXSARGS;
967 if (2 != items) {
968 log_err ("Usage: Collectd::plugin_log(level, message)");
969 XSRETURN_EMPTY;
970 }
972 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
973 XSRETURN_YES;
974 } /* static XS (Collectd_plugin_log) */
976 /*
977 * Collectd::call_by_name (...).
978 *
979 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
980 */
981 static XS (Collectd_call_by_name)
982 {
983 SV *tmp = NULL;
984 char *name = NULL;
986 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
987 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
988 CLEAR_STACK_FRAME;
989 return;
990 }
992 name = SvPV_nolen (tmp);
994 if (NULL == get_cv (name, 0)) {
995 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
996 CLEAR_STACK_FRAME;
997 return;
998 }
1000 /* simply pass on the subroutine call without touching the stack,
1001 * thus leaving any arguments and return values in place */
1002 call_pv (name, 0);
1003 } /* static XS (Collectd_call_by_name) */
1005 /*
1006 * collectd's perl interpreter based thread implementation.
1007 *
1008 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1009 */
1011 /* must be called with perl_threads->mutex locked */
1012 static void c_ithread_destroy (c_ithread_t *ithread)
1013 {
1014 dTHXa (ithread->interp);
1016 assert (NULL != perl_threads);
1018 PERL_SET_CONTEXT (aTHX);
1019 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1021 #if COLLECT_DEBUG
1022 sv_report_used ();
1024 --perl_threads->number_of_threads;
1025 #endif /* COLLECT_DEBUG */
1027 perl_destruct (aTHX);
1028 perl_free (aTHX);
1030 if (NULL == ithread->prev)
1031 perl_threads->head = ithread->next;
1032 else
1033 ithread->prev->next = ithread->next;
1035 if (NULL == ithread->next)
1036 perl_threads->tail = ithread->prev;
1037 else
1038 ithread->next->prev = ithread->prev;
1040 sfree (ithread);
1041 return;
1042 } /* static void c_ithread_destroy (c_ithread_t *) */
1044 static void c_ithread_destructor (void *arg)
1045 {
1046 c_ithread_t *ithread = (c_ithread_t *)arg;
1047 c_ithread_t *t = NULL;
1049 if (NULL == perl_threads)
1050 return;
1052 pthread_mutex_lock (&perl_threads->mutex);
1054 for (t = perl_threads->head; NULL != t; t = t->next)
1055 if (t == ithread)
1056 break;
1058 /* the ithread no longer exists */
1059 if (NULL == t)
1060 return;
1062 c_ithread_destroy (ithread);
1064 pthread_mutex_unlock (&perl_threads->mutex);
1065 return;
1066 } /* static void c_ithread_destructor (void *) */
1068 /* must be called with perl_threads->mutex locked */
1069 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1070 {
1071 c_ithread_t *t = NULL;
1072 dTHXa (NULL);
1074 assert (NULL != perl_threads);
1076 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1077 memset (t, 0, sizeof (c_ithread_t));
1079 t->interp = (NULL == base)
1080 ? NULL
1081 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1083 aTHX = t->interp;
1085 if (NULL != base) {
1086 av_clear (PL_endav);
1087 av_undef (PL_endav);
1088 PL_endav = Nullav;
1089 }
1091 #if COLLECT_DEBUG
1092 ++perl_threads->number_of_threads;
1093 #endif /* COLLECT_DEBUG */
1095 t->next = NULL;
1097 if (NULL == perl_threads->tail) {
1098 perl_threads->head = t;
1099 t->prev = NULL;
1100 }
1101 else {
1102 perl_threads->tail->next = t;
1103 t->prev = perl_threads->tail;
1104 }
1106 perl_threads->tail = t;
1108 pthread_setspecific (perl_thr_key, (const void *)t);
1109 return t;
1110 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1112 /*
1113 * Interface to collectd.
1114 */
1116 static int perl_init (void)
1117 {
1118 dTHX;
1120 if (NULL == perl_threads)
1121 return 0;
1123 if (NULL == aTHX) {
1124 c_ithread_t *t = NULL;
1126 pthread_mutex_lock (&perl_threads->mutex);
1127 t = c_ithread_create (perl_threads->head->interp);
1128 pthread_mutex_unlock (&perl_threads->mutex);
1130 aTHX = t->interp;
1131 }
1133 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1134 aTHX, perl_threads->number_of_threads);
1135 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1136 } /* static int perl_init (void) */
1138 static int perl_read (void)
1139 {
1140 dTHX;
1142 if (NULL == perl_threads)
1143 return 0;
1145 if (NULL == aTHX) {
1146 c_ithread_t *t = NULL;
1148 pthread_mutex_lock (&perl_threads->mutex);
1149 t = c_ithread_create (perl_threads->head->interp);
1150 pthread_mutex_unlock (&perl_threads->mutex);
1152 aTHX = t->interp;
1153 }
1155 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1156 aTHX, perl_threads->number_of_threads);
1157 return pplugin_call_all (aTHX_ PLUGIN_READ);
1158 } /* static int perl_read (void) */
1160 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1161 {
1162 dTHX;
1164 if (NULL == perl_threads)
1165 return 0;
1167 if (NULL == aTHX) {
1168 c_ithread_t *t = NULL;
1170 pthread_mutex_lock (&perl_threads->mutex);
1171 t = c_ithread_create (perl_threads->head->interp);
1172 pthread_mutex_unlock (&perl_threads->mutex);
1174 aTHX = t->interp;
1175 }
1177 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1178 aTHX, perl_threads->number_of_threads);
1179 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1180 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1182 static void perl_log (int level, const char *msg)
1183 {
1184 dTHX;
1186 if (NULL == perl_threads)
1187 return;
1189 if (NULL == aTHX) {
1190 c_ithread_t *t = NULL;
1192 pthread_mutex_lock (&perl_threads->mutex);
1193 t = c_ithread_create (perl_threads->head->interp);
1194 pthread_mutex_unlock (&perl_threads->mutex);
1196 aTHX = t->interp;
1197 }
1199 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1200 return;
1201 } /* static void perl_log (int, const char *) */
1203 static int perl_notify (const notification_t *notif)
1204 {
1205 dTHX;
1207 if (NULL == perl_threads)
1208 return 0;
1210 if (NULL == aTHX) {
1211 c_ithread_t *t = NULL;
1213 pthread_mutex_lock (&perl_threads->mutex);
1214 t = c_ithread_create (perl_threads->head->interp);
1215 pthread_mutex_unlock (&perl_threads->mutex);
1217 aTHX = t->interp;
1218 }
1219 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1220 } /* static int perl_notify (const notification_t *) */
1222 static int perl_shutdown (void)
1223 {
1224 c_ithread_t *t = NULL;
1226 int ret = 0;
1228 dTHX;
1230 plugin_unregister_complex_config ("perl");
1232 if (NULL == perl_threads)
1233 return 0;
1235 if (NULL == aTHX) {
1236 c_ithread_t *t = NULL;
1238 pthread_mutex_lock (&perl_threads->mutex);
1239 t = c_ithread_create (perl_threads->head->interp);
1240 pthread_mutex_unlock (&perl_threads->mutex);
1242 aTHX = t->interp;
1243 }
1245 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1246 aTHX, perl_threads->number_of_threads);
1248 plugin_unregister_log ("perl");
1249 plugin_unregister_notification ("perl");
1250 plugin_unregister_init ("perl");
1251 plugin_unregister_read ("perl");
1252 plugin_unregister_write ("perl");
1254 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1256 pthread_mutex_lock (&perl_threads->mutex);
1257 t = perl_threads->tail;
1259 while (NULL != t) {
1260 c_ithread_t *thr = t;
1262 /* the pointer has to be advanced before destroying
1263 * the thread as this will free the memory */
1264 t = t->prev;
1266 c_ithread_destroy (thr);
1267 }
1269 pthread_mutex_unlock (&perl_threads->mutex);
1270 pthread_mutex_destroy (&perl_threads->mutex);
1272 sfree (perl_threads);
1274 pthread_key_delete (perl_thr_key);
1276 PERL_SYS_TERM ();
1278 plugin_unregister_shutdown ("perl");
1279 return ret;
1280 } /* static void perl_shutdown (void) */
1282 /*
1283 * Access functions for global variables.
1284 *
1285 * These functions implement the "magic" used to access
1286 * the global variables from Perl.
1287 */
1289 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1290 {
1291 char *pv = mg->mg_ptr;
1292 sv_setpv (var, pv);
1293 return 0;
1294 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1296 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1297 {
1298 char *pv = mg->mg_ptr;
1299 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1300 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1301 return 0;
1302 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1304 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1305 {
1306 int *iv = (int *)mg->mg_ptr;
1307 sv_setiv (var, *iv);
1308 return 0;
1309 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1311 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1312 {
1313 int *iv = (int *)mg->mg_ptr;
1314 *iv = (int)SvIV (var);
1315 return 0;
1316 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1318 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1319 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1321 /* bootstrap the Collectd module */
1322 static void xs_init (pTHX)
1323 {
1324 HV *stash = NULL;
1325 SV *tmp = NULL;
1326 char *file = __FILE__;
1328 int i = 0;
1330 dXSUB_SYS;
1332 /* enable usage of Perl modules using shared libraries */
1333 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1335 /* register API */
1336 for (i = 0; NULL != api[i].f; ++i)
1337 newXS (api[i].name, api[i].f, file);
1339 stash = gv_stashpv ("Collectd", 1);
1341 /* export "constants" */
1342 for (i = 0; '\0' != constants[i].name[0]; ++i)
1343 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1345 /* export global variables
1346 * by adding "magic" to the SV's representing the globale variables
1347 * perl is able to automagically call the get/set function when
1348 * accessing any such variable (this is basically the same as using
1349 * tie() in Perl) */
1350 /* global strings */
1351 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1352 tmp = get_sv (g_strings[i].name, 1);
1353 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1354 g_strings[i].var, 0);
1355 }
1357 /* global integers */
1358 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1359 tmp = get_sv (g_integers[i].name, 1);
1360 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1361 (char *)g_integers[i].var, 0);
1362 }
1363 return;
1364 } /* static void xs_init (pTHX) */
1366 /* Initialize the global Perl interpreter. */
1367 static int init_pi (int argc, char **argv)
1368 {
1369 dTHXa (NULL);
1371 if (NULL != perl_threads)
1372 return 0;
1374 log_info ("Initializing Perl interpreter...");
1375 #if COLLECT_DEBUG
1376 {
1377 int i = 0;
1379 for (i = 0; i < argc; ++i)
1380 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1381 }
1382 #endif /* COLLECT_DEBUG */
1384 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1385 log_err ("init_pi: pthread_key_create failed");
1387 /* this must not happen - cowardly giving up if it does */
1388 exit (1);
1389 }
1391 PERL_SYS_INIT3 (&argc, &argv, &environ);
1393 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1394 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1396 pthread_mutex_init (&perl_threads->mutex, NULL);
1397 /* locking the mutex should not be necessary at this point
1398 * but let's just do it for the sake of completeness */
1399 pthread_mutex_lock (&perl_threads->mutex);
1401 perl_threads->head = c_ithread_create (NULL);
1402 perl_threads->tail = perl_threads->head;
1404 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1405 log_err ("init_pi: Not enough memory.");
1406 exit (3);
1407 }
1409 aTHX = perl_threads->head->interp;
1410 pthread_mutex_unlock (&perl_threads->mutex);
1412 perl_construct (aTHX);
1414 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1416 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1417 log_err ("init_pi: Unable to bootstrap Collectd.");
1418 exit (1);
1419 }
1421 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1422 sv_setpv (get_sv ("0", 0), "collectd");
1424 perl_run (aTHX);
1426 plugin_register_log ("perl", perl_log);
1427 plugin_register_notification ("perl", perl_notify);
1428 plugin_register_init ("perl", perl_init);
1430 plugin_register_read ("perl", perl_read);
1432 plugin_register_write ("perl", perl_write);
1433 plugin_register_shutdown ("perl", perl_shutdown);
1434 return 0;
1435 } /* static int init_pi (const char **, const int) */
1437 /*
1438 * LoadPlugin "<Plugin>"
1439 */
1440 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1441 {
1442 char module_name[DATA_MAX_NAME_LEN];
1444 char *value = NULL;
1446 if ((0 != ci->children_num) || (1 != ci->values_num)
1447 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1448 log_err ("LoadPlugin expects a single string argument.");
1449 return 1;
1450 }
1452 value = ci->values[0].value.string;
1454 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1455 log_err ("Invalid module name %s", value);
1456 return (1);
1457 }
1459 init_pi (perl_argc, perl_argv);
1460 assert (NULL != perl_threads);
1461 assert (NULL != perl_threads->head);
1463 aTHX = perl_threads->head->interp;
1465 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1466 load_module (PERL_LOADMOD_NOIMPORT,
1467 newSVpv (module_name, strlen (module_name)), Nullsv);
1468 return 0;
1469 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1471 /*
1472 * BaseName "<Name>"
1473 */
1474 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1475 {
1476 char *value = NULL;
1478 if ((0 != ci->children_num) || (1 != ci->values_num)
1479 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1480 log_err ("BaseName expects a single string argument.");
1481 return 1;
1482 }
1484 value = ci->values[0].value.string;
1486 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1487 strncpy (base_name, value, sizeof (base_name));
1488 base_name[sizeof (base_name) - 1] = '\0';
1489 return 0;
1490 } /* static int perl_config_basename (oconfig_item_it *) */
1492 /*
1493 * EnableDebugger "<Package>"|""
1494 */
1495 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1496 {
1497 char *value = NULL;
1499 if ((0 != ci->children_num) || (1 != ci->values_num)
1500 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1501 log_err ("EnableDebugger expects a single string argument.");
1502 return 1;
1503 }
1505 if (NULL != perl_threads) {
1506 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1507 return 1;
1508 }
1510 value = ci->values[0].value.string;
1512 perl_argv = (char **)realloc (perl_argv,
1513 (++perl_argc + 1) * sizeof (char *));
1515 if (NULL == perl_argv) {
1516 log_err ("perl_config: Not enough memory.");
1517 exit (3);
1518 }
1520 if ('\0' == value[0]) {
1521 perl_argv[perl_argc - 1] = "-d";
1522 }
1523 else {
1524 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1525 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1526 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1527 }
1529 perl_argv[perl_argc] = NULL;
1530 return 0;
1531 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1533 /*
1534 * IncludeDir "<Dir>"
1535 */
1536 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1537 {
1538 char *value = NULL;
1540 if ((0 != ci->children_num) || (1 != ci->values_num)
1541 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1542 log_err ("IncludeDir expects a single string argument.");
1543 return 1;
1544 }
1546 value = ci->values[0].value.string;
1548 if (NULL == aTHX) {
1549 perl_argv = (char **)realloc (perl_argv,
1550 (++perl_argc + 1) * sizeof (char *));
1552 if (NULL == perl_argv) {
1553 log_err ("perl_config: Not enough memory.");
1554 exit (3);
1555 }
1557 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1558 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1559 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1561 perl_argv[perl_argc] = NULL;
1562 }
1563 else {
1564 /* prepend the directory to @INC */
1565 av_unshift (GvAVn (PL_incgv), 1);
1566 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1567 }
1568 return 0;
1569 } /* static int perl_config_includedir (oconfig_item_it *) */
1571 static int perl_config (oconfig_item_t *ci)
1572 {
1573 int i = 0;
1575 dTHX;
1577 /* dTHX does not get any valid values in case Perl
1578 * has not been initialized */
1579 if (NULL == perl_threads)
1580 aTHX = NULL;
1582 for (i = 0; i < ci->children_num; ++i) {
1583 oconfig_item_t *c = ci->children + i;
1585 if (0 == strcasecmp (c->key, "LoadPlugin"))
1586 perl_config_loadplugin (aTHX_ c);
1587 else if (0 == strcasecmp (c->key, "BaseName"))
1588 perl_config_basename (aTHX_ c);
1589 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1590 perl_config_enabledebugger (aTHX_ c);
1591 else if (0 == strcasecmp (c->key, "IncludeDir"))
1592 perl_config_includedir (aTHX_ c);
1593 else
1594 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1595 }
1596 return 0;
1597 } /* static int perl_config (oconfig_item_t *) */
1599 void module_register (void)
1600 {
1601 perl_argc = 4;
1602 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1604 /* default options for the Perl interpreter */
1605 perl_argv[0] = "";
1606 perl_argv[1] = "-MCollectd";
1607 perl_argv[2] = "-e";
1608 perl_argv[3] = "1";
1609 perl_argv[4] = NULL;
1611 plugin_register_complex_config ("perl", perl_config);
1612 return;
1613 } /* void module_register (void) */
1615 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */