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
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);
88 static XS (Collectd_plugin_dispatch_notification);
89 static XS (Collectd_plugin_log);
90 static XS (Collectd_call_by_name);
96 typedef struct c_ithread_s {
97 /* the thread's Perl interpreter */
98 PerlInterpreter *interp;
100 /* double linked list of threads */
101 struct c_ithread_s *prev;
102 struct c_ithread_s *next;
110 /* some usage stats */
111 int number_of_threads;
112 #endif /* COLLECT_DEBUG */
114 pthread_mutex_t mutex;
121 /* if perl_threads != NULL perl_threads->head must
122 * point to the "base" thread */
123 static c_ithread_list_t *perl_threads = NULL;
125 /* the key used to store each pthread's ithread */
126 static pthread_key_t perl_thr_key;
128 static int perl_argc = 0;
129 static char **perl_argv = NULL;
131 static char base_name[DATA_MAX_NAME_LEN] = "";
138 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
139 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
140 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
141 { "Collectd::_plugin_flush", Collectd__plugin_flush },
142 { "Collectd::plugin_dispatch_notification",
143 Collectd_plugin_dispatch_notification },
144 { "Collectd::plugin_log", Collectd_plugin_log },
145 { "Collectd::call_by_name", Collectd_call_by_name },
154 { "Collectd::TYPE_INIT", PLUGIN_INIT },
155 { "Collectd::TYPE_READ", PLUGIN_READ },
156 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
157 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
158 { "Collectd::TYPE_LOG", PLUGIN_LOG },
159 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
160 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
161 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
162 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
163 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
164 { "Collectd::LOG_ERR", LOG_ERR },
165 { "Collectd::LOG_WARNING", LOG_WARNING },
166 { "Collectd::LOG_NOTICE", LOG_NOTICE },
167 { "Collectd::LOG_INFO", LOG_INFO },
168 { "Collectd::LOG_DEBUG", LOG_DEBUG },
169 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
170 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
171 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
180 { "Collectd::hostname_g", hostname_g },
189 { "Collectd::interval_g", &interval_g },
194 * Helper functions for data type conversion.
209 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
213 if ((NULL == hash) || (NULL == ds))
216 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
217 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
220 log_err ("hv2data_source: No DS name given.");
224 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
225 ds->type = SvIV (*tmp);
227 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
228 log_err ("hv2data_source: Invalid DS type.");
233 ds->type = DS_TYPE_COUNTER;
236 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
237 ds->min = SvNV (*tmp);
241 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
242 ds->max = SvNV (*tmp);
246 } /* static data_source_t *hv2data_source (HV *) */
248 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
250 const data_set_t *ds;
254 if ((NULL == name) || (NULL == array) || (NULL == value))
257 if (av_len (array) < len - 1)
258 len = av_len (array) + 1;
263 ds = plugin_get_ds (name);
265 log_err ("av2value: Unknown dataset \"%s\"", name);
269 if (ds->ds_num < len) {
270 log_warn ("av2value: Value length exceeds data set length.");
274 for (i = 0; i < len; ++i) {
275 SV **tmp = av_fetch (array, i, 0);
278 if (DS_TYPE_COUNTER == ds->ds[i].type)
279 value[i].counter = SvIV (*tmp);
281 value[i].gauge = SvNV (*tmp);
288 } /* static int av2value (char *, AV *, value_t *, int) */
290 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
294 if ((NULL == ds) || (NULL == array))
297 av_extend (array, ds->ds_num);
299 for (i = 0; i < ds->ds_num; ++i) {
300 HV *source = newHV ();
302 if (NULL == hv_store (source, "name", 4,
303 newSVpv (ds->ds[i].name, 0), 0))
306 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
309 if (! isnan (ds->ds[i].min))
310 if (NULL == hv_store (source, "min", 3,
311 newSVnv (ds->ds[i].min), 0))
314 if (! isnan (ds->ds[i].max))
315 if (NULL == hv_store (source, "max", 3,
316 newSVnv (ds->ds[i].max), 0))
319 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
323 } /* static int data_set2av (data_set_t *, AV *) */
325 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
332 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
335 len = vl->values_len;
337 if (ds->ds_num < len) {
338 log_warn ("value2av: Value length exceeds data set length.");
343 av_extend (values, len - 1);
345 for (i = 0; i < len; ++i) {
348 if (DS_TYPE_COUNTER == ds->ds[i].type)
349 val = newSViv (vl->values[i].counter);
351 val = newSVnv (vl->values[i].gauge);
353 if (NULL == av_store (values, i, val)) {
359 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
363 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
366 if ('\0' != vl->host[0])
367 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
370 if ('\0' != vl->plugin[0])
371 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
374 if ('\0' != vl->plugin_instance[0])
375 if (NULL == hv_store (hash, "plugin_instance", 15,
376 newSVpv (vl->plugin_instance, 0), 0))
379 if ('\0' != vl->type[0])
380 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
383 if ('\0' != vl->type_instance[0])
384 if (NULL == hv_store (hash, "type_instance", 13,
385 newSVpv (vl->type_instance, 0), 0))
388 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
390 static int notification2hv (pTHX_ notification_t *n, HV *hash)
392 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
396 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
399 if ('\0' != *n->message)
400 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
403 if ('\0' != *n->host)
404 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
407 if ('\0' != *n->plugin)
408 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
411 if ('\0' != *n->plugin_instance)
412 if (NULL == hv_store (hash, "plugin_instance", 15,
413 newSVpv (n->plugin_instance, 0), 0))
416 if ('\0' != *n->type)
417 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
420 if ('\0' != *n->type_instance)
421 if (NULL == hv_store (hash, "type_instance", 13,
422 newSVpv (n->type_instance, 0), 0))
425 } /* static int notification2hv (notification_t *, HV *) */
428 * Internal functions.
431 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
433 if (base_name[0] == '\0')
434 status = ssnprintf (buf, buf_len, "%s", module);
436 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
437 if ((status < 0) || ((unsigned int)status >= buf_len))
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 sstrncpy (set->type, name, sizeof (set->type));
486 set->ds_num = len + 1;
489 ret = plugin_register_data_set (set);
494 } /* static int pplugin_register_data_set (char *, SV *) */
497 * Remove a plugin's data set definition.
499 static int pplugin_unregister_data_set (char *name)
503 return plugin_unregister_data_set (name);
504 } /* static int pplugin_unregister_data_set (char *) */
507 * Submit the values to the write functions.
511 * values => [ @values ],
515 * plugin_instance => $pinstance,
516 * type_instance => $tinstance,
519 static int pplugin_dispatch_values (pTHX_ HV *values)
521 value_list_t list = VALUE_LIST_INIT;
531 if (NULL == (tmp = hv_fetch (values, "type", 4, 0))) {
532 log_err ("pplugin_dispatch_values: No type given.");
536 sstrncpy (list.type, SvPV_nolen (*tmp), sizeof (list.type));
538 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
539 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
540 log_err ("pplugin_dispatch_values: No valid values given.");
545 AV *array = (AV *)SvRV (*tmp);
546 int len = av_len (array) + 1;
551 val = (value_t *)smalloc (len * sizeof (value_t));
553 list.values_len = av2value (aTHX_ list.type, (AV *)SvRV (*tmp),
557 if (-1 == list.values_len) {
563 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
564 list.time = (time_t)SvIV (*tmp);
567 list.time = time (NULL);
570 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
571 sstrncpy (list.host, SvPV_nolen (*tmp), sizeof (list.host));
574 sstrncpy (list.host, hostname_g, sizeof (list.host));
577 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0)))
578 sstrncpy (list.plugin, SvPV_nolen (*tmp), sizeof (list.plugin));
580 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0)))
581 sstrncpy (list.plugin_instance, SvPV_nolen (*tmp),
582 sizeof (list.plugin_instance));
584 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0)))
585 sstrncpy (list.type_instance, SvPV_nolen (*tmp),
586 sizeof (list.type_instance));
588 ret = plugin_dispatch_values (&list);
592 } /* static int pplugin_dispatch_values (char *, HV *) */
595 * Dispatch a notification.
599 * severity => $severity,
605 * plugin_instance => $instance,
606 * type_instance => $type_instance
609 static int pplugin_dispatch_notification (pTHX_ HV *notif)
618 memset (&n, 0, sizeof (n));
620 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
621 n.severity = SvIV (*tmp);
623 n.severity = NOTIF_FAILURE;
625 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
626 n.time = (time_t)SvIV (*tmp);
628 n.time = time (NULL);
630 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
631 sstrncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
633 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
634 sstrncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
636 sstrncpy (n.host, hostname_g, sizeof (n.host));
638 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
639 sstrncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
641 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
642 sstrncpy (n.plugin_instance, SvPV_nolen (*tmp),
643 sizeof (n.plugin_instance));
645 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
646 sstrncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
648 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
649 sstrncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
650 return plugin_dispatch_notification (&n);
651 } /* static int pplugin_dispatch_notification (HV *) */
654 * Call all working functions of the given type.
656 static int pplugin_call_all (pTHX_ int type, ...)
665 if ((type < 0) || (type >= PLUGIN_TYPES))
675 XPUSHs (sv_2mortal (newSViv ((IV)type)));
677 if (PLUGIN_WRITE == type) {
679 * $_[0] = $plugin_type;
694 * values => [ $v1, ... ],
699 * plugin_instance => $instance,
700 * type_instance => $type_instance
709 ds = va_arg (ap, data_set_t *);
710 vl = va_arg (ap, value_list_t *);
712 if (-1 == data_set2av (aTHX_ ds, pds)) {
719 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
726 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
727 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
728 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
730 else if (PLUGIN_LOG == type) {
736 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
737 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
739 else if (PLUGIN_NOTIF == type) {
743 * severity => $severity,
749 * plugin_instance => $instance,
750 * type_instance => $type_instance
754 HV *notif = newHV ();
756 n = va_arg (ap, notification_t *);
758 if (-1 == notification2hv (aTHX_ n, notif)) {
765 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
767 else if (PLUGIN_FLUSH == type) {
770 * $_[1] = $identifier;
772 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
773 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
778 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
793 } /* static int pplugin_call_all (int, ...) */
800 * Collectd::plugin_register_data_set (type, dataset).
803 * type of the dataset
806 * dataset to be registered
808 static XS (Collectd_plugin_register_ds)
816 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
820 log_debug ("Collectd::plugin_register_data_set: "
821 "type = \"%s\", dataset = \"%s\"",
822 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
826 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
827 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
831 log_err ("Collectd::plugin_register_data_set: Invalid data.");
839 } /* static XS (Collectd_plugin_register_ds) */
842 * Collectd::plugin_unregister_data_set (type).
845 * type of the dataset
847 static XS (Collectd_plugin_unregister_ds)
852 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
856 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
857 SvPV_nolen (ST (0)));
859 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
863 } /* static XS (Collectd_plugin_register_ds) */
866 * Collectd::plugin_dispatch_values (name, values).
872 * value list to submit
874 static XS (Collectd_plugin_dispatch_values)
884 log_warn ("Collectd::plugin_dispatch_values with two arguments "
885 "is deprecated - pass the type through values->{type}.");
888 else if (1 != items) {
889 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
893 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
894 SvPV_nolen (ST (values_idx)));
896 values = ST (values_idx);
898 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
899 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
903 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
906 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
907 newSVsv (ST (0)), 0))) {
908 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
912 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
918 } /* static XS (Collectd_plugin_dispatch_values) */
921 * Collectd::_plugin_flush (plugin, timeout, identifier).
924 * name of the plugin to flush
927 * timeout to use when flushing the data
930 * data-set identifier to flush
932 static XS (Collectd__plugin_flush)
941 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
946 plugin = SvPV_nolen (ST (0));
949 timeout = (int)SvIV (ST (1));
952 id = SvPV_nolen (ST (2));
954 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
955 "id = \"%s\"", plugin, timeout, id);
957 if (0 == plugin_flush (plugin, timeout, id))
961 } /* static XS (Collectd__plugin_flush) */
964 * Collectd::plugin_dispatch_notification (notif).
967 * notification to dispatch
969 static XS (Collectd_plugin_dispatch_notification)
978 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
982 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
983 SvPV_nolen (ST (0)));
987 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
988 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
992 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
998 } /* static XS (Collectd_plugin_dispatch_notification) */
1001 * Collectd::plugin_log (level, message).
1004 * log level (LOG_DEBUG, ... LOG_ERR)
1009 static XS (Collectd_plugin_log)
1014 log_err ("Usage: Collectd::plugin_log(level, message)");
1018 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
1020 } /* static XS (Collectd_plugin_log) */
1023 * Collectd::call_by_name (...).
1025 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1027 static XS (Collectd_call_by_name)
1032 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1033 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1038 name = SvPV_nolen (tmp);
1040 if (NULL == get_cv (name, 0)) {
1041 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1046 /* simply pass on the subroutine call without touching the stack,
1047 * thus leaving any arguments and return values in place */
1049 } /* static XS (Collectd_call_by_name) */
1052 * collectd's perl interpreter based thread implementation.
1054 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1057 /* must be called with perl_threads->mutex locked */
1058 static void c_ithread_destroy (c_ithread_t *ithread)
1060 dTHXa (ithread->interp);
1062 assert (NULL != perl_threads);
1064 PERL_SET_CONTEXT (aTHX);
1065 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1070 --perl_threads->number_of_threads;
1071 #endif /* COLLECT_DEBUG */
1073 perl_destruct (aTHX);
1076 if (NULL == ithread->prev)
1077 perl_threads->head = ithread->next;
1079 ithread->prev->next = ithread->next;
1081 if (NULL == ithread->next)
1082 perl_threads->tail = ithread->prev;
1084 ithread->next->prev = ithread->prev;
1088 } /* static void c_ithread_destroy (c_ithread_t *) */
1090 static void c_ithread_destructor (void *arg)
1092 c_ithread_t *ithread = (c_ithread_t *)arg;
1093 c_ithread_t *t = NULL;
1095 if (NULL == perl_threads)
1098 pthread_mutex_lock (&perl_threads->mutex);
1100 for (t = perl_threads->head; NULL != t; t = t->next)
1104 /* the ithread no longer exists */
1108 c_ithread_destroy (ithread);
1110 pthread_mutex_unlock (&perl_threads->mutex);
1112 } /* static void c_ithread_destructor (void *) */
1114 /* must be called with perl_threads->mutex locked */
1115 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1117 c_ithread_t *t = NULL;
1120 assert (NULL != perl_threads);
1122 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1123 memset (t, 0, sizeof (c_ithread_t));
1125 t->interp = (NULL == base)
1127 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1131 if ((NULL != base) && (NULL != PL_endav)) {
1132 av_clear (PL_endav);
1133 av_undef (PL_endav);
1138 ++perl_threads->number_of_threads;
1139 #endif /* COLLECT_DEBUG */
1143 if (NULL == perl_threads->tail) {
1144 perl_threads->head = t;
1148 perl_threads->tail->next = t;
1149 t->prev = perl_threads->tail;
1152 perl_threads->tail = t;
1154 pthread_setspecific (perl_thr_key, (const void *)t);
1156 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1159 * Interface to collectd.
1162 static int perl_init (void)
1166 if (NULL == perl_threads)
1170 c_ithread_t *t = NULL;
1172 pthread_mutex_lock (&perl_threads->mutex);
1173 t = c_ithread_create (perl_threads->head->interp);
1174 pthread_mutex_unlock (&perl_threads->mutex);
1179 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1180 aTHX, perl_threads->number_of_threads);
1181 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1182 } /* static int perl_init (void) */
1184 static int perl_read (void)
1188 if (NULL == perl_threads)
1192 c_ithread_t *t = NULL;
1194 pthread_mutex_lock (&perl_threads->mutex);
1195 t = c_ithread_create (perl_threads->head->interp);
1196 pthread_mutex_unlock (&perl_threads->mutex);
1201 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1202 aTHX, perl_threads->number_of_threads);
1203 return pplugin_call_all (aTHX_ PLUGIN_READ);
1204 } /* static int perl_read (void) */
1206 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1210 if (NULL == perl_threads)
1214 c_ithread_t *t = NULL;
1216 pthread_mutex_lock (&perl_threads->mutex);
1217 t = c_ithread_create (perl_threads->head->interp);
1218 pthread_mutex_unlock (&perl_threads->mutex);
1223 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1224 aTHX, perl_threads->number_of_threads);
1225 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1226 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1228 static void perl_log (int level, const char *msg)
1232 if (NULL == perl_threads)
1236 c_ithread_t *t = NULL;
1238 pthread_mutex_lock (&perl_threads->mutex);
1239 t = c_ithread_create (perl_threads->head->interp);
1240 pthread_mutex_unlock (&perl_threads->mutex);
1245 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1247 } /* static void perl_log (int, const char *) */
1249 static int perl_notify (const notification_t *notif)
1253 if (NULL == perl_threads)
1257 c_ithread_t *t = NULL;
1259 pthread_mutex_lock (&perl_threads->mutex);
1260 t = c_ithread_create (perl_threads->head->interp);
1261 pthread_mutex_unlock (&perl_threads->mutex);
1265 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1266 } /* static int perl_notify (const notification_t *) */
1268 static int perl_flush (int timeout, const char *identifier)
1272 if (NULL == perl_threads)
1276 c_ithread_t *t = NULL;
1278 pthread_mutex_lock (&perl_threads->mutex);
1279 t = c_ithread_create (perl_threads->head->interp);
1280 pthread_mutex_unlock (&perl_threads->mutex);
1284 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
1285 } /* static int perl_flush (const int) */
1287 static int perl_shutdown (void)
1289 c_ithread_t *t = NULL;
1295 plugin_unregister_complex_config ("perl");
1297 if (NULL == perl_threads)
1301 c_ithread_t *t = NULL;
1303 pthread_mutex_lock (&perl_threads->mutex);
1304 t = c_ithread_create (perl_threads->head->interp);
1305 pthread_mutex_unlock (&perl_threads->mutex);
1310 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1311 aTHX, perl_threads->number_of_threads);
1313 plugin_unregister_log ("perl");
1314 plugin_unregister_notification ("perl");
1315 plugin_unregister_init ("perl");
1316 plugin_unregister_read ("perl");
1317 plugin_unregister_write ("perl");
1318 plugin_unregister_flush ("perl");
1320 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1322 pthread_mutex_lock (&perl_threads->mutex);
1323 t = perl_threads->tail;
1326 c_ithread_t *thr = t;
1328 /* the pointer has to be advanced before destroying
1329 * the thread as this will free the memory */
1332 c_ithread_destroy (thr);
1335 pthread_mutex_unlock (&perl_threads->mutex);
1336 pthread_mutex_destroy (&perl_threads->mutex);
1338 sfree (perl_threads);
1340 pthread_key_delete (perl_thr_key);
1344 plugin_unregister_shutdown ("perl");
1346 } /* static void perl_shutdown (void) */
1349 * Access functions for global variables.
1351 * These functions implement the "magic" used to access
1352 * the global variables from Perl.
1355 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1357 char *pv = mg->mg_ptr;
1360 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1362 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1364 char *pv = mg->mg_ptr;
1365 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1367 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1369 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1371 int *iv = (int *)mg->mg_ptr;
1372 sv_setiv (var, *iv);
1374 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1376 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1378 int *iv = (int *)mg->mg_ptr;
1379 *iv = (int)SvIV (var);
1381 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1383 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1384 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1386 /* bootstrap the Collectd module */
1387 static void xs_init (pTHX)
1391 char *file = __FILE__;
1397 /* enable usage of Perl modules using shared libraries */
1398 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1401 for (i = 0; NULL != api[i].f; ++i)
1402 newXS (api[i].name, api[i].f, file);
1404 stash = gv_stashpv ("Collectd", 1);
1406 /* export "constants" */
1407 for (i = 0; '\0' != constants[i].name[0]; ++i)
1408 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1410 /* export global variables
1411 * by adding "magic" to the SV's representing the globale variables
1412 * perl is able to automagically call the get/set function when
1413 * accessing any such variable (this is basically the same as using
1415 /* global strings */
1416 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1417 tmp = get_sv (g_strings[i].name, 1);
1418 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1419 g_strings[i].var, 0);
1422 /* global integers */
1423 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1424 tmp = get_sv (g_integers[i].name, 1);
1425 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1426 (char *)g_integers[i].var, 0);
1429 } /* static void xs_init (pTHX) */
1431 /* Initialize the global Perl interpreter. */
1432 static int init_pi (int argc, char **argv)
1436 if (NULL != perl_threads)
1439 log_info ("Initializing Perl interpreter...");
1444 for (i = 0; i < argc; ++i)
1445 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1447 #endif /* COLLECT_DEBUG */
1449 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1450 log_err ("init_pi: pthread_key_create failed");
1452 /* this must not happen - cowardly giving up if it does */
1457 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1458 * triggers a "value computed is not used" warning by gcc. */
1461 PERL_SYS_INIT3 (&argc, &argv, &environ);
1463 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1464 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1466 pthread_mutex_init (&perl_threads->mutex, NULL);
1467 /* locking the mutex should not be necessary at this point
1468 * but let's just do it for the sake of completeness */
1469 pthread_mutex_lock (&perl_threads->mutex);
1471 perl_threads->head = c_ithread_create (NULL);
1472 perl_threads->tail = perl_threads->head;
1474 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1475 log_err ("init_pi: Not enough memory.");
1479 aTHX = perl_threads->head->interp;
1480 pthread_mutex_unlock (&perl_threads->mutex);
1482 perl_construct (aTHX);
1484 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1486 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1487 log_err ("init_pi: Unable to bootstrap Collectd.");
1489 perl_destruct (perl_threads->head->interp);
1490 perl_free (perl_threads->head->interp);
1491 sfree (perl_threads);
1493 pthread_key_delete (perl_thr_key);
1497 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1498 sv_setpv (get_sv ("0", 0), "collectd");
1502 plugin_register_log ("perl", perl_log);
1503 plugin_register_notification ("perl", perl_notify);
1504 plugin_register_init ("perl", perl_init);
1506 plugin_register_read ("perl", perl_read);
1508 plugin_register_write ("perl", perl_write);
1509 plugin_register_flush ("perl", perl_flush);
1510 plugin_register_shutdown ("perl", perl_shutdown);
1512 } /* static int init_pi (const char **, const int) */
1515 * LoadPlugin "<Plugin>"
1517 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1519 char module_name[DATA_MAX_NAME_LEN];
1523 if ((0 != ci->children_num) || (1 != ci->values_num)
1524 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1525 log_err ("LoadPlugin expects a single string argument.");
1529 value = ci->values[0].value.string;
1531 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1532 log_err ("Invalid module name %s", value);
1536 if (0 != init_pi (perl_argc, perl_argv))
1539 assert (NULL != perl_threads);
1540 assert (NULL != perl_threads->head);
1542 aTHX = perl_threads->head->interp;
1544 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1545 load_module (PERL_LOADMOD_NOIMPORT,
1546 newSVpv (module_name, strlen (module_name)), Nullsv);
1548 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1553 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1557 if ((0 != ci->children_num) || (1 != ci->values_num)
1558 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1559 log_err ("BaseName expects a single string argument.");
1563 value = ci->values[0].value.string;
1565 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1566 sstrncpy (base_name, value, sizeof (base_name));
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 /* dTHX does not get any valid values in case Perl
1656 * has not been initialized */
1657 if (NULL == perl_threads)
1660 for (i = 0; i < ci->children_num; ++i) {
1661 oconfig_item_t *c = ci->children + i;
1663 if (0 == strcasecmp (c->key, "LoadPlugin"))
1664 perl_config_loadplugin (aTHX_ c);
1665 else if (0 == strcasecmp (c->key, "BaseName"))
1666 perl_config_basename (aTHX_ c);
1667 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1668 perl_config_enabledebugger (aTHX_ c);
1669 else if (0 == strcasecmp (c->key, "IncludeDir"))
1670 perl_config_includedir (aTHX_ c);
1672 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1675 } /* static int perl_config (oconfig_item_t *) */
1677 void module_register (void)
1680 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1682 /* default options for the Perl interpreter */
1684 perl_argv[1] = "-MCollectd";
1685 perl_argv[2] = "-e";
1687 perl_argv[4] = NULL;
1689 plugin_register_complex_config ("perl", perl_config);
1691 } /* void module_register (void) */
1693 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */