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
32 #include "configfile.h"
39 /* Some versions of Perl define their own version of DEBUG... :-/ */
44 /* ... while we want the definition found in plugin.h. */
50 #if !defined(USE_ITHREADS)
51 # error "Perl does not support ithreads!"
52 #endif /* !defined(USE_ITHREADS) */
54 /* clear the Perl sub's stack frame
55 * (this should only be used inside an XSUB) */
56 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
60 #define PLUGIN_WRITE 2
61 #define PLUGIN_SHUTDOWN 3
63 #define PLUGIN_NOTIF 5
64 #define PLUGIN_FLUSH 6
66 #define PLUGIN_TYPES 7
68 #define PLUGIN_DATASET 255
70 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
71 #define log_info(...) INFO ("perl: " __VA_ARGS__)
72 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
73 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
75 /* this is defined in DynaLoader.a */
76 void boot_DynaLoader (PerlInterpreter *, CV *);
78 static XS (Collectd_plugin_register_ds);
79 static XS (Collectd_plugin_unregister_ds);
80 static XS (Collectd_plugin_dispatch_values);
81 static XS (Collectd_plugin_flush_one);
82 static XS (Collectd_plugin_flush_all);
83 static XS (Collectd_plugin_dispatch_notification);
84 static XS (Collectd_plugin_log);
85 static XS (Collectd_call_by_name);
91 typedef struct c_ithread_s {
92 /* the thread's Perl interpreter */
93 PerlInterpreter *interp;
95 /* double linked list of threads */
96 struct c_ithread_s *prev;
97 struct c_ithread_s *next;
105 /* some usage stats */
106 int number_of_threads;
107 #endif /* COLLECT_DEBUG */
109 pthread_mutex_t mutex;
116 /* if perl_threads != NULL perl_threads->head must
117 * point to the "base" thread */
118 static c_ithread_list_t *perl_threads = NULL;
120 /* the key used to store each pthread's ithread */
121 static pthread_key_t perl_thr_key;
123 static int perl_argc = 0;
124 static char **perl_argv = NULL;
126 static char base_name[DATA_MAX_NAME_LEN] = "";
133 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
134 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
135 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
136 { "Collectd::plugin_flush_one", Collectd_plugin_flush_one },
137 { "Collectd::plugin_flush_all", Collectd_plugin_flush_all },
138 { "Collectd::plugin_dispatch_notification",
139 Collectd_plugin_dispatch_notification },
140 { "Collectd::plugin_log", Collectd_plugin_log },
141 { "Collectd::call_by_name", Collectd_call_by_name },
150 { "Collectd::TYPE_INIT", PLUGIN_INIT },
151 { "Collectd::TYPE_READ", PLUGIN_READ },
152 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
153 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
154 { "Collectd::TYPE_LOG", PLUGIN_LOG },
155 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
156 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
157 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
158 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
159 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
160 { "Collectd::LOG_ERR", LOG_ERR },
161 { "Collectd::LOG_WARNING", LOG_WARNING },
162 { "Collectd::LOG_NOTICE", LOG_NOTICE },
163 { "Collectd::LOG_INFO", LOG_INFO },
164 { "Collectd::LOG_DEBUG", LOG_DEBUG },
165 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
166 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
167 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
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 strcpy (list.host, hostname_g);
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)));
764 else if (PLUGIN_FLUSH == type) {
768 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
773 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
788 } /* static int pplugin_call_all (int, ...) */
795 * Collectd::plugin_register_data_set (type, dataset).
798 * type of the dataset
801 * dataset to be registered
803 static XS (Collectd_plugin_register_ds)
811 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
815 log_debug ("Collectd::plugin_register_data_set: "
816 "type = \"%s\", dataset = \"%s\"",
817 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
821 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
822 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
826 log_err ("Collectd::plugin_register_data_set: Invalid data.");
834 } /* static XS (Collectd_plugin_register_ds) */
837 * Collectd::plugin_unregister_data_set (type).
840 * type of the dataset
842 static XS (Collectd_plugin_unregister_ds)
847 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
851 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
852 SvPV_nolen (ST (0)));
854 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
858 } /* static XS (Collectd_plugin_register_ds) */
861 * Collectd::plugin_dispatch_values (name, values).
867 * value list to submit
869 static XS (Collectd_plugin_dispatch_values)
878 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
882 log_debug ("Collectd::plugin_dispatch_values: "
883 "name = \"%s\", values=\"%s\"",
884 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
888 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
889 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
893 if ((NULL == ST (0)) || (NULL == values))
896 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
897 (HV *)SvRV (values));
903 } /* static XS (Collectd_plugin_dispatch_values) */
906 * Collectd::plugin_flush_one (timeout, name).
909 * timeout to use when flushing the data
912 * name of the plugin to flush
914 static XS (Collectd_plugin_flush_one)
919 log_err ("Usage: Collectd::plugin_flush_one(timeout, name)");
923 log_debug ("Collectd::plugin_flush_one: timeout = %i, name = \"%s\"",
924 (int)SvIV (ST (0)), SvPV_nolen (ST (1)));
926 if (0 == plugin_flush_one ((int)SvIV (ST (0)), SvPV_nolen (ST (1))))
930 } /* static XS (Collectd_plugin_flush_one) */
933 * Collectd::plugin_flush_all (timeout).
936 * timeout to use when flushing the data
938 static XS (Collectd_plugin_flush_all)
943 log_err ("Usage: Collectd::plugin_flush_all(timeout)");
947 log_debug ("Collectd::plugin_flush_all: timeout = %i", (int)SvIV (ST (0)));
949 plugin_flush_all ((int)SvIV (ST (0)));
951 } /* static XS (Collectd_plugin_flush_all) */
954 * Collectd::plugin_dispatch_notification (notif).
957 * notification to dispatch
959 static XS (Collectd_plugin_dispatch_notification)
968 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
972 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
973 SvPV_nolen (ST (0)));
977 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
978 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
982 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
988 } /* static XS (Collectd_plugin_dispatch_notification) */
991 * Collectd::plugin_log (level, message).
994 * log level (LOG_DEBUG, ... LOG_ERR)
999 static XS (Collectd_plugin_log)
1004 log_err ("Usage: Collectd::plugin_log(level, message)");
1008 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
1010 } /* static XS (Collectd_plugin_log) */
1013 * Collectd::call_by_name (...).
1015 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1017 static XS (Collectd_call_by_name)
1022 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1023 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1028 name = SvPV_nolen (tmp);
1030 if (NULL == get_cv (name, 0)) {
1031 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1036 /* simply pass on the subroutine call without touching the stack,
1037 * thus leaving any arguments and return values in place */
1039 } /* static XS (Collectd_call_by_name) */
1042 * collectd's perl interpreter based thread implementation.
1044 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1047 /* must be called with perl_threads->mutex locked */
1048 static void c_ithread_destroy (c_ithread_t *ithread)
1050 dTHXa (ithread->interp);
1052 assert (NULL != perl_threads);
1054 PERL_SET_CONTEXT (aTHX);
1055 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1060 --perl_threads->number_of_threads;
1061 #endif /* COLLECT_DEBUG */
1063 perl_destruct (aTHX);
1066 if (NULL == ithread->prev)
1067 perl_threads->head = ithread->next;
1069 ithread->prev->next = ithread->next;
1071 if (NULL == ithread->next)
1072 perl_threads->tail = ithread->prev;
1074 ithread->next->prev = ithread->prev;
1078 } /* static void c_ithread_destroy (c_ithread_t *) */
1080 static void c_ithread_destructor (void *arg)
1082 c_ithread_t *ithread = (c_ithread_t *)arg;
1083 c_ithread_t *t = NULL;
1085 if (NULL == perl_threads)
1088 pthread_mutex_lock (&perl_threads->mutex);
1090 for (t = perl_threads->head; NULL != t; t = t->next)
1094 /* the ithread no longer exists */
1098 c_ithread_destroy (ithread);
1100 pthread_mutex_unlock (&perl_threads->mutex);
1102 } /* static void c_ithread_destructor (void *) */
1104 /* must be called with perl_threads->mutex locked */
1105 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1107 c_ithread_t *t = NULL;
1110 assert (NULL != perl_threads);
1112 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1113 memset (t, 0, sizeof (c_ithread_t));
1115 t->interp = (NULL == base)
1117 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1122 av_clear (PL_endav);
1123 av_undef (PL_endav);
1128 ++perl_threads->number_of_threads;
1129 #endif /* COLLECT_DEBUG */
1133 if (NULL == perl_threads->tail) {
1134 perl_threads->head = t;
1138 perl_threads->tail->next = t;
1139 t->prev = perl_threads->tail;
1142 perl_threads->tail = t;
1144 pthread_setspecific (perl_thr_key, (const void *)t);
1146 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1149 * Interface to collectd.
1152 static int perl_init (void)
1156 if (NULL == perl_threads)
1160 c_ithread_t *t = NULL;
1162 pthread_mutex_lock (&perl_threads->mutex);
1163 t = c_ithread_create (perl_threads->head->interp);
1164 pthread_mutex_unlock (&perl_threads->mutex);
1169 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1170 aTHX, perl_threads->number_of_threads);
1171 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1172 } /* static int perl_init (void) */
1174 static int perl_read (void)
1178 if (NULL == perl_threads)
1182 c_ithread_t *t = NULL;
1184 pthread_mutex_lock (&perl_threads->mutex);
1185 t = c_ithread_create (perl_threads->head->interp);
1186 pthread_mutex_unlock (&perl_threads->mutex);
1191 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1192 aTHX, perl_threads->number_of_threads);
1193 return pplugin_call_all (aTHX_ PLUGIN_READ);
1194 } /* static int perl_read (void) */
1196 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1200 if (NULL == perl_threads)
1204 c_ithread_t *t = NULL;
1206 pthread_mutex_lock (&perl_threads->mutex);
1207 t = c_ithread_create (perl_threads->head->interp);
1208 pthread_mutex_unlock (&perl_threads->mutex);
1213 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1214 aTHX, perl_threads->number_of_threads);
1215 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1216 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1218 static void perl_log (int level, const char *msg)
1222 if (NULL == perl_threads)
1226 c_ithread_t *t = NULL;
1228 pthread_mutex_lock (&perl_threads->mutex);
1229 t = c_ithread_create (perl_threads->head->interp);
1230 pthread_mutex_unlock (&perl_threads->mutex);
1235 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1237 } /* static void perl_log (int, const char *) */
1239 static int perl_notify (const notification_t *notif)
1243 if (NULL == perl_threads)
1247 c_ithread_t *t = NULL;
1249 pthread_mutex_lock (&perl_threads->mutex);
1250 t = c_ithread_create (perl_threads->head->interp);
1251 pthread_mutex_unlock (&perl_threads->mutex);
1255 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1256 } /* static int perl_notify (const notification_t *) */
1258 static int perl_flush (const int timeout)
1262 if (NULL == perl_threads)
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);
1274 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout);
1275 } /* static int perl_flush (const int) */
1277 static int perl_shutdown (void)
1279 c_ithread_t *t = NULL;
1285 plugin_unregister_complex_config ("perl");
1287 if (NULL == perl_threads)
1291 c_ithread_t *t = NULL;
1293 pthread_mutex_lock (&perl_threads->mutex);
1294 t = c_ithread_create (perl_threads->head->interp);
1295 pthread_mutex_unlock (&perl_threads->mutex);
1300 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1301 aTHX, perl_threads->number_of_threads);
1303 plugin_unregister_log ("perl");
1304 plugin_unregister_notification ("perl");
1305 plugin_unregister_init ("perl");
1306 plugin_unregister_read ("perl");
1307 plugin_unregister_write ("perl");
1308 plugin_unregister_flush ("perl");
1310 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1312 pthread_mutex_lock (&perl_threads->mutex);
1313 t = perl_threads->tail;
1316 c_ithread_t *thr = t;
1318 /* the pointer has to be advanced before destroying
1319 * the thread as this will free the memory */
1322 c_ithread_destroy (thr);
1325 pthread_mutex_unlock (&perl_threads->mutex);
1326 pthread_mutex_destroy (&perl_threads->mutex);
1328 sfree (perl_threads);
1330 pthread_key_delete (perl_thr_key);
1334 plugin_unregister_shutdown ("perl");
1336 } /* static void perl_shutdown (void) */
1339 * Access functions for global variables.
1341 * These functions implement the "magic" used to access
1342 * the global variables from Perl.
1345 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1347 char *pv = mg->mg_ptr;
1350 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1352 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1354 char *pv = mg->mg_ptr;
1355 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1356 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1358 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1360 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1362 int *iv = (int *)mg->mg_ptr;
1363 sv_setiv (var, *iv);
1365 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1367 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1369 int *iv = (int *)mg->mg_ptr;
1370 *iv = (int)SvIV (var);
1372 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1374 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1375 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1377 /* bootstrap the Collectd module */
1378 static void xs_init (pTHX)
1382 char *file = __FILE__;
1388 /* enable usage of Perl modules using shared libraries */
1389 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1392 for (i = 0; NULL != api[i].f; ++i)
1393 newXS (api[i].name, api[i].f, file);
1395 stash = gv_stashpv ("Collectd", 1);
1397 /* export "constants" */
1398 for (i = 0; '\0' != constants[i].name[0]; ++i)
1399 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1401 /* export global variables
1402 * by adding "magic" to the SV's representing the globale variables
1403 * perl is able to automagically call the get/set function when
1404 * accessing any such variable (this is basically the same as using
1406 /* global strings */
1407 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1408 tmp = get_sv (g_strings[i].name, 1);
1409 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1410 g_strings[i].var, 0);
1413 /* global integers */
1414 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1415 tmp = get_sv (g_integers[i].name, 1);
1416 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1417 (char *)g_integers[i].var, 0);
1420 } /* static void xs_init (pTHX) */
1422 /* Initialize the global Perl interpreter. */
1423 static int init_pi (int argc, char **argv)
1427 if (NULL != perl_threads)
1430 log_info ("Initializing Perl interpreter...");
1435 for (i = 0; i < argc; ++i)
1436 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1438 #endif /* COLLECT_DEBUG */
1440 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1441 log_err ("init_pi: pthread_key_create failed");
1443 /* this must not happen - cowardly giving up if it does */
1448 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1449 * triggers a "value computed is not used" warning by gcc. */
1452 PERL_SYS_INIT3 (&argc, &argv, &environ);
1454 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1455 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1457 pthread_mutex_init (&perl_threads->mutex, NULL);
1458 /* locking the mutex should not be necessary at this point
1459 * but let's just do it for the sake of completeness */
1460 pthread_mutex_lock (&perl_threads->mutex);
1462 perl_threads->head = c_ithread_create (NULL);
1463 perl_threads->tail = perl_threads->head;
1465 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1466 log_err ("init_pi: Not enough memory.");
1470 aTHX = perl_threads->head->interp;
1471 pthread_mutex_unlock (&perl_threads->mutex);
1473 perl_construct (aTHX);
1475 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1477 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1478 log_err ("init_pi: Unable to bootstrap Collectd.");
1482 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1483 sv_setpv (get_sv ("0", 0), "collectd");
1487 plugin_register_log ("perl", perl_log);
1488 plugin_register_notification ("perl", perl_notify);
1489 plugin_register_init ("perl", perl_init);
1491 plugin_register_read ("perl", perl_read);
1493 plugin_register_write ("perl", perl_write);
1494 plugin_register_flush ("perl", perl_flush);
1495 plugin_register_shutdown ("perl", perl_shutdown);
1497 } /* static int init_pi (const char **, const int) */
1500 * LoadPlugin "<Plugin>"
1502 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1504 char module_name[DATA_MAX_NAME_LEN];
1508 if ((0 != ci->children_num) || (1 != ci->values_num)
1509 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1510 log_err ("LoadPlugin expects a single string argument.");
1514 value = ci->values[0].value.string;
1516 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1517 log_err ("Invalid module name %s", value);
1521 init_pi (perl_argc, perl_argv);
1522 assert (NULL != perl_threads);
1523 assert (NULL != perl_threads->head);
1525 aTHX = perl_threads->head->interp;
1527 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1528 load_module (PERL_LOADMOD_NOIMPORT,
1529 newSVpv (module_name, strlen (module_name)), Nullsv);
1531 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1536 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1540 if ((0 != ci->children_num) || (1 != ci->values_num)
1541 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1542 log_err ("BaseName expects a single string argument.");
1546 value = ci->values[0].value.string;
1548 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1549 strncpy (base_name, value, sizeof (base_name));
1550 base_name[sizeof (base_name) - 1] = '\0';
1552 } /* static int perl_config_basename (oconfig_item_it *) */
1555 * EnableDebugger "<Package>"|""
1557 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1561 if ((0 != ci->children_num) || (1 != ci->values_num)
1562 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1563 log_err ("EnableDebugger expects a single string argument.");
1567 if (NULL != perl_threads) {
1568 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1572 value = ci->values[0].value.string;
1574 perl_argv = (char **)realloc (perl_argv,
1575 (++perl_argc + 1) * sizeof (char *));
1577 if (NULL == perl_argv) {
1578 log_err ("perl_config: Not enough memory.");
1582 if ('\0' == value[0]) {
1583 perl_argv[perl_argc - 1] = "-d";
1586 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1587 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1588 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1591 perl_argv[perl_argc] = NULL;
1593 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1596 * IncludeDir "<Dir>"
1598 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1602 if ((0 != ci->children_num) || (1 != ci->values_num)
1603 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1604 log_err ("IncludeDir expects a single string argument.");
1608 value = ci->values[0].value.string;
1611 perl_argv = (char **)realloc (perl_argv,
1612 (++perl_argc + 1) * sizeof (char *));
1614 if (NULL == perl_argv) {
1615 log_err ("perl_config: Not enough memory.");
1619 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1620 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1621 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1623 perl_argv[perl_argc] = NULL;
1626 /* prepend the directory to @INC */
1627 av_unshift (GvAVn (PL_incgv), 1);
1628 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1631 } /* static int perl_config_includedir (oconfig_item_it *) */
1633 static int perl_config (oconfig_item_t *ci)
1639 /* dTHX does not get any valid values in case Perl
1640 * has not been initialized */
1641 if (NULL == perl_threads)
1644 for (i = 0; i < ci->children_num; ++i) {
1645 oconfig_item_t *c = ci->children + i;
1647 if (0 == strcasecmp (c->key, "LoadPlugin"))
1648 perl_config_loadplugin (aTHX_ c);
1649 else if (0 == strcasecmp (c->key, "BaseName"))
1650 perl_config_basename (aTHX_ c);
1651 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1652 perl_config_enabledebugger (aTHX_ c);
1653 else if (0 == strcasecmp (c->key, "IncludeDir"))
1654 perl_config_includedir (aTHX_ c);
1656 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1659 } /* static int perl_config (oconfig_item_t *) */
1661 void module_register (void)
1664 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1666 /* default options for the Perl interpreter */
1668 perl_argv[1] = "-MCollectd";
1669 perl_argv[2] = "-e";
1671 perl_argv[4] = NULL;
1673 plugin_register_complex_config ("perl", perl_config);
1675 } /* void module_register (void) */
1677 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */