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
64 #define PLUGIN_FLUSH 6
66 #define PLUGIN_TYPES 7
68 #define PLUGIN_DATASET 255
70 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
71 #define log_info(...) INFO ("perl: " __VA_ARGS__)
72 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
73 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
75 /* this is defined in DynaLoader.a */
76 void boot_DynaLoader (PerlInterpreter *, CV *);
78 static XS (Collectd_plugin_register_ds);
79 static XS (Collectd_plugin_unregister_ds);
80 static XS (Collectd_plugin_dispatch_values);
81 static XS (Collectd_plugin_flush_one);
82 static XS (Collectd_plugin_flush_all);
83 static XS (Collectd_plugin_dispatch_notification);
84 static XS (Collectd_plugin_log);
85 static XS (Collectd_call_by_name);
87 /*
88 * private data types
89 */
91 typedef struct c_ithread_s {
92 /* the thread's Perl interpreter */
93 PerlInterpreter *interp;
95 /* double linked list of threads */
96 struct c_ithread_s *prev;
97 struct c_ithread_s *next;
98 } c_ithread_t;
100 typedef struct {
101 c_ithread_t *head;
102 c_ithread_t *tail;
104 #if COLLECT_DEBUG
105 /* some usage stats */
106 int number_of_threads;
107 #endif /* COLLECT_DEBUG */
109 pthread_mutex_t mutex;
110 } c_ithread_list_t;
112 /*
113 * private variables
114 */
116 /* if perl_threads != NULL perl_threads->head must
117 * point to the "base" thread */
118 static c_ithread_list_t *perl_threads = NULL;
120 /* the key used to store each pthread's ithread */
121 static pthread_key_t perl_thr_key;
123 static int perl_argc = 0;
124 static char **perl_argv = NULL;
126 static char base_name[DATA_MAX_NAME_LEN] = "";
128 static struct {
129 char name[64];
130 XS ((*f));
131 } api[] =
132 {
133 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
134 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
135 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
136 { "Collectd::plugin_flush_one", Collectd_plugin_flush_one },
137 { "Collectd::plugin_flush_all", Collectd_plugin_flush_all },
138 { "Collectd::plugin_dispatch_notification",
139 Collectd_plugin_dispatch_notification },
140 { "Collectd::plugin_log", Collectd_plugin_log },
141 { "Collectd::call_by_name", Collectd_call_by_name },
142 { "", NULL }
143 };
145 struct {
146 char name[64];
147 int value;
148 } constants[] =
149 {
150 { "Collectd::TYPE_INIT", PLUGIN_INIT },
151 { "Collectd::TYPE_READ", PLUGIN_READ },
152 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
153 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
154 { "Collectd::TYPE_LOG", PLUGIN_LOG },
155 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
156 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
157 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
158 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
159 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
160 { "Collectd::LOG_ERR", LOG_ERR },
161 { "Collectd::LOG_WARNING", LOG_WARNING },
162 { "Collectd::LOG_NOTICE", LOG_NOTICE },
163 { "Collectd::LOG_INFO", LOG_INFO },
164 { "Collectd::LOG_DEBUG", LOG_DEBUG },
165 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
166 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
167 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
168 { "", 0 }
169 };
171 struct {
172 char name[64];
173 char *var;
174 } g_strings[] =
175 {
176 { "Collectd::hostname_g", hostname_g },
177 { "", NULL }
178 };
180 struct {
181 char name[64];
182 int *var;
183 } g_integers[] =
184 {
185 { "Collectd::interval_g", &interval_g },
186 { "", NULL }
187 };
189 /*
190 * Helper functions for data type conversion.
191 */
193 /*
194 * data source:
195 * [
196 * {
197 * name => $ds_name,
198 * type => $ds_type,
199 * min => $ds_min,
200 * max => $ds_max
201 * },
202 * ...
203 * ]
204 */
205 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
206 {
207 SV **tmp = NULL;
209 if ((NULL == hash) || (NULL == ds))
210 return -1;
212 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
213 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
214 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
215 }
216 else {
217 log_err ("hv2data_source: No DS name given.");
218 return -1;
219 }
221 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
222 ds->type = SvIV (*tmp);
224 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
225 log_err ("hv2data_source: Invalid DS type.");
226 return -1;
227 }
228 }
229 else {
230 ds->type = DS_TYPE_COUNTER;
231 }
233 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
234 ds->min = SvNV (*tmp);
235 else
236 ds->min = NAN;
238 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
239 ds->max = SvNV (*tmp);
240 else
241 ds->max = NAN;
242 return 0;
243 } /* static data_source_t *hv2data_source (HV *) */
245 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
246 {
247 const data_set_t *ds;
249 int i = 0;
251 if ((NULL == name) || (NULL == array) || (NULL == value))
252 return -1;
254 if (av_len (array) < len - 1)
255 len = av_len (array) + 1;
257 if (0 >= len)
258 return -1;
260 ds = plugin_get_ds (name);
261 if (NULL == ds) {
262 log_err ("av2value: Unknown dataset \"%s\"", name);
263 return -1;
264 }
266 if (ds->ds_num < len) {
267 log_warn ("av2value: Value length exceeds data set length.");
268 len = ds->ds_num;
269 }
271 for (i = 0; i < len; ++i) {
272 SV **tmp = av_fetch (array, i, 0);
274 if (NULL != tmp) {
275 if (DS_TYPE_COUNTER == ds->ds[i].type)
276 value[i].counter = SvIV (*tmp);
277 else
278 value[i].gauge = SvNV (*tmp);
279 }
280 else {
281 return -1;
282 }
283 }
284 return len;
285 } /* static int av2value (char *, AV *, value_t *, int) */
287 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
288 {
289 int i = 0;
291 if ((NULL == ds) || (NULL == array))
292 return -1;
294 av_extend (array, ds->ds_num);
296 for (i = 0; i < ds->ds_num; ++i) {
297 HV *source = newHV ();
299 if (NULL == hv_store (source, "name", 4,
300 newSVpv (ds->ds[i].name, 0), 0))
301 return -1;
303 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
304 return -1;
306 if (! isnan (ds->ds[i].min))
307 if (NULL == hv_store (source, "min", 3,
308 newSVnv (ds->ds[i].min), 0))
309 return -1;
311 if (! isnan (ds->ds[i].max))
312 if (NULL == hv_store (source, "max", 3,
313 newSVnv (ds->ds[i].max), 0))
314 return -1;
316 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
317 return -1;
318 }
319 return 0;
320 } /* static int data_set2av (data_set_t *, AV *) */
322 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
323 {
324 AV *values = NULL;
326 int i = 0;
327 int len = 0;
329 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
330 return -1;
332 len = vl->values_len;
334 if (ds->ds_num < len) {
335 log_warn ("value2av: Value length exceeds data set length.");
336 len = ds->ds_num;
337 }
339 values = newAV ();
340 av_extend (values, len - 1);
342 for (i = 0; i < len; ++i) {
343 SV *val = NULL;
345 if (DS_TYPE_COUNTER == ds->ds[i].type)
346 val = newSViv (vl->values[i].counter);
347 else
348 val = newSVnv (vl->values[i].gauge);
350 if (NULL == av_store (values, i, val)) {
351 av_undef (values);
352 return -1;
353 }
354 }
356 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
357 return -1;
359 if (0 != vl->time)
360 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
361 return -1;
363 if ('\0' != vl->host[0])
364 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
365 return -1;
367 if ('\0' != vl->plugin[0])
368 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
369 return -1;
371 if ('\0' != vl->plugin_instance[0])
372 if (NULL == hv_store (hash, "plugin_instance", 15,
373 newSVpv (vl->plugin_instance, 0), 0))
374 return -1;
376 if ('\0' != vl->type_instance[0])
377 if (NULL == hv_store (hash, "type_instance", 13,
378 newSVpv (vl->type_instance, 0), 0))
379 return -1;
380 return 0;
381 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
383 static int notification2hv (pTHX_ notification_t *n, HV *hash)
384 {
385 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
386 return -1;
388 if (0 != n->time)
389 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
390 return -1;
392 if ('\0' != *n->message)
393 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
394 return -1;
396 if ('\0' != *n->host)
397 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
398 return -1;
400 if ('\0' != *n->plugin)
401 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
402 return -1;
404 if ('\0' != *n->plugin_instance)
405 if (NULL == hv_store (hash, "plugin_instance", 15,
406 newSVpv (n->plugin_instance, 0), 0))
407 return -1;
409 if ('\0' != *n->type)
410 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
411 return -1;
413 if ('\0' != *n->type_instance)
414 if (NULL == hv_store (hash, "type_instance", 13,
415 newSVpv (n->type_instance, 0), 0))
416 return -1;
417 return 0;
418 } /* static int notification2hv (notification_t *, HV *) */
420 /*
421 * Internal functions.
422 */
424 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
425 int status = 0;
426 if (base_name[0] == '\0')
427 status = snprintf (buf, buf_len, "%s", module);
428 else
429 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
430 if ((status < 0) || ((unsigned int)status >= buf_len))
431 return (NULL);
432 buf[buf_len - 1] = '\0';
433 return (buf);
434 } /* char *get_module_name */
436 /*
437 * Add a plugin's data set definition.
438 */
439 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
440 {
441 int len = -1;
442 int ret = 0;
443 int i = 0;
445 data_source_t *ds = NULL;
446 data_set_t *set = NULL;
448 if ((NULL == name) || (NULL == dataset))
449 return -1;
451 len = av_len (dataset);
453 if (-1 == len)
454 return -1;
456 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
457 set = (data_set_t *)smalloc (sizeof (data_set_t));
459 for (i = 0; i <= len; ++i) {
460 SV **elem = av_fetch (dataset, i, 0);
462 if (NULL == elem)
463 return -1;
465 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
466 log_err ("pplugin_register_data_set: Invalid data source.");
467 return -1;
468 }
470 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
471 return -1;
473 log_debug ("pplugin_register_data_set: "
474 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
475 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
476 }
478 strncpy (set->type, name, DATA_MAX_NAME_LEN);
479 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
481 set->ds_num = len + 1;
482 set->ds = ds;
484 ret = plugin_register_data_set (set);
486 free (ds);
487 free (set);
488 return ret;
489 } /* static int pplugin_register_data_set (char *, SV *) */
491 /*
492 * Remove a plugin's data set definition.
493 */
494 static int pplugin_unregister_data_set (char *name)
495 {
496 if (NULL == name)
497 return 0;
498 return plugin_unregister_data_set (name);
499 } /* static int pplugin_unregister_data_set (char *) */
501 /*
502 * Submit the values to the write functions.
503 *
504 * value list:
505 * {
506 * values => [ @values ],
507 * time => $time,
508 * host => $host,
509 * plugin => $plugin,
510 * plugin_instance => $pinstance,
511 * type_instance => $tinstance,
512 * }
513 */
514 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
515 {
516 value_list_t list = VALUE_LIST_INIT;
517 value_t *val = NULL;
519 SV **tmp = NULL;
521 int ret = 0;
523 if ((NULL == name) || (NULL == values))
524 return -1;
526 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
527 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
528 log_err ("pplugin_dispatch_values: No valid values given.");
529 return -1;
530 }
532 {
533 AV *array = (AV *)SvRV (*tmp);
534 int len = av_len (array) + 1;
536 if (len <= 0)
537 return -1;
539 val = (value_t *)smalloc (len * sizeof (value_t));
541 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
542 list.values = val;
544 if (-1 == list.values_len) {
545 sfree (val);
546 return -1;
547 }
548 }
550 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
551 list.time = (time_t)SvIV (*tmp);
552 }
553 else {
554 list.time = time (NULL);
555 }
557 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
558 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
559 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
560 }
561 else {
562 strcpy (list.host, hostname_g);
563 }
565 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
566 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
567 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
568 }
570 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
571 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
572 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
573 }
575 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
576 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
577 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
578 }
580 ret = plugin_dispatch_values (name, &list);
582 sfree (val);
583 return ret;
584 } /* static int pplugin_dispatch_values (char *, HV *) */
586 /*
587 * Dispatch a notification.
588 *
589 * notification:
590 * {
591 * severity => $severity,
592 * time => $time,
593 * message => $msg,
594 * host => $host,
595 * plugin => $plugin,
596 * type => $type,
597 * plugin_instance => $instance,
598 * type_instance => $type_instance
599 * }
600 */
601 static int pplugin_dispatch_notification (pTHX_ HV *notif)
602 {
603 notification_t n;
605 SV **tmp = NULL;
607 if (NULL == notif)
608 return -1;
610 memset (&n, 0, sizeof (n));
612 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
613 n.severity = SvIV (*tmp);
614 else
615 n.severity = NOTIF_FAILURE;
617 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
618 n.time = (time_t)SvIV (*tmp);
619 else
620 n.time = time (NULL);
622 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
623 strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
624 n.message[sizeof (n.message) - 1] = '\0';
626 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
627 strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
628 else
629 strncpy (n.host, hostname_g, sizeof (n.host));
630 n.host[sizeof (n.host) - 1] = '\0';
632 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
633 strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
634 n.plugin[sizeof (n.plugin) - 1] = '\0';
636 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
637 strncpy (n.plugin_instance, SvPV_nolen (*tmp),
638 sizeof (n.plugin_instance));
639 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
641 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
642 strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
643 n.type[sizeof (n.type) - 1] = '\0';
645 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
646 strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
647 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
648 return plugin_dispatch_notification (&n);
649 } /* static int pplugin_dispatch_notification (HV *) */
651 /*
652 * Call all working functions of the given type.
653 */
654 static int pplugin_call_all (pTHX_ int type, ...)
655 {
656 int retvals = 0;
658 va_list ap;
659 int ret = 0;
661 dSP;
663 if ((type < 0) || (type >= PLUGIN_TYPES))
664 return -1;
666 va_start (ap, type);
668 ENTER;
669 SAVETMPS;
671 PUSHMARK (SP);
673 XPUSHs (sv_2mortal (newSViv ((IV)type)));
675 if (PLUGIN_WRITE == type) {
676 /*
677 * $_[0] = $plugin_type;
678 *
679 * $_[1] =
680 * [
681 * {
682 * name => $ds_name,
683 * type => $ds_type,
684 * min => $ds_min,
685 * max => $ds_max
686 * },
687 * ...
688 * ];
689 *
690 * $_[2] =
691 * {
692 * values => [ $v1, ... ],
693 * time => $time,
694 * host => $hostname,
695 * plugin => $plugin,
696 * plugin_instance => $instance,
697 * type_instance => $type_instance
698 * };
699 */
700 data_set_t *ds;
701 value_list_t *vl;
703 AV *pds = newAV ();
704 HV *pvl = newHV ();
706 ds = va_arg (ap, data_set_t *);
707 vl = va_arg (ap, value_list_t *);
709 if (-1 == data_set2av (aTHX_ ds, pds)) {
710 av_clear (pds);
711 av_undef (pds);
712 pds = Nullav;
713 ret = -1;
714 }
716 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
717 hv_clear (pvl);
718 hv_undef (pvl);
719 pvl = Nullhv;
720 ret = -1;
721 }
723 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
724 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
725 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
726 }
727 else if (PLUGIN_LOG == type) {
728 /*
729 * $_[0] = $level;
730 *
731 * $_[1] = $message;
732 */
733 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
734 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
735 }
736 else if (PLUGIN_NOTIF == type) {
737 /*
738 * $_[0] =
739 * {
740 * severity => $severity,
741 * time => $time,
742 * message => $msg,
743 * host => $host,
744 * plugin => $plugin,
745 * type => $type,
746 * plugin_instance => $instance,
747 * type_instance => $type_instance
748 * };
749 */
750 notification_t *n;
751 HV *notif = newHV ();
753 n = va_arg (ap, notification_t *);
755 if (-1 == notification2hv (aTHX_ n, notif)) {
756 hv_clear (notif);
757 hv_undef (notif);
758 notif = Nullhv;
759 ret = -1;
760 }
762 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
763 }
764 else if (PLUGIN_FLUSH == type) {
765 /*
766 * $_[0] = $timeout;
767 */
768 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
769 }
771 PUTBACK;
773 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
775 SPAGAIN;
776 if (0 < retvals) {
777 SV *tmp = POPs;
778 if (! SvTRUE (tmp))
779 ret = -1;
780 }
782 PUTBACK;
783 FREETMPS;
784 LEAVE;
786 va_end (ap);
787 return ret;
788 } /* static int pplugin_call_all (int, ...) */
790 /*
791 * Exported Perl API.
792 */
794 /*
795 * Collectd::plugin_register_data_set (type, dataset).
796 *
797 * type:
798 * type of the dataset
799 *
800 * dataset:
801 * dataset to be registered
802 */
803 static XS (Collectd_plugin_register_ds)
804 {
805 SV *data = NULL;
806 int ret = 0;
808 dXSARGS;
810 if (2 != items) {
811 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
812 XSRETURN_EMPTY;
813 }
815 log_debug ("Collectd::plugin_register_data_set: "
816 "type = \"%s\", dataset = \"%s\"",
817 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
819 data = ST (1);
821 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
822 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
823 (AV *)SvRV (data));
824 }
825 else {
826 log_err ("Collectd::plugin_register_data_set: Invalid data.");
827 XSRETURN_EMPTY;
828 }
830 if (0 == ret)
831 XSRETURN_YES;
832 else
833 XSRETURN_EMPTY;
834 } /* static XS (Collectd_plugin_register_ds) */
836 /*
837 * Collectd::plugin_unregister_data_set (type).
838 *
839 * type:
840 * type of the dataset
841 */
842 static XS (Collectd_plugin_unregister_ds)
843 {
844 dXSARGS;
846 if (1 != items) {
847 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
848 XSRETURN_EMPTY;
849 }
851 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
852 SvPV_nolen (ST (0)));
854 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
855 XSRETURN_YES;
856 else
857 XSRETURN_EMPTY;
858 } /* static XS (Collectd_plugin_register_ds) */
860 /*
861 * Collectd::plugin_dispatch_values (name, values).
862 *
863 * name:
864 * name of the plugin
865 *
866 * values:
867 * value list to submit
868 */
869 static XS (Collectd_plugin_dispatch_values)
870 {
871 SV *values = NULL;
873 int ret = 0;
875 dXSARGS;
877 if (2 != items) {
878 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
879 XSRETURN_EMPTY;
880 }
882 log_debug ("Collectd::plugin_dispatch_values: "
883 "name = \"%s\", values=\"%s\"",
884 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
886 values = ST (1);
888 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
889 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
890 XSRETURN_EMPTY;
891 }
893 if ((NULL == ST (0)) || (NULL == values))
894 XSRETURN_EMPTY;
896 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
897 (HV *)SvRV (values));
899 if (0 == ret)
900 XSRETURN_YES;
901 else
902 XSRETURN_EMPTY;
903 } /* static XS (Collectd_plugin_dispatch_values) */
905 /*
906 * Collectd::plugin_flush_one (timeout, name).
907 *
908 * timeout:
909 * timeout to use when flushing the data
910 *
911 * name:
912 * name of the plugin to flush
913 */
914 static XS (Collectd_plugin_flush_one)
915 {
916 dXSARGS;
918 if (2 != items) {
919 log_err ("Usage: Collectd::plugin_flush_one(timeout, name)");
920 XSRETURN_EMPTY;
921 }
923 log_debug ("Collectd::plugin_flush_one: timeout = %i, name = \"%s\"",
924 (int)SvIV (ST (0)), SvPV_nolen (ST (1)));
926 if (0 == plugin_flush_one ((int)SvIV (ST (0)), SvPV_nolen (ST (1))))
927 XSRETURN_YES;
928 else
929 XSRETURN_EMPTY;
930 } /* static XS (Collectd_plugin_flush_one) */
932 /*
933 * Collectd::plugin_flush_all (timeout).
934 *
935 * timeout:
936 * timeout to use when flushing the data
937 */
938 static XS (Collectd_plugin_flush_all)
939 {
940 dXSARGS;
942 if (1 != items) {
943 log_err ("Usage: Collectd::plugin_flush_all(timeout)");
944 XSRETURN_EMPTY;
945 }
947 log_debug ("Collectd::plugin_flush_all: timeout = %i", (int)SvIV (ST (0)));
949 plugin_flush_all ((int)SvIV (ST (0)));
950 XSRETURN_YES;
951 } /* static XS (Collectd_plugin_flush_all) */
953 /*
954 * Collectd::plugin_dispatch_notification (notif).
955 *
956 * notif:
957 * notification to dispatch
958 */
959 static XS (Collectd_plugin_dispatch_notification)
960 {
961 SV *notif = NULL;
963 int ret = 0;
965 dXSARGS;
967 if (1 != items) {
968 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
969 XSRETURN_EMPTY;
970 }
972 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
973 SvPV_nolen (ST (0)));
975 notif = ST (0);
977 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
978 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
979 XSRETURN_EMPTY;
980 }
982 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
984 if (0 == ret)
985 XSRETURN_YES;
986 else
987 XSRETURN_EMPTY;
988 } /* static XS (Collectd_plugin_dispatch_notification) */
990 /*
991 * Collectd::plugin_log (level, message).
992 *
993 * level:
994 * log level (LOG_DEBUG, ... LOG_ERR)
995 *
996 * message:
997 * log message
998 */
999 static XS (Collectd_plugin_log)
1000 {
1001 dXSARGS;
1003 if (2 != items) {
1004 log_err ("Usage: Collectd::plugin_log(level, message)");
1005 XSRETURN_EMPTY;
1006 }
1008 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
1009 XSRETURN_YES;
1010 } /* static XS (Collectd_plugin_log) */
1012 /*
1013 * Collectd::call_by_name (...).
1014 *
1015 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1016 */
1017 static XS (Collectd_call_by_name)
1018 {
1019 SV *tmp = NULL;
1020 char *name = NULL;
1022 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1023 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1024 CLEAR_STACK_FRAME;
1025 return;
1026 }
1028 name = SvPV_nolen (tmp);
1030 if (NULL == get_cv (name, 0)) {
1031 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1032 CLEAR_STACK_FRAME;
1033 return;
1034 }
1036 /* simply pass on the subroutine call without touching the stack,
1037 * thus leaving any arguments and return values in place */
1038 call_pv (name, 0);
1039 } /* static XS (Collectd_call_by_name) */
1041 /*
1042 * collectd's perl interpreter based thread implementation.
1043 *
1044 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1045 */
1047 /* must be called with perl_threads->mutex locked */
1048 static void c_ithread_destroy (c_ithread_t *ithread)
1049 {
1050 dTHXa (ithread->interp);
1052 assert (NULL != perl_threads);
1054 PERL_SET_CONTEXT (aTHX);
1055 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1057 #if COLLECT_DEBUG
1058 sv_report_used ();
1060 --perl_threads->number_of_threads;
1061 #endif /* COLLECT_DEBUG */
1063 perl_destruct (aTHX);
1064 perl_free (aTHX);
1066 if (NULL == ithread->prev)
1067 perl_threads->head = ithread->next;
1068 else
1069 ithread->prev->next = ithread->next;
1071 if (NULL == ithread->next)
1072 perl_threads->tail = ithread->prev;
1073 else
1074 ithread->next->prev = ithread->prev;
1076 sfree (ithread);
1077 return;
1078 } /* static void c_ithread_destroy (c_ithread_t *) */
1080 static void c_ithread_destructor (void *arg)
1081 {
1082 c_ithread_t *ithread = (c_ithread_t *)arg;
1083 c_ithread_t *t = NULL;
1085 if (NULL == perl_threads)
1086 return;
1088 pthread_mutex_lock (&perl_threads->mutex);
1090 for (t = perl_threads->head; NULL != t; t = t->next)
1091 if (t == ithread)
1092 break;
1094 /* the ithread no longer exists */
1095 if (NULL == t)
1096 return;
1098 c_ithread_destroy (ithread);
1100 pthread_mutex_unlock (&perl_threads->mutex);
1101 return;
1102 } /* static void c_ithread_destructor (void *) */
1104 /* must be called with perl_threads->mutex locked */
1105 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1106 {
1107 c_ithread_t *t = NULL;
1108 dTHXa (NULL);
1110 assert (NULL != perl_threads);
1112 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1113 memset (t, 0, sizeof (c_ithread_t));
1115 t->interp = (NULL == base)
1116 ? NULL
1117 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1119 aTHX = t->interp;
1121 if (NULL != base) {
1122 av_clear (PL_endav);
1123 av_undef (PL_endav);
1124 PL_endav = Nullav;
1125 }
1127 #if COLLECT_DEBUG
1128 ++perl_threads->number_of_threads;
1129 #endif /* COLLECT_DEBUG */
1131 t->next = NULL;
1133 if (NULL == perl_threads->tail) {
1134 perl_threads->head = t;
1135 t->prev = NULL;
1136 }
1137 else {
1138 perl_threads->tail->next = t;
1139 t->prev = perl_threads->tail;
1140 }
1142 perl_threads->tail = t;
1144 pthread_setspecific (perl_thr_key, (const void *)t);
1145 return t;
1146 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1148 /*
1149 * Interface to collectd.
1150 */
1152 static int perl_init (void)
1153 {
1154 dTHX;
1156 if (NULL == perl_threads)
1157 return 0;
1159 if (NULL == aTHX) {
1160 c_ithread_t *t = NULL;
1162 pthread_mutex_lock (&perl_threads->mutex);
1163 t = c_ithread_create (perl_threads->head->interp);
1164 pthread_mutex_unlock (&perl_threads->mutex);
1166 aTHX = t->interp;
1167 }
1169 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1170 aTHX, perl_threads->number_of_threads);
1171 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1172 } /* static int perl_init (void) */
1174 static int perl_read (void)
1175 {
1176 dTHX;
1178 if (NULL == perl_threads)
1179 return 0;
1181 if (NULL == aTHX) {
1182 c_ithread_t *t = NULL;
1184 pthread_mutex_lock (&perl_threads->mutex);
1185 t = c_ithread_create (perl_threads->head->interp);
1186 pthread_mutex_unlock (&perl_threads->mutex);
1188 aTHX = t->interp;
1189 }
1191 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1192 aTHX, perl_threads->number_of_threads);
1193 return pplugin_call_all (aTHX_ PLUGIN_READ);
1194 } /* static int perl_read (void) */
1196 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1197 {
1198 dTHX;
1200 if (NULL == perl_threads)
1201 return 0;
1203 if (NULL == aTHX) {
1204 c_ithread_t *t = NULL;
1206 pthread_mutex_lock (&perl_threads->mutex);
1207 t = c_ithread_create (perl_threads->head->interp);
1208 pthread_mutex_unlock (&perl_threads->mutex);
1210 aTHX = t->interp;
1211 }
1213 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1214 aTHX, perl_threads->number_of_threads);
1215 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1216 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1218 static void perl_log (int level, const char *msg)
1219 {
1220 dTHX;
1222 if (NULL == perl_threads)
1223 return;
1225 if (NULL == aTHX) {
1226 c_ithread_t *t = NULL;
1228 pthread_mutex_lock (&perl_threads->mutex);
1229 t = c_ithread_create (perl_threads->head->interp);
1230 pthread_mutex_unlock (&perl_threads->mutex);
1232 aTHX = t->interp;
1233 }
1235 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1236 return;
1237 } /* static void perl_log (int, const char *) */
1239 static int perl_notify (const notification_t *notif)
1240 {
1241 dTHX;
1243 if (NULL == perl_threads)
1244 return 0;
1246 if (NULL == aTHX) {
1247 c_ithread_t *t = NULL;
1249 pthread_mutex_lock (&perl_threads->mutex);
1250 t = c_ithread_create (perl_threads->head->interp);
1251 pthread_mutex_unlock (&perl_threads->mutex);
1253 aTHX = t->interp;
1254 }
1255 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1256 } /* static int perl_notify (const notification_t *) */
1258 static int perl_flush (const int timeout)
1259 {
1260 dTHX;
1262 if (NULL == perl_threads)
1263 return 0;
1265 if (NULL == aTHX) {
1266 c_ithread_t *t = NULL;
1268 pthread_mutex_lock (&perl_threads->mutex);
1269 t = c_ithread_create (perl_threads->head->interp);
1270 pthread_mutex_unlock (&perl_threads->mutex);
1272 aTHX = t->interp;
1273 }
1274 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout);
1275 } /* static int perl_flush (const int) */
1277 static int perl_shutdown (void)
1278 {
1279 c_ithread_t *t = NULL;
1281 int ret = 0;
1283 dTHX;
1285 plugin_unregister_complex_config ("perl");
1287 if (NULL == perl_threads)
1288 return 0;
1290 if (NULL == aTHX) {
1291 c_ithread_t *t = NULL;
1293 pthread_mutex_lock (&perl_threads->mutex);
1294 t = c_ithread_create (perl_threads->head->interp);
1295 pthread_mutex_unlock (&perl_threads->mutex);
1297 aTHX = t->interp;
1298 }
1300 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1301 aTHX, perl_threads->number_of_threads);
1303 plugin_unregister_log ("perl");
1304 plugin_unregister_notification ("perl");
1305 plugin_unregister_init ("perl");
1306 plugin_unregister_read ("perl");
1307 plugin_unregister_write ("perl");
1308 plugin_unregister_flush ("perl");
1310 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1312 pthread_mutex_lock (&perl_threads->mutex);
1313 t = perl_threads->tail;
1315 while (NULL != t) {
1316 c_ithread_t *thr = t;
1318 /* the pointer has to be advanced before destroying
1319 * the thread as this will free the memory */
1320 t = t->prev;
1322 c_ithread_destroy (thr);
1323 }
1325 pthread_mutex_unlock (&perl_threads->mutex);
1326 pthread_mutex_destroy (&perl_threads->mutex);
1328 sfree (perl_threads);
1330 pthread_key_delete (perl_thr_key);
1332 PERL_SYS_TERM ();
1334 plugin_unregister_shutdown ("perl");
1335 return ret;
1336 } /* static void perl_shutdown (void) */
1338 /*
1339 * Access functions for global variables.
1340 *
1341 * These functions implement the "magic" used to access
1342 * the global variables from Perl.
1343 */
1345 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1346 {
1347 char *pv = mg->mg_ptr;
1348 sv_setpv (var, pv);
1349 return 0;
1350 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1352 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1353 {
1354 char *pv = mg->mg_ptr;
1355 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1356 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1357 return 0;
1358 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1360 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1361 {
1362 int *iv = (int *)mg->mg_ptr;
1363 sv_setiv (var, *iv);
1364 return 0;
1365 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1367 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1368 {
1369 int *iv = (int *)mg->mg_ptr;
1370 *iv = (int)SvIV (var);
1371 return 0;
1372 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1374 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1375 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1377 /* bootstrap the Collectd module */
1378 static void xs_init (pTHX)
1379 {
1380 HV *stash = NULL;
1381 SV *tmp = NULL;
1382 char *file = __FILE__;
1384 int i = 0;
1386 dXSUB_SYS;
1388 /* enable usage of Perl modules using shared libraries */
1389 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1391 /* register API */
1392 for (i = 0; NULL != api[i].f; ++i)
1393 newXS (api[i].name, api[i].f, file);
1395 stash = gv_stashpv ("Collectd", 1);
1397 /* export "constants" */
1398 for (i = 0; '\0' != constants[i].name[0]; ++i)
1399 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1401 /* export global variables
1402 * by adding "magic" to the SV's representing the globale variables
1403 * perl is able to automagically call the get/set function when
1404 * accessing any such variable (this is basically the same as using
1405 * tie() in Perl) */
1406 /* global strings */
1407 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1408 tmp = get_sv (g_strings[i].name, 1);
1409 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1410 g_strings[i].var, 0);
1411 }
1413 /* global integers */
1414 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1415 tmp = get_sv (g_integers[i].name, 1);
1416 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1417 (char *)g_integers[i].var, 0);
1418 }
1419 return;
1420 } /* static void xs_init (pTHX) */
1422 /* Initialize the global Perl interpreter. */
1423 static int init_pi (int argc, char **argv)
1424 {
1425 dTHXa (NULL);
1427 if (NULL != perl_threads)
1428 return 0;
1430 log_info ("Initializing Perl interpreter...");
1431 #if COLLECT_DEBUG
1432 {
1433 int i = 0;
1435 for (i = 0; i < argc; ++i)
1436 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1437 }
1438 #endif /* COLLECT_DEBUG */
1440 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1441 log_err ("init_pi: pthread_key_create failed");
1443 /* this must not happen - cowardly giving up if it does */
1444 exit (1);
1445 }
1447 #ifdef __FreeBSD__
1448 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1449 * triggers a "value computed is not used" warning by gcc. */
1450 (void)
1451 #endif
1452 PERL_SYS_INIT3 (&argc, &argv, &environ);
1454 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1455 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1457 pthread_mutex_init (&perl_threads->mutex, NULL);
1458 /* locking the mutex should not be necessary at this point
1459 * but let's just do it for the sake of completeness */
1460 pthread_mutex_lock (&perl_threads->mutex);
1462 perl_threads->head = c_ithread_create (NULL);
1463 perl_threads->tail = perl_threads->head;
1465 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1466 log_err ("init_pi: Not enough memory.");
1467 exit (3);
1468 }
1470 aTHX = perl_threads->head->interp;
1471 pthread_mutex_unlock (&perl_threads->mutex);
1473 perl_construct (aTHX);
1475 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1477 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1478 log_err ("init_pi: Unable to bootstrap Collectd.");
1479 exit (1);
1480 }
1482 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1483 sv_setpv (get_sv ("0", 0), "collectd");
1485 perl_run (aTHX);
1487 plugin_register_log ("perl", perl_log);
1488 plugin_register_notification ("perl", perl_notify);
1489 plugin_register_init ("perl", perl_init);
1491 plugin_register_read ("perl", perl_read);
1493 plugin_register_write ("perl", perl_write);
1494 plugin_register_flush ("perl", perl_flush);
1495 plugin_register_shutdown ("perl", perl_shutdown);
1496 return 0;
1497 } /* static int init_pi (const char **, const int) */
1499 /*
1500 * LoadPlugin "<Plugin>"
1501 */
1502 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1503 {
1504 char module_name[DATA_MAX_NAME_LEN];
1506 char *value = NULL;
1508 if ((0 != ci->children_num) || (1 != ci->values_num)
1509 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1510 log_err ("LoadPlugin expects a single string argument.");
1511 return 1;
1512 }
1514 value = ci->values[0].value.string;
1516 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1517 log_err ("Invalid module name %s", value);
1518 return (1);
1519 }
1521 init_pi (perl_argc, perl_argv);
1522 assert (NULL != perl_threads);
1523 assert (NULL != perl_threads->head);
1525 aTHX = perl_threads->head->interp;
1527 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1528 load_module (PERL_LOADMOD_NOIMPORT,
1529 newSVpv (module_name, strlen (module_name)), Nullsv);
1530 return 0;
1531 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1533 /*
1534 * BaseName "<Name>"
1535 */
1536 static int perl_config_basename (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 ("BaseName expects a single string argument.");
1543 return 1;
1544 }
1546 value = ci->values[0].value.string;
1548 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1549 strncpy (base_name, value, sizeof (base_name));
1550 base_name[sizeof (base_name) - 1] = '\0';
1551 return 0;
1552 } /* static int perl_config_basename (oconfig_item_it *) */
1554 /*
1555 * EnableDebugger "<Package>"|""
1556 */
1557 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1558 {
1559 char *value = NULL;
1561 if ((0 != ci->children_num) || (1 != ci->values_num)
1562 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1563 log_err ("EnableDebugger expects a single string argument.");
1564 return 1;
1565 }
1567 if (NULL != perl_threads) {
1568 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1569 return 1;
1570 }
1572 value = ci->values[0].value.string;
1574 perl_argv = (char **)realloc (perl_argv,
1575 (++perl_argc + 1) * sizeof (char *));
1577 if (NULL == perl_argv) {
1578 log_err ("perl_config: Not enough memory.");
1579 exit (3);
1580 }
1582 if ('\0' == value[0]) {
1583 perl_argv[perl_argc - 1] = "-d";
1584 }
1585 else {
1586 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1587 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1588 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1589 }
1591 perl_argv[perl_argc] = NULL;
1592 return 0;
1593 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1595 /*
1596 * IncludeDir "<Dir>"
1597 */
1598 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1599 {
1600 char *value = NULL;
1602 if ((0 != ci->children_num) || (1 != ci->values_num)
1603 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1604 log_err ("IncludeDir expects a single string argument.");
1605 return 1;
1606 }
1608 value = ci->values[0].value.string;
1610 if (NULL == aTHX) {
1611 perl_argv = (char **)realloc (perl_argv,
1612 (++perl_argc + 1) * sizeof (char *));
1614 if (NULL == perl_argv) {
1615 log_err ("perl_config: Not enough memory.");
1616 exit (3);
1617 }
1619 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1620 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1621 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1623 perl_argv[perl_argc] = NULL;
1624 }
1625 else {
1626 /* prepend the directory to @INC */
1627 av_unshift (GvAVn (PL_incgv), 1);
1628 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1629 }
1630 return 0;
1631 } /* static int perl_config_includedir (oconfig_item_it *) */
1633 static int perl_config (oconfig_item_t *ci)
1634 {
1635 int i = 0;
1637 dTHX;
1639 /* dTHX does not get any valid values in case Perl
1640 * has not been initialized */
1641 if (NULL == perl_threads)
1642 aTHX = NULL;
1644 for (i = 0; i < ci->children_num; ++i) {
1645 oconfig_item_t *c = ci->children + i;
1647 if (0 == strcasecmp (c->key, "LoadPlugin"))
1648 perl_config_loadplugin (aTHX_ c);
1649 else if (0 == strcasecmp (c->key, "BaseName"))
1650 perl_config_basename (aTHX_ c);
1651 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1652 perl_config_enabledebugger (aTHX_ c);
1653 else if (0 == strcasecmp (c->key, "IncludeDir"))
1654 perl_config_includedir (aTHX_ c);
1655 else
1656 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1657 }
1658 return 0;
1659 } /* static int perl_config (oconfig_item_t *) */
1661 void module_register (void)
1662 {
1663 perl_argc = 4;
1664 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1666 /* default options for the Perl interpreter */
1667 perl_argv[0] = "";
1668 perl_argv[1] = "-MCollectd";
1669 perl_argv[2] = "-e";
1670 perl_argv[3] = "1";
1671 perl_argv[4] = NULL;
1673 plugin_register_complex_config ("perl", perl_config);
1674 return;
1675 } /* void module_register (void) */
1677 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */