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 #define DONT_POISON_SPRINTF_YET 1
31 #include "collectd.h"
32 #undef DONT_POISON_SPRINTF_YET
34 #include "configfile.h"
36 #include <EXTERN.h>
37 #include <perl.h>
39 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
40 # pragma GCC poison sprintf
41 #endif
43 #include <XSUB.h>
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
46 #ifdef DEBUG
47 # undef DEBUG
48 #endif /* DEBUG */
50 /* ... while we want the definition found in plugin.h. */
51 #include "plugin.h"
52 #include "common.h"
54 #include <pthread.h>
56 #if !defined(USE_ITHREADS)
57 # error "Perl does not support ithreads!"
58 #endif /* !defined(USE_ITHREADS) */
60 /* clear the Perl sub's stack frame
61 * (this should only be used inside an XSUB) */
62 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
64 #define PLUGIN_INIT 0
65 #define PLUGIN_READ 1
66 #define PLUGIN_WRITE 2
67 #define PLUGIN_SHUTDOWN 3
68 #define PLUGIN_LOG 4
69 #define PLUGIN_NOTIF 5
70 #define PLUGIN_FLUSH 6
72 #define PLUGIN_TYPES 7
74 #define PLUGIN_CONFIG 254
75 #define PLUGIN_DATASET 255
77 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
78 #define log_info(...) INFO ("perl: " __VA_ARGS__)
79 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
80 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
82 /* this is defined in DynaLoader.a */
83 void boot_DynaLoader (PerlInterpreter *, CV *);
85 static XS (Collectd_plugin_register_ds);
86 static XS (Collectd_plugin_unregister_ds);
87 static XS (Collectd_plugin_dispatch_values);
88 static XS (Collectd__plugin_flush);
89 static XS (Collectd_plugin_dispatch_notification);
90 static XS (Collectd_plugin_log);
91 static XS (Collectd_call_by_name);
93 /*
94 * private data types
95 */
97 typedef struct c_ithread_s {
98 /* the thread's Perl interpreter */
99 PerlInterpreter *interp;
101 /* double linked list of threads */
102 struct c_ithread_s *prev;
103 struct c_ithread_s *next;
104 } c_ithread_t;
106 typedef struct {
107 c_ithread_t *head;
108 c_ithread_t *tail;
110 #if COLLECT_DEBUG
111 /* some usage stats */
112 int number_of_threads;
113 #endif /* COLLECT_DEBUG */
115 pthread_mutex_t mutex;
116 } c_ithread_list_t;
118 /*
119 * private variables
120 */
122 /* if perl_threads != NULL perl_threads->head must
123 * point to the "base" thread */
124 static c_ithread_list_t *perl_threads = NULL;
126 /* the key used to store each pthread's ithread */
127 static pthread_key_t perl_thr_key;
129 static int perl_argc = 0;
130 static char **perl_argv = NULL;
132 static char base_name[DATA_MAX_NAME_LEN] = "";
134 static struct {
135 char name[64];
136 XS ((*f));
137 } api[] =
138 {
139 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
140 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
141 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
142 { "Collectd::_plugin_flush", Collectd__plugin_flush },
143 { "Collectd::plugin_dispatch_notification",
144 Collectd_plugin_dispatch_notification },
145 { "Collectd::plugin_log", Collectd_plugin_log },
146 { "Collectd::call_by_name", Collectd_call_by_name },
147 { "", NULL }
148 };
150 struct {
151 char name[64];
152 int value;
153 } constants[] =
154 {
155 { "Collectd::TYPE_INIT", PLUGIN_INIT },
156 { "Collectd::TYPE_READ", PLUGIN_READ },
157 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
158 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
159 { "Collectd::TYPE_LOG", PLUGIN_LOG },
160 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
161 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
162 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
163 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
164 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
165 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
166 { "Collectd::LOG_ERR", LOG_ERR },
167 { "Collectd::LOG_WARNING", LOG_WARNING },
168 { "Collectd::LOG_NOTICE", LOG_NOTICE },
169 { "Collectd::LOG_INFO", LOG_INFO },
170 { "Collectd::LOG_DEBUG", LOG_DEBUG },
171 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
172 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
173 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
174 { "", 0 }
175 };
177 struct {
178 char name[64];
179 char *var;
180 } g_strings[] =
181 {
182 { "Collectd::hostname_g", hostname_g },
183 { "", NULL }
184 };
186 struct {
187 char name[64];
188 int *var;
189 } g_integers[] =
190 {
191 { "Collectd::interval_g", &interval_g },
192 { "", NULL }
193 };
195 /*
196 * Helper functions for data type conversion.
197 */
199 /*
200 * data source:
201 * [
202 * {
203 * name => $ds_name,
204 * type => $ds_type,
205 * min => $ds_min,
206 * max => $ds_max
207 * },
208 * ...
209 * ]
210 */
211 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
212 {
213 SV **tmp = NULL;
215 if ((NULL == hash) || (NULL == ds))
216 return -1;
218 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
219 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
220 }
221 else {
222 log_err ("hv2data_source: No DS name given.");
223 return -1;
224 }
226 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
227 ds->type = SvIV (*tmp);
229 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
230 log_err ("hv2data_source: Invalid DS type.");
231 return -1;
232 }
233 }
234 else {
235 ds->type = DS_TYPE_COUNTER;
236 }
238 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
239 ds->min = SvNV (*tmp);
240 else
241 ds->min = NAN;
243 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
244 ds->max = SvNV (*tmp);
245 else
246 ds->max = NAN;
247 return 0;
248 } /* static data_source_t *hv2data_source (HV *) */
250 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
251 {
252 const data_set_t *ds;
254 int i = 0;
256 if ((NULL == name) || (NULL == array) || (NULL == value))
257 return -1;
259 if (av_len (array) < len - 1)
260 len = av_len (array) + 1;
262 if (0 >= len)
263 return -1;
265 ds = plugin_get_ds (name);
266 if (NULL == ds) {
267 log_err ("av2value: Unknown dataset \"%s\"", name);
268 return -1;
269 }
271 if (ds->ds_num < len) {
272 log_warn ("av2value: Value length exceeds data set length.");
273 len = ds->ds_num;
274 }
276 for (i = 0; i < len; ++i) {
277 SV **tmp = av_fetch (array, i, 0);
279 if (NULL != tmp) {
280 if (DS_TYPE_COUNTER == ds->ds[i].type)
281 value[i].counter = SvIV (*tmp);
282 else
283 value[i].gauge = SvNV (*tmp);
284 }
285 else {
286 return -1;
287 }
288 }
289 return len;
290 } /* static int av2value (char *, AV *, value_t *, int) */
292 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
293 {
294 int i = 0;
296 if ((NULL == ds) || (NULL == array))
297 return -1;
299 av_extend (array, ds->ds_num);
301 for (i = 0; i < ds->ds_num; ++i) {
302 HV *source = newHV ();
304 if (NULL == hv_store (source, "name", 4,
305 newSVpv (ds->ds[i].name, 0), 0))
306 return -1;
308 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
309 return -1;
311 if (! isnan (ds->ds[i].min))
312 if (NULL == hv_store (source, "min", 3,
313 newSVnv (ds->ds[i].min), 0))
314 return -1;
316 if (! isnan (ds->ds[i].max))
317 if (NULL == hv_store (source, "max", 3,
318 newSVnv (ds->ds[i].max), 0))
319 return -1;
321 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
322 return -1;
323 }
324 return 0;
325 } /* static int data_set2av (data_set_t *, AV *) */
327 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
328 {
329 AV *values = NULL;
331 int i = 0;
332 int len = 0;
334 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
335 return -1;
337 len = vl->values_len;
339 if (ds->ds_num < len) {
340 log_warn ("value2av: Value length exceeds data set length.");
341 len = ds->ds_num;
342 }
344 values = newAV ();
345 av_extend (values, len - 1);
347 for (i = 0; i < len; ++i) {
348 SV *val = NULL;
350 if (DS_TYPE_COUNTER == ds->ds[i].type)
351 val = newSViv (vl->values[i].counter);
352 else
353 val = newSVnv (vl->values[i].gauge);
355 if (NULL == av_store (values, i, val)) {
356 av_undef (values);
357 return -1;
358 }
359 }
361 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
362 return -1;
364 if (0 != vl->time)
365 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
366 return -1;
368 if ('\0' != vl->host[0])
369 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
370 return -1;
372 if ('\0' != vl->plugin[0])
373 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
374 return -1;
376 if ('\0' != vl->plugin_instance[0])
377 if (NULL == hv_store (hash, "plugin_instance", 15,
378 newSVpv (vl->plugin_instance, 0), 0))
379 return -1;
381 if ('\0' != vl->type[0])
382 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
383 return -1;
385 if ('\0' != vl->type_instance[0])
386 if (NULL == hv_store (hash, "type_instance", 13,
387 newSVpv (vl->type_instance, 0), 0))
388 return -1;
389 return 0;
390 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
392 static int notification2hv (pTHX_ notification_t *n, HV *hash)
393 {
394 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
395 return -1;
397 if (0 != n->time)
398 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
399 return -1;
401 if ('\0' != *n->message)
402 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
403 return -1;
405 if ('\0' != *n->host)
406 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
407 return -1;
409 if ('\0' != *n->plugin)
410 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
411 return -1;
413 if ('\0' != *n->plugin_instance)
414 if (NULL == hv_store (hash, "plugin_instance", 15,
415 newSVpv (n->plugin_instance, 0), 0))
416 return -1;
418 if ('\0' != *n->type)
419 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
420 return -1;
422 if ('\0' != *n->type_instance)
423 if (NULL == hv_store (hash, "type_instance", 13,
424 newSVpv (n->type_instance, 0), 0))
425 return -1;
426 return 0;
427 } /* static int notification2hv (notification_t *, HV *) */
429 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
430 {
431 int i;
433 AV *values;
434 AV *children;
436 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
437 return -1;
439 values = newAV ();
440 if (0 < ci->values_num)
441 av_extend (values, ci->values_num);
443 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
444 av_clear (values);
445 av_undef (values);
446 return -1;
447 }
449 for (i = 0; i < ci->values_num; ++i) {
450 SV *value;
452 switch (ci->values[i].type) {
453 case OCONFIG_TYPE_STRING:
454 value = newSVpv (ci->values[i].value.string, 0);
455 break;
456 case OCONFIG_TYPE_NUMBER:
457 value = newSVnv ((NV)ci->values[i].value.number);
458 break;
459 case OCONFIG_TYPE_BOOLEAN:
460 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
461 break;
462 default:
463 log_err ("oconfig_item2hv: Invalid value type %i.",
464 ci->values[i].type);
465 value = &PL_sv_undef;
466 }
468 if (NULL == av_store (values, i, value)) {
469 sv_free (value);
470 return -1;
471 }
472 }
474 /* ignoring 'parent' member which is uninteresting in this case */
476 children = newAV ();
477 if (0 < ci->children_num)
478 av_extend (children, ci->children_num);
480 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
481 av_clear (children);
482 av_undef (children);
483 return -1;
484 }
486 for (i = 0; i < ci->children_num; ++i) {
487 HV *child = newHV ();
489 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
490 hv_clear (child);
491 hv_undef (child);
492 return -1;
493 }
495 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
496 hv_clear (child);
497 hv_undef (child);
498 return -1;
499 }
500 }
501 return 0;
502 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
504 /*
505 * Internal functions.
506 */
508 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
509 int status = 0;
510 if (base_name[0] == '\0')
511 status = ssnprintf (buf, buf_len, "%s", module);
512 else
513 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
514 if ((status < 0) || ((unsigned int)status >= buf_len))
515 return (NULL);
516 return (buf);
517 } /* char *get_module_name */
519 /*
520 * Add a plugin's data set definition.
521 */
522 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
523 {
524 int len = -1;
525 int ret = 0;
526 int i = 0;
528 data_source_t *ds = NULL;
529 data_set_t *set = NULL;
531 if ((NULL == name) || (NULL == dataset))
532 return -1;
534 len = av_len (dataset);
536 if (-1 == len)
537 return -1;
539 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
540 set = (data_set_t *)smalloc (sizeof (data_set_t));
542 for (i = 0; i <= len; ++i) {
543 SV **elem = av_fetch (dataset, i, 0);
545 if (NULL == elem)
546 return -1;
548 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
549 log_err ("pplugin_register_data_set: Invalid data source.");
550 return -1;
551 }
553 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
554 return -1;
556 log_debug ("pplugin_register_data_set: "
557 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
558 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
559 }
561 sstrncpy (set->type, name, sizeof (set->type));
563 set->ds_num = len + 1;
564 set->ds = ds;
566 ret = plugin_register_data_set (set);
568 free (ds);
569 free (set);
570 return ret;
571 } /* static int pplugin_register_data_set (char *, SV *) */
573 /*
574 * Remove a plugin's data set definition.
575 */
576 static int pplugin_unregister_data_set (char *name)
577 {
578 if (NULL == name)
579 return 0;
580 return plugin_unregister_data_set (name);
581 } /* static int pplugin_unregister_data_set (char *) */
583 /*
584 * Submit the values to the write functions.
585 *
586 * value list:
587 * {
588 * values => [ @values ],
589 * time => $time,
590 * host => $host,
591 * plugin => $plugin,
592 * plugin_instance => $pinstance,
593 * type_instance => $tinstance,
594 * }
595 */
596 static int pplugin_dispatch_values (pTHX_ HV *values)
597 {
598 value_list_t list = VALUE_LIST_INIT;
599 value_t *val = NULL;
601 SV **tmp = NULL;
603 int ret = 0;
605 if (NULL == values)
606 return -1;
608 if (NULL == (tmp = hv_fetch (values, "type", 4, 0))) {
609 log_err ("pplugin_dispatch_values: No type given.");
610 return -1;
611 }
613 sstrncpy (list.type, SvPV_nolen (*tmp), sizeof (list.type));
615 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
616 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
617 log_err ("pplugin_dispatch_values: No valid values given.");
618 return -1;
619 }
621 {
622 AV *array = (AV *)SvRV (*tmp);
623 int len = av_len (array) + 1;
625 if (len <= 0)
626 return -1;
628 val = (value_t *)smalloc (len * sizeof (value_t));
630 list.values_len = av2value (aTHX_ list.type, (AV *)SvRV (*tmp),
631 val, len);
632 list.values = val;
634 if (-1 == list.values_len) {
635 sfree (val);
636 return -1;
637 }
638 }
640 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
641 list.time = (time_t)SvIV (*tmp);
642 }
644 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
645 sstrncpy (list.host, SvPV_nolen (*tmp), sizeof (list.host));
646 }
647 else {
648 sstrncpy (list.host, hostname_g, sizeof (list.host));
649 }
651 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0)))
652 sstrncpy (list.plugin, SvPV_nolen (*tmp), sizeof (list.plugin));
654 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0)))
655 sstrncpy (list.plugin_instance, SvPV_nolen (*tmp),
656 sizeof (list.plugin_instance));
658 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0)))
659 sstrncpy (list.type_instance, SvPV_nolen (*tmp),
660 sizeof (list.type_instance));
662 ret = plugin_dispatch_values (&list);
664 sfree (val);
665 return ret;
666 } /* static int pplugin_dispatch_values (char *, HV *) */
668 /*
669 * Dispatch a notification.
670 *
671 * notification:
672 * {
673 * severity => $severity,
674 * time => $time,
675 * message => $msg,
676 * host => $host,
677 * plugin => $plugin,
678 * type => $type,
679 * plugin_instance => $instance,
680 * type_instance => $type_instance
681 * }
682 */
683 static int pplugin_dispatch_notification (pTHX_ HV *notif)
684 {
685 notification_t n;
687 SV **tmp = NULL;
689 if (NULL == notif)
690 return -1;
692 memset (&n, 0, sizeof (n));
694 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
695 n.severity = SvIV (*tmp);
696 else
697 n.severity = NOTIF_FAILURE;
699 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
700 n.time = (time_t)SvIV (*tmp);
701 else
702 n.time = time (NULL);
704 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
705 sstrncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
707 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
708 sstrncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
709 else
710 sstrncpy (n.host, hostname_g, sizeof (n.host));
712 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
713 sstrncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
715 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
716 sstrncpy (n.plugin_instance, SvPV_nolen (*tmp),
717 sizeof (n.plugin_instance));
719 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
720 sstrncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
722 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
723 sstrncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
724 return plugin_dispatch_notification (&n);
725 } /* static int pplugin_dispatch_notification (HV *) */
727 /*
728 * Call all working functions of the given type.
729 */
730 static int pplugin_call_all (pTHX_ int type, ...)
731 {
732 int retvals = 0;
734 va_list ap;
735 int ret = 0;
737 dSP;
739 if ((type < 0) || (type >= PLUGIN_TYPES))
740 return -1;
742 va_start (ap, type);
744 ENTER;
745 SAVETMPS;
747 PUSHMARK (SP);
749 XPUSHs (sv_2mortal (newSViv ((IV)type)));
751 if (PLUGIN_WRITE == type) {
752 /*
753 * $_[0] = $plugin_type;
754 *
755 * $_[1] =
756 * [
757 * {
758 * name => $ds_name,
759 * type => $ds_type,
760 * min => $ds_min,
761 * max => $ds_max
762 * },
763 * ...
764 * ];
765 *
766 * $_[2] =
767 * {
768 * values => [ $v1, ... ],
769 * time => $time,
770 * host => $hostname,
771 * plugin => $plugin,
772 * type => $type,
773 * plugin_instance => $instance,
774 * type_instance => $type_instance
775 * };
776 */
777 data_set_t *ds;
778 value_list_t *vl;
780 AV *pds = newAV ();
781 HV *pvl = newHV ();
783 ds = va_arg (ap, data_set_t *);
784 vl = va_arg (ap, value_list_t *);
786 if (-1 == data_set2av (aTHX_ ds, pds)) {
787 av_clear (pds);
788 av_undef (pds);
789 pds = Nullav;
790 ret = -1;
791 }
793 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
794 hv_clear (pvl);
795 hv_undef (pvl);
796 pvl = Nullhv;
797 ret = -1;
798 }
800 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
801 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
802 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
803 }
804 else if (PLUGIN_LOG == type) {
805 /*
806 * $_[0] = $level;
807 *
808 * $_[1] = $message;
809 */
810 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
811 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
812 }
813 else if (PLUGIN_NOTIF == type) {
814 /*
815 * $_[0] =
816 * {
817 * severity => $severity,
818 * time => $time,
819 * message => $msg,
820 * host => $host,
821 * plugin => $plugin,
822 * type => $type,
823 * plugin_instance => $instance,
824 * type_instance => $type_instance
825 * };
826 */
827 notification_t *n;
828 HV *notif = newHV ();
830 n = va_arg (ap, notification_t *);
832 if (-1 == notification2hv (aTHX_ n, notif)) {
833 hv_clear (notif);
834 hv_undef (notif);
835 notif = Nullhv;
836 ret = -1;
837 }
839 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
840 }
841 else if (PLUGIN_FLUSH == type) {
842 /*
843 * $_[0] = $timeout;
844 * $_[1] = $identifier;
845 */
846 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
847 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
848 }
850 PUTBACK;
852 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
854 SPAGAIN;
855 if (0 < retvals) {
856 SV *tmp = POPs;
857 if (! SvTRUE (tmp))
858 ret = -1;
859 }
861 PUTBACK;
862 FREETMPS;
863 LEAVE;
865 va_end (ap);
866 return ret;
867 } /* static int pplugin_call_all (int, ...) */
869 /*
870 * Exported Perl API.
871 */
873 /*
874 * Collectd::plugin_register_data_set (type, dataset).
875 *
876 * type:
877 * type of the dataset
878 *
879 * dataset:
880 * dataset to be registered
881 */
882 static XS (Collectd_plugin_register_ds)
883 {
884 SV *data = NULL;
885 int ret = 0;
887 dXSARGS;
889 if (2 != items) {
890 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
891 XSRETURN_EMPTY;
892 }
894 log_debug ("Collectd::plugin_register_data_set: "
895 "type = \"%s\", dataset = \"%s\"",
896 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
898 data = ST (1);
900 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
901 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
902 (AV *)SvRV (data));
903 }
904 else {
905 log_err ("Collectd::plugin_register_data_set: Invalid data.");
906 XSRETURN_EMPTY;
907 }
909 if (0 == ret)
910 XSRETURN_YES;
911 else
912 XSRETURN_EMPTY;
913 } /* static XS (Collectd_plugin_register_ds) */
915 /*
916 * Collectd::plugin_unregister_data_set (type).
917 *
918 * type:
919 * type of the dataset
920 */
921 static XS (Collectd_plugin_unregister_ds)
922 {
923 dXSARGS;
925 if (1 != items) {
926 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
927 XSRETURN_EMPTY;
928 }
930 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
931 SvPV_nolen (ST (0)));
933 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
934 XSRETURN_YES;
935 else
936 XSRETURN_EMPTY;
937 } /* static XS (Collectd_plugin_register_ds) */
939 /*
940 * Collectd::plugin_dispatch_values (name, values).
941 *
942 * name:
943 * name of the plugin
944 *
945 * values:
946 * value list to submit
947 */
948 static XS (Collectd_plugin_dispatch_values)
949 {
950 SV *values = NULL;
951 int values_idx = 0;
953 int ret = 0;
955 dXSARGS;
957 if (2 == items) {
958 log_warn ("Collectd::plugin_dispatch_values with two arguments "
959 "is deprecated - pass the type through values->{type}.");
960 values_idx = 1;
961 }
962 else if (1 != items) {
963 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
964 XSRETURN_EMPTY;
965 }
967 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
968 SvPV_nolen (ST (values_idx)));
970 values = ST (values_idx);
972 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
973 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
974 XSRETURN_EMPTY;
975 }
977 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
978 XSRETURN_EMPTY;
980 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
981 newSVsv (ST (0)), 0))) {
982 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
983 XSRETURN_EMPTY;
984 }
986 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
988 if (0 == ret)
989 XSRETURN_YES;
990 else
991 XSRETURN_EMPTY;
992 } /* static XS (Collectd_plugin_dispatch_values) */
994 /*
995 * Collectd::_plugin_flush (plugin, timeout, identifier).
996 *
997 * plugin:
998 * name of the plugin to flush
999 *
1000 * timeout:
1001 * timeout to use when flushing the data
1002 *
1003 * identifier:
1004 * data-set identifier to flush
1005 */
1006 static XS (Collectd__plugin_flush)
1007 {
1008 char *plugin = NULL;
1009 int timeout = -1;
1010 char *id = NULL;
1012 dXSARGS;
1014 if (3 != items) {
1015 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1016 XSRETURN_EMPTY;
1017 }
1019 if (SvOK (ST (0)))
1020 plugin = SvPV_nolen (ST (0));
1022 if (SvOK (ST (1)))
1023 timeout = (int)SvIV (ST (1));
1025 if (SvOK (ST (2)))
1026 id = SvPV_nolen (ST (2));
1028 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1029 "id = \"%s\"", plugin, timeout, id);
1031 if (0 == plugin_flush (plugin, timeout, id))
1032 XSRETURN_YES;
1033 else
1034 XSRETURN_EMPTY;
1035 } /* static XS (Collectd__plugin_flush) */
1037 /*
1038 * Collectd::plugin_dispatch_notification (notif).
1039 *
1040 * notif:
1041 * notification to dispatch
1042 */
1043 static XS (Collectd_plugin_dispatch_notification)
1044 {
1045 SV *notif = NULL;
1047 int ret = 0;
1049 dXSARGS;
1051 if (1 != items) {
1052 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1053 XSRETURN_EMPTY;
1054 }
1056 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1057 SvPV_nolen (ST (0)));
1059 notif = ST (0);
1061 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1062 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1063 XSRETURN_EMPTY;
1064 }
1066 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1068 if (0 == ret)
1069 XSRETURN_YES;
1070 else
1071 XSRETURN_EMPTY;
1072 } /* static XS (Collectd_plugin_dispatch_notification) */
1074 /*
1075 * Collectd::plugin_log (level, message).
1076 *
1077 * level:
1078 * log level (LOG_DEBUG, ... LOG_ERR)
1079 *
1080 * message:
1081 * log message
1082 */
1083 static XS (Collectd_plugin_log)
1084 {
1085 dXSARGS;
1087 if (2 != items) {
1088 log_err ("Usage: Collectd::plugin_log(level, message)");
1089 XSRETURN_EMPTY;
1090 }
1092 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1093 XSRETURN_YES;
1094 } /* static XS (Collectd_plugin_log) */
1096 /*
1097 * Collectd::call_by_name (...).
1098 *
1099 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1100 */
1101 static XS (Collectd_call_by_name)
1102 {
1103 SV *tmp = NULL;
1104 char *name = NULL;
1106 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1107 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1108 CLEAR_STACK_FRAME;
1109 return;
1110 }
1112 name = SvPV_nolen (tmp);
1114 if (NULL == get_cv (name, 0)) {
1115 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1116 CLEAR_STACK_FRAME;
1117 return;
1118 }
1120 /* simply pass on the subroutine call without touching the stack,
1121 * thus leaving any arguments and return values in place */
1122 call_pv (name, 0);
1123 } /* static XS (Collectd_call_by_name) */
1125 /*
1126 * collectd's perl interpreter based thread implementation.
1127 *
1128 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1129 */
1131 /* must be called with perl_threads->mutex locked */
1132 static void c_ithread_destroy (c_ithread_t *ithread)
1133 {
1134 dTHXa (ithread->interp);
1136 assert (NULL != perl_threads);
1138 PERL_SET_CONTEXT (aTHX);
1139 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1141 #if COLLECT_DEBUG
1142 sv_report_used ();
1144 --perl_threads->number_of_threads;
1145 #endif /* COLLECT_DEBUG */
1147 perl_destruct (aTHX);
1148 perl_free (aTHX);
1150 if (NULL == ithread->prev)
1151 perl_threads->head = ithread->next;
1152 else
1153 ithread->prev->next = ithread->next;
1155 if (NULL == ithread->next)
1156 perl_threads->tail = ithread->prev;
1157 else
1158 ithread->next->prev = ithread->prev;
1160 sfree (ithread);
1161 return;
1162 } /* static void c_ithread_destroy (c_ithread_t *) */
1164 static void c_ithread_destructor (void *arg)
1165 {
1166 c_ithread_t *ithread = (c_ithread_t *)arg;
1167 c_ithread_t *t = NULL;
1169 if (NULL == perl_threads)
1170 return;
1172 pthread_mutex_lock (&perl_threads->mutex);
1174 for (t = perl_threads->head; NULL != t; t = t->next)
1175 if (t == ithread)
1176 break;
1178 /* the ithread no longer exists */
1179 if (NULL == t)
1180 return;
1182 c_ithread_destroy (ithread);
1184 pthread_mutex_unlock (&perl_threads->mutex);
1185 return;
1186 } /* static void c_ithread_destructor (void *) */
1188 /* must be called with perl_threads->mutex locked */
1189 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1190 {
1191 c_ithread_t *t = NULL;
1192 dTHXa (NULL);
1194 assert (NULL != perl_threads);
1196 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1197 memset (t, 0, sizeof (c_ithread_t));
1199 t->interp = (NULL == base)
1200 ? NULL
1201 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1203 aTHX = t->interp;
1205 if ((NULL != base) && (NULL != PL_endav)) {
1206 av_clear (PL_endav);
1207 av_undef (PL_endav);
1208 PL_endav = Nullav;
1209 }
1211 #if COLLECT_DEBUG
1212 ++perl_threads->number_of_threads;
1213 #endif /* COLLECT_DEBUG */
1215 t->next = NULL;
1217 if (NULL == perl_threads->tail) {
1218 perl_threads->head = t;
1219 t->prev = NULL;
1220 }
1221 else {
1222 perl_threads->tail->next = t;
1223 t->prev = perl_threads->tail;
1224 }
1226 perl_threads->tail = t;
1228 pthread_setspecific (perl_thr_key, (const void *)t);
1229 return t;
1230 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1232 /*
1233 * Interface to collectd.
1234 */
1236 static int perl_init (void)
1237 {
1238 dTHX;
1240 if (NULL == perl_threads)
1241 return 0;
1243 if (NULL == aTHX) {
1244 c_ithread_t *t = NULL;
1246 pthread_mutex_lock (&perl_threads->mutex);
1247 t = c_ithread_create (perl_threads->head->interp);
1248 pthread_mutex_unlock (&perl_threads->mutex);
1250 aTHX = t->interp;
1251 }
1253 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1254 aTHX, perl_threads->number_of_threads);
1255 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1256 } /* static int perl_init (void) */
1258 static int perl_read (void)
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 }
1275 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1276 aTHX, perl_threads->number_of_threads);
1277 return pplugin_call_all (aTHX_ PLUGIN_READ);
1278 } /* static int perl_read (void) */
1280 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1281 {
1282 dTHX;
1284 if (NULL == perl_threads)
1285 return 0;
1287 if (NULL == aTHX) {
1288 c_ithread_t *t = NULL;
1290 pthread_mutex_lock (&perl_threads->mutex);
1291 t = c_ithread_create (perl_threads->head->interp);
1292 pthread_mutex_unlock (&perl_threads->mutex);
1294 aTHX = t->interp;
1295 }
1297 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1298 aTHX, perl_threads->number_of_threads);
1299 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1300 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1302 static void perl_log (int level, const char *msg)
1303 {
1304 dTHX;
1306 if (NULL == perl_threads)
1307 return;
1309 if (NULL == aTHX) {
1310 c_ithread_t *t = NULL;
1312 pthread_mutex_lock (&perl_threads->mutex);
1313 t = c_ithread_create (perl_threads->head->interp);
1314 pthread_mutex_unlock (&perl_threads->mutex);
1316 aTHX = t->interp;
1317 }
1319 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1320 return;
1321 } /* static void perl_log (int, const char *) */
1323 static int perl_notify (const notification_t *notif)
1324 {
1325 dTHX;
1327 if (NULL == perl_threads)
1328 return 0;
1330 if (NULL == aTHX) {
1331 c_ithread_t *t = NULL;
1333 pthread_mutex_lock (&perl_threads->mutex);
1334 t = c_ithread_create (perl_threads->head->interp);
1335 pthread_mutex_unlock (&perl_threads->mutex);
1337 aTHX = t->interp;
1338 }
1339 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1340 } /* static int perl_notify (const notification_t *) */
1342 static int perl_flush (int timeout, const char *identifier)
1343 {
1344 dTHX;
1346 if (NULL == perl_threads)
1347 return 0;
1349 if (NULL == aTHX) {
1350 c_ithread_t *t = NULL;
1352 pthread_mutex_lock (&perl_threads->mutex);
1353 t = c_ithread_create (perl_threads->head->interp);
1354 pthread_mutex_unlock (&perl_threads->mutex);
1356 aTHX = t->interp;
1357 }
1358 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
1359 } /* static int perl_flush (const int) */
1361 static int perl_shutdown (void)
1362 {
1363 c_ithread_t *t = NULL;
1365 int ret = 0;
1367 dTHX;
1369 plugin_unregister_complex_config ("perl");
1371 if (NULL == perl_threads)
1372 return 0;
1374 if (NULL == aTHX) {
1375 c_ithread_t *t = NULL;
1377 pthread_mutex_lock (&perl_threads->mutex);
1378 t = c_ithread_create (perl_threads->head->interp);
1379 pthread_mutex_unlock (&perl_threads->mutex);
1381 aTHX = t->interp;
1382 }
1384 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1385 aTHX, perl_threads->number_of_threads);
1387 plugin_unregister_log ("perl");
1388 plugin_unregister_notification ("perl");
1389 plugin_unregister_init ("perl");
1390 plugin_unregister_read ("perl");
1391 plugin_unregister_write ("perl");
1392 plugin_unregister_flush ("perl");
1394 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1396 pthread_mutex_lock (&perl_threads->mutex);
1397 t = perl_threads->tail;
1399 while (NULL != t) {
1400 c_ithread_t *thr = t;
1402 /* the pointer has to be advanced before destroying
1403 * the thread as this will free the memory */
1404 t = t->prev;
1406 c_ithread_destroy (thr);
1407 }
1409 pthread_mutex_unlock (&perl_threads->mutex);
1410 pthread_mutex_destroy (&perl_threads->mutex);
1412 sfree (perl_threads);
1414 pthread_key_delete (perl_thr_key);
1416 PERL_SYS_TERM ();
1418 plugin_unregister_shutdown ("perl");
1419 return ret;
1420 } /* static void perl_shutdown (void) */
1422 /*
1423 * Access functions for global variables.
1424 *
1425 * These functions implement the "magic" used to access
1426 * the global variables from Perl.
1427 */
1429 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1430 {
1431 char *pv = mg->mg_ptr;
1432 sv_setpv (var, pv);
1433 return 0;
1434 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1436 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1437 {
1438 char *pv = mg->mg_ptr;
1439 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1440 return 0;
1441 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1443 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1444 {
1445 int *iv = (int *)mg->mg_ptr;
1446 sv_setiv (var, *iv);
1447 return 0;
1448 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1450 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1451 {
1452 int *iv = (int *)mg->mg_ptr;
1453 *iv = (int)SvIV (var);
1454 return 0;
1455 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1457 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1458 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1460 /* bootstrap the Collectd module */
1461 static void xs_init (pTHX)
1462 {
1463 HV *stash = NULL;
1464 SV *tmp = NULL;
1465 char *file = __FILE__;
1467 int i = 0;
1469 dXSUB_SYS;
1471 /* enable usage of Perl modules using shared libraries */
1472 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1474 /* register API */
1475 for (i = 0; NULL != api[i].f; ++i)
1476 newXS (api[i].name, api[i].f, file);
1478 stash = gv_stashpv ("Collectd", 1);
1480 /* export "constants" */
1481 for (i = 0; '\0' != constants[i].name[0]; ++i)
1482 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1484 /* export global variables
1485 * by adding "magic" to the SV's representing the globale variables
1486 * perl is able to automagically call the get/set function when
1487 * accessing any such variable (this is basically the same as using
1488 * tie() in Perl) */
1489 /* global strings */
1490 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1491 tmp = get_sv (g_strings[i].name, 1);
1492 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1493 g_strings[i].var, 0);
1494 }
1496 /* global integers */
1497 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1498 tmp = get_sv (g_integers[i].name, 1);
1499 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1500 (char *)g_integers[i].var, 0);
1501 }
1502 return;
1503 } /* static void xs_init (pTHX) */
1505 /* Initialize the global Perl interpreter. */
1506 static int init_pi (int argc, char **argv)
1507 {
1508 dTHXa (NULL);
1510 if (NULL != perl_threads)
1511 return 0;
1513 log_info ("Initializing Perl interpreter...");
1514 #if COLLECT_DEBUG
1515 {
1516 int i = 0;
1518 for (i = 0; i < argc; ++i)
1519 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1520 }
1521 #endif /* COLLECT_DEBUG */
1523 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1524 log_err ("init_pi: pthread_key_create failed");
1526 /* this must not happen - cowardly giving up if it does */
1527 return -1;
1528 }
1530 #ifdef __FreeBSD__
1531 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1532 * triggers a "value computed is not used" warning by gcc. */
1533 (void)
1534 #endif
1535 PERL_SYS_INIT3 (&argc, &argv, &environ);
1537 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1538 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1540 pthread_mutex_init (&perl_threads->mutex, NULL);
1541 /* locking the mutex should not be necessary at this point
1542 * but let's just do it for the sake of completeness */
1543 pthread_mutex_lock (&perl_threads->mutex);
1545 perl_threads->head = c_ithread_create (NULL);
1546 perl_threads->tail = perl_threads->head;
1548 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1549 log_err ("init_pi: Not enough memory.");
1550 exit (3);
1551 }
1553 aTHX = perl_threads->head->interp;
1554 pthread_mutex_unlock (&perl_threads->mutex);
1556 perl_construct (aTHX);
1558 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1560 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1561 SV *err = get_sv ("@", 1);
1562 log_err ("init_pi: Unable to bootstrap Collectd: %s",
1563 SvPV_nolen (err));
1565 perl_destruct (perl_threads->head->interp);
1566 perl_free (perl_threads->head->interp);
1567 sfree (perl_threads);
1569 pthread_key_delete (perl_thr_key);
1570 return -1;
1571 }
1573 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1574 sv_setpv (get_sv ("0", 0), "collectd");
1576 perl_run (aTHX);
1578 plugin_register_log ("perl", perl_log);
1579 plugin_register_notification ("perl", perl_notify);
1580 plugin_register_init ("perl", perl_init);
1582 plugin_register_read ("perl", perl_read);
1584 plugin_register_write ("perl", perl_write);
1585 plugin_register_flush ("perl", perl_flush);
1586 plugin_register_shutdown ("perl", perl_shutdown);
1587 return 0;
1588 } /* static int init_pi (const char **, const int) */
1590 /*
1591 * LoadPlugin "<Plugin>"
1592 */
1593 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1594 {
1595 char module_name[DATA_MAX_NAME_LEN];
1597 char *value = NULL;
1599 if ((0 != ci->children_num) || (1 != ci->values_num)
1600 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1601 log_err ("LoadPlugin expects a single string argument.");
1602 return 1;
1603 }
1605 value = ci->values[0].value.string;
1607 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1608 log_err ("Invalid module name %s", value);
1609 return (1);
1610 }
1612 if (0 != init_pi (perl_argc, perl_argv))
1613 return -1;
1615 assert (NULL != perl_threads);
1616 assert (NULL != perl_threads->head);
1618 aTHX = perl_threads->head->interp;
1620 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1621 load_module (PERL_LOADMOD_NOIMPORT,
1622 newSVpv (module_name, strlen (module_name)), Nullsv);
1623 return 0;
1624 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1626 /*
1627 * BaseName "<Name>"
1628 */
1629 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1630 {
1631 char *value = NULL;
1633 if ((0 != ci->children_num) || (1 != ci->values_num)
1634 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1635 log_err ("BaseName expects a single string argument.");
1636 return 1;
1637 }
1639 value = ci->values[0].value.string;
1641 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1642 sstrncpy (base_name, value, sizeof (base_name));
1643 return 0;
1644 } /* static int perl_config_basename (oconfig_item_it *) */
1646 /*
1647 * EnableDebugger "<Package>"|""
1648 */
1649 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1650 {
1651 char *value = NULL;
1653 if ((0 != ci->children_num) || (1 != ci->values_num)
1654 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1655 log_err ("EnableDebugger expects a single string argument.");
1656 return 1;
1657 }
1659 if (NULL != perl_threads) {
1660 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1661 return 1;
1662 }
1664 value = ci->values[0].value.string;
1666 perl_argv = (char **)realloc (perl_argv,
1667 (++perl_argc + 1) * sizeof (char *));
1669 if (NULL == perl_argv) {
1670 log_err ("perl_config: Not enough memory.");
1671 exit (3);
1672 }
1674 if ('\0' == value[0]) {
1675 perl_argv[perl_argc - 1] = "-d";
1676 }
1677 else {
1678 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1679 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1680 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1681 }
1683 perl_argv[perl_argc] = NULL;
1684 return 0;
1685 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1687 /*
1688 * IncludeDir "<Dir>"
1689 */
1690 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1691 {
1692 char *value = NULL;
1694 if ((0 != ci->children_num) || (1 != ci->values_num)
1695 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1696 log_err ("IncludeDir expects a single string argument.");
1697 return 1;
1698 }
1700 value = ci->values[0].value.string;
1702 if (NULL == aTHX) {
1703 perl_argv = (char **)realloc (perl_argv,
1704 (++perl_argc + 1) * sizeof (char *));
1706 if (NULL == perl_argv) {
1707 log_err ("perl_config: Not enough memory.");
1708 exit (3);
1709 }
1711 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1712 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1713 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1715 perl_argv[perl_argc] = NULL;
1716 }
1717 else {
1718 /* prepend the directory to @INC */
1719 av_unshift (GvAVn (PL_incgv), 1);
1720 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1721 }
1722 return 0;
1723 } /* static int perl_config_includedir (oconfig_item_it *) */
1725 /*
1726 * <Plugin> block
1727 */
1728 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
1729 {
1730 int retvals = 0;
1731 int ret = 0;
1733 char *plugin;
1734 HV *config;
1736 dSP;
1738 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1739 log_err ("LoadPlugin expects a single string argument.");
1740 return 1;
1741 }
1743 plugin = ci->values[0].value.string;
1744 config = newHV ();
1746 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1747 hv_clear (config);
1748 hv_undef (config);
1750 log_err ("Unable to convert configuration to a Perl hash value.");
1751 config = Nullhv;
1752 }
1754 ENTER;
1755 SAVETMPS;
1757 PUSHMARK (SP);
1759 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
1760 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1762 PUTBACK;
1764 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
1766 SPAGAIN;
1767 if (0 < retvals) {
1768 SV *tmp = POPs;
1769 if (! SvTRUE (tmp))
1770 ret = 1;
1771 }
1772 else
1773 ret = 1;
1775 PUTBACK;
1776 FREETMPS;
1777 LEAVE;
1778 return ret;
1779 } /* static int perl_config_plugin (oconfig_item_it *) */
1781 static int perl_config (oconfig_item_t *ci)
1782 {
1783 int status = 0;
1784 int i = 0;
1786 dTHXa (NULL);
1788 for (i = 0; i < ci->children_num; ++i) {
1789 oconfig_item_t *c = ci->children + i;
1790 int current_status = 0;
1792 if (NULL != perl_threads)
1793 aTHX = PERL_GET_CONTEXT;
1795 if (0 == strcasecmp (c->key, "LoadPlugin"))
1796 current_status = perl_config_loadplugin (aTHX_ c);
1797 else if (0 == strcasecmp (c->key, "BaseName"))
1798 current_status = perl_config_basename (aTHX_ c);
1799 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1800 current_status = perl_config_enabledebugger (aTHX_ c);
1801 else if (0 == strcasecmp (c->key, "IncludeDir"))
1802 current_status = perl_config_includedir (aTHX_ c);
1803 else if (0 == strcasecmp (c->key, "Plugin"))
1804 current_status = perl_config_plugin (aTHX_ c);
1805 else
1806 {
1807 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1808 current_status = 0;
1809 }
1811 /* fatal error - it's up to perl_config_* to clean up */
1812 if (0 > current_status) {
1813 log_err ("Configuration failed with a fatal error - "
1814 "plugin disabled!");
1815 return current_status;
1816 }
1818 status += current_status;
1819 }
1820 return status;
1821 } /* static int perl_config (oconfig_item_t *) */
1823 void module_register (void)
1824 {
1825 perl_argc = 4;
1826 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1828 /* default options for the Perl interpreter */
1829 perl_argv[0] = "";
1830 perl_argv[1] = "-MCollectd";
1831 perl_argv[2] = "-e";
1832 perl_argv[3] = "1";
1833 perl_argv[4] = NULL;
1835 plugin_register_complex_config ("perl", perl_config);
1836 return;
1837 } /* void module_register (void) */
1839 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */