2 * collectd - src/perl.c
3 * Copyright (C) 2007 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
65 #define PLUGIN_TYPES 6
67 #define PLUGIN_DATASET 255
69 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
70 #define log_info(...) INFO ("perl: " __VA_ARGS__)
71 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
72 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
74 /* this is defined in DynaLoader.a */
75 void boot_DynaLoader (PerlInterpreter *, CV *);
77 static XS (Collectd_plugin_register_ds);
78 static XS (Collectd_plugin_unregister_ds);
79 static XS (Collectd_plugin_dispatch_values);
80 static XS (Collectd_plugin_dispatch_notification);
81 static XS (Collectd_plugin_log);
82 static XS (Collectd_call_by_name);
88 typedef struct c_ithread_s {
89 /* the thread's Perl interpreter */
90 PerlInterpreter *interp;
92 /* double linked list of threads */
93 struct c_ithread_s *prev;
94 struct c_ithread_s *next;
102 /* some usage stats */
103 int number_of_threads;
104 #endif /* COLLECT_DEBUG */
106 pthread_mutex_t mutex;
113 /* if perl_threads != NULL perl_threads->head must
114 * point to the "base" thread */
115 static c_ithread_list_t *perl_threads = NULL;
117 /* the key used to store each pthread's ithread */
118 static pthread_key_t perl_thr_key;
120 static int perl_argc = 0;
121 static char **perl_argv = NULL;
123 static char base_name[DATA_MAX_NAME_LEN] = "";
130 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
131 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
132 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
133 { "Collectd::plugin_dispatch_notification",
134 Collectd_plugin_dispatch_notification },
135 { "Collectd::plugin_log", Collectd_plugin_log },
136 { "Collectd::call_by_name", Collectd_call_by_name },
145 { "Collectd::TYPE_INIT", PLUGIN_INIT },
146 { "Collectd::TYPE_READ", PLUGIN_READ },
147 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
148 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
149 { "Collectd::TYPE_LOG", PLUGIN_LOG },
150 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
151 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
152 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
153 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
154 { "Collectd::LOG_ERR", LOG_ERR },
155 { "Collectd::LOG_WARNING", LOG_WARNING },
156 { "Collectd::LOG_NOTICE", LOG_NOTICE },
157 { "Collectd::LOG_INFO", LOG_INFO },
158 { "Collectd::LOG_DEBUG", LOG_DEBUG },
159 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
160 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
161 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
170 { "Collectd::hostname_g", hostname_g },
179 { "Collectd::interval_g", &interval_g },
184 * Helper functions for data type conversion.
199 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
203 if ((NULL == hash) || (NULL == ds))
206 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
207 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
208 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
211 log_err ("hv2data_source: No DS name given.");
215 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
216 ds->type = SvIV (*tmp);
218 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
219 log_err ("hv2data_source: Invalid DS type.");
224 ds->type = DS_TYPE_COUNTER;
227 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
228 ds->min = SvNV (*tmp);
232 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
233 ds->max = SvNV (*tmp);
237 } /* static data_source_t *hv2data_source (HV *) */
239 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
241 const data_set_t *ds;
245 if ((NULL == name) || (NULL == array) || (NULL == value))
248 if (av_len (array) < len - 1)
249 len = av_len (array) + 1;
254 ds = plugin_get_ds (name);
256 log_err ("av2value: Unknown dataset \"%s\"", name);
260 if (ds->ds_num < len) {
261 log_warn ("av2value: Value length exceeds data set length.");
265 for (i = 0; i < len; ++i) {
266 SV **tmp = av_fetch (array, i, 0);
269 if (DS_TYPE_COUNTER == ds->ds[i].type)
270 value[i].counter = SvIV (*tmp);
272 value[i].gauge = SvNV (*tmp);
279 } /* static int av2value (char *, AV *, value_t *, int) */
281 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
285 if ((NULL == ds) || (NULL == array))
288 av_extend (array, ds->ds_num);
290 for (i = 0; i < ds->ds_num; ++i) {
291 HV *source = newHV ();
293 if (NULL == hv_store (source, "name", 4,
294 newSVpv (ds->ds[i].name, 0), 0))
297 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
300 if (! isnan (ds->ds[i].min))
301 if (NULL == hv_store (source, "min", 3,
302 newSVnv (ds->ds[i].min), 0))
305 if (! isnan (ds->ds[i].max))
306 if (NULL == hv_store (source, "max", 3,
307 newSVnv (ds->ds[i].max), 0))
310 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
314 } /* static int data_set2av (data_set_t *, AV *) */
316 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
323 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
326 len = vl->values_len;
328 if (ds->ds_num < len) {
329 log_warn ("value2av: Value length exceeds data set length.");
334 av_extend (values, len - 1);
336 for (i = 0; i < len; ++i) {
339 if (DS_TYPE_COUNTER == ds->ds[i].type)
340 val = newSViv (vl->values[i].counter);
342 val = newSVnv (vl->values[i].gauge);
344 if (NULL == av_store (values, i, val)) {
350 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
354 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
357 if ('\0' != vl->host[0])
358 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
361 if ('\0' != vl->plugin[0])
362 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
365 if ('\0' != vl->plugin_instance[0])
366 if (NULL == hv_store (hash, "plugin_instance", 15,
367 newSVpv (vl->plugin_instance, 0), 0))
370 if ('\0' != vl->type_instance[0])
371 if (NULL == hv_store (hash, "type_instance", 13,
372 newSVpv (vl->type_instance, 0), 0))
375 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
377 static int notification2hv (pTHX_ notification_t *n, HV *hash)
379 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
383 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
386 if ('\0' != *n->message)
387 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
390 if ('\0' != *n->host)
391 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
394 if ('\0' != *n->plugin)
395 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
398 if ('\0' != *n->plugin_instance)
399 if (NULL == hv_store (hash, "plugin_instance", 15,
400 newSVpv (n->plugin_instance, 0), 0))
403 if ('\0' != *n->type)
404 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
407 if ('\0' != *n->type_instance)
408 if (NULL == hv_store (hash, "type_instance", 13,
409 newSVpv (n->type_instance, 0), 0))
412 } /* static int notification2hv (notification_t *, HV *) */
415 * Internal functions.
418 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
420 if (base_name[0] == '\0')
421 status = snprintf (buf, buf_len, "%s", module);
423 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
424 if ((status < 0) || ((unsigned int)status >= buf_len))
426 buf[buf_len - 1] = '\0';
428 } /* char *get_module_name */
431 * Add a plugin's data set definition.
433 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
439 data_source_t *ds = NULL;
440 data_set_t *set = NULL;
442 if ((NULL == name) || (NULL == dataset))
445 len = av_len (dataset);
450 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
451 set = (data_set_t *)smalloc (sizeof (data_set_t));
453 for (i = 0; i <= len; ++i) {
454 SV **elem = av_fetch (dataset, i, 0);
459 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
460 log_err ("pplugin_register_data_set: Invalid data source.");
464 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
467 log_debug ("pplugin_register_data_set: "
468 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
469 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
472 strncpy (set->type, name, DATA_MAX_NAME_LEN);
473 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
475 set->ds_num = len + 1;
478 ret = plugin_register_data_set (set);
483 } /* static int pplugin_register_data_set (char *, SV *) */
486 * Remove a plugin's data set definition.
488 static int pplugin_unregister_data_set (char *name)
492 return plugin_unregister_data_set (name);
493 } /* static int pplugin_unregister_data_set (char *) */
496 * Submit the values to the write functions.
500 * values => [ @values ],
504 * plugin_instance => $pinstance,
505 * type_instance => $tinstance,
508 static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
510 value_list_t list = VALUE_LIST_INIT;
517 if ((NULL == name) || (NULL == values))
520 if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
521 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
522 log_err ("pplugin_dispatch_values: No valid values given.");
527 AV *array = (AV *)SvRV (*tmp);
528 int len = av_len (array) + 1;
533 val = (value_t *)smalloc (len * sizeof (value_t));
535 list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
538 if (-1 == list.values_len) {
544 if (NULL != (tmp = hv_fetch (values, "time", 4, 0))) {
545 list.time = (time_t)SvIV (*tmp);
548 list.time = time (NULL);
551 if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
552 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
553 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
556 strcpy (list.host, hostname_g);
559 if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
560 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
561 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
564 if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
565 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
566 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
569 if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
570 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
571 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
574 ret = plugin_dispatch_values (name, &list);
578 } /* static int pplugin_dispatch_values (char *, HV *) */
581 * Dispatch a notification.
585 * severity => $severity,
591 * plugin_instance => $instance,
592 * type_instance => $type_instance
595 static int pplugin_dispatch_notification (pTHX_ HV *notif)
604 memset (&n, 0, sizeof (n));
606 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
607 n.severity = SvIV (*tmp);
609 n.severity = NOTIF_FAILURE;
611 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
612 n.time = (time_t)SvIV (*tmp);
614 n.time = time (NULL);
616 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
617 strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
618 n.message[sizeof (n.message) - 1] = '\0';
620 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
621 strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
623 strncpy (n.host, hostname_g, sizeof (n.host));
624 n.host[sizeof (n.host) - 1] = '\0';
626 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
627 strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
628 n.plugin[sizeof (n.plugin) - 1] = '\0';
630 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
631 strncpy (n.plugin_instance, SvPV_nolen (*tmp),
632 sizeof (n.plugin_instance));
633 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
635 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
636 strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
637 n.type[sizeof (n.type) - 1] = '\0';
639 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
640 strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
641 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
642 return plugin_dispatch_notification (&n);
643 } /* static int pplugin_dispatch_notification (HV *) */
646 * Call all working functions of the given type.
648 static int pplugin_call_all (pTHX_ int type, ...)
657 if ((type < 0) || (type >= PLUGIN_TYPES))
667 XPUSHs (sv_2mortal (newSViv ((IV)type)));
669 if (PLUGIN_WRITE == type) {
671 * $_[0] = $plugin_type;
686 * values => [ $v1, ... ],
690 * plugin_instance => $instance,
691 * type_instance => $type_instance
700 ds = va_arg (ap, data_set_t *);
701 vl = va_arg (ap, value_list_t *);
703 if (-1 == data_set2av (aTHX_ ds, pds))
706 if (-1 == value_list2hv (aTHX_ vl, ds, pvl))
709 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
710 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
711 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
713 else if (PLUGIN_LOG == type) {
719 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
720 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
722 else if (PLUGIN_NOTIF == type) {
726 * severity => $severity,
732 * plugin_instance => $instance,
733 * type_instance => $type_instance
737 HV *notif = newHV ();
739 n = va_arg (ap, notification_t *);
741 if (-1 == notification2hv (aTHX_ n, notif))
744 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
749 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
764 } /* static int pplugin_call_all (int, ...) */
771 * Collectd::plugin_register_data_set (type, dataset).
774 * type of the dataset
777 * dataset to be registered
779 static XS (Collectd_plugin_register_ds)
787 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
791 log_debug ("Collectd::plugin_register_data_set: "
792 "type = \"%s\", dataset = \"%s\"",
793 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
797 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
798 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
802 log_err ("Collectd::plugin_register_data_set: Invalid data.");
810 } /* static XS (Collectd_plugin_register_ds) */
813 * Collectd::plugin_unregister_data_set (type).
816 * type of the dataset
818 static XS (Collectd_plugin_unregister_ds)
823 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
827 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
828 SvPV_nolen (ST (0)));
830 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
834 } /* static XS (Collectd_plugin_register_ds) */
837 * Collectd::plugin_dispatch_values (name, values).
843 * value list to submit
845 static XS (Collectd_plugin_dispatch_values)
854 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
858 log_debug ("Collectd::plugin_dispatch_values: "
859 "name = \"%s\", values=\"%s\"",
860 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
864 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
865 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
869 if ((NULL == ST (0)) || (NULL == values))
872 ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
873 (HV *)SvRV (values));
879 } /* static XS (Collectd_plugin_dispatch_values) */
882 * Collectd::plugin_dispatch_notification (notif).
885 * notification to dispatch
887 static XS (Collectd_plugin_dispatch_notification)
896 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
900 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
901 SvPV_nolen (ST (0)));
905 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
906 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
910 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
916 } /* static XS (Collectd_plugin_dispatch_notification) */
919 * Collectd::plugin_log (level, message).
922 * log level (LOG_DEBUG, ... LOG_ERR)
927 static XS (Collectd_plugin_log)
932 log_err ("Usage: Collectd::plugin_log(level, message)");
936 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
938 } /* static XS (Collectd_plugin_log) */
941 * Collectd::call_by_name (...).
943 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
945 static XS (Collectd_call_by_name)
950 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
951 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
956 name = SvPV_nolen (tmp);
958 if (NULL == get_cv (name, 0)) {
959 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
964 /* simply pass on the subroutine call without touching the stack,
965 * thus leaving any arguments and return values in place */
967 } /* static XS (Collectd_call_by_name) */
970 * collectd's perl interpreter based thread implementation.
972 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
975 /* must be called with perl_threads->mutex locked */
976 static void c_ithread_destroy (c_ithread_t *ithread)
978 dTHXa (ithread->interp);
980 assert (NULL != perl_threads);
982 PERL_SET_CONTEXT (aTHX);
983 log_debug ("Shutting down Perl interpreter %p...", aTHX);
988 --perl_threads->number_of_threads;
989 #endif /* COLLECT_DEBUG */
991 perl_destruct (aTHX);
994 if (NULL == ithread->prev)
995 perl_threads->head = ithread->next;
997 ithread->prev->next = ithread->next;
999 if (NULL == ithread->next)
1000 perl_threads->tail = ithread->prev;
1002 ithread->next->prev = ithread->prev;
1006 } /* static void c_ithread_destroy (c_ithread_t *) */
1008 static void c_ithread_destructor (void *arg)
1010 c_ithread_t *ithread = (c_ithread_t *)arg;
1011 c_ithread_t *t = NULL;
1013 if (NULL == perl_threads)
1016 pthread_mutex_lock (&perl_threads->mutex);
1018 for (t = perl_threads->head; NULL != t; t = t->next)
1022 /* the ithread no longer exists */
1026 c_ithread_destroy (ithread);
1028 pthread_mutex_unlock (&perl_threads->mutex);
1030 } /* static void c_ithread_destructor (void *) */
1032 /* must be called with perl_threads->mutex locked */
1033 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1035 c_ithread_t *t = NULL;
1038 assert (NULL != perl_threads);
1040 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1041 memset (t, 0, sizeof (c_ithread_t));
1043 t->interp = (NULL == base)
1045 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1050 av_clear (PL_endav);
1051 av_undef (PL_endav);
1056 ++perl_threads->number_of_threads;
1057 #endif /* COLLECT_DEBUG */
1061 if (NULL == perl_threads->tail) {
1062 perl_threads->head = t;
1066 perl_threads->tail->next = t;
1067 t->prev = perl_threads->tail;
1070 perl_threads->tail = t;
1072 pthread_setspecific (perl_thr_key, (const void *)t);
1074 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1077 * Interface to collectd.
1080 static int perl_init (void)
1084 if (NULL == perl_threads)
1088 c_ithread_t *t = NULL;
1090 pthread_mutex_lock (&perl_threads->mutex);
1091 t = c_ithread_create (perl_threads->head->interp);
1092 pthread_mutex_unlock (&perl_threads->mutex);
1097 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1098 aTHX, perl_threads->number_of_threads);
1099 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1100 } /* static int perl_init (void) */
1102 static int perl_read (void)
1106 if (NULL == perl_threads)
1110 c_ithread_t *t = NULL;
1112 pthread_mutex_lock (&perl_threads->mutex);
1113 t = c_ithread_create (perl_threads->head->interp);
1114 pthread_mutex_unlock (&perl_threads->mutex);
1119 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1120 aTHX, perl_threads->number_of_threads);
1121 return pplugin_call_all (aTHX_ PLUGIN_READ);
1122 } /* static int perl_read (void) */
1124 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1128 if (NULL == perl_threads)
1132 c_ithread_t *t = NULL;
1134 pthread_mutex_lock (&perl_threads->mutex);
1135 t = c_ithread_create (perl_threads->head->interp);
1136 pthread_mutex_unlock (&perl_threads->mutex);
1141 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1142 aTHX, perl_threads->number_of_threads);
1143 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1144 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1146 static void perl_log (int level, const char *msg)
1150 if (NULL == perl_threads)
1154 c_ithread_t *t = NULL;
1156 pthread_mutex_lock (&perl_threads->mutex);
1157 t = c_ithread_create (perl_threads->head->interp);
1158 pthread_mutex_unlock (&perl_threads->mutex);
1163 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1165 } /* static void perl_log (int, const char *) */
1167 static int perl_notify (const notification_t *notif)
1171 if (NULL == perl_threads)
1175 c_ithread_t *t = NULL;
1177 pthread_mutex_lock (&perl_threads->mutex);
1178 t = c_ithread_create (perl_threads->head->interp);
1179 pthread_mutex_unlock (&perl_threads->mutex);
1183 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1184 } /* static int perl_notify (const notification_t *) */
1186 static int perl_shutdown (void)
1188 c_ithread_t *t = NULL;
1194 plugin_unregister_complex_config ("perl");
1196 if (NULL == perl_threads)
1200 c_ithread_t *t = NULL;
1202 pthread_mutex_lock (&perl_threads->mutex);
1203 t = c_ithread_create (perl_threads->head->interp);
1204 pthread_mutex_unlock (&perl_threads->mutex);
1209 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1210 aTHX, perl_threads->number_of_threads);
1212 plugin_unregister_log ("perl");
1213 plugin_unregister_notification ("perl");
1214 plugin_unregister_init ("perl");
1215 plugin_unregister_read ("perl");
1216 plugin_unregister_write ("perl");
1218 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1220 pthread_mutex_lock (&perl_threads->mutex);
1221 t = perl_threads->tail;
1224 c_ithread_t *thr = t;
1226 /* the pointer has to be advanced before destroying
1227 * the thread as this will free the memory */
1230 c_ithread_destroy (thr);
1233 pthread_mutex_unlock (&perl_threads->mutex);
1234 pthread_mutex_destroy (&perl_threads->mutex);
1236 sfree (perl_threads);
1238 pthread_key_delete (perl_thr_key);
1242 plugin_unregister_shutdown ("perl");
1244 } /* static void perl_shutdown (void) */
1247 * Access functions for global variables.
1249 * These functions implement the "magic" used to access
1250 * the global variables from Perl.
1253 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1255 char *pv = mg->mg_ptr;
1258 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1260 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1262 char *pv = mg->mg_ptr;
1263 strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1264 pv[DATA_MAX_NAME_LEN - 1] = '\0';
1266 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1268 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1270 int *iv = (int *)mg->mg_ptr;
1271 sv_setiv (var, *iv);
1273 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1275 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1277 int *iv = (int *)mg->mg_ptr;
1278 *iv = (int)SvIV (var);
1280 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1282 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1283 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1285 /* bootstrap the Collectd module */
1286 static void xs_init (pTHX)
1290 char *file = __FILE__;
1296 /* enable usage of Perl modules using shared libraries */
1297 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1300 for (i = 0; NULL != api[i].f; ++i)
1301 newXS (api[i].name, api[i].f, file);
1303 stash = gv_stashpv ("Collectd", 1);
1305 /* export "constants" */
1306 for (i = 0; '\0' != constants[i].name[0]; ++i)
1307 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1309 /* export global variables
1310 * by adding "magic" to the SV's representing the globale variables
1311 * perl is able to automagically call the get/set function when
1312 * accessing any such variable (this is basically the same as using
1314 /* global strings */
1315 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1316 tmp = get_sv (g_strings[i].name, 1);
1317 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1318 g_strings[i].var, 0);
1321 /* global integers */
1322 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1323 tmp = get_sv (g_integers[i].name, 1);
1324 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1325 (char *)g_integers[i].var, 0);
1328 } /* static void xs_init (pTHX) */
1330 /* Initialize the global Perl interpreter. */
1331 static int init_pi (int argc, char **argv)
1335 if (NULL != perl_threads)
1338 log_info ("Initializing Perl interpreter...");
1343 for (i = 0; i < argc; ++i)
1344 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1346 #endif /* COLLECT_DEBUG */
1348 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1349 log_err ("init_pi: pthread_key_create failed");
1351 /* this must not happen - cowardly giving up if it does */
1355 PERL_SYS_INIT3 (&argc, &argv, &environ);
1357 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1358 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1360 pthread_mutex_init (&perl_threads->mutex, NULL);
1361 /* locking the mutex should not be necessary at this point
1362 * but let's just do it for the sake of completeness */
1363 pthread_mutex_lock (&perl_threads->mutex);
1365 perl_threads->head = c_ithread_create (NULL);
1366 perl_threads->tail = perl_threads->head;
1368 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1369 log_err ("init_pi: Not enough memory.");
1373 aTHX = perl_threads->head->interp;
1374 pthread_mutex_unlock (&perl_threads->mutex);
1376 perl_construct (aTHX);
1378 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1380 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1381 log_err ("init_pi: Unable to bootstrap Collectd.");
1385 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1386 sv_setpv (get_sv ("0", 0), "collectd");
1390 plugin_register_log ("perl", perl_log);
1391 plugin_register_notification ("perl", perl_notify);
1392 plugin_register_init ("perl", perl_init);
1394 plugin_register_read ("perl", perl_read);
1396 plugin_register_write ("perl", perl_write);
1397 plugin_register_shutdown ("perl", perl_shutdown);
1399 } /* static int init_pi (const char **, const int) */
1402 * LoadPlugin "<Plugin>"
1404 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1406 char module_name[DATA_MAX_NAME_LEN];
1410 if ((0 != ci->children_num) || (1 != ci->values_num)
1411 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1412 log_err ("LoadPlugin expects a single string argument.");
1416 value = ci->values[0].value.string;
1418 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1419 log_err ("Invalid module name %s", value);
1423 init_pi (perl_argc, perl_argv);
1424 assert (NULL != perl_threads);
1425 assert (NULL != perl_threads->head);
1427 aTHX = perl_threads->head->interp;
1429 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1430 load_module (PERL_LOADMOD_NOIMPORT,
1431 newSVpv (module_name, strlen (module_name)), Nullsv);
1433 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1438 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1442 if ((0 != ci->children_num) || (1 != ci->values_num)
1443 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1444 log_err ("BaseName expects a single string argument.");
1448 value = ci->values[0].value.string;
1450 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1451 strncpy (base_name, value, sizeof (base_name));
1452 base_name[sizeof (base_name) - 1] = '\0';
1454 } /* static int perl_config_basename (oconfig_item_it *) */
1457 * EnableDebugger "<Package>"|""
1459 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1463 if ((0 != ci->children_num) || (1 != ci->values_num)
1464 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1465 log_err ("EnableDebugger expects a single string argument.");
1469 value = ci->values[0].value.string;
1471 perl_argv = (char **)realloc (perl_argv,
1472 (++perl_argc + 1) * sizeof (char *));
1474 if (NULL == perl_argv) {
1475 log_err ("perl_config: Not enough memory.");
1479 if ('\0' == value[0]) {
1480 perl_argv[perl_argc - 1] = "-d";
1483 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1484 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1485 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1488 perl_argv[perl_argc] = NULL;
1490 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1493 * IncludeDir "<Dir>"
1495 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1499 if ((0 != ci->children_num) || (1 != ci->values_num)
1500 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1501 log_err ("IncludeDir expects a single string argument.");
1505 value = ci->values[0].value.string;
1508 perl_argv = (char **)realloc (perl_argv,
1509 (++perl_argc + 1) * sizeof (char *));
1511 if (NULL == perl_argv) {
1512 log_err ("perl_config: Not enough memory.");
1516 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1517 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1518 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1520 perl_argv[perl_argc] = NULL;
1523 /* prepend the directory to @INC */
1524 av_unshift (GvAVn (PL_incgv), 1);
1525 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1528 } /* static int perl_config_includedir (oconfig_item_it *) */
1530 static int perl_config (oconfig_item_t *ci)
1536 /* dTHX does not get any valid values in case Perl
1537 * has not been initialized */
1538 if (NULL == perl_threads)
1541 for (i = 0; i < ci->children_num; ++i) {
1542 oconfig_item_t *c = ci->children + i;
1544 if (0 == strcasecmp (c->key, "LoadPlugin"))
1545 perl_config_loadplugin (aTHX_ c);
1546 else if (0 == strcasecmp (c->key, "BaseName"))
1547 perl_config_basename (aTHX_ c);
1548 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1549 perl_config_enabledebugger (aTHX_ c);
1550 else if (0 == strcasecmp (c->key, "IncludeDir"))
1551 perl_config_includedir (aTHX_ c);
1553 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1556 } /* static int perl_config (oconfig_item_t *) */
1558 void module_register (void)
1561 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1563 /* default options for the Perl interpreter */
1565 perl_argv[1] = "-MCollectd";
1566 perl_argv[2] = "-e";
1568 perl_argv[4] = NULL;
1570 plugin_register_complex_config ("perl", perl_config);
1572 } /* void module_register (void) */
1574 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */