2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 Sebastian Harl
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Sebastian Harl <sh at tokkee.org>
28 * This plugin embeds a Perl interpreter into collectd and provides an
29 * interface for collectd plugins written in perl.
32 /* do not automatically get the thread specific perl interpreter */
33 #define PERL_NO_GET_CONTEXT
35 #define DONT_POISON_SPRINTF_YET 1
37 #undef DONT_POISON_SPRINTF_YET
39 #include "configfile.h"
48 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
50 # pragma GCC poison sprintf
55 /* Some versions of Perl define their own version of DEBUG... :-/ */
60 /* ... while we want the definition found in plugin.h. */
64 #include "filter_chain.h"
68 #if !defined(USE_ITHREADS)
69 # error "Perl does not support ithreads!"
70 #endif /* !defined(USE_ITHREADS) */
72 /* clear the Perl sub's stack frame
73 * (this should only be used inside an XSUB) */
74 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
78 #define PLUGIN_WRITE 2
79 #define PLUGIN_SHUTDOWN 3
81 #define PLUGIN_NOTIF 5
82 #define PLUGIN_FLUSH 6
84 #define PLUGIN_TYPES 7
86 #define PLUGIN_CONFIG 254
87 #define PLUGIN_DATASET 255
94 #define FC_CB_CREATE 0
95 #define FC_CB_DESTROY 1
100 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
101 #define log_info(...) INFO ("perl: " __VA_ARGS__)
102 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
103 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
105 /* this is defined in DynaLoader.a */
106 void boot_DynaLoader (PerlInterpreter *, CV *);
108 static XS (Collectd_plugin_register_ds);
109 static XS (Collectd_plugin_unregister_ds);
110 static XS (Collectd_plugin_dispatch_values);
111 static XS (Collectd_plugin_get_interval);
112 static XS (Collectd__plugin_write);
113 static XS (Collectd__plugin_flush);
114 static XS (Collectd_plugin_dispatch_notification);
115 static XS (Collectd_plugin_log);
116 static XS (Collectd__fc_register);
117 static XS (Collectd_call_by_name);
123 typedef struct c_ithread_s {
124 /* the thread's Perl interpreter */
125 PerlInterpreter *interp;
127 /* double linked list of threads */
128 struct c_ithread_s *prev;
129 struct c_ithread_s *next;
137 /* some usage stats */
138 int number_of_threads;
139 #endif /* COLLECT_DEBUG */
141 pthread_mutex_t mutex;
144 /* name / user_data for Perl matches / targets */
150 #define PFC_USER_DATA_FREE(data) \
152 sfree ((data)->name); \
153 if (NULL != (data)->user_data) \
154 sv_free ((data)->user_data); \
161 extern char **environ;
167 /* if perl_threads != NULL perl_threads->head must
168 * point to the "base" thread */
169 static c_ithread_list_t *perl_threads = NULL;
171 /* the key used to store each pthread's ithread */
172 static pthread_key_t perl_thr_key;
174 static int perl_argc = 0;
175 static char **perl_argv = NULL;
177 static char base_name[DATA_MAX_NAME_LEN] = "";
184 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
185 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
186 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
187 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
188 { "Collectd::_plugin_write", Collectd__plugin_write },
189 { "Collectd::_plugin_flush", Collectd__plugin_flush },
190 { "Collectd::plugin_dispatch_notification",
191 Collectd_plugin_dispatch_notification },
192 { "Collectd::plugin_log", Collectd_plugin_log },
193 { "Collectd::_fc_register", Collectd__fc_register },
194 { "Collectd::call_by_name", Collectd_call_by_name },
203 { "Collectd::TYPE_INIT", PLUGIN_INIT },
204 { "Collectd::TYPE_READ", PLUGIN_READ },
205 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
206 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
207 { "Collectd::TYPE_LOG", PLUGIN_LOG },
208 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
209 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
210 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
211 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
212 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
213 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
214 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
215 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
216 { "Collectd::LOG_ERR", LOG_ERR },
217 { "Collectd::LOG_WARNING", LOG_WARNING },
218 { "Collectd::LOG_NOTICE", LOG_NOTICE },
219 { "Collectd::LOG_INFO", LOG_INFO },
220 { "Collectd::LOG_DEBUG", LOG_DEBUG },
221 { "Collectd::FC_MATCH", FC_MATCH },
222 { "Collectd::FC_TARGET", FC_TARGET },
223 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
224 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
225 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
226 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
227 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
228 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
229 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
230 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
231 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
232 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
233 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
242 { "Collectd::hostname_g", hostname_g },
247 * Helper functions for data type conversion.
262 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
266 if ((NULL == hash) || (NULL == ds))
269 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
270 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
273 log_err ("hv2data_source: No DS name given.");
277 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
278 ds->type = SvIV (*tmp);
280 if ((DS_TYPE_COUNTER != ds->type)
281 && (DS_TYPE_GAUGE != ds->type)
282 && (DS_TYPE_DERIVE != ds->type)
283 && (DS_TYPE_ABSOLUTE != ds->type)) {
284 log_err ("hv2data_source: Invalid DS type.");
289 ds->type = DS_TYPE_COUNTER;
292 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
293 ds->min = SvNV (*tmp);
297 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
298 ds->max = SvNV (*tmp);
302 } /* static int hv2data_source (HV *, data_source_t *) */
304 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
306 const data_set_t *ds;
310 if ((NULL == name) || (NULL == array) || (NULL == value))
313 if (av_len (array) < len - 1)
314 len = av_len (array) + 1;
319 ds = plugin_get_ds (name);
321 log_err ("av2value: Unknown dataset \"%s\"", name);
325 if (ds->ds_num < len) {
326 log_warn ("av2value: Value length exceeds data set length.");
330 for (i = 0; i < len; ++i) {
331 SV **tmp = av_fetch (array, i, 0);
334 if (DS_TYPE_COUNTER == ds->ds[i].type)
335 value[i].counter = SvIV (*tmp);
336 else if (DS_TYPE_GAUGE == ds->ds[i].type)
337 value[i].gauge = SvNV (*tmp);
338 else if (DS_TYPE_DERIVE == ds->ds[i].type)
339 value[i].derive = SvIV (*tmp);
340 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
341 value[i].absolute = SvIV (*tmp);
348 } /* static int av2value (char *, AV *, value_t *, int) */
353 * values => [ @values ],
357 * plugin_instance => $pinstance,
358 * type_instance => $tinstance,
361 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
365 if ((NULL == hash) || (NULL == vl))
368 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
369 log_err ("hv2value_list: No type given.");
373 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
375 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
376 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
377 log_err ("hv2value_list: No valid values given.");
382 AV *array = (AV *)SvRV (*tmp);
383 int len = av_len (array) + 1;
388 vl->values = (value_t *)smalloc (len * sizeof (value_t));
389 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
392 if (-1 == vl->values_len) {
398 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
400 double t = SvNV (*tmp);
401 vl->time = DOUBLE_TO_CDTIME_T (t);
404 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
406 double t = SvNV (*tmp);
407 vl->interval = DOUBLE_TO_CDTIME_T (t);
410 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
411 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
413 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
415 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
416 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
418 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
419 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
420 sizeof (vl->plugin_instance));
422 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
423 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
424 sizeof (vl->type_instance));
426 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
428 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
432 if ((NULL == array) || (NULL == name) || (NULL == ds))
435 len = av_len (array);
438 log_err ("av2data_set: Invalid data set.");
442 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
443 ds->ds_num = len + 1;
445 for (i = 0; i <= len; ++i) {
446 SV **elem = av_fetch (array, i, 0);
449 log_err ("av2data_set: Failed to fetch data source %i.", i);
453 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
454 log_err ("av2data_set: Invalid data source.");
458 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
461 log_debug ("av2data_set: "
462 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
463 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
466 sstrncpy (ds->type, name, sizeof (ds->type));
468 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
473 * severity => $severity,
479 * plugin_instance => $instance,
480 * type_instance => $type_instance,
481 * meta => [ { name => <name>, value => <value> }, ... ]
484 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
486 notification_meta_t **m = meta;
488 int len = av_len (array);
491 for (i = 0; i <= len; ++i) {
492 SV **tmp = av_fetch (array, i, 0);
498 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
499 log_warn ("av2notification_meta: Skipping invalid "
500 "meta information.");
504 hash = (HV *)SvRV (*tmp);
506 *m = (notification_meta_t *)smalloc (sizeof (**m));
508 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
509 log_warn ("av2notification_meta: Skipping invalid "
510 "meta information.");
514 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
516 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
517 log_warn ("av2notification_meta: Skipping invalid "
518 "meta information.");
525 (*m)->nm_value.nm_double = SvNVX (*tmp);
526 (*m)->type = NM_TYPE_DOUBLE;
528 else if (SvUOK (*tmp)) {
529 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
530 (*m)->type = NM_TYPE_UNSIGNED_INT;
532 else if (SvIOK (*tmp)) {
533 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
534 (*m)->type = NM_TYPE_SIGNED_INT;
537 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
538 (*m)->type = NM_TYPE_STRING;
545 } /* static int av2notification_meta (AV *, notification_meta_t *) */
547 static int hv2notification (pTHX_ HV *hash, notification_t *n)
551 if ((NULL == hash) || (NULL == n))
554 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
555 n->severity = SvIV (*tmp);
557 n->severity = NOTIF_FAILURE;
559 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
561 double t = SvNV (*tmp);
562 n->time = DOUBLE_TO_CDTIME_T (t);
567 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
568 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
570 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
571 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
573 sstrncpy (n->host, hostname_g, sizeof (n->host));
575 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
576 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
578 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
579 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
580 sizeof (n->plugin_instance));
582 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
583 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
585 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
586 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
587 sizeof (n->type_instance));
590 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
591 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
592 log_warn ("hv2notification: Ignoring invalid meta information.");
596 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
597 plugin_notification_meta_free (n->meta);
604 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
606 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
610 if ((NULL == ds) || (NULL == array))
613 av_extend (array, ds->ds_num);
615 for (i = 0; i < ds->ds_num; ++i) {
616 HV *source = newHV ();
618 if (NULL == hv_store (source, "name", 4,
619 newSVpv (ds->ds[i].name, 0), 0))
622 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
625 if (! isnan (ds->ds[i].min))
626 if (NULL == hv_store (source, "min", 3,
627 newSVnv (ds->ds[i].min), 0))
630 if (! isnan (ds->ds[i].max))
631 if (NULL == hv_store (source, "max", 3,
632 newSVnv (ds->ds[i].max), 0))
635 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
639 } /* static int data_set2av (data_set_t *, AV *) */
641 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
648 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
651 len = vl->values_len;
653 if (ds->ds_num < len) {
654 log_warn ("value2av: Value length exceeds data set length.");
659 av_extend (values, len - 1);
661 for (i = 0; i < len; ++i) {
664 if (DS_TYPE_COUNTER == ds->ds[i].type)
665 val = newSViv (vl->values[i].counter);
666 else if (DS_TYPE_GAUGE == ds->ds[i].type)
667 val = newSVnv (vl->values[i].gauge);
668 else if (DS_TYPE_DERIVE == ds->ds[i].type)
669 val = newSViv (vl->values[i].derive);
670 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
671 val = newSViv (vl->values[i].absolute);
673 if (NULL == av_store (values, i, val)) {
679 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
684 double t = CDTIME_T_TO_DOUBLE (vl->time);
685 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
690 double t = CDTIME_T_TO_DOUBLE (vl->interval);
691 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
695 if ('\0' != vl->host[0])
696 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
699 if ('\0' != vl->plugin[0])
700 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
703 if ('\0' != vl->plugin_instance[0])
704 if (NULL == hv_store (hash, "plugin_instance", 15,
705 newSVpv (vl->plugin_instance, 0), 0))
708 if ('\0' != vl->type[0])
709 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
712 if ('\0' != vl->type_instance[0])
713 if (NULL == hv_store (hash, "type_instance", 13,
714 newSVpv (vl->type_instance, 0), 0))
717 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
719 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
729 av_extend (array, meta_num);
731 for (i = 0; NULL != meta; meta = meta->next, ++i) {
735 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
738 if (NM_TYPE_STRING == meta->type)
739 value = newSVpv (meta->nm_value.nm_string, 0);
740 else if (NM_TYPE_SIGNED_INT == meta->type)
741 value = newSViv (meta->nm_value.nm_signed_int);
742 else if (NM_TYPE_UNSIGNED_INT == meta->type)
743 value = newSVuv (meta->nm_value.nm_unsigned_int);
744 else if (NM_TYPE_DOUBLE == meta->type)
745 value = newSVnv (meta->nm_value.nm_double);
746 else if (NM_TYPE_BOOLEAN == meta->type)
747 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
751 if (NULL == hv_store (m, "value", 5, value, 0)) {
756 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
763 } /* static int notification_meta2av (notification_meta_t *, AV *) */
765 static int notification2hv (pTHX_ notification_t *n, HV *hash)
767 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
772 double t = CDTIME_T_TO_DOUBLE (n->time);
773 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
777 if ('\0' != *n->message)
778 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
781 if ('\0' != *n->host)
782 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
785 if ('\0' != *n->plugin)
786 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
789 if ('\0' != *n->plugin_instance)
790 if (NULL == hv_store (hash, "plugin_instance", 15,
791 newSVpv (n->plugin_instance, 0), 0))
794 if ('\0' != *n->type)
795 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
798 if ('\0' != *n->type_instance)
799 if (NULL == hv_store (hash, "type_instance", 13,
800 newSVpv (n->type_instance, 0), 0))
803 if (NULL != n->meta) {
805 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
806 || (NULL == hv_store (hash, "meta", 4,
807 newRV_noinc ((SV *)meta), 0))) {
814 } /* static int notification2hv (notification_t *, HV *) */
816 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
823 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
827 if (0 < ci->values_num)
828 av_extend (values, ci->values_num);
830 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
836 for (i = 0; i < ci->values_num; ++i) {
839 switch (ci->values[i].type) {
840 case OCONFIG_TYPE_STRING:
841 value = newSVpv (ci->values[i].value.string, 0);
843 case OCONFIG_TYPE_NUMBER:
844 value = newSVnv ((NV)ci->values[i].value.number);
846 case OCONFIG_TYPE_BOOLEAN:
847 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
850 log_err ("oconfig_item2hv: Invalid value type %i.",
852 value = &PL_sv_undef;
855 if (NULL == av_store (values, i, value)) {
861 /* ignoring 'parent' member which is uninteresting in this case */
864 if (0 < ci->children_num)
865 av_extend (children, ci->children_num);
867 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
873 for (i = 0; i < ci->children_num; ++i) {
874 HV *child = newHV ();
876 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
882 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
889 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
892 * Internal functions.
895 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
897 if (base_name[0] == '\0')
898 status = ssnprintf (buf, buf_len, "%s", module);
900 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
901 if ((status < 0) || ((unsigned int)status >= buf_len))
904 } /* char *get_module_name */
907 * Add a plugin's data set definition.
909 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
915 if ((NULL == name) || (NULL == dataset))
918 if (0 != av2data_set (aTHX_ dataset, name, &ds))
921 ret = plugin_register_data_set (&ds);
925 } /* static int pplugin_register_data_set (char *, SV *) */
928 * Remove a plugin's data set definition.
930 static int pplugin_unregister_data_set (char *name)
934 return plugin_unregister_data_set (name);
935 } /* static int pplugin_unregister_data_set (char *) */
938 * Submit the values to the write functions.
940 static int pplugin_dispatch_values (pTHX_ HV *values)
942 value_list_t vl = VALUE_LIST_INIT;
949 if (0 != hv2value_list (aTHX_ values, &vl))
952 ret = plugin_dispatch_values (&vl);
956 } /* static int pplugin_dispatch_values (char *, HV *) */
959 * Submit the values to a single write function.
961 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
964 value_list_t vl = VALUE_LIST_INIT;
971 if (0 != hv2value_list (aTHX_ values, &vl))
974 if ((NULL != data_set)
975 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
978 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
980 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
981 NULL == plugin ? "<any>" : plugin, ret);
983 if (NULL != data_set)
987 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
990 * Dispatch a notification.
992 static int pplugin_dispatch_notification (pTHX_ HV *notif)
1001 memset (&n, 0, sizeof (n));
1003 if (0 != hv2notification (aTHX_ notif, &n))
1006 ret = plugin_dispatch_notification (&n);
1007 plugin_notification_meta_free (n.meta);
1009 } /* static int pplugin_dispatch_notification (HV *) */
1012 * Call all working functions of the given type.
1014 static int pplugin_call_all (pTHX_ int type, ...)
1023 if ((type < 0) || (type >= PLUGIN_TYPES))
1026 va_start (ap, type);
1033 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1035 if (PLUGIN_WRITE == type) {
1037 * $_[0] = $plugin_type;
1052 * values => [ $v1, ... ],
1054 * host => $hostname,
1055 * plugin => $plugin,
1057 * plugin_instance => $instance,
1058 * type_instance => $type_instance
1067 ds = va_arg (ap, data_set_t *);
1068 vl = va_arg (ap, value_list_t *);
1070 if (-1 == data_set2av (aTHX_ ds, pds)) {
1073 pds = (AV *)&PL_sv_undef;
1077 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1080 pvl = (HV *)&PL_sv_undef;
1084 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1085 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1086 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1088 else if (PLUGIN_LOG == type) {
1094 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1095 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1097 else if (PLUGIN_NOTIF == type) {
1101 * severity => $severity,
1105 * plugin => $plugin,
1107 * plugin_instance => $instance,
1108 * type_instance => $type_instance
1112 HV *notif = newHV ();
1114 n = va_arg (ap, notification_t *);
1116 if (-1 == notification2hv (aTHX_ n, notif)) {
1119 notif = (HV *)&PL_sv_undef;
1123 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1125 else if (PLUGIN_FLUSH == type) {
1130 * $_[1] = $identifier;
1132 timeout = va_arg (ap, cdtime_t);
1134 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1135 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1140 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1155 } /* static int pplugin_call_all (int, ...) */
1158 * collectd's perl interpreter based thread implementation.
1160 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1163 /* must be called with perl_threads->mutex locked */
1164 static void c_ithread_destroy (c_ithread_t *ithread)
1166 dTHXa (ithread->interp);
1168 assert (NULL != perl_threads);
1170 PERL_SET_CONTEXT (aTHX);
1171 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1176 --perl_threads->number_of_threads;
1177 #endif /* COLLECT_DEBUG */
1179 perl_destruct (aTHX);
1182 if (NULL == ithread->prev)
1183 perl_threads->head = ithread->next;
1185 ithread->prev->next = ithread->next;
1187 if (NULL == ithread->next)
1188 perl_threads->tail = ithread->prev;
1190 ithread->next->prev = ithread->prev;
1194 } /* static void c_ithread_destroy (c_ithread_t *) */
1196 static void c_ithread_destructor (void *arg)
1198 c_ithread_t *ithread = (c_ithread_t *)arg;
1199 c_ithread_t *t = NULL;
1201 if (NULL == perl_threads)
1204 pthread_mutex_lock (&perl_threads->mutex);
1206 for (t = perl_threads->head; NULL != t; t = t->next)
1210 /* the ithread no longer exists */
1214 c_ithread_destroy (ithread);
1216 pthread_mutex_unlock (&perl_threads->mutex);
1218 } /* static void c_ithread_destructor (void *) */
1220 /* must be called with perl_threads->mutex locked */
1221 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1223 c_ithread_t *t = NULL;
1226 assert (NULL != perl_threads);
1228 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1229 memset (t, 0, sizeof (c_ithread_t));
1231 t->interp = (NULL == base)
1233 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1237 if ((NULL != base) && (NULL != PL_endav)) {
1238 av_clear (PL_endav);
1239 av_undef (PL_endav);
1244 ++perl_threads->number_of_threads;
1245 #endif /* COLLECT_DEBUG */
1249 if (NULL == perl_threads->tail) {
1250 perl_threads->head = t;
1254 perl_threads->tail->next = t;
1255 t->prev = perl_threads->tail;
1258 perl_threads->tail = t;
1260 pthread_setspecific (perl_thr_key, (const void *)t);
1262 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1265 * Filter chains implementation.
1268 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1275 notification_meta_t **meta = NULL;
1280 if ((type < 0) || (type >= FC_TYPES))
1283 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1286 va_start (ap, data);
1293 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1294 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1295 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1297 if (FC_CB_CREATE == cb_type) {
1300 * $_[1] = $user_data;
1303 HV *config = newHV ();
1305 ci = va_arg (ap, oconfig_item_t *);
1307 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1310 config = (HV *)&PL_sv_undef;
1314 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1316 else if (FC_CB_DESTROY == cb_type) {
1318 * $_[1] = $user_data;
1321 /* nothing to be done - the user data pointer
1322 * is pushed onto the stack later */
1324 else if (FC_CB_EXEC == cb_type) {
1329 * $_[3] = $user_data;
1337 ds = va_arg (ap, data_set_t *);
1338 vl = va_arg (ap, value_list_t *);
1339 meta = va_arg (ap, notification_meta_t **);
1341 if (0 != data_set2av (aTHX_ ds, pds)) {
1344 pds = (AV *)&PL_sv_undef;
1348 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1351 pvl = (HV *)&PL_sv_undef;
1358 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1361 pmeta = (AV *)&PL_sv_undef;
1366 pmeta = (AV *)&PL_sv_undef;
1369 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1370 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1371 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1374 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1378 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1380 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1381 assert (pmeta != NULL);
1383 plugin_notification_meta_free (*meta);
1384 av2notification_meta (aTHX_ pmeta, meta);
1391 /* the exec callbacks return a status, while
1392 * the others return a boolean value */
1393 if (FC_CB_EXEC == cb_type)
1395 else if (! SvTRUE (tmp))
1405 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1407 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1409 pfc_user_data_t *data;
1415 if (NULL == perl_threads)
1419 c_ithread_t *t = NULL;
1421 pthread_mutex_lock (&perl_threads->mutex);
1422 t = c_ithread_create (perl_threads->head->interp);
1423 pthread_mutex_unlock (&perl_threads->mutex);
1428 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1429 aTHX, perl_threads->number_of_threads);
1431 if ((1 != ci->values_num)
1432 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1433 log_warn ("A \"%s\" block expects a single string argument.",
1434 (FC_MATCH == type) ? "Match" : "Target");
1438 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1439 data->name = sstrdup (ci->values[0].value.string);
1440 data->user_data = newSV (0);
1442 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1445 PFC_USER_DATA_FREE (data);
1449 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1451 static int fc_destroy (int type, void **user_data)
1453 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1459 if ((NULL == perl_threads) || (NULL == data))
1463 c_ithread_t *t = NULL;
1465 pthread_mutex_lock (&perl_threads->mutex);
1466 t = c_ithread_create (perl_threads->head->interp);
1467 pthread_mutex_unlock (&perl_threads->mutex);
1472 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1473 aTHX, perl_threads->number_of_threads);
1475 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1477 PFC_USER_DATA_FREE (data);
1480 } /* static int fc_destroy (int, void **) */
1482 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1483 notification_meta_t **meta, void **user_data)
1485 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1489 if (NULL == perl_threads)
1492 assert (NULL != data);
1495 c_ithread_t *t = NULL;
1497 pthread_mutex_lock (&perl_threads->mutex);
1498 t = c_ithread_create (perl_threads->head->interp);
1499 pthread_mutex_unlock (&perl_threads->mutex);
1504 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1505 aTHX, perl_threads->number_of_threads);
1507 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1508 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1509 notification_meta_t **, void **) */
1511 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1513 return fc_create (FC_MATCH, ci, user_data);
1514 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1516 static int pmatch_destroy (void **user_data)
1518 return fc_destroy (FC_MATCH, user_data);
1519 } /* static int pmatch_destroy (void **) */
1521 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1522 notification_meta_t **meta, void **user_data)
1524 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1525 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1526 notification_meta_t **, void **) */
1528 static match_proc_t pmatch = {
1529 pmatch_create, pmatch_destroy, pmatch_match
1532 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1534 return fc_create (FC_TARGET, ci, user_data);
1535 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1537 static int ptarget_destroy (void **user_data)
1539 return fc_destroy (FC_TARGET, user_data);
1540 } /* static int ptarget_destroy (void **) */
1542 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1543 notification_meta_t **meta, void **user_data)
1545 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1546 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1547 notification_meta_t **, void **) */
1549 static target_proc_t ptarget = {
1550 ptarget_create, ptarget_destroy, ptarget_invoke
1554 * Exported Perl API.
1558 * Collectd::plugin_register_data_set (type, dataset).
1561 * type of the dataset
1564 * dataset to be registered
1566 static XS (Collectd_plugin_register_ds)
1573 log_warn ("Using plugin_register() to register new data-sets is "
1574 "deprecated - add new entries to a custom types.db instead.");
1577 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1581 log_debug ("Collectd::plugin_register_data_set: "
1582 "type = \"%s\", dataset = \"%s\"",
1583 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1587 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1588 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1592 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1600 } /* static XS (Collectd_plugin_register_ds) */
1603 * Collectd::plugin_unregister_data_set (type).
1606 * type of the dataset
1608 static XS (Collectd_plugin_unregister_ds)
1613 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1617 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1618 SvPV_nolen (ST (0)));
1620 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1624 } /* static XS (Collectd_plugin_register_ds) */
1627 * Collectd::plugin_dispatch_values (name, values).
1630 * name of the plugin
1633 * value list to submit
1635 static XS (Collectd_plugin_dispatch_values)
1644 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1648 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1649 SvPV_nolen (ST (/* stack index = */ 0)));
1651 values = ST (/* stack index = */ 0);
1653 /* Make sure the argument is a hash reference. */
1654 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1655 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1662 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1668 } /* static XS (Collectd_plugin_dispatch_values) */
1671 * Collectd::plugin_get_interval ().
1673 static XS (Collectd_plugin_get_interval)
1677 /* make sure we don't get any unused variable warnings for 'items';
1678 * don't abort, though */
1680 log_err ("Usage: Collectd::plugin_get_interval()");
1682 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1683 } /* static XS (Collectd_plugin_get_interval) */
1685 /* Collectd::plugin_write (plugin, ds, vl).
1688 * name of the plugin to call, may be 'undef'
1691 * data-set that describes the submitted values, may be 'undef'
1694 * value-list to be written
1696 static XS (Collectd__plugin_write)
1707 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1711 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1712 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1713 SvPV_nolen (ST (2)));
1715 if (! SvOK (ST (0)))
1718 plugin = SvPV_nolen (ST (0));
1721 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1722 ds_array = (AV *)SvRV (ds);
1723 else if (! SvOK (ds))
1726 log_err ("Collectd::plugin_write: Invalid data-set.");
1731 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1732 log_err ("Collectd::plugin_write: Invalid value-list.");
1736 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1742 } /* static XS (Collectd__plugin_write) */
1745 * Collectd::_plugin_flush (plugin, timeout, identifier).
1748 * name of the plugin to flush
1751 * timeout to use when flushing the data
1754 * data-set identifier to flush
1756 static XS (Collectd__plugin_flush)
1758 char *plugin = NULL;
1765 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1770 plugin = SvPV_nolen (ST (0));
1773 timeout = (int)SvIV (ST (1));
1776 id = SvPV_nolen (ST (2));
1778 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1779 "id = \"%s\"", plugin, timeout, id);
1781 if (0 == plugin_flush (plugin, timeout, id))
1785 } /* static XS (Collectd__plugin_flush) */
1788 * Collectd::plugin_dispatch_notification (notif).
1791 * notification to dispatch
1793 static XS (Collectd_plugin_dispatch_notification)
1802 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1806 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1807 SvPV_nolen (ST (0)));
1811 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1812 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1816 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1822 } /* static XS (Collectd_plugin_dispatch_notification) */
1825 * Collectd::plugin_log (level, message).
1828 * log level (LOG_DEBUG, ... LOG_ERR)
1833 static XS (Collectd_plugin_log)
1838 log_err ("Usage: Collectd::plugin_log(level, message)");
1842 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1844 } /* static XS (Collectd_plugin_log) */
1847 * Collectd::_fc_register (type, name)
1855 static XS (Collectd__fc_register)
1865 log_err ("Usage: Collectd::_fc_register(type, name)");
1869 type = SvIV (ST (0));
1870 name = SvPV_nolen (ST (1));
1872 if (FC_MATCH == type)
1873 ret = fc_register_match (name, pmatch);
1874 else if (FC_TARGET == type)
1875 ret = fc_register_target (name, ptarget);
1881 } /* static XS (Collectd_fc_register) */
1884 * Collectd::call_by_name (...).
1886 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1888 static XS (Collectd_call_by_name)
1893 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1894 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1899 name = SvPV_nolen (tmp);
1901 if (NULL == get_cv (name, 0)) {
1902 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1907 /* simply pass on the subroutine call without touching the stack,
1908 * thus leaving any arguments and return values in place */
1910 } /* static XS (Collectd_call_by_name) */
1913 * Interface to collectd.
1916 static int perl_init (void)
1920 if (NULL == perl_threads)
1924 c_ithread_t *t = NULL;
1926 pthread_mutex_lock (&perl_threads->mutex);
1927 t = c_ithread_create (perl_threads->head->interp);
1928 pthread_mutex_unlock (&perl_threads->mutex);
1933 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1934 aTHX, perl_threads->number_of_threads);
1935 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1936 } /* static int perl_init (void) */
1938 static int perl_read (void)
1942 if (NULL == perl_threads)
1946 c_ithread_t *t = NULL;
1948 pthread_mutex_lock (&perl_threads->mutex);
1949 t = c_ithread_create (perl_threads->head->interp);
1950 pthread_mutex_unlock (&perl_threads->mutex);
1955 /* Assert that we're not running as the base thread. Otherwise, we might
1956 * run into concurrency issues with c_ithread_create(). See
1957 * https://github.com/collectd/collectd/issues/9 for details. */
1958 assert (aTHX != perl_threads->head->interp);
1960 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1961 aTHX, perl_threads->number_of_threads);
1962 return pplugin_call_all (aTHX_ PLUGIN_READ);
1963 } /* static int perl_read (void) */
1965 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1966 user_data_t __attribute__((unused)) *user_data)
1971 if (NULL == perl_threads)
1975 c_ithread_t *t = NULL;
1977 pthread_mutex_lock (&perl_threads->mutex);
1978 t = c_ithread_create (perl_threads->head->interp);
1979 pthread_mutex_unlock (&perl_threads->mutex);
1984 /* Lock the base thread if this is not called from one of the read threads
1985 * to avoid race conditions with c_ithread_create(). See
1986 * https://github.com/collectd/collectd/issues/9 for details. */
1987 if (aTHX == perl_threads->head->interp)
1988 pthread_mutex_lock (&perl_threads->mutex);
1990 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1991 aTHX, perl_threads->number_of_threads);
1992 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1994 if (aTHX == perl_threads->head->interp)
1995 pthread_mutex_unlock (&perl_threads->mutex);
1998 } /* static int perl_write (const data_set_t *, const value_list_t *) */
2000 static void perl_log (int level, const char *msg,
2001 user_data_t __attribute__((unused)) *user_data)
2005 if (NULL == perl_threads)
2009 c_ithread_t *t = NULL;
2011 pthread_mutex_lock (&perl_threads->mutex);
2012 t = c_ithread_create (perl_threads->head->interp);
2013 pthread_mutex_unlock (&perl_threads->mutex);
2018 /* Lock the base thread if this is not called from one of the read threads
2019 * to avoid race conditions with c_ithread_create(). See
2020 * https://github.com/collectd/collectd/issues/9 for details. */
2021 if (aTHX == perl_threads->head->interp)
2022 pthread_mutex_lock (&perl_threads->mutex);
2024 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2026 if (aTHX == perl_threads->head->interp)
2027 pthread_mutex_unlock (&perl_threads->mutex);
2030 } /* static void perl_log (int, const char *) */
2032 static int perl_notify (const notification_t *notif,
2033 user_data_t __attribute__((unused)) *user_data)
2037 if (NULL == perl_threads)
2041 c_ithread_t *t = NULL;
2043 pthread_mutex_lock (&perl_threads->mutex);
2044 t = c_ithread_create (perl_threads->head->interp);
2045 pthread_mutex_unlock (&perl_threads->mutex);
2049 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2050 } /* static int perl_notify (const notification_t *) */
2052 static int perl_flush (cdtime_t timeout, const char *identifier,
2053 user_data_t __attribute__((unused)) *user_data)
2057 if (NULL == perl_threads)
2061 c_ithread_t *t = NULL;
2063 pthread_mutex_lock (&perl_threads->mutex);
2064 t = c_ithread_create (perl_threads->head->interp);
2065 pthread_mutex_unlock (&perl_threads->mutex);
2069 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2070 } /* static int perl_flush (const int) */
2072 static int perl_shutdown (void)
2074 c_ithread_t *t = NULL;
2080 plugin_unregister_complex_config ("perl");
2082 if (NULL == perl_threads)
2086 c_ithread_t *t = NULL;
2088 pthread_mutex_lock (&perl_threads->mutex);
2089 t = c_ithread_create (perl_threads->head->interp);
2090 pthread_mutex_unlock (&perl_threads->mutex);
2095 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2096 aTHX, perl_threads->number_of_threads);
2098 plugin_unregister_log ("perl");
2099 plugin_unregister_notification ("perl");
2100 plugin_unregister_init ("perl");
2101 plugin_unregister_read ("perl");
2102 plugin_unregister_write ("perl");
2103 plugin_unregister_flush ("perl");
2105 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2107 pthread_mutex_lock (&perl_threads->mutex);
2108 t = perl_threads->tail;
2111 c_ithread_t *thr = t;
2113 /* the pointer has to be advanced before destroying
2114 * the thread as this will free the memory */
2117 c_ithread_destroy (thr);
2120 pthread_mutex_unlock (&perl_threads->mutex);
2121 pthread_mutex_destroy (&perl_threads->mutex);
2123 sfree (perl_threads);
2125 pthread_key_delete (perl_thr_key);
2129 plugin_unregister_shutdown ("perl");
2131 } /* static void perl_shutdown (void) */
2134 * Access functions for global variables.
2136 * These functions implement the "magic" used to access
2137 * the global variables from Perl.
2140 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2142 char *pv = mg->mg_ptr;
2145 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2147 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2149 char *pv = mg->mg_ptr;
2150 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2152 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2154 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2156 log_warn ("Accessing $interval_g is deprecated (and might not "
2157 "give the desired results) - plugin_get_interval() should "
2158 "be used instead.");
2159 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2161 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2163 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2165 double nv = (double)SvNV (var);
2166 log_warn ("Accessing $interval_g is deprecated (and might not "
2167 "give the desired results) - plugin_get_interval() should "
2168 "be used instead.");
2169 interval_g = DOUBLE_TO_CDTIME_T (nv);
2171 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2173 static MGVTBL g_pv_vtbl = {
2174 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2175 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2179 static MGVTBL g_interval_vtbl = {
2180 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2181 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2186 /* bootstrap the Collectd module */
2187 static void xs_init (pTHX)
2191 char *file = __FILE__;
2197 /* enable usage of Perl modules using shared libraries */
2198 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2201 for (i = 0; NULL != api[i].f; ++i)
2202 newXS (api[i].name, api[i].f, file);
2204 stash = gv_stashpv ("Collectd", 1);
2206 /* export "constants" */
2207 for (i = 0; '\0' != constants[i].name[0]; ++i)
2208 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2210 /* export global variables
2211 * by adding "magic" to the SV's representing the globale variables
2212 * perl is able to automagically call the get/set function when
2213 * accessing any such variable (this is basically the same as using
2215 /* global strings */
2216 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2217 tmp = get_sv (g_strings[i].name, 1);
2218 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2219 g_strings[i].var, 0);
2222 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2223 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2224 /* vtbl = */ &g_interval_vtbl,
2225 /* name = */ NULL, /* namelen = */ 0);
2228 } /* static void xs_init (pTHX) */
2230 /* Initialize the global Perl interpreter. */
2231 static int init_pi (int argc, char **argv)
2235 if (NULL != perl_threads)
2238 log_info ("Initializing Perl interpreter...");
2243 for (i = 0; i < argc; ++i)
2244 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2246 #endif /* COLLECT_DEBUG */
2248 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2249 log_err ("init_pi: pthread_key_create failed");
2251 /* this must not happen - cowardly giving up if it does */
2256 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2257 * triggers a "value computed is not used" warning by gcc. */
2260 PERL_SYS_INIT3 (&argc, &argv, &environ);
2262 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2263 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2265 pthread_mutex_init (&perl_threads->mutex, NULL);
2266 /* locking the mutex should not be necessary at this point
2267 * but let's just do it for the sake of completeness */
2268 pthread_mutex_lock (&perl_threads->mutex);
2270 perl_threads->head = c_ithread_create (NULL);
2271 perl_threads->tail = perl_threads->head;
2273 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2274 log_err ("init_pi: Not enough memory.");
2278 aTHX = perl_threads->head->interp;
2279 pthread_mutex_unlock (&perl_threads->mutex);
2281 perl_construct (aTHX);
2283 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2285 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2286 SV *err = get_sv ("@", 1);
2287 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2290 perl_destruct (perl_threads->head->interp);
2291 perl_free (perl_threads->head->interp);
2292 sfree (perl_threads);
2294 pthread_key_delete (perl_thr_key);
2298 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2299 sv_setpv (get_sv ("0", 0), "collectd");
2303 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2304 plugin_register_notification ("perl", perl_notify,
2305 /* user_data = */ NULL);
2306 plugin_register_init ("perl", perl_init);
2308 plugin_register_read ("perl", perl_read);
2310 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2311 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2312 plugin_register_shutdown ("perl", perl_shutdown);
2314 } /* static int init_pi (const char **, const int) */
2317 * LoadPlugin "<Plugin>"
2319 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2321 char module_name[DATA_MAX_NAME_LEN];
2325 if ((0 != ci->children_num) || (1 != ci->values_num)
2326 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2327 log_err ("LoadPlugin expects a single string argument.");
2331 value = ci->values[0].value.string;
2333 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2334 log_err ("Invalid module name %s", value);
2338 if (0 != init_pi (perl_argc, perl_argv))
2341 assert (NULL != perl_threads);
2342 assert (NULL != perl_threads->head);
2344 aTHX = perl_threads->head->interp;
2346 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2347 load_module (PERL_LOADMOD_NOIMPORT,
2348 newSVpv (module_name, strlen (module_name)), Nullsv);
2350 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2355 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2359 if ((0 != ci->children_num) || (1 != ci->values_num)
2360 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2361 log_err ("BaseName expects a single string argument.");
2365 value = ci->values[0].value.string;
2367 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2368 sstrncpy (base_name, value, sizeof (base_name));
2370 } /* static int perl_config_basename (oconfig_item_it *) */
2373 * EnableDebugger "<Package>"|""
2375 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2379 if ((0 != ci->children_num) || (1 != ci->values_num)
2380 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2381 log_err ("EnableDebugger expects a single string argument.");
2385 if (NULL != perl_threads) {
2386 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2390 value = ci->values[0].value.string;
2392 perl_argv = (char **)realloc (perl_argv,
2393 (++perl_argc + 1) * sizeof (char *));
2395 if (NULL == perl_argv) {
2396 log_err ("perl_config: Not enough memory.");
2400 if ('\0' == value[0]) {
2401 perl_argv[perl_argc - 1] = "-d";
2404 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2405 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2406 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2409 perl_argv[perl_argc] = NULL;
2411 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2414 * IncludeDir "<Dir>"
2416 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2420 if ((0 != ci->children_num) || (1 != ci->values_num)
2421 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2422 log_err ("IncludeDir expects a single string argument.");
2426 value = ci->values[0].value.string;
2429 perl_argv = (char **)realloc (perl_argv,
2430 (++perl_argc + 1) * sizeof (char *));
2432 if (NULL == perl_argv) {
2433 log_err ("perl_config: Not enough memory.");
2437 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2438 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2439 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2441 perl_argv[perl_argc] = NULL;
2444 /* prepend the directory to @INC */
2445 av_unshift (GvAVn (PL_incgv), 1);
2446 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2449 } /* static int perl_config_includedir (oconfig_item_it *) */
2454 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2464 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2465 log_err ("LoadPlugin expects a single string argument.");
2469 plugin = ci->values[0].value.string;
2472 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2476 log_err ("Unable to convert configuration to a Perl hash value.");
2477 config = (HV *)&PL_sv_undef;
2485 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2486 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2490 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2505 } /* static int perl_config_plugin (oconfig_item_it *) */
2507 static int perl_config (oconfig_item_t *ci)
2514 for (i = 0; i < ci->children_num; ++i) {
2515 oconfig_item_t *c = ci->children + i;
2516 int current_status = 0;
2518 if (NULL != perl_threads)
2519 aTHX = PERL_GET_CONTEXT;
2521 if (0 == strcasecmp (c->key, "LoadPlugin"))
2522 current_status = perl_config_loadplugin (aTHX_ c);
2523 else if (0 == strcasecmp (c->key, "BaseName"))
2524 current_status = perl_config_basename (aTHX_ c);
2525 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2526 current_status = perl_config_enabledebugger (aTHX_ c);
2527 else if (0 == strcasecmp (c->key, "IncludeDir"))
2528 current_status = perl_config_includedir (aTHX_ c);
2529 else if (0 == strcasecmp (c->key, "Plugin"))
2530 current_status = perl_config_plugin (aTHX_ c);
2533 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2537 /* fatal error - it's up to perl_config_* to clean up */
2538 if (0 > current_status) {
2539 log_err ("Configuration failed with a fatal error - "
2540 "plugin disabled!");
2541 return current_status;
2544 status += current_status;
2547 } /* static int perl_config (oconfig_item_t *) */
2549 void module_register (void)
2552 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2554 /* default options for the Perl interpreter */
2556 perl_argv[1] = "-MCollectd";
2557 perl_argv[2] = "-e";
2559 perl_argv[4] = NULL;
2561 plugin_register_complex_config ("perl", perl_config);
2563 } /* void module_register (void) */
2565 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */