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_dispatch_notification);
82 static XS (Collectd_plugin_log);
83 static XS (Collectd_call_by_name);
89 typedef struct c_ithread_s {
90 /* the thread's Perl interpreter */
91 PerlInterpreter *interp;
93 /* double linked list of threads */
94 struct c_ithread_s *prev;
95 struct c_ithread_s *next;
103 /* some usage stats */
104 int number_of_threads;
105 #endif /* COLLECT_DEBUG */
107 pthread_mutex_t mutex;
114 /* if perl_threads != NULL perl_threads->head must
115 * point to the "base" thread */
116 static c_ithread_list_t *perl_threads = NULL;
118 /* the key used to store each pthread's ithread */
119 static pthread_key_t perl_thr_key;
121 static int perl_argc = 0;
122 static char **perl_argv = NULL;
124 static char base_name[DATA_MAX_NAME_LEN] = "";
131 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
132 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
133 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
134 { "Collectd::plugin_dispatch_notification",
135 Collectd_plugin_dispatch_notification },
136 { "Collectd::plugin_log", Collectd_plugin_log },
137 { "Collectd::call_by_name", Collectd_call_by_name },
146 { "Collectd::TYPE_INIT", PLUGIN_INIT },
147 { "Collectd::TYPE_READ", PLUGIN_READ },
148 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
149 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
150 { "Collectd::TYPE_LOG", PLUGIN_LOG },
151 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
152 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
153 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
154 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
155 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
156 { "Collectd::LOG_ERR", LOG_ERR },
157 { "Collectd::LOG_WARNING", LOG_WARNING },
158 { "Collectd::LOG_NOTICE", LOG_NOTICE },
159 { "Collectd::LOG_INFO", LOG_INFO },
160 { "Collectd::LOG_DEBUG", LOG_DEBUG },
161 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
162 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
163 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
172 { "Collectd::hostname_g", hostname_g },
181 { "Collectd::interval_g", &interval_g },
186 * Helper functions for data type conversion.
201 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
205 if ((NULL == hash) || (NULL == ds))
208 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
209 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
210 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
213 log_err ("hv2data_source: No DS name given.");
217 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
218 ds->type = SvIV (*tmp);
220 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
221 log_err ("hv2data_source: Invalid DS type.");
226 ds->type = DS_TYPE_COUNTER;
229 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
230 ds->min = SvNV (*tmp);
234 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
235 ds->max = SvNV (*tmp);
239 } /* static data_source_t *hv2data_source (HV *) */
241 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
243 const data_set_t *ds;
247 if ((NULL == name) || (NULL == array) || (NULL == value))
250 if (av_len (array) < len - 1)
251 len = av_len (array) + 1;
256 ds = plugin_get_ds (name);
258 log_err ("av2value: Unknown dataset \"%s\"", name);
262 if (ds->ds_num < len) {
263 log_warn ("av2value: Value length exceeds data set length.");
267 for (i = 0; i < len; ++i) {
268 SV **tmp = av_fetch (array, i, 0);
271 if (DS_TYPE_COUNTER == ds->ds[i].type)
272 value[i].counter = SvIV (*tmp);
274 value[i].gauge = SvNV (*tmp);
281 } /* static int av2value (char *, AV *, value_t *, int) */
283 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
287 if ((NULL == ds) || (NULL == array))
290 av_extend (array, ds->ds_num);
292 for (i = 0; i < ds->ds_num; ++i) {
293 HV *source = newHV ();
295 if (NULL == hv_store (source, "name", 4,
296 newSVpv (ds->ds[i].name, 0), 0))
299 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
302 if (! isnan (ds->ds[i].min))
303 if (NULL == hv_store (source, "min", 3,
304 newSVnv (ds->ds[i].min), 0))
307 if (! isnan (ds->ds[i].max))
308 if (NULL == hv_store (source, "max", 3,
309 newSVnv (ds->ds[i].max), 0))
312 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
316 } /* static int data_set2av (data_set_t *, AV *) */
318 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
325 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
328 len = vl->values_len;
330 if (ds->ds_num < len) {
331 log_warn ("value2av: Value length exceeds data set length.");
336 av_extend (values, len - 1);
338 for (i = 0; i < len; ++i) {
341 if (DS_TYPE_COUNTER == ds->ds[i].type)
342 val = newSViv (vl->values[i].counter);
344 val = newSVnv (vl->values[i].gauge);
346 if (NULL == av_store (values, i, val)) {
352 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
356 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
359 if ('\0' != vl->host[0])
360 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
363 if ('\0' != vl->plugin[0])
364 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
367 if ('\0' != vl->plugin_instance[0])
368 if (NULL == hv_store (hash, "plugin_instance", 15,
369 newSVpv (vl->plugin_instance, 0), 0))
372 if ('\0' != vl->type_instance[0])
373 if (NULL == hv_store (hash, "type_instance", 13,
374 newSVpv (vl->type_instance, 0), 0))
377 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
379 static int notification2hv (pTHX_ notification_t *n, HV *hash)
381 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
385 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
388 if ('\0' != *n->message)
389 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
392 if ('\0' != *n->host)
393 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
396 if ('\0' != *n->plugin)
397 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
400 if ('\0' != *n->plugin_instance)
401 if (NULL == hv_store (hash, "plugin_instance", 15,
402 newSVpv (n->plugin_instance, 0), 0))
405 if ('\0' != *n->type)
406 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
409 if ('\0' != *n->type_instance)
410 if (NULL == hv_store (hash, "type_instance", 13,
411 newSVpv (n->type_instance, 0), 0))
414 } /* static int notification2hv (notification_t *, HV *) */
417 * Internal functions.
420 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
422 if (base_name[0] == '\0')
423 status = snprintf (buf, buf_len, "%s", module);
425 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
426 if ((status < 0) || ((unsigned int)status >= buf_len))
428 buf[buf_len - 1] = '\0';
430 } /* char *get_module_name */
433 * Add a plugin's data set definition.
435 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
441 data_source_t *ds = NULL;
442 data_set_t *set = NULL;
444 if ((NULL == name) || (NULL == dataset))
447 len = av_len (dataset);
452 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
453 set = (data_set_t *)smalloc (sizeof (data_set_t));
455 for (i = 0; i <= len; ++i) {
456 SV **elem = av_fetch (dataset, i, 0);
461 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
462 log_err ("pplugin_register_data_set: Invalid data source.");
466 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
469 log_debug ("pplugin_register_data_set: "
470 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
471 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
474 strncpy (set->type, name, DATA_MAX_NAME_LEN);
475 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
477 set->ds_num = len + 1;
480 ret = plugin_register_data_set (set);
485 } /* static int pplugin_register_data_set (char *, SV *) */
488 * Remove a plugin's data set definition.
490 static int pplugin_unregister_data_set (char *name)
494 return plugin_unregister_data_set (name);
495 } /* static int pplugin_unregister_data_set (char *) */
498 * Submit the values to the write functions.
502 * values => [ @values ],
506 * plugin_instance => $pinstance,
507 * type_instance => $tinstance,
510 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
512 value_list_t list = VALUE_LIST_INIT;
519 if ((NULL == name) || (NULL == values))
522 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
523 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
524 log_err ("pplugin_dispatch_values: No valid values given.");
529 AV *array = (AV *)SvRV (*tmp);
530 int len = av_len (array) + 1;
535 val = (value_t *)smalloc (len * sizeof (value_t));
537 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
540 if (-1 == list.values_len) {
546 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
547 list.time = (time_t)SvIV (*tmp);
550 list.time = time (NULL);
553 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
554 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
555 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
558 strcpy (list.host, hostname_g);
561 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
562 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
563 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
566 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
567 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
568 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
571 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
572 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
573 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
576 ret = plugin_dispatch_values (name, &list);
580 } /* static int pplugin_dispatch_values (char *, HV *) */
583 * Dispatch a notification.
587 * severity => $severity,
593 * plugin_instance => $instance,
594 * type_instance => $type_instance
597 static int pplugin_dispatch_notification (pTHX_ HV *notif)
606 memset (&n, 0, sizeof (n));
608 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
609 n.severity = SvIV (*tmp);
611 n.severity = NOTIF_FAILURE;
613 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
614 n.time = (time_t)SvIV (*tmp);
616 n.time = time (NULL);
618 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
619 strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
620 n.message[sizeof (n.message) - 1] = '\0';
622 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
623 strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
625 strncpy (n.host, hostname_g, sizeof (n.host));
626 n.host[sizeof (n.host) - 1] = '\0';
628 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
629 strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
630 n.plugin[sizeof (n.plugin) - 1] = '\0';
632 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
633 strncpy (n.plugin_instance, SvPV_nolen (*tmp),
634 sizeof (n.plugin_instance));
635 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
637 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
638 strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
639 n.type[sizeof (n.type) - 1] = '\0';
641 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
642 strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
643 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
644 return plugin_dispatch_notification (&n);
645 } /* static int pplugin_dispatch_notification (HV *) */
648 * Call all working functions of the given type.
650 static int pplugin_call_all (pTHX_ int type, ...)
659 if ((type < 0) || (type >= PLUGIN_TYPES))
669 XPUSHs (sv_2mortal (newSViv ((IV)type)));
671 if (PLUGIN_WRITE == type) {
673 * $_[0] = $plugin_type;
688 * values => [ $v1, ... ],
692 * plugin_instance => $instance,
693 * type_instance => $type_instance
702 ds = va_arg (ap, data_set_t *);
703 vl = va_arg (ap, value_list_t *);
705 if (-1 == data_set2av (aTHX_ ds, pds)) {
712 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
719 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
720 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
721 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
723 else if (PLUGIN_LOG == type) {
729 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
730 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
732 else if (PLUGIN_NOTIF == type) {
736 * severity => $severity,
742 * plugin_instance => $instance,
743 * type_instance => $type_instance
747 HV *notif = newHV ();
749 n = va_arg (ap, notification_t *);
751 if (-1 == notification2hv (aTHX_ n, notif)) {
758 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
760 else if (PLUGIN_FLUSH == type) {
764 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
769 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
784 } /* static int pplugin_call_all (int, ...) */
791 * Collectd::plugin_register_data_set (type, dataset).
794 * type of the dataset
797 * dataset to be registered
799 static XS (Collectd_plugin_register_ds)
807 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
811 log_debug ("Collectd::plugin_register_data_set: "
812 "type = \"%s\", dataset = \"%s\"",
813 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
817 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
818 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
822 log_err ("Collectd::plugin_register_data_set: Invalid data.");
830 } /* static XS (Collectd_plugin_register_ds) */
833 * Collectd::plugin_unregister_data_set (type).
836 * type of the dataset
838 static XS (Collectd_plugin_unregister_ds)
843 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
847 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
848 SvPV_nolen (ST (0)));
850 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
854 } /* static XS (Collectd_plugin_register_ds) */
857 * Collectd::plugin_dispatch_values (name, values).
863 * value list to submit
865 static XS (Collectd_plugin_dispatch_values)
874 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
878 log_debug ("Collectd::plugin_dispatch_values: "
879 "name = \"%s\", values=\"%s\"",
880 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
884 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
885 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
889 if ((NULL == ST (0)) || (NULL == values))
892 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
893 (HV *)SvRV (values));
899 } /* static XS (Collectd_plugin_dispatch_values) */
902 * Collectd::plugin_dispatch_notification (notif).
905 * notification to dispatch
907 static XS (Collectd_plugin_dispatch_notification)
916 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
920 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
921 SvPV_nolen (ST (0)));
925 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
926 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
930 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
936 } /* static XS (Collectd_plugin_dispatch_notification) */
939 * Collectd::plugin_log (level, message).
942 * log level (LOG_DEBUG, ... LOG_ERR)
947 static XS (Collectd_plugin_log)
952 log_err ("Usage: Collectd::plugin_log(level, message)");
956 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
958 } /* static XS (Collectd_plugin_log) */
961 * Collectd::call_by_name (...).
963 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
965 static XS (Collectd_call_by_name)
970 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
971 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
976 name = SvPV_nolen (tmp);
978 if (NULL == get_cv (name, 0)) {
979 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
984 /* simply pass on the subroutine call without touching the stack,
985 * thus leaving any arguments and return values in place */
987 } /* static XS (Collectd_call_by_name) */
990 * collectd's perl interpreter based thread implementation.
992 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
995 /* must be called with perl_threads->mutex locked */
996 static void c_ithread_destroy (c_ithread_t *ithread)
998 dTHXa (ithread->interp);
1000 assert (NULL != perl_threads);
1002 PERL_SET_CONTEXT (aTHX);
1003 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1008 --perl_threads->number_of_threads;
1009 #endif /* COLLECT_DEBUG */
1011 perl_destruct (aTHX);
1014 if (NULL == ithread->prev)
1015 perl_threads->head = ithread->next;
1017 ithread->prev->next = ithread->next;
1019 if (NULL == ithread->next)
1020 perl_threads->tail = ithread->prev;
1022 ithread->next->prev = ithread->prev;
1026 } /* static void c_ithread_destroy (c_ithread_t *) */
1028 static void c_ithread_destructor (void *arg)
1030 c_ithread_t *ithread = (c_ithread_t *)arg;
1031 c_ithread_t *t = NULL;
1033 if (NULL == perl_threads)
1036 pthread_mutex_lock (&perl_threads->mutex);
1038 for (t = perl_threads->head; NULL != t; t = t->next)
1042 /* the ithread no longer exists */
1046 c_ithread_destroy (ithread);
1048 pthread_mutex_unlock (&perl_threads->mutex);
1050 } /* static void c_ithread_destructor (void *) */
1052 /* must be called with perl_threads->mutex locked */
1053 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1055 c_ithread_t *t = NULL;
1058 assert (NULL != perl_threads);
1060 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1061 memset (t, 0, sizeof (c_ithread_t));
1063 t->interp = (NULL == base)
1065 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1070 av_clear (PL_endav);
1071 av_undef (PL_endav);
1076 ++perl_threads->number_of_threads;
1077 #endif /* COLLECT_DEBUG */
1081 if (NULL == perl_threads->tail) {
1082 perl_threads->head = t;
1086 perl_threads->tail->next = t;
1087 t->prev = perl_threads->tail;
1090 perl_threads->tail = t;
1092 pthread_setspecific (perl_thr_key, (const void *)t);
1094 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1097 * Interface to collectd.
1100 static int perl_init (void)
1104 if (NULL == perl_threads)
1108 c_ithread_t *t = NULL;
1110 pthread_mutex_lock (&perl_threads->mutex);
1111 t = c_ithread_create (perl_threads->head->interp);
1112 pthread_mutex_unlock (&perl_threads->mutex);
1117 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1118 aTHX, perl_threads->number_of_threads);
1119 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1120 } /* static int perl_init (void) */
1122 static int perl_read (void)
1126 if (NULL == perl_threads)
1130 c_ithread_t *t = NULL;
1132 pthread_mutex_lock (&perl_threads->mutex);
1133 t = c_ithread_create (perl_threads->head->interp);
1134 pthread_mutex_unlock (&perl_threads->mutex);
1139 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1140 aTHX, perl_threads->number_of_threads);
1141 return pplugin_call_all (aTHX_ PLUGIN_READ);
1142 } /* static int perl_read (void) */
1144 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1148 if (NULL == perl_threads)
1152 c_ithread_t *t = NULL;
1154 pthread_mutex_lock (&perl_threads->mutex);
1155 t = c_ithread_create (perl_threads->head->interp);
1156 pthread_mutex_unlock (&perl_threads->mutex);
1161 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1162 aTHX, perl_threads->number_of_threads);
1163 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1164 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1166 static void perl_log (int level, const char *msg)
1170 if (NULL == perl_threads)
1174 c_ithread_t *t = NULL;
1176 pthread_mutex_lock (&perl_threads->mutex);
1177 t = c_ithread_create (perl_threads->head->interp);
1178 pthread_mutex_unlock (&perl_threads->mutex);
1183 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1185 } /* static void perl_log (int, const char *) */
1187 static int perl_notify (const notification_t *notif)
1191 if (NULL == perl_threads)
1195 c_ithread_t *t = NULL;
1197 pthread_mutex_lock (&perl_threads->mutex);
1198 t = c_ithread_create (perl_threads->head->interp);
1199 pthread_mutex_unlock (&perl_threads->mutex);
1203 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1204 } /* static int perl_notify (const notification_t *) */
1206 static int perl_flush (const int timeout)
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);
1222 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout);
1223 } /* static int perl_flush (const int) */
1225 static int perl_shutdown (void)
1227 c_ithread_t *t = NULL;
1233 plugin_unregister_complex_config ("perl");
1235 if (NULL == perl_threads)
1239 c_ithread_t *t = NULL;
1241 pthread_mutex_lock (&perl_threads->mutex);
1242 t = c_ithread_create (perl_threads->head->interp);
1243 pthread_mutex_unlock (&perl_threads->mutex);
1248 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1249 aTHX, perl_threads->number_of_threads);
1251 plugin_unregister_log ("perl");
1252 plugin_unregister_notification ("perl");
1253 plugin_unregister_init ("perl");
1254 plugin_unregister_read ("perl");
1255 plugin_unregister_write ("perl");
1256 plugin_unregister_flush ("perl");
1258 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1260 pthread_mutex_lock (&perl_threads->mutex);
1261 t = perl_threads->tail;
1264 c_ithread_t *thr = t;
1266 /* the pointer has to be advanced before destroying
1267 * the thread as this will free the memory */
1270 c_ithread_destroy (thr);
1273 pthread_mutex_unlock (&perl_threads->mutex);
1274 pthread_mutex_destroy (&perl_threads->mutex);
1276 sfree (perl_threads);
1278 pthread_key_delete (perl_thr_key);
1282 plugin_unregister_shutdown ("perl");
1284 } /* static void perl_shutdown (void) */
1287 * Access functions for global variables.
1289 * These functions implement the "magic" used to access
1290 * the global variables from Perl.
1293 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1295 char *pv = mg->mg_ptr;
1298 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1300 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1302 char *pv = mg->mg_ptr;
1303 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1304 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1306 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1308 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1310 int *iv = (int *)mg->mg_ptr;
1311 sv_setiv (var, *iv);
1313 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1315 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1317 int *iv = (int *)mg->mg_ptr;
1318 *iv = (int)SvIV (var);
1320 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1322 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1323 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1325 /* bootstrap the Collectd module */
1326 static void xs_init (pTHX)
1330 char *file = __FILE__;
1336 /* enable usage of Perl modules using shared libraries */
1337 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1340 for (i = 0; NULL != api[i].f; ++i)
1341 newXS (api[i].name, api[i].f, file);
1343 stash = gv_stashpv ("Collectd", 1);
1345 /* export "constants" */
1346 for (i = 0; '\0' != constants[i].name[0]; ++i)
1347 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1349 /* export global variables
1350 * by adding "magic" to the SV's representing the globale variables
1351 * perl is able to automagically call the get/set function when
1352 * accessing any such variable (this is basically the same as using
1354 /* global strings */
1355 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1356 tmp = get_sv (g_strings[i].name, 1);
1357 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1358 g_strings[i].var, 0);
1361 /* global integers */
1362 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1363 tmp = get_sv (g_integers[i].name, 1);
1364 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1365 (char *)g_integers[i].var, 0);
1368 } /* static void xs_init (pTHX) */
1370 /* Initialize the global Perl interpreter. */
1371 static int init_pi (int argc, char **argv)
1375 if (NULL != perl_threads)
1378 log_info ("Initializing Perl interpreter...");
1383 for (i = 0; i < argc; ++i)
1384 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1386 #endif /* COLLECT_DEBUG */
1388 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1389 log_err ("init_pi: pthread_key_create failed");
1391 /* this must not happen - cowardly giving up if it does */
1395 PERL_SYS_INIT3 (&argc, &argv, &environ);
1397 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1398 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1400 pthread_mutex_init (&perl_threads->mutex, NULL);
1401 /* locking the mutex should not be necessary at this point
1402 * but let's just do it for the sake of completeness */
1403 pthread_mutex_lock (&perl_threads->mutex);
1405 perl_threads->head = c_ithread_create (NULL);
1406 perl_threads->tail = perl_threads->head;
1408 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1409 log_err ("init_pi: Not enough memory.");
1413 aTHX = perl_threads->head->interp;
1414 pthread_mutex_unlock (&perl_threads->mutex);
1416 perl_construct (aTHX);
1418 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1420 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1421 log_err ("init_pi: Unable to bootstrap Collectd.");
1425 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1426 sv_setpv (get_sv ("0", 0), "collectd");
1430 plugin_register_log ("perl", perl_log);
1431 plugin_register_notification ("perl", perl_notify);
1432 plugin_register_init ("perl", perl_init);
1434 plugin_register_read ("perl", perl_read);
1436 plugin_register_write ("perl", perl_write);
1437 plugin_register_flush ("perl", perl_flush);
1438 plugin_register_shutdown ("perl", perl_shutdown);
1440 } /* static int init_pi (const char **, const int) */
1443 * LoadPlugin "<Plugin>"
1445 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1447 char module_name[DATA_MAX_NAME_LEN];
1451 if ((0 != ci->children_num) || (1 != ci->values_num)
1452 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1453 log_err ("LoadPlugin expects a single string argument.");
1457 value = ci->values[0].value.string;
1459 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1460 log_err ("Invalid module name %s", value);
1464 init_pi (perl_argc, perl_argv);
1465 assert (NULL != perl_threads);
1466 assert (NULL != perl_threads->head);
1468 aTHX = perl_threads->head->interp;
1470 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1471 load_module (PERL_LOADMOD_NOIMPORT,
1472 newSVpv (module_name, strlen (module_name)), Nullsv);
1474 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1479 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1483 if ((0 != ci->children_num) || (1 != ci->values_num)
1484 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1485 log_err ("BaseName expects a single string argument.");
1489 value = ci->values[0].value.string;
1491 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1492 strncpy (base_name, value, sizeof (base_name));
1493 base_name[sizeof (base_name) - 1] = '\0';
1495 } /* static int perl_config_basename (oconfig_item_it *) */
1498 * EnableDebugger "<Package>"|""
1500 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1504 if ((0 != ci->children_num) || (1 != ci->values_num)
1505 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1506 log_err ("EnableDebugger expects a single string argument.");
1510 if (NULL != perl_threads) {
1511 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1515 value = ci->values[0].value.string;
1517 perl_argv = (char **)realloc (perl_argv,
1518 (++perl_argc + 1) * sizeof (char *));
1520 if (NULL == perl_argv) {
1521 log_err ("perl_config: Not enough memory.");
1525 if ('\0' == value[0]) {
1526 perl_argv[perl_argc - 1] = "-d";
1529 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1530 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1531 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1534 perl_argv[perl_argc] = NULL;
1536 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1539 * IncludeDir "<Dir>"
1541 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1545 if ((0 != ci->children_num) || (1 != ci->values_num)
1546 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1547 log_err ("IncludeDir expects a single string argument.");
1551 value = ci->values[0].value.string;
1554 perl_argv = (char **)realloc (perl_argv,
1555 (++perl_argc + 1) * sizeof (char *));
1557 if (NULL == perl_argv) {
1558 log_err ("perl_config: Not enough memory.");
1562 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1563 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1564 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1566 perl_argv[perl_argc] = NULL;
1569 /* prepend the directory to @INC */
1570 av_unshift (GvAVn (PL_incgv), 1);
1571 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1574 } /* static int perl_config_includedir (oconfig_item_it *) */
1576 static int perl_config (oconfig_item_t *ci)
1582 /* dTHX does not get any valid values in case Perl
1583 * has not been initialized */
1584 if (NULL == perl_threads)
1587 for (i = 0; i < ci->children_num; ++i) {
1588 oconfig_item_t *c = ci->children + i;
1590 if (0 == strcasecmp (c->key, "LoadPlugin"))
1591 perl_config_loadplugin (aTHX_ c);
1592 else if (0 == strcasecmp (c->key, "BaseName"))
1593 perl_config_basename (aTHX_ c);
1594 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1595 perl_config_enabledebugger (aTHX_ c);
1596 else if (0 == strcasecmp (c->key, "IncludeDir"))
1597 perl_config_includedir (aTHX_ c);
1599 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1602 } /* static int perl_config (oconfig_item_t *) */
1604 void module_register (void)
1607 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1609 /* default options for the Perl interpreter */
1611 perl_argv[1] = "-MCollectd";
1612 perl_argv[2] = "-e";
1614 perl_argv[4] = NULL;
1616 plugin_register_complex_config ("perl", perl_config);
1618 } /* void module_register (void) */
1620 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */