2 * collectd - src/perl.c
3 * Copyright (C) 2007, 2008 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
30 #define DONT_POISON_SPRINTF_YET 1
32 #undef DONT_POISON_SPRINTF_YET
34 #include "configfile.h"
39 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
40 # pragma GCC poison sprintf
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
50 /* ... while we want the definition found in plugin.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
66 #define PLUGIN_WRITE 2
67 #define PLUGIN_SHUTDOWN 3
69 #define PLUGIN_NOTIF 5
70 #define PLUGIN_FLUSH 6
72 #define PLUGIN_TYPES 7
74 #define PLUGIN_DATASET 255
76 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
77 #define log_info(...) INFO ("perl: " __VA_ARGS__)
78 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
79 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
81 /* this is defined in DynaLoader.a */
82 void boot_DynaLoader (PerlInterpreter *, CV *);
84 static XS (Collectd_plugin_register_ds);
85 static XS (Collectd_plugin_unregister_ds);
86 static XS (Collectd_plugin_dispatch_values);
87 static XS (Collectd_plugin_flush_one);
88 static XS (Collectd_plugin_flush_all);
89 static XS (Collectd_plugin_dispatch_notification);
90 static XS (Collectd_plugin_log);
91 static XS (Collectd_call_by_name);
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;
111 /* some usage stats */
112 int number_of_threads;
113 #endif /* COLLECT_DEBUG */
115 pthread_mutex_t mutex;
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] = "";
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_one", Collectd_plugin_flush_one },
143 { "Collectd::plugin_flush_all", Collectd_plugin_flush_all },
144 { "Collectd::plugin_dispatch_notification",
145 Collectd_plugin_dispatch_notification },
146 { "Collectd::plugin_log", Collectd_plugin_log },
147 { "Collectd::call_by_name", Collectd_call_by_name },
156 { "Collectd::TYPE_INIT", PLUGIN_INIT },
157 { "Collectd::TYPE_READ", PLUGIN_READ },
158 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
159 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
160 { "Collectd::TYPE_LOG", PLUGIN_LOG },
161 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
162 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
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 },
182 { "Collectd::hostname_g", hostname_g },
191 { "Collectd::interval_g", &interval_g },
196 * Helper functions for data type conversion.
211 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
215 if ((NULL == hash) || (NULL == ds))
218 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
219 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
220 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
223 log_err ("hv2data_source: No DS name given.");
227 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
228 ds->type = SvIV (*tmp);
230 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
231 log_err ("hv2data_source: Invalid DS type.");
236 ds->type = DS_TYPE_COUNTER;
239 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
240 ds->min = SvNV (*tmp);
244 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
245 ds->max = SvNV (*tmp);
249 } /* static data_source_t *hv2data_source (HV *) */
251 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
253 const data_set_t *ds;
257 if ((NULL == name) || (NULL == array) || (NULL == value))
260 if (av_len (array) < len - 1)
261 len = av_len (array) + 1;
266 ds = plugin_get_ds (name);
268 log_err ("av2value: Unknown dataset \"%s\"", name);
272 if (ds->ds_num < len) {
273 log_warn ("av2value: Value length exceeds data set length.");
277 for (i = 0; i < len; ++i) {
278 SV **tmp = av_fetch (array, i, 0);
281 if (DS_TYPE_COUNTER == ds->ds[i].type)
282 value[i].counter = SvIV (*tmp);
284 value[i].gauge = SvNV (*tmp);
291 } /* static int av2value (char *, AV *, value_t *, int) */
293 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
297 if ((NULL == ds) || (NULL == array))
300 av_extend (array, ds->ds_num);
302 for (i = 0; i < ds->ds_num; ++i) {
303 HV *source = newHV ();
305 if (NULL == hv_store (source, "name", 4,
306 newSVpv (ds->ds[i].name, 0), 0))
309 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
312 if (! isnan (ds->ds[i].min))
313 if (NULL == hv_store (source, "min", 3,
314 newSVnv (ds->ds[i].min), 0))
317 if (! isnan (ds->ds[i].max))
318 if (NULL == hv_store (source, "max", 3,
319 newSVnv (ds->ds[i].max), 0))
322 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
326 } /* static int data_set2av (data_set_t *, AV *) */
328 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
335 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
338 len = vl->values_len;
340 if (ds->ds_num < len) {
341 log_warn ("value2av: Value length exceeds data set length.");
346 av_extend (values, len - 1);
348 for (i = 0; i < len; ++i) {
351 if (DS_TYPE_COUNTER == ds->ds[i].type)
352 val = newSViv (vl->values[i].counter);
354 val = newSVnv (vl->values[i].gauge);
356 if (NULL == av_store (values, i, val)) {
362 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
366 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
369 if ('\0' != vl->host[0])
370 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
373 if ('\0' != vl->plugin[0])
374 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
377 if ('\0' != vl->plugin_instance[0])
378 if (NULL == hv_store (hash, "plugin_instance", 15,
379 newSVpv (vl->plugin_instance, 0), 0))
382 if ('\0' != vl->type_instance[0])
383 if (NULL == hv_store (hash, "type_instance", 13,
384 newSVpv (vl->type_instance, 0), 0))
387 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
389 static int notification2hv (pTHX_ notification_t *n, HV *hash)
391 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
395 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
398 if ('\0' != *n->message)
399 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
402 if ('\0' != *n->host)
403 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
406 if ('\0' != *n->plugin)
407 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
410 if ('\0' != *n->plugin_instance)
411 if (NULL == hv_store (hash, "plugin_instance", 15,
412 newSVpv (n->plugin_instance, 0), 0))
415 if ('\0' != *n->type)
416 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
419 if ('\0' != *n->type_instance)
420 if (NULL == hv_store (hash, "type_instance", 13,
421 newSVpv (n->type_instance, 0), 0))
424 } /* static int notification2hv (notification_t *, HV *) */
427 * Internal functions.
430 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
432 if (base_name[0] == '\0')
433 status = snprintf (buf, buf_len, "%s", module);
435 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
436 if ((status < 0) || ((unsigned int)status >= buf_len))
438 buf[buf_len - 1] = '\0';
440 } /* char *get_module_name */
443 * Add a plugin's data set definition.
445 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
451 data_source_t *ds = NULL;
452 data_set_t *set = NULL;
454 if ((NULL == name) || (NULL == dataset))
457 len = av_len (dataset);
462 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
463 set = (data_set_t *)smalloc (sizeof (data_set_t));
465 for (i = 0; i <= len; ++i) {
466 SV **elem = av_fetch (dataset, i, 0);
471 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
472 log_err ("pplugin_register_data_set: Invalid data source.");
476 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
479 log_debug ("pplugin_register_data_set: "
480 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
481 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
484 strncpy (set->type, name, DATA_MAX_NAME_LEN);
485 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
487 set->ds_num = len + 1;
490 ret = plugin_register_data_set (set);
495 } /* static int pplugin_register_data_set (char *, SV *) */
498 * Remove a plugin's data set definition.
500 static int pplugin_unregister_data_set (char *name)
504 return plugin_unregister_data_set (name);
505 } /* static int pplugin_unregister_data_set (char *) */
508 * Submit the values to the write functions.
512 * values => [ @values ],
516 * plugin_instance => $pinstance,
517 * type_instance => $tinstance,
520 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
522 value_list_t list = VALUE_LIST_INIT;
529 if ((NULL == name) || (NULL == values))
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.");
539 AV *array = (AV *)SvRV (*tmp);
540 int len = av_len (array) + 1;
545 val = (value_t *)smalloc (len * sizeof (value_t));
547 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
550 if (-1 == list.values_len) {
556 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
557 list.time = (time_t)SvIV (*tmp);
560 list.time = time (NULL);
563 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
564 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
565 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
568 sstrncpy (list.host, hostname_g, sizeof (list.host));
571 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
572 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
573 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
576 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
577 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
578 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
581 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
582 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
583 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
586 ret = plugin_dispatch_values (name, &list);
590 } /* static int pplugin_dispatch_values (char *, HV *) */
593 * Dispatch a notification.
597 * severity => $severity,
603 * plugin_instance => $instance,
604 * type_instance => $type_instance
607 static int pplugin_dispatch_notification (pTHX_ HV *notif)
616 memset (&n, 0, sizeof (n));
618 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
619 n.severity = SvIV (*tmp);
621 n.severity = NOTIF_FAILURE;
623 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
624 n.time = (time_t)SvIV (*tmp);
626 n.time = time (NULL);
628 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
629 strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
630 n.message[sizeof (n.message) - 1] = '\0';
632 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
633 strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
635 strncpy (n.host, hostname_g, sizeof (n.host));
636 n.host[sizeof (n.host) - 1] = '\0';
638 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
639 strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
640 n.plugin[sizeof (n.plugin) - 1] = '\0';
642 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
643 strncpy (n.plugin_instance, SvPV_nolen (*tmp),
644 sizeof (n.plugin_instance));
645 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
647 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
648 strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
649 n.type[sizeof (n.type) - 1] = '\0';
651 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
652 strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
653 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
654 return plugin_dispatch_notification (&n);
655 } /* static int pplugin_dispatch_notification (HV *) */
658 * Call all working functions of the given type.
660 static int pplugin_call_all (pTHX_ int type, ...)
669 if ((type < 0) || (type >= PLUGIN_TYPES))
679 XPUSHs (sv_2mortal (newSViv ((IV)type)));
681 if (PLUGIN_WRITE == type) {
683 * $_[0] = $plugin_type;
698 * values => [ $v1, ... ],
702 * plugin_instance => $instance,
703 * type_instance => $type_instance
712 ds = va_arg (ap, data_set_t *);
713 vl = va_arg (ap, value_list_t *);
715 if (-1 == data_set2av (aTHX_ ds, pds)) {
722 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
729 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
730 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
731 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
733 else if (PLUGIN_LOG == type) {
739 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
740 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
742 else if (PLUGIN_NOTIF == type) {
746 * severity => $severity,
752 * plugin_instance => $instance,
753 * type_instance => $type_instance
757 HV *notif = newHV ();
759 n = va_arg (ap, notification_t *);
761 if (-1 == notification2hv (aTHX_ n, notif)) {
768 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
770 else if (PLUGIN_FLUSH == type) {
774 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
779 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
794 } /* static int pplugin_call_all (int, ...) */
801 * Collectd::plugin_register_data_set (type, dataset).
804 * type of the dataset
807 * dataset to be registered
809 static XS (Collectd_plugin_register_ds)
817 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
821 log_debug ("Collectd::plugin_register_data_set: "
822 "type = \"%s\", dataset = \"%s\"",
823 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
827 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
828 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
832 log_err ("Collectd::plugin_register_data_set: Invalid data.");
840 } /* static XS (Collectd_plugin_register_ds) */
843 * Collectd::plugin_unregister_data_set (type).
846 * type of the dataset
848 static XS (Collectd_plugin_unregister_ds)
853 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
857 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
858 SvPV_nolen (ST (0)));
860 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
864 } /* static XS (Collectd_plugin_register_ds) */
867 * Collectd::plugin_dispatch_values (name, values).
873 * value list to submit
875 static XS (Collectd_plugin_dispatch_values)
884 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
888 log_debug ("Collectd::plugin_dispatch_values: "
889 "name = \"%s\", values=\"%s\"",
890 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
894 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
895 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
899 if ((NULL == ST (0)) || (NULL == values))
902 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
903 (HV *)SvRV (values));
909 } /* static XS (Collectd_plugin_dispatch_values) */
912 * Collectd::plugin_flush_one (timeout, name).
915 * timeout to use when flushing the data
918 * name of the plugin to flush
920 static XS (Collectd_plugin_flush_one)
925 log_err ("Usage: Collectd::plugin_flush_one(timeout, name)");
929 log_debug ("Collectd::plugin_flush_one: timeout = %i, name = \"%s\"",
930 (int)SvIV (ST (0)), SvPV_nolen (ST (1)));
932 if (0 == plugin_flush_one ((int)SvIV (ST (0)), SvPV_nolen (ST (1))))
936 } /* static XS (Collectd_plugin_flush_one) */
939 * Collectd::plugin_flush_all (timeout).
942 * timeout to use when flushing the data
944 static XS (Collectd_plugin_flush_all)
949 log_err ("Usage: Collectd::plugin_flush_all(timeout)");
953 log_debug ("Collectd::plugin_flush_all: timeout = %i", (int)SvIV (ST (0)));
955 plugin_flush_all ((int)SvIV (ST (0)));
957 } /* static XS (Collectd_plugin_flush_all) */
960 * Collectd::plugin_dispatch_notification (notif).
963 * notification to dispatch
965 static XS (Collectd_plugin_dispatch_notification)
974 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
978 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
979 SvPV_nolen (ST (0)));
983 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
984 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
988 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
994 } /* static XS (Collectd_plugin_dispatch_notification) */
997 * Collectd::plugin_log (level, message).
1000 * log level (LOG_DEBUG, ... LOG_ERR)
1005 static XS (Collectd_plugin_log)
1010 log_err ("Usage: Collectd::plugin_log(level, message)");
1014 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
1016 } /* static XS (Collectd_plugin_log) */
1019 * Collectd::call_by_name (...).
1021 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1023 static XS (Collectd_call_by_name)
1028 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1029 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1034 name = SvPV_nolen (tmp);
1036 if (NULL == get_cv (name, 0)) {
1037 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1042 /* simply pass on the subroutine call without touching the stack,
1043 * thus leaving any arguments and return values in place */
1045 } /* static XS (Collectd_call_by_name) */
1048 * collectd's perl interpreter based thread implementation.
1050 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1053 /* must be called with perl_threads->mutex locked */
1054 static void c_ithread_destroy (c_ithread_t *ithread)
1056 dTHXa (ithread->interp);
1058 assert (NULL != perl_threads);
1060 PERL_SET_CONTEXT (aTHX);
1061 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1066 --perl_threads->number_of_threads;
1067 #endif /* COLLECT_DEBUG */
1069 perl_destruct (aTHX);
1072 if (NULL == ithread->prev)
1073 perl_threads->head = ithread->next;
1075 ithread->prev->next = ithread->next;
1077 if (NULL == ithread->next)
1078 perl_threads->tail = ithread->prev;
1080 ithread->next->prev = ithread->prev;
1084 } /* static void c_ithread_destroy (c_ithread_t *) */
1086 static void c_ithread_destructor (void *arg)
1088 c_ithread_t *ithread = (c_ithread_t *)arg;
1089 c_ithread_t *t = NULL;
1091 if (NULL == perl_threads)
1094 pthread_mutex_lock (&perl_threads->mutex);
1096 for (t = perl_threads->head; NULL != t; t = t->next)
1100 /* the ithread no longer exists */
1104 c_ithread_destroy (ithread);
1106 pthread_mutex_unlock (&perl_threads->mutex);
1108 } /* static void c_ithread_destructor (void *) */
1110 /* must be called with perl_threads->mutex locked */
1111 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1113 c_ithread_t *t = NULL;
1116 assert (NULL != perl_threads);
1118 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1119 memset (t, 0, sizeof (c_ithread_t));
1121 t->interp = (NULL == base)
1123 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1127 if ((NULL != base) && (NULL != PL_endav)) {
1128 av_clear (PL_endav);
1129 av_undef (PL_endav);
1134 ++perl_threads->number_of_threads;
1135 #endif /* COLLECT_DEBUG */
1139 if (NULL == perl_threads->tail) {
1140 perl_threads->head = t;
1144 perl_threads->tail->next = t;
1145 t->prev = perl_threads->tail;
1148 perl_threads->tail = t;
1150 pthread_setspecific (perl_thr_key, (const void *)t);
1152 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1155 * Interface to collectd.
1158 static int perl_init (void)
1162 if (NULL == perl_threads)
1166 c_ithread_t *t = NULL;
1168 pthread_mutex_lock (&perl_threads->mutex);
1169 t = c_ithread_create (perl_threads->head->interp);
1170 pthread_mutex_unlock (&perl_threads->mutex);
1175 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1176 aTHX, perl_threads->number_of_threads);
1177 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1178 } /* static int perl_init (void) */
1180 static int perl_read (void)
1184 if (NULL == perl_threads)
1188 c_ithread_t *t = NULL;
1190 pthread_mutex_lock (&perl_threads->mutex);
1191 t = c_ithread_create (perl_threads->head->interp);
1192 pthread_mutex_unlock (&perl_threads->mutex);
1197 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1198 aTHX, perl_threads->number_of_threads);
1199 return pplugin_call_all (aTHX_ PLUGIN_READ);
1200 } /* static int perl_read (void) */
1202 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1206 if (NULL == perl_threads)
1210 c_ithread_t *t = NULL;
1212 pthread_mutex_lock (&perl_threads->mutex);
1213 t = c_ithread_create (perl_threads->head->interp);
1214 pthread_mutex_unlock (&perl_threads->mutex);
1219 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1220 aTHX, perl_threads->number_of_threads);
1221 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1222 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1224 static void perl_log (int level, const char *msg)
1228 if (NULL == perl_threads)
1232 c_ithread_t *t = NULL;
1234 pthread_mutex_lock (&perl_threads->mutex);
1235 t = c_ithread_create (perl_threads->head->interp);
1236 pthread_mutex_unlock (&perl_threads->mutex);
1241 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1243 } /* static void perl_log (int, const char *) */
1245 static int perl_notify (const notification_t *notif)
1249 if (NULL == perl_threads)
1253 c_ithread_t *t = NULL;
1255 pthread_mutex_lock (&perl_threads->mutex);
1256 t = c_ithread_create (perl_threads->head->interp);
1257 pthread_mutex_unlock (&perl_threads->mutex);
1261 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1262 } /* static int perl_notify (const notification_t *) */
1264 static int perl_flush (const int timeout)
1268 if (NULL == perl_threads)
1272 c_ithread_t *t = NULL;
1274 pthread_mutex_lock (&perl_threads->mutex);
1275 t = c_ithread_create (perl_threads->head->interp);
1276 pthread_mutex_unlock (&perl_threads->mutex);
1280 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout);
1281 } /* static int perl_flush (const int) */
1283 static int perl_shutdown (void)
1285 c_ithread_t *t = NULL;
1291 plugin_unregister_complex_config ("perl");
1293 if (NULL == perl_threads)
1297 c_ithread_t *t = NULL;
1299 pthread_mutex_lock (&perl_threads->mutex);
1300 t = c_ithread_create (perl_threads->head->interp);
1301 pthread_mutex_unlock (&perl_threads->mutex);
1306 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1307 aTHX, perl_threads->number_of_threads);
1309 plugin_unregister_log ("perl");
1310 plugin_unregister_notification ("perl");
1311 plugin_unregister_init ("perl");
1312 plugin_unregister_read ("perl");
1313 plugin_unregister_write ("perl");
1314 plugin_unregister_flush ("perl");
1316 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1318 pthread_mutex_lock (&perl_threads->mutex);
1319 t = perl_threads->tail;
1322 c_ithread_t *thr = t;
1324 /* the pointer has to be advanced before destroying
1325 * the thread as this will free the memory */
1328 c_ithread_destroy (thr);
1331 pthread_mutex_unlock (&perl_threads->mutex);
1332 pthread_mutex_destroy (&perl_threads->mutex);
1334 sfree (perl_threads);
1336 pthread_key_delete (perl_thr_key);
1340 plugin_unregister_shutdown ("perl");
1342 } /* static void perl_shutdown (void) */
1345 * Access functions for global variables.
1347 * These functions implement the "magic" used to access
1348 * the global variables from Perl.
1351 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1353 char *pv = mg->mg_ptr;
1356 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1358 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1360 char *pv = mg->mg_ptr;
1361 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1362 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1364 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1366 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1368 int *iv = (int *)mg->mg_ptr;
1369 sv_setiv (var, *iv);
1371 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1373 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1375 int *iv = (int *)mg->mg_ptr;
1376 *iv = (int)SvIV (var);
1378 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1380 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1381 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1383 /* bootstrap the Collectd module */
1384 static void xs_init (pTHX)
1388 char *file = __FILE__;
1394 /* enable usage of Perl modules using shared libraries */
1395 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1398 for (i = 0; NULL != api[i].f; ++i)
1399 newXS (api[i].name, api[i].f, file);
1401 stash = gv_stashpv ("Collectd", 1);
1403 /* export "constants" */
1404 for (i = 0; '\0' != constants[i].name[0]; ++i)
1405 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1407 /* export global variables
1408 * by adding "magic" to the SV's representing the globale variables
1409 * perl is able to automagically call the get/set function when
1410 * accessing any such variable (this is basically the same as using
1412 /* global strings */
1413 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1414 tmp = get_sv (g_strings[i].name, 1);
1415 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1416 g_strings[i].var, 0);
1419 /* global integers */
1420 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1421 tmp = get_sv (g_integers[i].name, 1);
1422 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1423 (char *)g_integers[i].var, 0);
1426 } /* static void xs_init (pTHX) */
1428 /* Initialize the global Perl interpreter. */
1429 static int init_pi (int argc, char **argv)
1433 if (NULL != perl_threads)
1436 log_info ("Initializing Perl interpreter...");
1441 for (i = 0; i < argc; ++i)
1442 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1444 #endif /* COLLECT_DEBUG */
1446 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1447 log_err ("init_pi: pthread_key_create failed");
1449 /* this must not happen - cowardly giving up if it does */
1454 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1455 * triggers a "value computed is not used" warning by gcc. */
1458 PERL_SYS_INIT3 (&argc, &argv, &environ);
1460 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1461 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1463 pthread_mutex_init (&perl_threads->mutex, NULL);
1464 /* locking the mutex should not be necessary at this point
1465 * but let's just do it for the sake of completeness */
1466 pthread_mutex_lock (&perl_threads->mutex);
1468 perl_threads->head = c_ithread_create (NULL);
1469 perl_threads->tail = perl_threads->head;
1471 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1472 log_err ("init_pi: Not enough memory.");
1476 aTHX = perl_threads->head->interp;
1477 pthread_mutex_unlock (&perl_threads->mutex);
1479 perl_construct (aTHX);
1481 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1483 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1484 SV *err = get_sv ("@", 1);
1485 log_err ("init_pi: Unable to bootstrap Collectd: %s",
1488 perl_destruct (perl_threads->head->interp);
1489 perl_free (perl_threads->head->interp);
1490 sfree (perl_threads);
1492 pthread_key_delete (perl_thr_key);
1496 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1497 sv_setpv (get_sv ("0", 0), "collectd");
1501 plugin_register_log ("perl", perl_log);
1502 plugin_register_notification ("perl", perl_notify);
1503 plugin_register_init ("perl", perl_init);
1505 plugin_register_read ("perl", perl_read);
1507 plugin_register_write ("perl", perl_write);
1508 plugin_register_flush ("perl", perl_flush);
1509 plugin_register_shutdown ("perl", perl_shutdown);
1511 } /* static int init_pi (const char **, const int) */
1514 * LoadPlugin "<Plugin>"
1516 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1518 char module_name[DATA_MAX_NAME_LEN];
1522 if ((0 != ci->children_num) || (1 != ci->values_num)
1523 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1524 log_err ("LoadPlugin expects a single string argument.");
1528 value = ci->values[0].value.string;
1530 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1531 log_err ("Invalid module name %s", value);
1535 if (0 != init_pi (perl_argc, perl_argv))
1538 assert (NULL != perl_threads);
1539 assert (NULL != perl_threads->head);
1541 aTHX = perl_threads->head->interp;
1543 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1544 load_module (PERL_LOADMOD_NOIMPORT,
1545 newSVpv (module_name, strlen (module_name)), Nullsv);
1547 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1552 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1556 if ((0 != ci->children_num) || (1 != ci->values_num)
1557 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1558 log_err ("BaseName expects a single string argument.");
1562 value = ci->values[0].value.string;
1564 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1565 strncpy (base_name, value, sizeof (base_name));
1566 base_name[sizeof (base_name) - 1] = '\0';
1568 } /* static int perl_config_basename (oconfig_item_it *) */
1571 * EnableDebugger "<Package>"|""
1573 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1577 if ((0 != ci->children_num) || (1 != ci->values_num)
1578 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1579 log_err ("EnableDebugger expects a single string argument.");
1583 if (NULL != perl_threads) {
1584 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1588 value = ci->values[0].value.string;
1590 perl_argv = (char **)realloc (perl_argv,
1591 (++perl_argc + 1) * sizeof (char *));
1593 if (NULL == perl_argv) {
1594 log_err ("perl_config: Not enough memory.");
1598 if ('\0' == value[0]) {
1599 perl_argv[perl_argc - 1] = "-d";
1602 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1603 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1604 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1607 perl_argv[perl_argc] = NULL;
1609 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1612 * IncludeDir "<Dir>"
1614 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1618 if ((0 != ci->children_num) || (1 != ci->values_num)
1619 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1620 log_err ("IncludeDir expects a single string argument.");
1624 value = ci->values[0].value.string;
1627 perl_argv = (char **)realloc (perl_argv,
1628 (++perl_argc + 1) * sizeof (char *));
1630 if (NULL == perl_argv) {
1631 log_err ("perl_config: Not enough memory.");
1635 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1636 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1637 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1639 perl_argv[perl_argc] = NULL;
1642 /* prepend the directory to @INC */
1643 av_unshift (GvAVn (PL_incgv), 1);
1644 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1647 } /* static int perl_config_includedir (oconfig_item_it *) */
1649 static int perl_config (oconfig_item_t *ci)
1655 for (i = 0; i < ci->children_num; ++i) {
1656 oconfig_item_t *c = ci->children + i;
1658 if (NULL != perl_threads)
1659 aTHX = PERL_GET_CONTEXT;
1661 if (0 == strcasecmp (c->key, "LoadPlugin"))
1662 perl_config_loadplugin (aTHX_ c);
1663 else if (0 == strcasecmp (c->key, "BaseName"))
1664 perl_config_basename (aTHX_ c);
1665 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1666 perl_config_enabledebugger (aTHX_ c);
1667 else if (0 == strcasecmp (c->key, "IncludeDir"))
1668 perl_config_includedir (aTHX_ c);
1670 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1673 } /* static int perl_config (oconfig_item_t *) */
1675 void module_register (void)
1678 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1680 /* default options for the Perl interpreter */
1682 perl_argv[1] = "-MCollectd";
1683 perl_argv[2] = "-e";
1685 perl_argv[4] = NULL;
1687 plugin_register_complex_config ("perl", perl_config);
1689 } /* void module_register (void) */
1691 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */