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.");
518 (*m)->nm_value.nm_double = SvNVX (*tmp);
519 (*m)->type = NM_TYPE_DOUBLE;
521 else if (SvUOK (*tmp)) {
522 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
523 (*m)->type = NM_TYPE_UNSIGNED_INT;
525 else if (SvIOK (*tmp)) {
526 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
527 (*m)->type = NM_TYPE_SIGNED_INT;
530 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
531 (*m)->type = NM_TYPE_STRING;
538 } /* static int av2notification_meta (AV *, notification_meta_t *) */
540 static int hv2notification (pTHX_ HV *hash, notification_t *n)
544 if ((NULL == hash) || (NULL == n))
547 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
548 n->severity = SvIV (*tmp);
550 n->severity = NOTIF_FAILURE;
552 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
554 double t = SvNV (*tmp);
555 n->time = DOUBLE_TO_CDTIME_T (t);
560 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
561 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
563 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
564 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
566 sstrncpy (n->host, hostname_g, sizeof (n->host));
568 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
569 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
571 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
572 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
573 sizeof (n->plugin_instance));
575 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
576 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
578 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
579 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
580 sizeof (n->type_instance));
583 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
584 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
585 log_warn ("hv2notification: Ignoring invalid meta information.");
589 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
590 plugin_notification_meta_free (n->meta);
597 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
599 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
603 if ((NULL == ds) || (NULL == array))
606 av_extend (array, ds->ds_num);
608 for (i = 0; i < ds->ds_num; ++i) {
609 HV *source = newHV ();
611 if (NULL == hv_store (source, "name", 4,
612 newSVpv (ds->ds[i].name, 0), 0))
615 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
618 if (! isnan (ds->ds[i].min))
619 if (NULL == hv_store (source, "min", 3,
620 newSVnv (ds->ds[i].min), 0))
623 if (! isnan (ds->ds[i].max))
624 if (NULL == hv_store (source, "max", 3,
625 newSVnv (ds->ds[i].max), 0))
628 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
632 } /* static int data_set2av (data_set_t *, AV *) */
634 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
641 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
644 len = vl->values_len;
646 if (ds->ds_num < len) {
647 log_warn ("value2av: Value length exceeds data set length.");
652 av_extend (values, len - 1);
654 for (i = 0; i < len; ++i) {
657 if (DS_TYPE_COUNTER == ds->ds[i].type)
658 val = newSViv (vl->values[i].counter);
659 else if (DS_TYPE_GAUGE == ds->ds[i].type)
660 val = newSVnv (vl->values[i].gauge);
661 else if (DS_TYPE_DERIVE == ds->ds[i].type)
662 val = newSViv (vl->values[i].derive);
663 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
664 val = newSViv (vl->values[i].absolute);
666 if (NULL == av_store (values, i, val)) {
672 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
677 double t = CDTIME_T_TO_DOUBLE (vl->time);
678 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
683 double t = CDTIME_T_TO_DOUBLE (vl->interval);
684 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
688 if ('\0' != vl->host[0])
689 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
692 if ('\0' != vl->plugin[0])
693 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
696 if ('\0' != vl->plugin_instance[0])
697 if (NULL == hv_store (hash, "plugin_instance", 15,
698 newSVpv (vl->plugin_instance, 0), 0))
701 if ('\0' != vl->type[0])
702 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
705 if ('\0' != vl->type_instance[0])
706 if (NULL == hv_store (hash, "type_instance", 13,
707 newSVpv (vl->type_instance, 0), 0))
710 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
712 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
722 av_extend (array, meta_num);
724 for (i = 0; NULL != meta; meta = meta->next, ++i) {
728 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
731 if (NM_TYPE_STRING == meta->type)
732 value = newSVpv (meta->nm_value.nm_string, 0);
733 else if (NM_TYPE_SIGNED_INT == meta->type)
734 value = newSViv (meta->nm_value.nm_signed_int);
735 else if (NM_TYPE_UNSIGNED_INT == meta->type)
736 value = newSVuv (meta->nm_value.nm_unsigned_int);
737 else if (NM_TYPE_DOUBLE == meta->type)
738 value = newSVnv (meta->nm_value.nm_double);
739 else if (NM_TYPE_BOOLEAN == meta->type)
740 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
744 if (NULL == hv_store (m, "value", 5, value, 0)) {
749 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
756 } /* static int notification_meta2av (notification_meta_t *, AV *) */
758 static int notification2hv (pTHX_ notification_t *n, HV *hash)
760 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
765 double t = CDTIME_T_TO_DOUBLE (n->time);
766 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
770 if ('\0' != *n->message)
771 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
774 if ('\0' != *n->host)
775 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
778 if ('\0' != *n->plugin)
779 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
782 if ('\0' != *n->plugin_instance)
783 if (NULL == hv_store (hash, "plugin_instance", 15,
784 newSVpv (n->plugin_instance, 0), 0))
787 if ('\0' != *n->type)
788 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
791 if ('\0' != *n->type_instance)
792 if (NULL == hv_store (hash, "type_instance", 13,
793 newSVpv (n->type_instance, 0), 0))
796 if (NULL != n->meta) {
798 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
799 || (NULL == hv_store (hash, "meta", 4,
800 newRV_noinc ((SV *)meta), 0))) {
807 } /* static int notification2hv (notification_t *, HV *) */
809 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
816 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
820 if (0 < ci->values_num)
821 av_extend (values, ci->values_num);
823 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
829 for (i = 0; i < ci->values_num; ++i) {
832 switch (ci->values[i].type) {
833 case OCONFIG_TYPE_STRING:
834 value = newSVpv (ci->values[i].value.string, 0);
836 case OCONFIG_TYPE_NUMBER:
837 value = newSVnv ((NV)ci->values[i].value.number);
839 case OCONFIG_TYPE_BOOLEAN:
840 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
843 log_err ("oconfig_item2hv: Invalid value type %i.",
845 value = &PL_sv_undef;
848 if (NULL == av_store (values, i, value)) {
854 /* ignoring 'parent' member which is uninteresting in this case */
857 if (0 < ci->children_num)
858 av_extend (children, ci->children_num);
860 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
866 for (i = 0; i < ci->children_num; ++i) {
867 HV *child = newHV ();
869 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
875 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
882 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
885 * Internal functions.
888 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
890 if (base_name[0] == '\0')
891 status = ssnprintf (buf, buf_len, "%s", module);
893 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
894 if ((status < 0) || ((unsigned int)status >= buf_len))
897 } /* char *get_module_name */
900 * Add a plugin's data set definition.
902 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
908 if ((NULL == name) || (NULL == dataset))
911 if (0 != av2data_set (aTHX_ dataset, name, &ds))
914 ret = plugin_register_data_set (&ds);
918 } /* static int pplugin_register_data_set (char *, SV *) */
921 * Remove a plugin's data set definition.
923 static int pplugin_unregister_data_set (char *name)
927 return plugin_unregister_data_set (name);
928 } /* static int pplugin_unregister_data_set (char *) */
931 * Submit the values to the write functions.
933 static int pplugin_dispatch_values (pTHX_ HV *values)
935 value_list_t vl = VALUE_LIST_INIT;
942 if (0 != hv2value_list (aTHX_ values, &vl))
945 ret = plugin_dispatch_values (&vl);
949 } /* static int pplugin_dispatch_values (char *, HV *) */
952 * Submit the values to a single write function.
954 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
957 value_list_t vl = VALUE_LIST_INIT;
964 if (0 != hv2value_list (aTHX_ values, &vl))
967 if ((NULL != data_set)
968 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
971 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
973 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
974 NULL == plugin ? "<any>" : plugin, ret);
976 if (NULL != data_set)
980 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
983 * Dispatch a notification.
985 static int pplugin_dispatch_notification (pTHX_ HV *notif)
994 memset (&n, 0, sizeof (n));
996 if (0 != hv2notification (aTHX_ notif, &n))
999 ret = plugin_dispatch_notification (&n);
1000 plugin_notification_meta_free (n.meta);
1002 } /* static int pplugin_dispatch_notification (HV *) */
1005 * Call all working functions of the given type.
1007 static int pplugin_call_all (pTHX_ int type, ...)
1016 if ((type < 0) || (type >= PLUGIN_TYPES))
1019 va_start (ap, type);
1026 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1028 if (PLUGIN_WRITE == type) {
1030 * $_[0] = $plugin_type;
1045 * values => [ $v1, ... ],
1047 * host => $hostname,
1048 * plugin => $plugin,
1050 * plugin_instance => $instance,
1051 * type_instance => $type_instance
1060 ds = va_arg (ap, data_set_t *);
1061 vl = va_arg (ap, value_list_t *);
1063 if (-1 == data_set2av (aTHX_ ds, pds)) {
1066 pds = (AV *)&PL_sv_undef;
1070 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1073 pvl = (HV *)&PL_sv_undef;
1077 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1078 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1079 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1081 else if (PLUGIN_LOG == type) {
1087 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1088 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1090 else if (PLUGIN_NOTIF == type) {
1094 * severity => $severity,
1098 * plugin => $plugin,
1100 * plugin_instance => $instance,
1101 * type_instance => $type_instance
1105 HV *notif = newHV ();
1107 n = va_arg (ap, notification_t *);
1109 if (-1 == notification2hv (aTHX_ n, notif)) {
1112 notif = (HV *)&PL_sv_undef;
1116 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1118 else if (PLUGIN_FLUSH == type) {
1123 * $_[1] = $identifier;
1125 timeout = va_arg (ap, cdtime_t);
1127 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1128 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1133 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1148 } /* static int pplugin_call_all (int, ...) */
1151 * collectd's perl interpreter based thread implementation.
1153 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1156 /* must be called with perl_threads->mutex locked */
1157 static void c_ithread_destroy (c_ithread_t *ithread)
1159 dTHXa (ithread->interp);
1161 assert (NULL != perl_threads);
1163 PERL_SET_CONTEXT (aTHX);
1164 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1169 --perl_threads->number_of_threads;
1170 #endif /* COLLECT_DEBUG */
1172 perl_destruct (aTHX);
1175 if (NULL == ithread->prev)
1176 perl_threads->head = ithread->next;
1178 ithread->prev->next = ithread->next;
1180 if (NULL == ithread->next)
1181 perl_threads->tail = ithread->prev;
1183 ithread->next->prev = ithread->prev;
1187 } /* static void c_ithread_destroy (c_ithread_t *) */
1189 static void c_ithread_destructor (void *arg)
1191 c_ithread_t *ithread = (c_ithread_t *)arg;
1192 c_ithread_t *t = NULL;
1194 if (NULL == perl_threads)
1197 pthread_mutex_lock (&perl_threads->mutex);
1199 for (t = perl_threads->head; NULL != t; t = t->next)
1203 /* the ithread no longer exists */
1207 c_ithread_destroy (ithread);
1209 pthread_mutex_unlock (&perl_threads->mutex);
1211 } /* static void c_ithread_destructor (void *) */
1213 /* must be called with perl_threads->mutex locked */
1214 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1216 c_ithread_t *t = NULL;
1219 assert (NULL != perl_threads);
1221 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1222 memset (t, 0, sizeof (c_ithread_t));
1224 t->interp = (NULL == base)
1226 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1230 if ((NULL != base) && (NULL != PL_endav)) {
1231 av_clear (PL_endav);
1232 av_undef (PL_endav);
1237 ++perl_threads->number_of_threads;
1238 #endif /* COLLECT_DEBUG */
1242 if (NULL == perl_threads->tail) {
1243 perl_threads->head = t;
1247 perl_threads->tail->next = t;
1248 t->prev = perl_threads->tail;
1251 perl_threads->tail = t;
1253 pthread_setspecific (perl_thr_key, (const void *)t);
1255 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1258 * Filter chains implementation.
1261 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1268 notification_meta_t **meta = NULL;
1273 if ((type < 0) || (type >= FC_TYPES))
1276 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1279 va_start (ap, data);
1286 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1287 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1288 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1290 if (FC_CB_CREATE == cb_type) {
1293 * $_[1] = $user_data;
1296 HV *config = newHV ();
1298 ci = va_arg (ap, oconfig_item_t *);
1300 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1303 config = (HV *)&PL_sv_undef;
1307 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1309 else if (FC_CB_DESTROY == cb_type) {
1311 * $_[1] = $user_data;
1314 /* nothing to be done - the user data pointer
1315 * is pushed onto the stack later */
1317 else if (FC_CB_EXEC == cb_type) {
1322 * $_[3] = $user_data;
1330 ds = va_arg (ap, data_set_t *);
1331 vl = va_arg (ap, value_list_t *);
1332 meta = va_arg (ap, notification_meta_t **);
1334 if (0 != data_set2av (aTHX_ ds, pds)) {
1337 pds = (AV *)&PL_sv_undef;
1341 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1344 pvl = (HV *)&PL_sv_undef;
1351 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1354 pmeta = (AV *)&PL_sv_undef;
1359 pmeta = (AV *)&PL_sv_undef;
1362 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1363 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1364 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1367 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1371 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1373 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1374 assert (pmeta != NULL);
1376 plugin_notification_meta_free (*meta);
1377 av2notification_meta (aTHX_ pmeta, meta);
1384 /* the exec callbacks return a status, while
1385 * the others return a boolean value */
1386 if (FC_CB_EXEC == cb_type)
1388 else if (! SvTRUE (tmp))
1398 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1400 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1402 pfc_user_data_t *data;
1408 if (NULL == perl_threads)
1412 c_ithread_t *t = NULL;
1414 pthread_mutex_lock (&perl_threads->mutex);
1415 t = c_ithread_create (perl_threads->head->interp);
1416 pthread_mutex_unlock (&perl_threads->mutex);
1421 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1422 aTHX, perl_threads->number_of_threads);
1424 if ((1 != ci->values_num)
1425 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1426 log_warn ("A \"%s\" block expects a single string argument.",
1427 (FC_MATCH == type) ? "Match" : "Target");
1431 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1432 data->name = sstrdup (ci->values[0].value.string);
1433 data->user_data = newSV (0);
1435 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1438 PFC_USER_DATA_FREE (data);
1442 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1444 static int fc_destroy (int type, void **user_data)
1446 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1452 if ((NULL == perl_threads) || (NULL == data))
1456 c_ithread_t *t = NULL;
1458 pthread_mutex_lock (&perl_threads->mutex);
1459 t = c_ithread_create (perl_threads->head->interp);
1460 pthread_mutex_unlock (&perl_threads->mutex);
1465 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1466 aTHX, perl_threads->number_of_threads);
1468 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1470 PFC_USER_DATA_FREE (data);
1473 } /* static int fc_destroy (int, void **) */
1475 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1476 notification_meta_t **meta, void **user_data)
1478 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1482 if (NULL == perl_threads)
1485 assert (NULL != data);
1488 c_ithread_t *t = NULL;
1490 pthread_mutex_lock (&perl_threads->mutex);
1491 t = c_ithread_create (perl_threads->head->interp);
1492 pthread_mutex_unlock (&perl_threads->mutex);
1497 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1498 aTHX, perl_threads->number_of_threads);
1500 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1501 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1502 notification_meta_t **, void **) */
1504 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1506 return fc_create (FC_MATCH, ci, user_data);
1507 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1509 static int pmatch_destroy (void **user_data)
1511 return fc_destroy (FC_MATCH, user_data);
1512 } /* static int pmatch_destroy (void **) */
1514 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1515 notification_meta_t **meta, void **user_data)
1517 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1518 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1519 notification_meta_t **, void **) */
1521 static match_proc_t pmatch = {
1522 pmatch_create, pmatch_destroy, pmatch_match
1525 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1527 return fc_create (FC_TARGET, ci, user_data);
1528 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1530 static int ptarget_destroy (void **user_data)
1532 return fc_destroy (FC_TARGET, user_data);
1533 } /* static int ptarget_destroy (void **) */
1535 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1536 notification_meta_t **meta, void **user_data)
1538 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1539 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1540 notification_meta_t **, void **) */
1542 static target_proc_t ptarget = {
1543 ptarget_create, ptarget_destroy, ptarget_invoke
1547 * Exported Perl API.
1551 * Collectd::plugin_register_data_set (type, dataset).
1554 * type of the dataset
1557 * dataset to be registered
1559 static XS (Collectd_plugin_register_ds)
1566 log_warn ("Using plugin_register() to register new data-sets is "
1567 "deprecated - add new entries to a custom types.db instead.");
1570 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1574 log_debug ("Collectd::plugin_register_data_set: "
1575 "type = \"%s\", dataset = \"%s\"",
1576 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1580 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1581 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1585 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1593 } /* static XS (Collectd_plugin_register_ds) */
1596 * Collectd::plugin_unregister_data_set (type).
1599 * type of the dataset
1601 static XS (Collectd_plugin_unregister_ds)
1606 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1610 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1611 SvPV_nolen (ST (0)));
1613 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1617 } /* static XS (Collectd_plugin_register_ds) */
1620 * Collectd::plugin_dispatch_values (name, values).
1623 * name of the plugin
1626 * value list to submit
1628 static XS (Collectd_plugin_dispatch_values)
1637 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1641 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1642 SvPV_nolen (ST (/* stack index = */ 0)));
1644 values = ST (/* stack index = */ 0);
1646 /* Make sure the argument is a hash reference. */
1647 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1648 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1655 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1661 } /* static XS (Collectd_plugin_dispatch_values) */
1664 * Collectd::plugin_get_interval ().
1666 static XS (Collectd_plugin_get_interval)
1670 /* make sure we don't get any unused variable warnings for 'items';
1671 * don't abort, though */
1673 log_err ("Usage: Collectd::plugin_get_interval()");
1675 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1676 } /* static XS (Collectd_plugin_get_interval) */
1678 /* Collectd::plugin_write (plugin, ds, vl).
1681 * name of the plugin to call, may be 'undef'
1684 * data-set that describes the submitted values, may be 'undef'
1687 * value-list to be written
1689 static XS (Collectd__plugin_write)
1700 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1704 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1705 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1706 SvPV_nolen (ST (2)));
1708 if (! SvOK (ST (0)))
1711 plugin = SvPV_nolen (ST (0));
1714 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1715 ds_array = (AV *)SvRV (ds);
1716 else if (! SvOK (ds))
1719 log_err ("Collectd::plugin_write: Invalid data-set.");
1724 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1725 log_err ("Collectd::plugin_write: Invalid value-list.");
1729 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1735 } /* static XS (Collectd__plugin_write) */
1738 * Collectd::_plugin_flush (plugin, timeout, identifier).
1741 * name of the plugin to flush
1744 * timeout to use when flushing the data
1747 * data-set identifier to flush
1749 static XS (Collectd__plugin_flush)
1751 char *plugin = NULL;
1758 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1763 plugin = SvPV_nolen (ST (0));
1766 timeout = (int)SvIV (ST (1));
1769 id = SvPV_nolen (ST (2));
1771 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1772 "id = \"%s\"", plugin, timeout, id);
1774 if (0 == plugin_flush (plugin, timeout, id))
1778 } /* static XS (Collectd__plugin_flush) */
1781 * Collectd::plugin_dispatch_notification (notif).
1784 * notification to dispatch
1786 static XS (Collectd_plugin_dispatch_notification)
1795 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1799 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1800 SvPV_nolen (ST (0)));
1804 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1805 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1809 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1815 } /* static XS (Collectd_plugin_dispatch_notification) */
1818 * Collectd::plugin_log (level, message).
1821 * log level (LOG_DEBUG, ... LOG_ERR)
1826 static XS (Collectd_plugin_log)
1831 log_err ("Usage: Collectd::plugin_log(level, message)");
1835 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1837 } /* static XS (Collectd_plugin_log) */
1840 * Collectd::_fc_register (type, name)
1848 static XS (Collectd__fc_register)
1858 log_err ("Usage: Collectd::_fc_register(type, name)");
1862 type = SvIV (ST (0));
1863 name = SvPV_nolen (ST (1));
1865 if (FC_MATCH == type)
1866 ret = fc_register_match (name, pmatch);
1867 else if (FC_TARGET == type)
1868 ret = fc_register_target (name, ptarget);
1874 } /* static XS (Collectd_fc_register) */
1877 * Collectd::call_by_name (...).
1879 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1881 static XS (Collectd_call_by_name)
1886 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1887 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1892 name = SvPV_nolen (tmp);
1894 if (NULL == get_cv (name, 0)) {
1895 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1900 /* simply pass on the subroutine call without touching the stack,
1901 * thus leaving any arguments and return values in place */
1903 } /* static XS (Collectd_call_by_name) */
1906 * Interface to collectd.
1909 static int perl_init (void)
1913 if (NULL == perl_threads)
1917 c_ithread_t *t = NULL;
1919 pthread_mutex_lock (&perl_threads->mutex);
1920 t = c_ithread_create (perl_threads->head->interp);
1921 pthread_mutex_unlock (&perl_threads->mutex);
1926 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1927 aTHX, perl_threads->number_of_threads);
1928 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1929 } /* static int perl_init (void) */
1931 static int perl_read (void)
1935 if (NULL == perl_threads)
1939 c_ithread_t *t = NULL;
1941 pthread_mutex_lock (&perl_threads->mutex);
1942 t = c_ithread_create (perl_threads->head->interp);
1943 pthread_mutex_unlock (&perl_threads->mutex);
1948 /* Assert that we're not running as the base thread. Otherwise, we might
1949 * run into concurrency issues with c_ithread_create(). See
1950 * https://github.com/collectd/collectd/issues/9 for details. */
1951 assert (aTHX != perl_threads->head->interp);
1953 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1954 aTHX, perl_threads->number_of_threads);
1955 return pplugin_call_all (aTHX_ PLUGIN_READ);
1956 } /* static int perl_read (void) */
1958 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1959 user_data_t __attribute__((unused)) *user_data)
1964 if (NULL == perl_threads)
1968 c_ithread_t *t = NULL;
1970 pthread_mutex_lock (&perl_threads->mutex);
1971 t = c_ithread_create (perl_threads->head->interp);
1972 pthread_mutex_unlock (&perl_threads->mutex);
1977 /* Lock the base thread if this is not called from one of the read threads
1978 * to avoid race conditions with c_ithread_create(). See
1979 * https://github.com/collectd/collectd/issues/9 for details. */
1980 if (aTHX == perl_threads->head->interp)
1981 pthread_mutex_lock (&perl_threads->mutex);
1983 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1984 aTHX, perl_threads->number_of_threads);
1985 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1987 if (aTHX == perl_threads->head->interp)
1988 pthread_mutex_unlock (&perl_threads->mutex);
1991 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1993 static void perl_log (int level, const char *msg,
1994 user_data_t __attribute__((unused)) *user_data)
1998 if (NULL == perl_threads)
2002 c_ithread_t *t = NULL;
2004 pthread_mutex_lock (&perl_threads->mutex);
2005 t = c_ithread_create (perl_threads->head->interp);
2006 pthread_mutex_unlock (&perl_threads->mutex);
2011 /* Lock the base thread if this is not called from one of the read threads
2012 * to avoid race conditions with c_ithread_create(). See
2013 * https://github.com/collectd/collectd/issues/9 for details. */
2014 if (aTHX == perl_threads->head->interp)
2015 pthread_mutex_lock (&perl_threads->mutex);
2017 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2019 if (aTHX == perl_threads->head->interp)
2020 pthread_mutex_unlock (&perl_threads->mutex);
2023 } /* static void perl_log (int, const char *) */
2025 static int perl_notify (const notification_t *notif,
2026 user_data_t __attribute__((unused)) *user_data)
2030 if (NULL == perl_threads)
2034 c_ithread_t *t = NULL;
2036 pthread_mutex_lock (&perl_threads->mutex);
2037 t = c_ithread_create (perl_threads->head->interp);
2038 pthread_mutex_unlock (&perl_threads->mutex);
2042 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2043 } /* static int perl_notify (const notification_t *) */
2045 static int perl_flush (cdtime_t timeout, const char *identifier,
2046 user_data_t __attribute__((unused)) *user_data)
2050 if (NULL == perl_threads)
2054 c_ithread_t *t = NULL;
2056 pthread_mutex_lock (&perl_threads->mutex);
2057 t = c_ithread_create (perl_threads->head->interp);
2058 pthread_mutex_unlock (&perl_threads->mutex);
2062 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2063 } /* static int perl_flush (const int) */
2065 static int perl_shutdown (void)
2067 c_ithread_t *t = NULL;
2073 plugin_unregister_complex_config ("perl");
2075 if (NULL == perl_threads)
2079 c_ithread_t *t = NULL;
2081 pthread_mutex_lock (&perl_threads->mutex);
2082 t = c_ithread_create (perl_threads->head->interp);
2083 pthread_mutex_unlock (&perl_threads->mutex);
2088 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2089 aTHX, perl_threads->number_of_threads);
2091 plugin_unregister_log ("perl");
2092 plugin_unregister_notification ("perl");
2093 plugin_unregister_init ("perl");
2094 plugin_unregister_read ("perl");
2095 plugin_unregister_write ("perl");
2096 plugin_unregister_flush ("perl");
2098 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2100 pthread_mutex_lock (&perl_threads->mutex);
2101 t = perl_threads->tail;
2104 c_ithread_t *thr = t;
2106 /* the pointer has to be advanced before destroying
2107 * the thread as this will free the memory */
2110 c_ithread_destroy (thr);
2113 pthread_mutex_unlock (&perl_threads->mutex);
2114 pthread_mutex_destroy (&perl_threads->mutex);
2116 sfree (perl_threads);
2118 pthread_key_delete (perl_thr_key);
2122 plugin_unregister_shutdown ("perl");
2124 } /* static void perl_shutdown (void) */
2127 * Access functions for global variables.
2129 * These functions implement the "magic" used to access
2130 * the global variables from Perl.
2133 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2135 char *pv = mg->mg_ptr;
2138 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2140 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2142 char *pv = mg->mg_ptr;
2143 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2145 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2147 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2149 log_warn ("Accessing $interval_g is deprecated (and might not "
2150 "give the desired results) - plugin_get_interval() should "
2151 "be used instead.");
2152 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2154 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2156 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2158 double nv = (double)SvNV (var);
2159 log_warn ("Accessing $interval_g is deprecated (and might not "
2160 "give the desired results) - plugin_get_interval() should "
2161 "be used instead.");
2162 interval_g = DOUBLE_TO_CDTIME_T (nv);
2164 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2166 static MGVTBL g_pv_vtbl = {
2167 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2168 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2172 static MGVTBL g_interval_vtbl = {
2173 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2174 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2179 /* bootstrap the Collectd module */
2180 static void xs_init (pTHX)
2184 char *file = __FILE__;
2190 /* enable usage of Perl modules using shared libraries */
2191 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2194 for (i = 0; NULL != api[i].f; ++i)
2195 newXS (api[i].name, api[i].f, file);
2197 stash = gv_stashpv ("Collectd", 1);
2199 /* export "constants" */
2200 for (i = 0; '\0' != constants[i].name[0]; ++i)
2201 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2203 /* export global variables
2204 * by adding "magic" to the SV's representing the globale variables
2205 * perl is able to automagically call the get/set function when
2206 * accessing any such variable (this is basically the same as using
2208 /* global strings */
2209 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2210 tmp = get_sv (g_strings[i].name, 1);
2211 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2212 g_strings[i].var, 0);
2215 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2216 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2217 /* vtbl = */ &g_interval_vtbl,
2218 /* name = */ NULL, /* namelen = */ 0);
2221 } /* static void xs_init (pTHX) */
2223 /* Initialize the global Perl interpreter. */
2224 static int init_pi (int argc, char **argv)
2228 if (NULL != perl_threads)
2231 log_info ("Initializing Perl interpreter...");
2236 for (i = 0; i < argc; ++i)
2237 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2239 #endif /* COLLECT_DEBUG */
2241 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2242 log_err ("init_pi: pthread_key_create failed");
2244 /* this must not happen - cowardly giving up if it does */
2249 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2250 * triggers a "value computed is not used" warning by gcc. */
2253 PERL_SYS_INIT3 (&argc, &argv, &environ);
2255 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2256 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2258 pthread_mutex_init (&perl_threads->mutex, NULL);
2259 /* locking the mutex should not be necessary at this point
2260 * but let's just do it for the sake of completeness */
2261 pthread_mutex_lock (&perl_threads->mutex);
2263 perl_threads->head = c_ithread_create (NULL);
2264 perl_threads->tail = perl_threads->head;
2266 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2267 log_err ("init_pi: Not enough memory.");
2271 aTHX = perl_threads->head->interp;
2272 pthread_mutex_unlock (&perl_threads->mutex);
2274 perl_construct (aTHX);
2276 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2278 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2279 SV *err = get_sv ("@", 1);
2280 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2283 perl_destruct (perl_threads->head->interp);
2284 perl_free (perl_threads->head->interp);
2285 sfree (perl_threads);
2287 pthread_key_delete (perl_thr_key);
2291 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2292 sv_setpv (get_sv ("0", 0), "collectd");
2296 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2297 plugin_register_notification ("perl", perl_notify,
2298 /* user_data = */ NULL);
2299 plugin_register_init ("perl", perl_init);
2301 plugin_register_read ("perl", perl_read);
2303 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2304 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2305 plugin_register_shutdown ("perl", perl_shutdown);
2307 } /* static int init_pi (const char **, const int) */
2310 * LoadPlugin "<Plugin>"
2312 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2314 char module_name[DATA_MAX_NAME_LEN];
2318 if ((0 != ci->children_num) || (1 != ci->values_num)
2319 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2320 log_err ("LoadPlugin expects a single string argument.");
2324 value = ci->values[0].value.string;
2326 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2327 log_err ("Invalid module name %s", value);
2331 if (0 != init_pi (perl_argc, perl_argv))
2334 assert (NULL != perl_threads);
2335 assert (NULL != perl_threads->head);
2337 aTHX = perl_threads->head->interp;
2339 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2340 load_module (PERL_LOADMOD_NOIMPORT,
2341 newSVpv (module_name, strlen (module_name)), Nullsv);
2343 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2348 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2352 if ((0 != ci->children_num) || (1 != ci->values_num)
2353 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2354 log_err ("BaseName expects a single string argument.");
2358 value = ci->values[0].value.string;
2360 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2361 sstrncpy (base_name, value, sizeof (base_name));
2363 } /* static int perl_config_basename (oconfig_item_it *) */
2366 * EnableDebugger "<Package>"|""
2368 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2372 if ((0 != ci->children_num) || (1 != ci->values_num)
2373 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2374 log_err ("EnableDebugger expects a single string argument.");
2378 if (NULL != perl_threads) {
2379 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2383 value = ci->values[0].value.string;
2385 perl_argv = (char **)realloc (perl_argv,
2386 (++perl_argc + 1) * sizeof (char *));
2388 if (NULL == perl_argv) {
2389 log_err ("perl_config: Not enough memory.");
2393 if ('\0' == value[0]) {
2394 perl_argv[perl_argc - 1] = "-d";
2397 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2398 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2399 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2402 perl_argv[perl_argc] = NULL;
2404 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2407 * IncludeDir "<Dir>"
2409 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2413 if ((0 != ci->children_num) || (1 != ci->values_num)
2414 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2415 log_err ("IncludeDir expects a single string argument.");
2419 value = ci->values[0].value.string;
2422 perl_argv = (char **)realloc (perl_argv,
2423 (++perl_argc + 1) * sizeof (char *));
2425 if (NULL == perl_argv) {
2426 log_err ("perl_config: Not enough memory.");
2430 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2431 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2432 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2434 perl_argv[perl_argc] = NULL;
2437 /* prepend the directory to @INC */
2438 av_unshift (GvAVn (PL_incgv), 1);
2439 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2442 } /* static int perl_config_includedir (oconfig_item_it *) */
2447 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2457 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2458 log_err ("LoadPlugin expects a single string argument.");
2462 plugin = ci->values[0].value.string;
2465 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2469 log_err ("Unable to convert configuration to a Perl hash value.");
2470 config = (HV *)&PL_sv_undef;
2478 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2479 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2483 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2498 } /* static int perl_config_plugin (oconfig_item_it *) */
2500 static int perl_config (oconfig_item_t *ci)
2507 for (i = 0; i < ci->children_num; ++i) {
2508 oconfig_item_t *c = ci->children + i;
2509 int current_status = 0;
2511 if (NULL != perl_threads)
2512 aTHX = PERL_GET_CONTEXT;
2514 if (0 == strcasecmp (c->key, "LoadPlugin"))
2515 current_status = perl_config_loadplugin (aTHX_ c);
2516 else if (0 == strcasecmp (c->key, "BaseName"))
2517 current_status = perl_config_basename (aTHX_ c);
2518 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2519 current_status = perl_config_enabledebugger (aTHX_ c);
2520 else if (0 == strcasecmp (c->key, "IncludeDir"))
2521 current_status = perl_config_includedir (aTHX_ c);
2522 else if (0 == strcasecmp (c->key, "Plugin"))
2523 current_status = perl_config_plugin (aTHX_ c);
2526 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2530 /* fatal error - it's up to perl_config_* to clean up */
2531 if (0 > current_status) {
2532 log_err ("Configuration failed with a fatal error - "
2533 "plugin disabled!");
2534 return current_status;
2537 status += current_status;
2540 } /* static int perl_config (oconfig_item_t *) */
2542 void module_register (void)
2545 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2547 /* default options for the Perl interpreter */
2549 perl_argv[1] = "-MCollectd";
2550 perl_argv[2] = "-e";
2552 perl_argv[4] = NULL;
2554 plugin_register_complex_config ("perl", perl_config);
2556 } /* void module_register (void) */
2558 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */