2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 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"
43 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
44 # pragma GCC poison sprintf
49 /* Some versions of Perl define their own version of DEBUG... :-/ */
54 /* ... while we want the definition found in plugin.h. */
58 #include "filter_chain.h"
62 #if !defined(USE_ITHREADS)
63 # error "Perl does not support ithreads!"
64 #endif /* !defined(USE_ITHREADS) */
66 /* clear the Perl sub's stack frame
67 * (this should only be used inside an XSUB) */
68 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
72 #define PLUGIN_WRITE 2
73 #define PLUGIN_SHUTDOWN 3
75 #define PLUGIN_NOTIF 5
76 #define PLUGIN_FLUSH 6
78 #define PLUGIN_TYPES 7
80 #define PLUGIN_CONFIG 254
81 #define PLUGIN_DATASET 255
88 #define FC_CB_CREATE 0
89 #define FC_CB_DESTROY 1
94 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
95 #define log_info(...) INFO ("perl: " __VA_ARGS__)
96 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
97 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
99 /* this is defined in DynaLoader.a */
100 void boot_DynaLoader (PerlInterpreter *, CV *);
102 static XS (Collectd_plugin_register_ds);
103 static XS (Collectd_plugin_unregister_ds);
104 static XS (Collectd_plugin_dispatch_values);
105 static XS (Collectd_plugin_get_interval);
106 static XS (Collectd__plugin_write);
107 static XS (Collectd__plugin_flush);
108 static XS (Collectd_plugin_dispatch_notification);
109 static XS (Collectd_plugin_log);
110 static XS (Collectd__fc_register);
111 static XS (Collectd_call_by_name);
117 typedef struct c_ithread_s {
118 /* the thread's Perl interpreter */
119 PerlInterpreter *interp;
121 /* double linked list of threads */
122 struct c_ithread_s *prev;
123 struct c_ithread_s *next;
131 /* some usage stats */
132 int number_of_threads;
133 #endif /* COLLECT_DEBUG */
135 pthread_mutex_t mutex;
138 /* name / user_data for Perl matches / targets */
144 #define PFC_USER_DATA_FREE(data) \
146 sfree ((data)->name); \
147 if (NULL != (data)->user_data) \
148 sv_free ((data)->user_data); \
155 extern char **environ;
161 /* if perl_threads != NULL perl_threads->head must
162 * point to the "base" thread */
163 static c_ithread_list_t *perl_threads = NULL;
165 /* the key used to store each pthread's ithread */
166 static pthread_key_t perl_thr_key;
168 static int perl_argc = 0;
169 static char **perl_argv = NULL;
171 static char base_name[DATA_MAX_NAME_LEN] = "";
178 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
179 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
180 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
181 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
182 { "Collectd::_plugin_write", Collectd__plugin_write },
183 { "Collectd::_plugin_flush", Collectd__plugin_flush },
184 { "Collectd::plugin_dispatch_notification",
185 Collectd_plugin_dispatch_notification },
186 { "Collectd::plugin_log", Collectd_plugin_log },
187 { "Collectd::_fc_register", Collectd__fc_register },
188 { "Collectd::call_by_name", Collectd_call_by_name },
197 { "Collectd::TYPE_INIT", PLUGIN_INIT },
198 { "Collectd::TYPE_READ", PLUGIN_READ },
199 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
200 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
201 { "Collectd::TYPE_LOG", PLUGIN_LOG },
202 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
203 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
204 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
205 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
206 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
207 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
208 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
209 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
210 { "Collectd::LOG_ERR", LOG_ERR },
211 { "Collectd::LOG_WARNING", LOG_WARNING },
212 { "Collectd::LOG_NOTICE", LOG_NOTICE },
213 { "Collectd::LOG_INFO", LOG_INFO },
214 { "Collectd::LOG_DEBUG", LOG_DEBUG },
215 { "Collectd::FC_MATCH", FC_MATCH },
216 { "Collectd::FC_TARGET", FC_TARGET },
217 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
218 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
219 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
220 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
221 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
222 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
223 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
224 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
225 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
226 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
227 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
236 { "Collectd::hostname_g", hostname_g },
241 * Helper functions for data type conversion.
256 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
260 if ((NULL == hash) || (NULL == ds))
263 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
264 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
267 log_err ("hv2data_source: No DS name given.");
271 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
272 ds->type = SvIV (*tmp);
274 if ((DS_TYPE_COUNTER != ds->type)
275 && (DS_TYPE_GAUGE != ds->type)
276 && (DS_TYPE_DERIVE != ds->type)
277 && (DS_TYPE_ABSOLUTE != ds->type)) {
278 log_err ("hv2data_source: Invalid DS type.");
283 ds->type = DS_TYPE_COUNTER;
286 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
287 ds->min = SvNV (*tmp);
291 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
292 ds->max = SvNV (*tmp);
296 } /* static int hv2data_source (HV *, data_source_t *) */
298 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
300 const data_set_t *ds;
304 if ((NULL == name) || (NULL == array) || (NULL == value))
307 if (av_len (array) < len - 1)
308 len = av_len (array) + 1;
313 ds = plugin_get_ds (name);
315 log_err ("av2value: Unknown dataset \"%s\"", name);
319 if (ds->ds_num < len) {
320 log_warn ("av2value: Value length exceeds data set length.");
324 for (i = 0; i < len; ++i) {
325 SV **tmp = av_fetch (array, i, 0);
328 if (DS_TYPE_COUNTER == ds->ds[i].type)
329 value[i].counter = SvIV (*tmp);
330 else if (DS_TYPE_GAUGE == ds->ds[i].type)
331 value[i].gauge = SvNV (*tmp);
332 else if (DS_TYPE_DERIVE == ds->ds[i].type)
333 value[i].derive = SvIV (*tmp);
334 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
335 value[i].absolute = SvIV (*tmp);
342 } /* static int av2value (char *, AV *, value_t *, int) */
347 * values => [ @values ],
351 * plugin_instance => $pinstance,
352 * type_instance => $tinstance,
355 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
359 if ((NULL == hash) || (NULL == vl))
362 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
363 log_err ("hv2value_list: No type given.");
367 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
369 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
370 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
371 log_err ("hv2value_list: No valid values given.");
376 AV *array = (AV *)SvRV (*tmp);
377 int len = av_len (array) + 1;
382 vl->values = (value_t *)smalloc (len * sizeof (value_t));
383 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
386 if (-1 == vl->values_len) {
392 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
394 double t = SvNV (*tmp);
395 vl->time = DOUBLE_TO_CDTIME_T (t);
398 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
400 double t = SvNV (*tmp);
401 vl->interval = DOUBLE_TO_CDTIME_T (t);
404 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
405 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
407 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
409 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
410 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
412 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
413 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
414 sizeof (vl->plugin_instance));
416 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
417 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
418 sizeof (vl->type_instance));
420 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
422 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
426 if ((NULL == array) || (NULL == name) || (NULL == ds))
429 len = av_len (array);
432 log_err ("av2data_set: Invalid data set.");
436 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
437 ds->ds_num = len + 1;
439 for (i = 0; i <= len; ++i) {
440 SV **elem = av_fetch (array, i, 0);
443 log_err ("av2data_set: Failed to fetch data source %i.", i);
447 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
448 log_err ("av2data_set: Invalid data source.");
452 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
455 log_debug ("av2data_set: "
456 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
457 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
460 sstrncpy (ds->type, name, sizeof (ds->type));
462 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
467 * severity => $severity,
473 * plugin_instance => $instance,
474 * type_instance => $type_instance,
475 * meta => [ { name => <name>, value => <value> }, ... ]
478 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
480 notification_meta_t **m = meta;
482 int len = av_len (array);
485 for (i = 0; i <= len; ++i) {
486 SV **tmp = av_fetch (array, i, 0);
492 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
493 log_warn ("av2notification_meta: Skipping invalid "
494 "meta information.");
498 hash = (HV *)SvRV (*tmp);
500 *m = (notification_meta_t *)smalloc (sizeof (**m));
502 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
503 log_warn ("av2notification_meta: Skipping invalid "
504 "meta information.");
508 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
510 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
511 log_warn ("av2notification_meta: Skipping invalid "
512 "meta information.");
519 (*m)->nm_value.nm_double = SvNVX (*tmp);
520 (*m)->type = NM_TYPE_DOUBLE;
522 else if (SvUOK (*tmp)) {
523 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
524 (*m)->type = NM_TYPE_UNSIGNED_INT;
526 else if (SvIOK (*tmp)) {
527 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
528 (*m)->type = NM_TYPE_SIGNED_INT;
531 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
532 (*m)->type = NM_TYPE_STRING;
539 } /* static int av2notification_meta (AV *, notification_meta_t *) */
541 static int hv2notification (pTHX_ HV *hash, notification_t *n)
545 if ((NULL == hash) || (NULL == n))
548 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
549 n->severity = SvIV (*tmp);
551 n->severity = NOTIF_FAILURE;
553 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
555 double t = SvNV (*tmp);
556 n->time = DOUBLE_TO_CDTIME_T (t);
561 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
562 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
564 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
565 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
567 sstrncpy (n->host, hostname_g, sizeof (n->host));
569 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
570 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
572 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
573 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
574 sizeof (n->plugin_instance));
576 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
577 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
579 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
580 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
581 sizeof (n->type_instance));
584 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
585 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
586 log_warn ("hv2notification: Ignoring invalid meta information.");
590 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
591 plugin_notification_meta_free (n->meta);
598 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
600 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
604 if ((NULL == ds) || (NULL == array))
607 av_extend (array, ds->ds_num);
609 for (i = 0; i < ds->ds_num; ++i) {
610 HV *source = newHV ();
612 if (NULL == hv_store (source, "name", 4,
613 newSVpv (ds->ds[i].name, 0), 0))
616 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
619 if (! isnan (ds->ds[i].min))
620 if (NULL == hv_store (source, "min", 3,
621 newSVnv (ds->ds[i].min), 0))
624 if (! isnan (ds->ds[i].max))
625 if (NULL == hv_store (source, "max", 3,
626 newSVnv (ds->ds[i].max), 0))
629 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
633 } /* static int data_set2av (data_set_t *, AV *) */
635 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
642 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
645 len = vl->values_len;
647 if (ds->ds_num < len) {
648 log_warn ("value2av: Value length exceeds data set length.");
653 av_extend (values, len - 1);
655 for (i = 0; i < len; ++i) {
658 if (DS_TYPE_COUNTER == ds->ds[i].type)
659 val = newSViv (vl->values[i].counter);
660 else if (DS_TYPE_GAUGE == ds->ds[i].type)
661 val = newSVnv (vl->values[i].gauge);
662 else if (DS_TYPE_DERIVE == ds->ds[i].type)
663 val = newSViv (vl->values[i].derive);
664 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
665 val = newSViv (vl->values[i].absolute);
667 if (NULL == av_store (values, i, val)) {
673 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
678 double t = CDTIME_T_TO_DOUBLE (vl->time);
679 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
684 double t = CDTIME_T_TO_DOUBLE (vl->interval);
685 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
689 if ('\0' != vl->host[0])
690 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
693 if ('\0' != vl->plugin[0])
694 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
697 if ('\0' != vl->plugin_instance[0])
698 if (NULL == hv_store (hash, "plugin_instance", 15,
699 newSVpv (vl->plugin_instance, 0), 0))
702 if ('\0' != vl->type[0])
703 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
706 if ('\0' != vl->type_instance[0])
707 if (NULL == hv_store (hash, "type_instance", 13,
708 newSVpv (vl->type_instance, 0), 0))
711 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
713 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
723 av_extend (array, meta_num);
725 for (i = 0; NULL != meta; meta = meta->next, ++i) {
729 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
732 if (NM_TYPE_STRING == meta->type)
733 value = newSVpv (meta->nm_value.nm_string, 0);
734 else if (NM_TYPE_SIGNED_INT == meta->type)
735 value = newSViv (meta->nm_value.nm_signed_int);
736 else if (NM_TYPE_UNSIGNED_INT == meta->type)
737 value = newSVuv (meta->nm_value.nm_unsigned_int);
738 else if (NM_TYPE_DOUBLE == meta->type)
739 value = newSVnv (meta->nm_value.nm_double);
740 else if (NM_TYPE_BOOLEAN == meta->type)
741 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
745 if (NULL == hv_store (m, "value", 5, value, 0)) {
750 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
757 } /* static int notification_meta2av (notification_meta_t *, AV *) */
759 static int notification2hv (pTHX_ notification_t *n, HV *hash)
761 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
766 double t = CDTIME_T_TO_DOUBLE (n->time);
767 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
771 if ('\0' != *n->message)
772 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
775 if ('\0' != *n->host)
776 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
779 if ('\0' != *n->plugin)
780 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
783 if ('\0' != *n->plugin_instance)
784 if (NULL == hv_store (hash, "plugin_instance", 15,
785 newSVpv (n->plugin_instance, 0), 0))
788 if ('\0' != *n->type)
789 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
792 if ('\0' != *n->type_instance)
793 if (NULL == hv_store (hash, "type_instance", 13,
794 newSVpv (n->type_instance, 0), 0))
797 if (NULL != n->meta) {
799 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
800 || (NULL == hv_store (hash, "meta", 4,
801 newRV_noinc ((SV *)meta), 0))) {
808 } /* static int notification2hv (notification_t *, HV *) */
810 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
817 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
821 if (0 < ci->values_num)
822 av_extend (values, ci->values_num);
824 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
830 for (i = 0; i < ci->values_num; ++i) {
833 switch (ci->values[i].type) {
834 case OCONFIG_TYPE_STRING:
835 value = newSVpv (ci->values[i].value.string, 0);
837 case OCONFIG_TYPE_NUMBER:
838 value = newSVnv ((NV)ci->values[i].value.number);
840 case OCONFIG_TYPE_BOOLEAN:
841 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
844 log_err ("oconfig_item2hv: Invalid value type %i.",
846 value = &PL_sv_undef;
849 if (NULL == av_store (values, i, value)) {
855 /* ignoring 'parent' member which is uninteresting in this case */
858 if (0 < ci->children_num)
859 av_extend (children, ci->children_num);
861 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
867 for (i = 0; i < ci->children_num; ++i) {
868 HV *child = newHV ();
870 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
876 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
883 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
886 * Internal functions.
889 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
891 if (base_name[0] == '\0')
892 status = ssnprintf (buf, buf_len, "%s", module);
894 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
895 if ((status < 0) || ((unsigned int)status >= buf_len))
898 } /* char *get_module_name */
901 * Add a plugin's data set definition.
903 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
909 if ((NULL == name) || (NULL == dataset))
912 if (0 != av2data_set (aTHX_ dataset, name, &ds))
915 ret = plugin_register_data_set (&ds);
919 } /* static int pplugin_register_data_set (char *, SV *) */
922 * Remove a plugin's data set definition.
924 static int pplugin_unregister_data_set (char *name)
928 return plugin_unregister_data_set (name);
929 } /* static int pplugin_unregister_data_set (char *) */
932 * Submit the values to the write functions.
934 static int pplugin_dispatch_values (pTHX_ HV *values)
936 value_list_t vl = VALUE_LIST_INIT;
943 if (0 != hv2value_list (aTHX_ values, &vl))
946 ret = plugin_dispatch_values (&vl);
950 } /* static int pplugin_dispatch_values (char *, HV *) */
953 * Submit the values to a single write function.
955 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
958 value_list_t vl = VALUE_LIST_INIT;
965 if (0 != hv2value_list (aTHX_ values, &vl))
968 if ((NULL != data_set)
969 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
972 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
974 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
975 NULL == plugin ? "<any>" : plugin, ret);
977 if (NULL != data_set)
981 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
984 * Dispatch a notification.
986 static int pplugin_dispatch_notification (pTHX_ HV *notif)
995 memset (&n, 0, sizeof (n));
997 if (0 != hv2notification (aTHX_ notif, &n))
1000 ret = plugin_dispatch_notification (&n);
1001 plugin_notification_meta_free (n.meta);
1003 } /* static int pplugin_dispatch_notification (HV *) */
1006 * Call all working functions of the given type.
1008 static int pplugin_call_all (pTHX_ int type, ...)
1017 if ((type < 0) || (type >= PLUGIN_TYPES))
1020 va_start (ap, type);
1027 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1029 if (PLUGIN_WRITE == type) {
1031 * $_[0] = $plugin_type;
1046 * values => [ $v1, ... ],
1048 * host => $hostname,
1049 * plugin => $plugin,
1051 * plugin_instance => $instance,
1052 * type_instance => $type_instance
1061 ds = va_arg (ap, data_set_t *);
1062 vl = va_arg (ap, value_list_t *);
1064 if (-1 == data_set2av (aTHX_ ds, pds)) {
1067 pds = (AV *)&PL_sv_undef;
1071 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1074 pvl = (HV *)&PL_sv_undef;
1078 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1079 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1080 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1082 else if (PLUGIN_LOG == type) {
1088 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1089 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1091 else if (PLUGIN_NOTIF == type) {
1095 * severity => $severity,
1099 * plugin => $plugin,
1101 * plugin_instance => $instance,
1102 * type_instance => $type_instance
1106 HV *notif = newHV ();
1108 n = va_arg (ap, notification_t *);
1110 if (-1 == notification2hv (aTHX_ n, notif)) {
1113 notif = (HV *)&PL_sv_undef;
1117 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1119 else if (PLUGIN_FLUSH == type) {
1124 * $_[1] = $identifier;
1126 timeout = va_arg (ap, cdtime_t);
1128 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1129 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1134 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1149 } /* static int pplugin_call_all (int, ...) */
1152 * collectd's perl interpreter based thread implementation.
1154 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1157 /* must be called with perl_threads->mutex locked */
1158 static void c_ithread_destroy (c_ithread_t *ithread)
1160 dTHXa (ithread->interp);
1162 assert (NULL != perl_threads);
1164 PERL_SET_CONTEXT (aTHX);
1165 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1170 --perl_threads->number_of_threads;
1171 #endif /* COLLECT_DEBUG */
1173 perl_destruct (aTHX);
1176 if (NULL == ithread->prev)
1177 perl_threads->head = ithread->next;
1179 ithread->prev->next = ithread->next;
1181 if (NULL == ithread->next)
1182 perl_threads->tail = ithread->prev;
1184 ithread->next->prev = ithread->prev;
1188 } /* static void c_ithread_destroy (c_ithread_t *) */
1190 static void c_ithread_destructor (void *arg)
1192 c_ithread_t *ithread = (c_ithread_t *)arg;
1193 c_ithread_t *t = NULL;
1195 if (NULL == perl_threads)
1198 pthread_mutex_lock (&perl_threads->mutex);
1200 for (t = perl_threads->head; NULL != t; t = t->next)
1204 /* the ithread no longer exists */
1208 c_ithread_destroy (ithread);
1210 pthread_mutex_unlock (&perl_threads->mutex);
1212 } /* static void c_ithread_destructor (void *) */
1214 /* must be called with perl_threads->mutex locked */
1215 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1217 c_ithread_t *t = NULL;
1220 assert (NULL != perl_threads);
1222 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1223 memset (t, 0, sizeof (c_ithread_t));
1225 t->interp = (NULL == base)
1227 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1231 if ((NULL != base) && (NULL != PL_endav)) {
1232 av_clear (PL_endav);
1233 av_undef (PL_endav);
1238 ++perl_threads->number_of_threads;
1239 #endif /* COLLECT_DEBUG */
1243 if (NULL == perl_threads->tail) {
1244 perl_threads->head = t;
1248 perl_threads->tail->next = t;
1249 t->prev = perl_threads->tail;
1252 perl_threads->tail = t;
1254 pthread_setspecific (perl_thr_key, (const void *)t);
1256 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1259 * Filter chains implementation.
1262 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1269 notification_meta_t **meta = NULL;
1274 if ((type < 0) || (type >= FC_TYPES))
1277 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1280 va_start (ap, data);
1287 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1288 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1289 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1291 if (FC_CB_CREATE == cb_type) {
1294 * $_[1] = $user_data;
1297 HV *config = newHV ();
1299 ci = va_arg (ap, oconfig_item_t *);
1301 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1304 config = (HV *)&PL_sv_undef;
1308 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1310 else if (FC_CB_DESTROY == cb_type) {
1312 * $_[1] = $user_data;
1315 /* nothing to be done - the user data pointer
1316 * is pushed onto the stack later */
1318 else if (FC_CB_EXEC == cb_type) {
1323 * $_[3] = $user_data;
1331 ds = va_arg (ap, data_set_t *);
1332 vl = va_arg (ap, value_list_t *);
1333 meta = va_arg (ap, notification_meta_t **);
1335 if (0 != data_set2av (aTHX_ ds, pds)) {
1338 pds = (AV *)&PL_sv_undef;
1342 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1345 pvl = (HV *)&PL_sv_undef;
1352 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1355 pmeta = (AV *)&PL_sv_undef;
1360 pmeta = (AV *)&PL_sv_undef;
1363 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1364 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1365 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1368 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1372 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1374 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1375 assert (pmeta != NULL);
1377 plugin_notification_meta_free (*meta);
1378 av2notification_meta (aTHX_ pmeta, meta);
1385 /* the exec callbacks return a status, while
1386 * the others return a boolean value */
1387 if (FC_CB_EXEC == cb_type)
1389 else if (! SvTRUE (tmp))
1399 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1401 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1403 pfc_user_data_t *data;
1409 if (NULL == perl_threads)
1413 c_ithread_t *t = NULL;
1415 pthread_mutex_lock (&perl_threads->mutex);
1416 t = c_ithread_create (perl_threads->head->interp);
1417 pthread_mutex_unlock (&perl_threads->mutex);
1422 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1423 aTHX, perl_threads->number_of_threads);
1425 if ((1 != ci->values_num)
1426 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1427 log_warn ("A \"%s\" block expects a single string argument.",
1428 (FC_MATCH == type) ? "Match" : "Target");
1432 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1433 data->name = sstrdup (ci->values[0].value.string);
1434 data->user_data = newSV (0);
1436 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1439 PFC_USER_DATA_FREE (data);
1443 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1445 static int fc_destroy (int type, void **user_data)
1447 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1453 if ((NULL == perl_threads) || (NULL == data))
1457 c_ithread_t *t = NULL;
1459 pthread_mutex_lock (&perl_threads->mutex);
1460 t = c_ithread_create (perl_threads->head->interp);
1461 pthread_mutex_unlock (&perl_threads->mutex);
1466 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1467 aTHX, perl_threads->number_of_threads);
1469 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1471 PFC_USER_DATA_FREE (data);
1474 } /* static int fc_destroy (int, void **) */
1476 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1477 notification_meta_t **meta, void **user_data)
1479 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1483 if (NULL == perl_threads)
1486 assert (NULL != data);
1489 c_ithread_t *t = NULL;
1491 pthread_mutex_lock (&perl_threads->mutex);
1492 t = c_ithread_create (perl_threads->head->interp);
1493 pthread_mutex_unlock (&perl_threads->mutex);
1498 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1499 aTHX, perl_threads->number_of_threads);
1501 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1502 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1503 notification_meta_t **, void **) */
1505 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1507 return fc_create (FC_MATCH, ci, user_data);
1508 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1510 static int pmatch_destroy (void **user_data)
1512 return fc_destroy (FC_MATCH, user_data);
1513 } /* static int pmatch_destroy (void **) */
1515 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1516 notification_meta_t **meta, void **user_data)
1518 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1519 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1520 notification_meta_t **, void **) */
1522 static match_proc_t pmatch = {
1523 pmatch_create, pmatch_destroy, pmatch_match
1526 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1528 return fc_create (FC_TARGET, ci, user_data);
1529 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1531 static int ptarget_destroy (void **user_data)
1533 return fc_destroy (FC_TARGET, user_data);
1534 } /* static int ptarget_destroy (void **) */
1536 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1537 notification_meta_t **meta, void **user_data)
1539 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1540 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1541 notification_meta_t **, void **) */
1543 static target_proc_t ptarget = {
1544 ptarget_create, ptarget_destroy, ptarget_invoke
1548 * Exported Perl API.
1552 * Collectd::plugin_register_data_set (type, dataset).
1555 * type of the dataset
1558 * dataset to be registered
1560 static XS (Collectd_plugin_register_ds)
1567 log_warn ("Using plugin_register() to register new data-sets is "
1568 "deprecated - add new entries to a custom types.db instead.");
1571 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1575 log_debug ("Collectd::plugin_register_data_set: "
1576 "type = \"%s\", dataset = \"%s\"",
1577 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1581 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1582 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1586 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1594 } /* static XS (Collectd_plugin_register_ds) */
1597 * Collectd::plugin_unregister_data_set (type).
1600 * type of the dataset
1602 static XS (Collectd_plugin_unregister_ds)
1607 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1611 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1612 SvPV_nolen (ST (0)));
1614 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1618 } /* static XS (Collectd_plugin_register_ds) */
1621 * Collectd::plugin_dispatch_values (name, values).
1624 * name of the plugin
1627 * value list to submit
1629 static XS (Collectd_plugin_dispatch_values)
1638 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1642 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1643 SvPV_nolen (ST (/* stack index = */ 0)));
1645 values = ST (/* stack index = */ 0);
1647 /* Make sure the argument is a hash reference. */
1648 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1649 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1656 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1662 } /* static XS (Collectd_plugin_dispatch_values) */
1665 * Collectd::plugin_get_interval ().
1667 static XS (Collectd_plugin_get_interval)
1671 /* make sure we don't get any unused variable warnings for 'items';
1672 * don't abort, though */
1674 log_err ("Usage: Collectd::plugin_get_interval()");
1676 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1677 } /* static XS (Collectd_plugin_get_interval) */
1679 /* Collectd::plugin_write (plugin, ds, vl).
1682 * name of the plugin to call, may be 'undef'
1685 * data-set that describes the submitted values, may be 'undef'
1688 * value-list to be written
1690 static XS (Collectd__plugin_write)
1701 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1705 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1706 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1707 SvPV_nolen (ST (2)));
1709 if (! SvOK (ST (0)))
1712 plugin = SvPV_nolen (ST (0));
1715 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1716 ds_array = (AV *)SvRV (ds);
1717 else if (! SvOK (ds))
1720 log_err ("Collectd::plugin_write: Invalid data-set.");
1725 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1726 log_err ("Collectd::plugin_write: Invalid value-list.");
1730 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1736 } /* static XS (Collectd__plugin_write) */
1739 * Collectd::_plugin_flush (plugin, timeout, identifier).
1742 * name of the plugin to flush
1745 * timeout to use when flushing the data
1748 * data-set identifier to flush
1750 static XS (Collectd__plugin_flush)
1752 char *plugin = NULL;
1759 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1764 plugin = SvPV_nolen (ST (0));
1767 timeout = (int)SvIV (ST (1));
1770 id = SvPV_nolen (ST (2));
1772 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1773 "id = \"%s\"", plugin, timeout, id);
1775 if (0 == plugin_flush (plugin, timeout, id))
1779 } /* static XS (Collectd__plugin_flush) */
1782 * Collectd::plugin_dispatch_notification (notif).
1785 * notification to dispatch
1787 static XS (Collectd_plugin_dispatch_notification)
1796 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1800 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1801 SvPV_nolen (ST (0)));
1805 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1806 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1810 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1816 } /* static XS (Collectd_plugin_dispatch_notification) */
1819 * Collectd::plugin_log (level, message).
1822 * log level (LOG_DEBUG, ... LOG_ERR)
1827 static XS (Collectd_plugin_log)
1832 log_err ("Usage: Collectd::plugin_log(level, message)");
1836 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1838 } /* static XS (Collectd_plugin_log) */
1841 * Collectd::_fc_register (type, name)
1849 static XS (Collectd__fc_register)
1859 log_err ("Usage: Collectd::_fc_register(type, name)");
1863 type = SvIV (ST (0));
1864 name = SvPV_nolen (ST (1));
1866 if (FC_MATCH == type)
1867 ret = fc_register_match (name, pmatch);
1868 else if (FC_TARGET == type)
1869 ret = fc_register_target (name, ptarget);
1875 } /* static XS (Collectd_fc_register) */
1878 * Collectd::call_by_name (...).
1880 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1882 static XS (Collectd_call_by_name)
1887 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1888 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1893 name = SvPV_nolen (tmp);
1895 if (NULL == get_cv (name, 0)) {
1896 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1901 /* simply pass on the subroutine call without touching the stack,
1902 * thus leaving any arguments and return values in place */
1904 } /* static XS (Collectd_call_by_name) */
1907 * Interface to collectd.
1910 static int perl_init (void)
1914 if (NULL == perl_threads)
1918 c_ithread_t *t = NULL;
1920 pthread_mutex_lock (&perl_threads->mutex);
1921 t = c_ithread_create (perl_threads->head->interp);
1922 pthread_mutex_unlock (&perl_threads->mutex);
1927 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1928 aTHX, perl_threads->number_of_threads);
1929 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1930 } /* static int perl_init (void) */
1932 static int perl_read (void)
1936 if (NULL == perl_threads)
1940 c_ithread_t *t = NULL;
1942 pthread_mutex_lock (&perl_threads->mutex);
1943 t = c_ithread_create (perl_threads->head->interp);
1944 pthread_mutex_unlock (&perl_threads->mutex);
1949 /* Assert that we're not running as the base thread. Otherwise, we might
1950 * run into concurrency issues with c_ithread_create(). See
1951 * https://github.com/collectd/collectd/issues/9 for details. */
1952 assert (aTHX != perl_threads->head->interp);
1954 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1955 aTHX, perl_threads->number_of_threads);
1956 return pplugin_call_all (aTHX_ PLUGIN_READ);
1957 } /* static int perl_read (void) */
1959 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1960 user_data_t __attribute__((unused)) *user_data)
1965 if (NULL == perl_threads)
1969 c_ithread_t *t = NULL;
1971 pthread_mutex_lock (&perl_threads->mutex);
1972 t = c_ithread_create (perl_threads->head->interp);
1973 pthread_mutex_unlock (&perl_threads->mutex);
1978 /* Lock the base thread if this is not called from one of the read threads
1979 * to avoid race conditions with c_ithread_create(). See
1980 * https://github.com/collectd/collectd/issues/9 for details. */
1981 if (aTHX == perl_threads->head->interp)
1982 pthread_mutex_lock (&perl_threads->mutex);
1984 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1985 aTHX, perl_threads->number_of_threads);
1986 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1988 if (aTHX == perl_threads->head->interp)
1989 pthread_mutex_unlock (&perl_threads->mutex);
1992 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1994 static void perl_log (int level, const char *msg,
1995 user_data_t __attribute__((unused)) *user_data)
1999 if (NULL == perl_threads)
2003 c_ithread_t *t = NULL;
2005 pthread_mutex_lock (&perl_threads->mutex);
2006 t = c_ithread_create (perl_threads->head->interp);
2007 pthread_mutex_unlock (&perl_threads->mutex);
2012 /* Lock the base thread if this is not called from one of the read threads
2013 * to avoid race conditions with c_ithread_create(). See
2014 * https://github.com/collectd/collectd/issues/9 for details. */
2015 if (aTHX == perl_threads->head->interp)
2016 pthread_mutex_lock (&perl_threads->mutex);
2018 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2020 if (aTHX == perl_threads->head->interp)
2021 pthread_mutex_unlock (&perl_threads->mutex);
2024 } /* static void perl_log (int, const char *) */
2026 static int perl_notify (const notification_t *notif,
2027 user_data_t __attribute__((unused)) *user_data)
2031 if (NULL == perl_threads)
2035 c_ithread_t *t = NULL;
2037 pthread_mutex_lock (&perl_threads->mutex);
2038 t = c_ithread_create (perl_threads->head->interp);
2039 pthread_mutex_unlock (&perl_threads->mutex);
2043 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2044 } /* static int perl_notify (const notification_t *) */
2046 static int perl_flush (cdtime_t timeout, const char *identifier,
2047 user_data_t __attribute__((unused)) *user_data)
2051 if (NULL == perl_threads)
2055 c_ithread_t *t = NULL;
2057 pthread_mutex_lock (&perl_threads->mutex);
2058 t = c_ithread_create (perl_threads->head->interp);
2059 pthread_mutex_unlock (&perl_threads->mutex);
2063 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2064 } /* static int perl_flush (const int) */
2066 static int perl_shutdown (void)
2068 c_ithread_t *t = NULL;
2074 plugin_unregister_complex_config ("perl");
2076 if (NULL == perl_threads)
2080 c_ithread_t *t = NULL;
2082 pthread_mutex_lock (&perl_threads->mutex);
2083 t = c_ithread_create (perl_threads->head->interp);
2084 pthread_mutex_unlock (&perl_threads->mutex);
2089 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2090 aTHX, perl_threads->number_of_threads);
2092 plugin_unregister_log ("perl");
2093 plugin_unregister_notification ("perl");
2094 plugin_unregister_init ("perl");
2095 plugin_unregister_read ("perl");
2096 plugin_unregister_write ("perl");
2097 plugin_unregister_flush ("perl");
2099 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2101 pthread_mutex_lock (&perl_threads->mutex);
2102 t = perl_threads->tail;
2105 c_ithread_t *thr = t;
2107 /* the pointer has to be advanced before destroying
2108 * the thread as this will free the memory */
2111 c_ithread_destroy (thr);
2114 pthread_mutex_unlock (&perl_threads->mutex);
2115 pthread_mutex_destroy (&perl_threads->mutex);
2117 sfree (perl_threads);
2119 pthread_key_delete (perl_thr_key);
2123 plugin_unregister_shutdown ("perl");
2125 } /* static void perl_shutdown (void) */
2128 * Access functions for global variables.
2130 * These functions implement the "magic" used to access
2131 * the global variables from Perl.
2134 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2136 char *pv = mg->mg_ptr;
2139 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2141 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2143 char *pv = mg->mg_ptr;
2144 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2146 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2148 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2150 log_warn ("Accessing $interval_g is deprecated (and might not "
2151 "give the desired results) - plugin_get_interval() should "
2152 "be used instead.");
2153 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2155 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2157 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2159 double nv = (double)SvNV (var);
2160 log_warn ("Accessing $interval_g is deprecated (and might not "
2161 "give the desired results) - plugin_get_interval() should "
2162 "be used instead.");
2163 interval_g = DOUBLE_TO_CDTIME_T (nv);
2165 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2167 static MGVTBL g_pv_vtbl = {
2168 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2169 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2173 static MGVTBL g_interval_vtbl = {
2174 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2175 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2180 /* bootstrap the Collectd module */
2181 static void xs_init (pTHX)
2185 char *file = __FILE__;
2191 /* enable usage of Perl modules using shared libraries */
2192 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2195 for (i = 0; NULL != api[i].f; ++i)
2196 newXS (api[i].name, api[i].f, file);
2198 stash = gv_stashpv ("Collectd", 1);
2200 /* export "constants" */
2201 for (i = 0; '\0' != constants[i].name[0]; ++i)
2202 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2204 /* export global variables
2205 * by adding "magic" to the SV's representing the globale variables
2206 * perl is able to automagically call the get/set function when
2207 * accessing any such variable (this is basically the same as using
2209 /* global strings */
2210 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2211 tmp = get_sv (g_strings[i].name, 1);
2212 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2213 g_strings[i].var, 0);
2216 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2217 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2218 /* vtbl = */ &g_interval_vtbl,
2219 /* name = */ NULL, /* namelen = */ 0);
2222 } /* static void xs_init (pTHX) */
2224 /* Initialize the global Perl interpreter. */
2225 static int init_pi (int argc, char **argv)
2229 if (NULL != perl_threads)
2232 log_info ("Initializing Perl interpreter...");
2237 for (i = 0; i < argc; ++i)
2238 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2240 #endif /* COLLECT_DEBUG */
2242 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2243 log_err ("init_pi: pthread_key_create failed");
2245 /* this must not happen - cowardly giving up if it does */
2250 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2251 * triggers a "value computed is not used" warning by gcc. */
2254 PERL_SYS_INIT3 (&argc, &argv, &environ);
2256 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2257 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2259 pthread_mutex_init (&perl_threads->mutex, NULL);
2260 /* locking the mutex should not be necessary at this point
2261 * but let's just do it for the sake of completeness */
2262 pthread_mutex_lock (&perl_threads->mutex);
2264 perl_threads->head = c_ithread_create (NULL);
2265 perl_threads->tail = perl_threads->head;
2267 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2268 log_err ("init_pi: Not enough memory.");
2272 aTHX = perl_threads->head->interp;
2273 pthread_mutex_unlock (&perl_threads->mutex);
2275 perl_construct (aTHX);
2277 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2279 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2280 SV *err = get_sv ("@", 1);
2281 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2284 perl_destruct (perl_threads->head->interp);
2285 perl_free (perl_threads->head->interp);
2286 sfree (perl_threads);
2288 pthread_key_delete (perl_thr_key);
2292 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2293 sv_setpv (get_sv ("0", 0), "collectd");
2297 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2298 plugin_register_notification ("perl", perl_notify,
2299 /* user_data = */ NULL);
2300 plugin_register_init ("perl", perl_init);
2302 plugin_register_read ("perl", perl_read);
2304 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2305 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2306 plugin_register_shutdown ("perl", perl_shutdown);
2308 } /* static int init_pi (const char **, const int) */
2311 * LoadPlugin "<Plugin>"
2313 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2315 char module_name[DATA_MAX_NAME_LEN];
2319 if ((0 != ci->children_num) || (1 != ci->values_num)
2320 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2321 log_err ("LoadPlugin expects a single string argument.");
2325 value = ci->values[0].value.string;
2327 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2328 log_err ("Invalid module name %s", value);
2332 if (0 != init_pi (perl_argc, perl_argv))
2335 assert (NULL != perl_threads);
2336 assert (NULL != perl_threads->head);
2338 aTHX = perl_threads->head->interp;
2340 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2341 load_module (PERL_LOADMOD_NOIMPORT,
2342 newSVpv (module_name, strlen (module_name)), Nullsv);
2344 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2349 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2353 if ((0 != ci->children_num) || (1 != ci->values_num)
2354 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2355 log_err ("BaseName expects a single string argument.");
2359 value = ci->values[0].value.string;
2361 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2362 sstrncpy (base_name, value, sizeof (base_name));
2364 } /* static int perl_config_basename (oconfig_item_it *) */
2367 * EnableDebugger "<Package>"|""
2369 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2373 if ((0 != ci->children_num) || (1 != ci->values_num)
2374 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2375 log_err ("EnableDebugger expects a single string argument.");
2379 if (NULL != perl_threads) {
2380 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2384 value = ci->values[0].value.string;
2386 perl_argv = (char **)realloc (perl_argv,
2387 (++perl_argc + 1) * sizeof (char *));
2389 if (NULL == perl_argv) {
2390 log_err ("perl_config: Not enough memory.");
2394 if ('\0' == value[0]) {
2395 perl_argv[perl_argc - 1] = "-d";
2398 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2399 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2400 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2403 perl_argv[perl_argc] = NULL;
2405 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2408 * IncludeDir "<Dir>"
2410 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2414 if ((0 != ci->children_num) || (1 != ci->values_num)
2415 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2416 log_err ("IncludeDir expects a single string argument.");
2420 value = ci->values[0].value.string;
2423 perl_argv = (char **)realloc (perl_argv,
2424 (++perl_argc + 1) * sizeof (char *));
2426 if (NULL == perl_argv) {
2427 log_err ("perl_config: Not enough memory.");
2431 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2432 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2433 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2435 perl_argv[perl_argc] = NULL;
2438 /* prepend the directory to @INC */
2439 av_unshift (GvAVn (PL_incgv), 1);
2440 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2443 } /* static int perl_config_includedir (oconfig_item_it *) */
2448 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2458 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2459 log_err ("LoadPlugin expects a single string argument.");
2463 plugin = ci->values[0].value.string;
2466 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2470 log_err ("Unable to convert configuration to a Perl hash value.");
2471 config = (HV *)&PL_sv_undef;
2479 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2480 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2484 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2499 } /* static int perl_config_plugin (oconfig_item_it *) */
2501 static int perl_config (oconfig_item_t *ci)
2508 for (i = 0; i < ci->children_num; ++i) {
2509 oconfig_item_t *c = ci->children + i;
2510 int current_status = 0;
2512 if (NULL != perl_threads)
2513 aTHX = PERL_GET_CONTEXT;
2515 if (0 == strcasecmp (c->key, "LoadPlugin"))
2516 current_status = perl_config_loadplugin (aTHX_ c);
2517 else if (0 == strcasecmp (c->key, "BaseName"))
2518 current_status = perl_config_basename (aTHX_ c);
2519 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2520 current_status = perl_config_enabledebugger (aTHX_ c);
2521 else if (0 == strcasecmp (c->key, "IncludeDir"))
2522 current_status = perl_config_includedir (aTHX_ c);
2523 else if (0 == strcasecmp (c->key, "Plugin"))
2524 current_status = perl_config_plugin (aTHX_ c);
2527 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2531 /* fatal error - it's up to perl_config_* to clean up */
2532 if (0 > current_status) {
2533 log_err ("Configuration failed with a fatal error - "
2534 "plugin disabled!");
2535 return current_status;
2538 status += current_status;
2541 } /* static int perl_config (oconfig_item_t *) */
2543 void module_register (void)
2546 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2548 /* default options for the Perl interpreter */
2550 perl_argv[1] = "-MCollectd";
2551 perl_argv[2] = "-e";
2553 perl_argv[4] = NULL;
2555 plugin_register_complex_config ("perl", perl_config);
2557 } /* void module_register (void) */
2559 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */