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"
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
71 #define PLUGIN_TYPES 6
73 #define PLUGIN_DATASET 255
75 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
76 #define log_info(...) INFO ("perl: " __VA_ARGS__)
77 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
78 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
80 /* this is defined in DynaLoader.a */
81 void boot_DynaLoader (PerlInterpreter *, CV *);
83 static XS (Collectd_plugin_register_ds);
84 static XS (Collectd_plugin_unregister_ds);
85 static XS (Collectd_plugin_dispatch_values);
86 static XS (Collectd_plugin_dispatch_notification);
87 static XS (Collectd_plugin_log);
88 static XS (Collectd_call_by_name);
94 typedef struct c_ithread_s {
95 /* the thread's Perl interpreter */
96 PerlInterpreter *interp;
98 /* double linked list of threads */
99 struct c_ithread_s *prev;
100 struct c_ithread_s *next;
108 /* some usage stats */
109 int number_of_threads;
110 #endif /* COLLECT_DEBUG */
112 pthread_mutex_t mutex;
119 /* if perl_threads != NULL perl_threads->head must
120 * point to the "base" thread */
121 static c_ithread_list_t *perl_threads = NULL;
123 /* the key used to store each pthread's ithread */
124 static pthread_key_t perl_thr_key;
126 static int perl_argc = 0;
127 static char **perl_argv = NULL;
129 static char base_name[DATA_MAX_NAME_LEN] = "";
136 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
137 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
138 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
139 { "Collectd::plugin_dispatch_notification",
140 Collectd_plugin_dispatch_notification },
141 { "Collectd::plugin_log", Collectd_plugin_log },
142 { "Collectd::call_by_name", Collectd_call_by_name },
151 { "Collectd::TYPE_INIT", PLUGIN_INIT },
152 { "Collectd::TYPE_READ", PLUGIN_READ },
153 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
154 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
155 { "Collectd::TYPE_LOG", PLUGIN_LOG },
156 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
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 },
176 { "Collectd::hostname_g", hostname_g },
185 { "Collectd::interval_g", &interval_g },
190 * Helper functions for data type conversion.
205 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
209 if ((NULL == hash) || (NULL == ds))
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';
217 log_err ("hv2data_source: No DS name given.");
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.");
230 ds->type = DS_TYPE_COUNTER;
233 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
234 ds->min = SvNV (*tmp);
238 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
239 ds->max = SvNV (*tmp);
243 } /* static data_source_t *hv2data_source (HV *) */
245 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
247 const data_set_t *ds;
251 if ((NULL == name) || (NULL == array) || (NULL == value))
254 if (av_len (array) < len - 1)
255 len = av_len (array) + 1;
260 ds = plugin_get_ds (name);
262 log_err ("av2value: Unknown dataset \"%s\"", name);
266 if (ds->ds_num < len) {
267 log_warn ("av2value: Value length exceeds data set length.");
271 for (i = 0; i < len; ++i) {
272 SV **tmp = av_fetch (array, i, 0);
275 if (DS_TYPE_COUNTER == ds->ds[i].type)
276 value[i].counter = SvIV (*tmp);
278 value[i].gauge = SvNV (*tmp);
285 } /* static int av2value (char *, AV *, value_t *, int) */
287 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
291 if ((NULL == ds) || (NULL == array))
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))
303 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
306 if (! isnan (ds->ds[i].min))
307 if (NULL == hv_store (source, "min", 3,
308 newSVnv (ds->ds[i].min), 0))
311 if (! isnan (ds->ds[i].max))
312 if (NULL == hv_store (source, "max", 3,
313 newSVnv (ds->ds[i].max), 0))
316 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
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)
329 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
332 len = vl->values_len;
334 if (ds->ds_num < len) {
335 log_warn ("value2av: Value length exceeds data set length.");
340 av_extend (values, len - 1);
342 for (i = 0; i < len; ++i) {
345 if (DS_TYPE_COUNTER == ds->ds[i].type)
346 val = newSViv (vl->values[i].counter);
348 val = newSVnv (vl->values[i].gauge);
350 if (NULL == av_store (values, i, val)) {
356 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
360 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
363 if ('\0' != vl->host[0])
364 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
367 if ('\0' != vl->plugin[0])
368 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
371 if ('\0' != vl->plugin_instance[0])
372 if (NULL == hv_store (hash, "plugin_instance", 15,
373 newSVpv (vl->plugin_instance, 0), 0))
376 if ('\0' != vl->type_instance[0])
377 if (NULL == hv_store (hash, "type_instance", 13,
378 newSVpv (vl->type_instance, 0), 0))
381 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
383 static int notification2hv (pTHX_ notification_t *n, HV *hash)
385 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
389 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
392 if ('\0' != *n->message)
393 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
396 if ('\0' != *n->host)
397 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
400 if ('\0' != *n->plugin)
401 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
404 if ('\0' != *n->plugin_instance)
405 if (NULL == hv_store (hash, "plugin_instance", 15,
406 newSVpv (n->plugin_instance, 0), 0))
409 if ('\0' != *n->type)
410 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
413 if ('\0' != *n->type_instance)
414 if (NULL == hv_store (hash, "type_instance", 13,
415 newSVpv (n->type_instance, 0), 0))
418 } /* static int notification2hv (notification_t *, HV *) */
421 * Internal functions.
424 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
426 if (base_name[0] == '\0')
427 status = snprintf (buf, buf_len, "%s", module);
429 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
430 if ((status < 0) || ((unsigned int)status >= buf_len))
432 buf[buf_len - 1] = '\0';
434 } /* char *get_module_name */
437 * Add a plugin's data set definition.
439 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
445 data_source_t *ds = NULL;
446 data_set_t *set = NULL;
448 if ((NULL == name) || (NULL == dataset))
451 len = av_len (dataset);
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);
465 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
466 log_err ("pplugin_register_data_set: Invalid data source.");
470 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
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);
478 strncpy (set->type, name, DATA_MAX_NAME_LEN);
479 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
481 set->ds_num = len + 1;
484 ret = plugin_register_data_set (set);
489 } /* static int pplugin_register_data_set (char *, SV *) */
492 * Remove a plugin's data set definition.
494 static int pplugin_unregister_data_set (char *name)
498 return plugin_unregister_data_set (name);
499 } /* static int pplugin_unregister_data_set (char *) */
502 * Submit the values to the write functions.
506 * values => [ @values ],
510 * plugin_instance => $pinstance,
511 * type_instance => $tinstance,
514 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
516 value_list_t list = VALUE_LIST_INIT;
523 if ((NULL == name) || (NULL == values))
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.");
533 AV *array = (AV *)SvRV (*tmp);
534 int len = av_len (array) + 1;
539 val = (value_t *)smalloc (len * sizeof (value_t));
541 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
544 if (-1 == list.values_len) {
550 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
551 list.time = (time_t)SvIV (*tmp);
554 list.time = time (NULL);
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';
562 sstrncpy (list.host, hostname_g, sizeof (list.host));
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';
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';
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';
580 ret = plugin_dispatch_values (name, &list);
584 } /* static int pplugin_dispatch_values (char *, HV *) */
587 * Dispatch a notification.
591 * severity => $severity,
597 * plugin_instance => $instance,
598 * type_instance => $type_instance
601 static int pplugin_dispatch_notification (pTHX_ HV *notif)
610 memset (&n, 0, sizeof (n));
612 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
613 n.severity = SvIV (*tmp);
615 n.severity = NOTIF_FAILURE;
617 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
618 n.time = (time_t)SvIV (*tmp);
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));
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 *) */
652 * Call all working functions of the given type.
654 static int pplugin_call_all (pTHX_ int type, ...)
663 if ((type < 0) || (type >= PLUGIN_TYPES))
673 XPUSHs (sv_2mortal (newSViv ((IV)type)));
675 if (PLUGIN_WRITE == type) {
677 * $_[0] = $plugin_type;
692 * values => [ $v1, ... ],
696 * plugin_instance => $instance,
697 * type_instance => $type_instance
706 ds = va_arg (ap, data_set_t *);
707 vl = va_arg (ap, value_list_t *);
709 if (-1 == data_set2av (aTHX_ ds, pds)) {
716 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
723 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
724 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
725 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
727 else if (PLUGIN_LOG == type) {
733 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
734 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
736 else if (PLUGIN_NOTIF == type) {
740 * severity => $severity,
746 * plugin_instance => $instance,
747 * type_instance => $type_instance
751 HV *notif = newHV ();
753 n = va_arg (ap, notification_t *);
755 if (-1 == notification2hv (aTHX_ n, notif)) {
762 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
767 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
782 } /* static int pplugin_call_all (int, ...) */
789 * Collectd::plugin_register_data_set (type, dataset).
792 * type of the dataset
795 * dataset to be registered
797 static XS (Collectd_plugin_register_ds)
805 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
809 log_debug ("Collectd::plugin_register_data_set: "
810 "type = \"%s\", dataset = \"%s\"",
811 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
815 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
816 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
820 log_err ("Collectd::plugin_register_data_set: Invalid data.");
828 } /* static XS (Collectd_plugin_register_ds) */
831 * Collectd::plugin_unregister_data_set (type).
834 * type of the dataset
836 static XS (Collectd_plugin_unregister_ds)
841 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
845 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
846 SvPV_nolen (ST (0)));
848 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
852 } /* static XS (Collectd_plugin_register_ds) */
855 * Collectd::plugin_dispatch_values (name, values).
861 * value list to submit
863 static XS (Collectd_plugin_dispatch_values)
872 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
876 log_debug ("Collectd::plugin_dispatch_values: "
877 "name = \"%s\", values=\"%s\"",
878 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
882 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
883 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
887 if ((NULL == ST (0)) || (NULL == values))
890 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
891 (HV *)SvRV (values));
897 } /* static XS (Collectd_plugin_dispatch_values) */
900 * Collectd::plugin_dispatch_notification (notif).
903 * notification to dispatch
905 static XS (Collectd_plugin_dispatch_notification)
914 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
918 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
919 SvPV_nolen (ST (0)));
923 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
924 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
928 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
934 } /* static XS (Collectd_plugin_dispatch_notification) */
937 * Collectd::plugin_log (level, message).
940 * log level (LOG_DEBUG, ... LOG_ERR)
945 static XS (Collectd_plugin_log)
950 log_err ("Usage: Collectd::plugin_log(level, message)");
954 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
956 } /* static XS (Collectd_plugin_log) */
959 * Collectd::call_by_name (...).
961 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
963 static XS (Collectd_call_by_name)
968 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
969 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
974 name = SvPV_nolen (tmp);
976 if (NULL == get_cv (name, 0)) {
977 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
982 /* simply pass on the subroutine call without touching the stack,
983 * thus leaving any arguments and return values in place */
985 } /* static XS (Collectd_call_by_name) */
988 * collectd's perl interpreter based thread implementation.
990 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
993 /* must be called with perl_threads->mutex locked */
994 static void c_ithread_destroy (c_ithread_t *ithread)
996 dTHXa (ithread->interp);
998 assert (NULL != perl_threads);
1000 PERL_SET_CONTEXT (aTHX);
1001 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1006 --perl_threads->number_of_threads;
1007 #endif /* COLLECT_DEBUG */
1009 perl_destruct (aTHX);
1012 if (NULL == ithread->prev)
1013 perl_threads->head = ithread->next;
1015 ithread->prev->next = ithread->next;
1017 if (NULL == ithread->next)
1018 perl_threads->tail = ithread->prev;
1020 ithread->next->prev = ithread->prev;
1024 } /* static void c_ithread_destroy (c_ithread_t *) */
1026 static void c_ithread_destructor (void *arg)
1028 c_ithread_t *ithread = (c_ithread_t *)arg;
1029 c_ithread_t *t = NULL;
1031 if (NULL == perl_threads)
1034 pthread_mutex_lock (&perl_threads->mutex);
1036 for (t = perl_threads->head; NULL != t; t = t->next)
1040 /* the ithread no longer exists */
1044 c_ithread_destroy (ithread);
1046 pthread_mutex_unlock (&perl_threads->mutex);
1048 } /* static void c_ithread_destructor (void *) */
1050 /* must be called with perl_threads->mutex locked */
1051 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1053 c_ithread_t *t = NULL;
1056 assert (NULL != perl_threads);
1058 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1059 memset (t, 0, sizeof (c_ithread_t));
1061 t->interp = (NULL == base)
1063 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1068 av_clear (PL_endav);
1069 av_undef (PL_endav);
1074 ++perl_threads->number_of_threads;
1075 #endif /* COLLECT_DEBUG */
1079 if (NULL == perl_threads->tail) {
1080 perl_threads->head = t;
1084 perl_threads->tail->next = t;
1085 t->prev = perl_threads->tail;
1088 perl_threads->tail = t;
1090 pthread_setspecific (perl_thr_key, (const void *)t);
1092 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1095 * Interface to collectd.
1098 static int perl_init (void)
1102 if (NULL == perl_threads)
1106 c_ithread_t *t = NULL;
1108 pthread_mutex_lock (&perl_threads->mutex);
1109 t = c_ithread_create (perl_threads->head->interp);
1110 pthread_mutex_unlock (&perl_threads->mutex);
1115 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1116 aTHX, perl_threads->number_of_threads);
1117 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1118 } /* static int perl_init (void) */
1120 static int perl_read (void)
1124 if (NULL == perl_threads)
1128 c_ithread_t *t = NULL;
1130 pthread_mutex_lock (&perl_threads->mutex);
1131 t = c_ithread_create (perl_threads->head->interp);
1132 pthread_mutex_unlock (&perl_threads->mutex);
1137 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1138 aTHX, perl_threads->number_of_threads);
1139 return pplugin_call_all (aTHX_ PLUGIN_READ);
1140 } /* static int perl_read (void) */
1142 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1146 if (NULL == perl_threads)
1150 c_ithread_t *t = NULL;
1152 pthread_mutex_lock (&perl_threads->mutex);
1153 t = c_ithread_create (perl_threads->head->interp);
1154 pthread_mutex_unlock (&perl_threads->mutex);
1159 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1160 aTHX, perl_threads->number_of_threads);
1161 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1162 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1164 static void perl_log (int level, const char *msg)
1168 if (NULL == perl_threads)
1172 c_ithread_t *t = NULL;
1174 pthread_mutex_lock (&perl_threads->mutex);
1175 t = c_ithread_create (perl_threads->head->interp);
1176 pthread_mutex_unlock (&perl_threads->mutex);
1181 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1183 } /* static void perl_log (int, const char *) */
1185 static int perl_notify (const notification_t *notif)
1189 if (NULL == perl_threads)
1193 c_ithread_t *t = NULL;
1195 pthread_mutex_lock (&perl_threads->mutex);
1196 t = c_ithread_create (perl_threads->head->interp);
1197 pthread_mutex_unlock (&perl_threads->mutex);
1201 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1202 } /* static int perl_notify (const notification_t *) */
1204 static int perl_shutdown (void)
1206 c_ithread_t *t = NULL;
1212 plugin_unregister_complex_config ("perl");
1214 if (NULL == perl_threads)
1218 c_ithread_t *t = NULL;
1220 pthread_mutex_lock (&perl_threads->mutex);
1221 t = c_ithread_create (perl_threads->head->interp);
1222 pthread_mutex_unlock (&perl_threads->mutex);
1227 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1228 aTHX, perl_threads->number_of_threads);
1230 plugin_unregister_log ("perl");
1231 plugin_unregister_notification ("perl");
1232 plugin_unregister_init ("perl");
1233 plugin_unregister_read ("perl");
1234 plugin_unregister_write ("perl");
1236 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1238 pthread_mutex_lock (&perl_threads->mutex);
1239 t = perl_threads->tail;
1242 c_ithread_t *thr = t;
1244 /* the pointer has to be advanced before destroying
1245 * the thread as this will free the memory */
1248 c_ithread_destroy (thr);
1251 pthread_mutex_unlock (&perl_threads->mutex);
1252 pthread_mutex_destroy (&perl_threads->mutex);
1254 sfree (perl_threads);
1256 pthread_key_delete (perl_thr_key);
1260 plugin_unregister_shutdown ("perl");
1262 } /* static void perl_shutdown (void) */
1265 * Access functions for global variables.
1267 * These functions implement the "magic" used to access
1268 * the global variables from Perl.
1271 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1273 char *pv = mg->mg_ptr;
1276 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1278 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1280 char *pv = mg->mg_ptr;
1281 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1282 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1284 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1286 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1288 int *iv = (int *)mg->mg_ptr;
1289 sv_setiv (var, *iv);
1291 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1293 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1295 int *iv = (int *)mg->mg_ptr;
1296 *iv = (int)SvIV (var);
1298 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1300 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1301 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1303 /* bootstrap the Collectd module */
1304 static void xs_init (pTHX)
1308 char *file = __FILE__;
1314 /* enable usage of Perl modules using shared libraries */
1315 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1318 for (i = 0; NULL != api[i].f; ++i)
1319 newXS (api[i].name, api[i].f, file);
1321 stash = gv_stashpv ("Collectd", 1);
1323 /* export "constants" */
1324 for (i = 0; '\0' != constants[i].name[0]; ++i)
1325 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1327 /* export global variables
1328 * by adding "magic" to the SV's representing the globale variables
1329 * perl is able to automagically call the get/set function when
1330 * accessing any such variable (this is basically the same as using
1332 /* global strings */
1333 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1334 tmp = get_sv (g_strings[i].name, 1);
1335 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1336 g_strings[i].var, 0);
1339 /* global integers */
1340 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1341 tmp = get_sv (g_integers[i].name, 1);
1342 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1343 (char *)g_integers[i].var, 0);
1346 } /* static void xs_init (pTHX) */
1348 /* Initialize the global Perl interpreter. */
1349 static int init_pi (int argc, char **argv)
1353 if (NULL != perl_threads)
1356 log_info ("Initializing Perl interpreter...");
1361 for (i = 0; i < argc; ++i)
1362 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1364 #endif /* COLLECT_DEBUG */
1366 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1367 log_err ("init_pi: pthread_key_create failed");
1369 /* this must not happen - cowardly giving up if it does */
1374 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1375 * triggers a "value computed is not used" warning by gcc. */
1378 PERL_SYS_INIT3 (&argc, &argv, &environ);
1380 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1381 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1383 pthread_mutex_init (&perl_threads->mutex, NULL);
1384 /* locking the mutex should not be necessary at this point
1385 * but let's just do it for the sake of completeness */
1386 pthread_mutex_lock (&perl_threads->mutex);
1388 perl_threads->head = c_ithread_create (NULL);
1389 perl_threads->tail = perl_threads->head;
1391 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1392 log_err ("init_pi: Not enough memory.");
1396 aTHX = perl_threads->head->interp;
1397 pthread_mutex_unlock (&perl_threads->mutex);
1399 perl_construct (aTHX);
1401 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1403 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1404 log_err ("init_pi: Unable to bootstrap Collectd.");
1408 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1409 sv_setpv (get_sv ("0", 0), "collectd");
1413 plugin_register_log ("perl", perl_log);
1414 plugin_register_notification ("perl", perl_notify);
1415 plugin_register_init ("perl", perl_init);
1417 plugin_register_read ("perl", perl_read);
1419 plugin_register_write ("perl", perl_write);
1420 plugin_register_shutdown ("perl", perl_shutdown);
1422 } /* static int init_pi (const char **, const int) */
1425 * LoadPlugin "<Plugin>"
1427 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1429 char module_name[DATA_MAX_NAME_LEN];
1433 if ((0 != ci->children_num) || (1 != ci->values_num)
1434 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1435 log_err ("LoadPlugin expects a single string argument.");
1439 value = ci->values[0].value.string;
1441 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1442 log_err ("Invalid module name %s", value);
1446 init_pi (perl_argc, perl_argv);
1447 assert (NULL != perl_threads);
1448 assert (NULL != perl_threads->head);
1450 aTHX = perl_threads->head->interp;
1452 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1453 load_module (PERL_LOADMOD_NOIMPORT,
1454 newSVpv (module_name, strlen (module_name)), Nullsv);
1456 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1461 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1465 if ((0 != ci->children_num) || (1 != ci->values_num)
1466 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1467 log_err ("BaseName expects a single string argument.");
1471 value = ci->values[0].value.string;
1473 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1474 strncpy (base_name, value, sizeof (base_name));
1475 base_name[sizeof (base_name) - 1] = '\0';
1477 } /* static int perl_config_basename (oconfig_item_it *) */
1480 * EnableDebugger "<Package>"|""
1482 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1486 if ((0 != ci->children_num) || (1 != ci->values_num)
1487 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1488 log_err ("EnableDebugger expects a single string argument.");
1492 if (NULL != perl_threads) {
1493 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1497 value = ci->values[0].value.string;
1499 perl_argv = (char **)realloc (perl_argv,
1500 (++perl_argc + 1) * sizeof (char *));
1502 if (NULL == perl_argv) {
1503 log_err ("perl_config: Not enough memory.");
1507 if ('\0' == value[0]) {
1508 perl_argv[perl_argc - 1] = "-d";
1511 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1512 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1513 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1516 perl_argv[perl_argc] = NULL;
1518 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1521 * IncludeDir "<Dir>"
1523 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1527 if ((0 != ci->children_num) || (1 != ci->values_num)
1528 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1529 log_err ("IncludeDir expects a single string argument.");
1533 value = ci->values[0].value.string;
1536 perl_argv = (char **)realloc (perl_argv,
1537 (++perl_argc + 1) * sizeof (char *));
1539 if (NULL == perl_argv) {
1540 log_err ("perl_config: Not enough memory.");
1544 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1545 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1546 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1548 perl_argv[perl_argc] = NULL;
1551 /* prepend the directory to @INC */
1552 av_unshift (GvAVn (PL_incgv), 1);
1553 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1556 } /* static int perl_config_includedir (oconfig_item_it *) */
1558 static int perl_config (oconfig_item_t *ci)
1564 for (i = 0; i < ci->children_num; ++i) {
1565 oconfig_item_t *c = ci->children + i;
1567 if (NULL != perl_threads)
1568 aTHX = PERL_GET_CONTEXT;
1570 if (0 == strcasecmp (c->key, "LoadPlugin"))
1571 perl_config_loadplugin (aTHX_ c);
1572 else if (0 == strcasecmp (c->key, "BaseName"))
1573 perl_config_basename (aTHX_ c);
1574 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1575 perl_config_enabledebugger (aTHX_ c);
1576 else if (0 == strcasecmp (c->key, "IncludeDir"))
1577 perl_config_includedir (aTHX_ c);
1579 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1582 } /* static int perl_config (oconfig_item_t *) */
1584 void module_register (void)
1587 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1589 /* default options for the Perl interpreter */
1591 perl_argv[1] = "-MCollectd";
1592 perl_argv[2] = "-e";
1594 perl_argv[4] = NULL;
1596 plugin_register_complex_config ("perl", perl_config);
1598 } /* void module_register (void) */
1600 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */