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"
39 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
40 # pragma GCC poison sprintf
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
50 /* ... while we want the definition found in plugin.h. */
54 #include "filter_chain.h"
58 #if !defined(USE_ITHREADS)
59 # error "Perl does not support ithreads!"
60 #endif /* !defined(USE_ITHREADS) */
62 /* clear the Perl sub's stack frame
63 * (this should only be used inside an XSUB) */
64 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
68 #define PLUGIN_WRITE 2
69 #define PLUGIN_SHUTDOWN 3
71 #define PLUGIN_NOTIF 5
72 #define PLUGIN_FLUSH 6
74 #define PLUGIN_TYPES 7
76 #define PLUGIN_CONFIG 254
77 #define PLUGIN_DATASET 255
84 #define FC_CB_CREATE 0
85 #define FC_CB_DESTROY 1
90 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
91 #define log_info(...) INFO ("perl: " __VA_ARGS__)
92 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
93 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
95 /* this is defined in DynaLoader.a */
96 void boot_DynaLoader (PerlInterpreter *, CV *);
98 static XS (Collectd_plugin_register_ds);
99 static XS (Collectd_plugin_unregister_ds);
100 static XS (Collectd_plugin_dispatch_values);
101 static XS (Collectd__plugin_write);
102 static XS (Collectd__plugin_flush);
103 static XS (Collectd_plugin_dispatch_notification);
104 static XS (Collectd_plugin_log);
105 static XS (Collectd__fc_register);
106 static XS (Collectd_call_by_name);
112 typedef struct c_ithread_s {
113 /* the thread's Perl interpreter */
114 PerlInterpreter *interp;
116 /* double linked list of threads */
117 struct c_ithread_s *prev;
118 struct c_ithread_s *next;
126 /* some usage stats */
127 int number_of_threads;
128 #endif /* COLLECT_DEBUG */
130 pthread_mutex_t mutex;
133 /* name / user_data for Perl matches / targets */
139 #define PFC_USER_DATA_FREE(data) \
141 sfree ((data)->name); \
142 if (NULL != (data)->user_data) \
143 sv_free ((data)->user_data); \
151 /* if perl_threads != NULL perl_threads->head must
152 * point to the "base" thread */
153 static c_ithread_list_t *perl_threads = NULL;
155 /* the key used to store each pthread's ithread */
156 static pthread_key_t perl_thr_key;
158 static int perl_argc = 0;
159 static char **perl_argv = NULL;
161 static char base_name[DATA_MAX_NAME_LEN] = "";
168 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
169 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
170 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
171 { "Collectd::_plugin_write", Collectd__plugin_write },
172 { "Collectd::_plugin_flush", Collectd__plugin_flush },
173 { "Collectd::plugin_dispatch_notification",
174 Collectd_plugin_dispatch_notification },
175 { "Collectd::plugin_log", Collectd_plugin_log },
176 { "Collectd::_fc_register", Collectd__fc_register },
177 { "Collectd::call_by_name", Collectd_call_by_name },
186 { "Collectd::TYPE_INIT", PLUGIN_INIT },
187 { "Collectd::TYPE_READ", PLUGIN_READ },
188 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
189 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
190 { "Collectd::TYPE_LOG", PLUGIN_LOG },
191 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
192 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
193 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
194 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
195 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
196 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
197 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
198 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
199 { "Collectd::LOG_ERR", LOG_ERR },
200 { "Collectd::LOG_WARNING", LOG_WARNING },
201 { "Collectd::LOG_NOTICE", LOG_NOTICE },
202 { "Collectd::LOG_INFO", LOG_INFO },
203 { "Collectd::LOG_DEBUG", LOG_DEBUG },
204 { "Collectd::FC_MATCH", FC_MATCH },
205 { "Collectd::FC_TARGET", FC_TARGET },
206 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
207 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
208 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
209 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
210 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
211 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
212 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
213 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
214 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
215 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
216 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
225 { "Collectd::hostname_g", hostname_g },
234 { "Collectd::interval_g", &interval_g },
239 * Helper functions for data type conversion.
254 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
258 if ((NULL == hash) || (NULL == ds))
261 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
262 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
265 log_err ("hv2data_source: No DS name given.");
269 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
270 ds->type = SvIV (*tmp);
272 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type) && (DS_TYPE_DERIVE != ds->type) && (DS_TYPE_ABSOLUTE != ds->type)) {
273 log_err ("hv2data_source: Invalid DS type.");
278 ds->type = DS_TYPE_COUNTER;
281 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
282 ds->min = SvNV (*tmp);
286 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
287 ds->max = SvNV (*tmp);
291 } /* static int hv2data_source (HV *, data_source_t *) */
293 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
295 const data_set_t *ds;
299 if ((NULL == name) || (NULL == array) || (NULL == value))
302 if (av_len (array) < len - 1)
303 len = av_len (array) + 1;
308 ds = plugin_get_ds (name);
310 log_err ("av2value: Unknown dataset \"%s\"", name);
314 if (ds->ds_num < len) {
315 log_warn ("av2value: Value length exceeds data set length.");
319 for (i = 0; i < len; ++i) {
320 SV **tmp = av_fetch (array, i, 0);
323 if (DS_TYPE_COUNTER == ds->ds[i].type)
324 value[i].counter = SvIV (*tmp);
325 else if (DS_TYPE_GAUGE == ds->ds[i].type)
326 value[i].gauge = SvNV (*tmp);
327 else if (DS_TYPE_DERIVE == ds->ds[i].type)
328 value[i].derive = SvIV (*tmp);
329 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
330 value[i].absolute = SvIV (*tmp);
337 } /* static int av2value (char *, AV *, value_t *, int) */
342 * values => [ @values ],
346 * plugin_instance => $pinstance,
347 * type_instance => $tinstance,
350 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
354 if ((NULL == hash) || (NULL == vl))
357 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
358 log_err ("hv2value_list: No type given.");
362 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
364 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
365 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
366 log_err ("hv2value_list: No valid values given.");
371 AV *array = (AV *)SvRV (*tmp);
372 int len = av_len (array) + 1;
377 vl->values = (value_t *)smalloc (len * sizeof (value_t));
378 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
381 if (-1 == vl->values_len) {
387 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
388 vl->time = (time_t)SvIV (*tmp);
390 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
391 vl->interval = SvIV (*tmp);
393 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
394 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
396 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
398 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
399 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
401 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
402 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
403 sizeof (vl->plugin_instance));
405 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
406 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
407 sizeof (vl->type_instance));
409 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
411 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
415 if ((NULL == array) || (NULL == name) || (NULL == ds))
418 len = av_len (array);
421 log_err ("av2data_set: Invalid data set.");
425 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
426 ds->ds_num = len + 1;
428 for (i = 0; i <= len; ++i) {
429 SV **elem = av_fetch (array, i, 0);
432 log_err ("av2data_set: Failed to fetch data source %i.", i);
436 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
437 log_err ("av2data_set: Invalid data source.");
441 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
444 log_debug ("av2data_set: "
445 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
446 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
449 sstrncpy (ds->type, name, sizeof (ds->type));
451 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
456 * severity => $severity,
462 * plugin_instance => $instance,
463 * type_instance => $type_instance,
464 * meta => [ { name => <name>, value => <value> }, ... ]
467 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
469 notification_meta_t **m = meta;
471 int len = av_len (array);
474 for (i = 0; i <= len; ++i) {
475 SV **tmp = av_fetch (array, i, 0);
481 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
482 log_warn ("av2notification_meta: Skipping invalid "
483 "meta information.");
487 hash = (HV *)SvRV (*tmp);
489 *m = (notification_meta_t *)smalloc (sizeof (**m));
491 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
492 log_warn ("av2notification_meta: Skipping invalid "
493 "meta information.");
497 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
499 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
500 log_warn ("av2notification_meta: Skipping invalid "
501 "meta information.");
508 (*m)->nm_value.nm_double = SvNVX (*tmp);
509 (*m)->type = NM_TYPE_DOUBLE;
511 else if (SvUOK (*tmp)) {
512 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
513 (*m)->type = NM_TYPE_UNSIGNED_INT;
515 else if (SvIOK (*tmp)) {
516 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
517 (*m)->type = NM_TYPE_SIGNED_INT;
520 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
521 (*m)->type = NM_TYPE_STRING;
528 } /* static int av2notification_meta (AV *, notification_meta_t *) */
530 static int hv2notification (pTHX_ HV *hash, notification_t *n)
534 if ((NULL == hash) || (NULL == n))
537 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
538 n->severity = SvIV (*tmp);
540 n->severity = NOTIF_FAILURE;
542 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
543 n->time = (time_t)SvIV (*tmp);
545 n->time = time (NULL);
547 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
548 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
550 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
551 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
553 sstrncpy (n->host, hostname_g, sizeof (n->host));
555 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
556 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
558 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
559 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
560 sizeof (n->plugin_instance));
562 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
563 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
565 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
566 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
567 sizeof (n->type_instance));
570 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
571 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
572 log_warn ("hv2notification: Ignoring invalid meta information.");
576 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
577 plugin_notification_meta_free (n->meta);
584 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
586 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
590 if ((NULL == ds) || (NULL == array))
593 av_extend (array, ds->ds_num);
595 for (i = 0; i < ds->ds_num; ++i) {
596 HV *source = newHV ();
598 if (NULL == hv_store (source, "name", 4,
599 newSVpv (ds->ds[i].name, 0), 0))
602 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
605 if (! isnan (ds->ds[i].min))
606 if (NULL == hv_store (source, "min", 3,
607 newSVnv (ds->ds[i].min), 0))
610 if (! isnan (ds->ds[i].max))
611 if (NULL == hv_store (source, "max", 3,
612 newSVnv (ds->ds[i].max), 0))
615 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
619 } /* static int data_set2av (data_set_t *, AV *) */
621 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
628 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
631 len = vl->values_len;
633 if (ds->ds_num < len) {
634 log_warn ("value2av: Value length exceeds data set length.");
639 av_extend (values, len - 1);
641 for (i = 0; i < len; ++i) {
644 if (DS_TYPE_COUNTER == ds->ds[i].type)
645 val = newSViv (vl->values[i].counter);
646 else if (DS_TYPE_GAUGE == ds->ds[i].type)
647 val = newSVnv (vl->values[i].gauge);
648 else if (DS_TYPE_DERIVE == ds->ds[i].type)
649 val = newSVnv (vl->values[i].derive);
650 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
651 val = newSVnv (vl->values[i].absolute);
653 if (NULL == av_store (values, i, val)) {
659 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
663 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
666 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
669 if ('\0' != vl->host[0])
670 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
673 if ('\0' != vl->plugin[0])
674 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
677 if ('\0' != vl->plugin_instance[0])
678 if (NULL == hv_store (hash, "plugin_instance", 15,
679 newSVpv (vl->plugin_instance, 0), 0))
682 if ('\0' != vl->type[0])
683 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
686 if ('\0' != vl->type_instance[0])
687 if (NULL == hv_store (hash, "type_instance", 13,
688 newSVpv (vl->type_instance, 0), 0))
691 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
693 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
703 av_extend (array, meta_num);
705 for (i = 0; NULL != meta; meta = meta->next, ++i) {
709 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
712 if (NM_TYPE_STRING == meta->type)
713 value = newSVpv (meta->nm_value.nm_string, 0);
714 else if (NM_TYPE_SIGNED_INT == meta->type)
715 value = newSViv (meta->nm_value.nm_signed_int);
716 else if (NM_TYPE_UNSIGNED_INT == meta->type)
717 value = newSVuv (meta->nm_value.nm_unsigned_int);
718 else if (NM_TYPE_DOUBLE == meta->type)
719 value = newSVnv (meta->nm_value.nm_double);
720 else if (NM_TYPE_BOOLEAN == meta->type)
721 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
725 if (NULL == hv_store (m, "value", 5, value, 0)) {
730 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
737 } /* static int notification_meta2av (notification_meta_t *, AV *) */
739 static int notification2hv (pTHX_ notification_t *n, HV *hash)
741 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
745 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
748 if ('\0' != *n->message)
749 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
752 if ('\0' != *n->host)
753 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
756 if ('\0' != *n->plugin)
757 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
760 if ('\0' != *n->plugin_instance)
761 if (NULL == hv_store (hash, "plugin_instance", 15,
762 newSVpv (n->plugin_instance, 0), 0))
765 if ('\0' != *n->type)
766 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
769 if ('\0' != *n->type_instance)
770 if (NULL == hv_store (hash, "type_instance", 13,
771 newSVpv (n->type_instance, 0), 0))
774 if (NULL != n->meta) {
776 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
777 || (NULL == hv_store (hash, "meta", 4,
778 newRV_noinc ((SV *)meta), 0))) {
785 } /* static int notification2hv (notification_t *, HV *) */
787 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
794 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
798 if (0 < ci->values_num)
799 av_extend (values, ci->values_num);
801 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
807 for (i = 0; i < ci->values_num; ++i) {
810 switch (ci->values[i].type) {
811 case OCONFIG_TYPE_STRING:
812 value = newSVpv (ci->values[i].value.string, 0);
814 case OCONFIG_TYPE_NUMBER:
815 value = newSVnv ((NV)ci->values[i].value.number);
817 case OCONFIG_TYPE_BOOLEAN:
818 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
821 log_err ("oconfig_item2hv: Invalid value type %i.",
823 value = &PL_sv_undef;
826 if (NULL == av_store (values, i, value)) {
832 /* ignoring 'parent' member which is uninteresting in this case */
835 if (0 < ci->children_num)
836 av_extend (children, ci->children_num);
838 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
844 for (i = 0; i < ci->children_num; ++i) {
845 HV *child = newHV ();
847 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
853 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
860 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
863 * Internal functions.
866 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
868 if (base_name[0] == '\0')
869 status = ssnprintf (buf, buf_len, "%s", module);
871 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
872 if ((status < 0) || ((unsigned int)status >= buf_len))
875 } /* char *get_module_name */
878 * Add a plugin's data set definition.
880 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
886 if ((NULL == name) || (NULL == dataset))
889 if (0 != av2data_set (aTHX_ dataset, name, &ds))
892 ret = plugin_register_data_set (&ds);
896 } /* static int pplugin_register_data_set (char *, SV *) */
899 * Remove a plugin's data set definition.
901 static int pplugin_unregister_data_set (char *name)
905 return plugin_unregister_data_set (name);
906 } /* static int pplugin_unregister_data_set (char *) */
909 * Submit the values to the write functions.
911 static int pplugin_dispatch_values (pTHX_ HV *values)
913 value_list_t vl = VALUE_LIST_INIT;
920 if (0 != hv2value_list (aTHX_ values, &vl))
923 ret = plugin_dispatch_values (&vl);
927 } /* static int pplugin_dispatch_values (char *, HV *) */
930 * Submit the values to a single write function.
932 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
935 value_list_t vl = VALUE_LIST_INIT;
942 if (0 != hv2value_list (aTHX_ values, &vl))
945 if ((NULL != data_set)
946 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
949 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
951 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
952 NULL == plugin ? "<any>" : plugin, ret);
954 if (NULL != data_set)
958 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
961 * Dispatch a notification.
963 static int pplugin_dispatch_notification (pTHX_ HV *notif)
972 memset (&n, 0, sizeof (n));
974 if (0 != hv2notification (aTHX_ notif, &n))
977 ret = plugin_dispatch_notification (&n);
978 plugin_notification_meta_free (n.meta);
980 } /* static int pplugin_dispatch_notification (HV *) */
983 * Call all working functions of the given type.
985 static int pplugin_call_all (pTHX_ int type, ...)
994 if ((type < 0) || (type >= PLUGIN_TYPES))
1004 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1006 if (PLUGIN_WRITE == type) {
1008 * $_[0] = $plugin_type;
1023 * values => [ $v1, ... ],
1025 * host => $hostname,
1026 * plugin => $plugin,
1028 * plugin_instance => $instance,
1029 * type_instance => $type_instance
1038 ds = va_arg (ap, data_set_t *);
1039 vl = va_arg (ap, value_list_t *);
1041 if (-1 == data_set2av (aTHX_ ds, pds)) {
1044 pds = (AV *)&PL_sv_undef;
1048 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1051 pvl = (HV *)&PL_sv_undef;
1055 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1056 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1057 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1059 else if (PLUGIN_LOG == type) {
1065 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1066 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1068 else if (PLUGIN_NOTIF == type) {
1072 * severity => $severity,
1076 * plugin => $plugin,
1078 * plugin_instance => $instance,
1079 * type_instance => $type_instance
1083 HV *notif = newHV ();
1085 n = va_arg (ap, notification_t *);
1087 if (-1 == notification2hv (aTHX_ n, notif)) {
1090 notif = (HV *)&PL_sv_undef;
1094 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1096 else if (PLUGIN_FLUSH == type) {
1099 * $_[1] = $identifier;
1101 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1102 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1107 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1122 } /* static int pplugin_call_all (int, ...) */
1125 * collectd's perl interpreter based thread implementation.
1127 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1130 /* must be called with perl_threads->mutex locked */
1131 static void c_ithread_destroy (c_ithread_t *ithread)
1133 dTHXa (ithread->interp);
1135 assert (NULL != perl_threads);
1137 PERL_SET_CONTEXT (aTHX);
1138 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1143 --perl_threads->number_of_threads;
1144 #endif /* COLLECT_DEBUG */
1146 perl_destruct (aTHX);
1149 if (NULL == ithread->prev)
1150 perl_threads->head = ithread->next;
1152 ithread->prev->next = ithread->next;
1154 if (NULL == ithread->next)
1155 perl_threads->tail = ithread->prev;
1157 ithread->next->prev = ithread->prev;
1161 } /* static void c_ithread_destroy (c_ithread_t *) */
1163 static void c_ithread_destructor (void *arg)
1165 c_ithread_t *ithread = (c_ithread_t *)arg;
1166 c_ithread_t *t = NULL;
1168 if (NULL == perl_threads)
1171 pthread_mutex_lock (&perl_threads->mutex);
1173 for (t = perl_threads->head; NULL != t; t = t->next)
1177 /* the ithread no longer exists */
1181 c_ithread_destroy (ithread);
1183 pthread_mutex_unlock (&perl_threads->mutex);
1185 } /* static void c_ithread_destructor (void *) */
1187 /* must be called with perl_threads->mutex locked */
1188 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1190 c_ithread_t *t = NULL;
1193 assert (NULL != perl_threads);
1195 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1196 memset (t, 0, sizeof (c_ithread_t));
1198 t->interp = (NULL == base)
1200 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1204 if ((NULL != base) && (NULL != PL_endav)) {
1205 av_clear (PL_endav);
1206 av_undef (PL_endav);
1211 ++perl_threads->number_of_threads;
1212 #endif /* COLLECT_DEBUG */
1216 if (NULL == perl_threads->tail) {
1217 perl_threads->head = t;
1221 perl_threads->tail->next = t;
1222 t->prev = perl_threads->tail;
1225 perl_threads->tail = t;
1227 pthread_setspecific (perl_thr_key, (const void *)t);
1229 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1232 * Filter chains implementation.
1235 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1242 notification_meta_t **meta = NULL;
1247 if ((type < 0) || (type >= FC_TYPES))
1250 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1253 va_start (ap, data);
1260 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1261 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1262 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1264 if (FC_CB_CREATE == cb_type) {
1267 * $_[1] = $user_data;
1270 HV *config = newHV ();
1272 ci = va_arg (ap, oconfig_item_t *);
1274 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1277 config = (HV *)&PL_sv_undef;
1281 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1283 else if (FC_CB_DESTROY == cb_type) {
1285 * $_[1] = $user_data;
1288 /* nothing to be done - the user data pointer
1289 * is pushed onto the stack later */
1291 else if (FC_CB_EXEC == cb_type) {
1296 * $_[3] = $user_data;
1304 ds = va_arg (ap, data_set_t *);
1305 vl = va_arg (ap, value_list_t *);
1306 meta = va_arg (ap, notification_meta_t **);
1308 if (0 != data_set2av (aTHX_ ds, pds)) {
1311 pds = (AV *)&PL_sv_undef;
1315 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1318 pvl = (HV *)&PL_sv_undef;
1325 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1328 pmeta = (AV *)&PL_sv_undef;
1333 pmeta = (AV *)&PL_sv_undef;
1336 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1337 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1338 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1341 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1345 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1347 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1348 assert (pmeta != NULL);
1350 plugin_notification_meta_free (*meta);
1351 av2notification_meta (aTHX_ pmeta, meta);
1358 /* the exec callbacks return a status, while
1359 * the others return a boolean value */
1360 if (FC_CB_EXEC == cb_type)
1362 else if (! SvTRUE (tmp))
1372 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1374 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1376 pfc_user_data_t *data;
1382 if (NULL == perl_threads)
1386 c_ithread_t *t = NULL;
1388 pthread_mutex_lock (&perl_threads->mutex);
1389 t = c_ithread_create (perl_threads->head->interp);
1390 pthread_mutex_unlock (&perl_threads->mutex);
1395 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1396 aTHX, perl_threads->number_of_threads);
1398 if ((1 != ci->values_num)
1399 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1400 log_warn ("A \"%s\" block expects a single string argument.",
1401 (FC_MATCH == type) ? "Match" : "Target");
1405 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1406 data->name = sstrdup (ci->values[0].value.string);
1407 data->user_data = newSV (0);
1409 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1412 PFC_USER_DATA_FREE (data);
1416 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1418 static int fc_destroy (int type, void **user_data)
1420 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1426 if ((NULL == perl_threads) || (NULL == data))
1430 c_ithread_t *t = NULL;
1432 pthread_mutex_lock (&perl_threads->mutex);
1433 t = c_ithread_create (perl_threads->head->interp);
1434 pthread_mutex_unlock (&perl_threads->mutex);
1439 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1440 aTHX, perl_threads->number_of_threads);
1442 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1444 PFC_USER_DATA_FREE (data);
1447 } /* static int fc_destroy (int, void **) */
1449 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1450 notification_meta_t **meta, void **user_data)
1452 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1456 if (NULL == perl_threads)
1459 assert (NULL != data);
1462 c_ithread_t *t = NULL;
1464 pthread_mutex_lock (&perl_threads->mutex);
1465 t = c_ithread_create (perl_threads->head->interp);
1466 pthread_mutex_unlock (&perl_threads->mutex);
1471 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1472 aTHX, perl_threads->number_of_threads);
1474 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1475 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1476 notification_meta_t **, void **) */
1478 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1480 return fc_create (FC_MATCH, ci, user_data);
1481 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1483 static int pmatch_destroy (void **user_data)
1485 return fc_destroy (FC_MATCH, user_data);
1486 } /* static int pmatch_destroy (void **) */
1488 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1489 notification_meta_t **meta, void **user_data)
1491 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1492 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1493 notification_meta_t **, void **) */
1495 static match_proc_t pmatch = {
1496 pmatch_create, pmatch_destroy, pmatch_match
1499 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1501 return fc_create (FC_TARGET, ci, user_data);
1502 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1504 static int ptarget_destroy (void **user_data)
1506 return fc_destroy (FC_TARGET, user_data);
1507 } /* static int ptarget_destroy (void **) */
1509 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1510 notification_meta_t **meta, void **user_data)
1512 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1513 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1514 notification_meta_t **, void **) */
1516 static target_proc_t ptarget = {
1517 ptarget_create, ptarget_destroy, ptarget_invoke
1521 * Exported Perl API.
1525 * Collectd::plugin_register_data_set (type, dataset).
1528 * type of the dataset
1531 * dataset to be registered
1533 static XS (Collectd_plugin_register_ds)
1540 log_warn ("Using plugin_register() to register new data-sets is "
1541 "deprecated - add new entries to a custom types.db instead.");
1544 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1548 log_debug ("Collectd::plugin_register_data_set: "
1549 "type = \"%s\", dataset = \"%s\"",
1550 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1554 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1555 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1559 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1567 } /* static XS (Collectd_plugin_register_ds) */
1570 * Collectd::plugin_unregister_data_set (type).
1573 * type of the dataset
1575 static XS (Collectd_plugin_unregister_ds)
1580 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1584 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1585 SvPV_nolen (ST (0)));
1587 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1591 } /* static XS (Collectd_plugin_register_ds) */
1594 * Collectd::plugin_dispatch_values (name, values).
1597 * name of the plugin
1600 * value list to submit
1602 static XS (Collectd_plugin_dispatch_values)
1612 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1613 "is deprecated - pass the type through values->{type}.");
1616 else if (1 != items) {
1617 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1621 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1622 SvPV_nolen (ST (values_idx)));
1624 values = ST (values_idx);
1626 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1627 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1631 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1634 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1635 newSVsv (ST (0)), 0))) {
1636 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1640 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1646 } /* static XS (Collectd_plugin_dispatch_values) */
1648 /* Collectd::plugin_write (plugin, ds, vl).
1651 * name of the plugin to call, may be 'undef'
1654 * data-set that describes the submitted values, may be 'undef'
1657 * value-list to be written
1659 static XS (Collectd__plugin_write)
1670 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1674 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1675 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1676 SvPV_nolen (ST (2)));
1678 if (! SvOK (ST (0)))
1681 plugin = SvPV_nolen (ST (0));
1684 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1685 ds_array = (AV *)SvRV (ds);
1686 else if (! SvOK (ds))
1689 log_err ("Collectd::plugin_write: Invalid data-set.");
1694 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1695 log_err ("Collectd::plugin_write: Invalid value-list.");
1699 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1705 } /* static XS (Collectd__plugin_write) */
1708 * Collectd::_plugin_flush (plugin, timeout, identifier).
1711 * name of the plugin to flush
1714 * timeout to use when flushing the data
1717 * data-set identifier to flush
1719 static XS (Collectd__plugin_flush)
1721 char *plugin = NULL;
1728 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1733 plugin = SvPV_nolen (ST (0));
1736 timeout = (int)SvIV (ST (1));
1739 id = SvPV_nolen (ST (2));
1741 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1742 "id = \"%s\"", plugin, timeout, id);
1744 if (0 == plugin_flush (plugin, timeout, id))
1748 } /* static XS (Collectd__plugin_flush) */
1751 * Collectd::plugin_dispatch_notification (notif).
1754 * notification to dispatch
1756 static XS (Collectd_plugin_dispatch_notification)
1765 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1769 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1770 SvPV_nolen (ST (0)));
1774 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1775 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1779 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1785 } /* static XS (Collectd_plugin_dispatch_notification) */
1788 * Collectd::plugin_log (level, message).
1791 * log level (LOG_DEBUG, ... LOG_ERR)
1796 static XS (Collectd_plugin_log)
1801 log_err ("Usage: Collectd::plugin_log(level, message)");
1805 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1807 } /* static XS (Collectd_plugin_log) */
1810 * Collectd::_fc_register (type, name)
1818 static XS (Collectd__fc_register)
1828 log_err ("Usage: Collectd::_fc_register(type, name)");
1832 type = SvIV (ST (0));
1833 name = SvPV_nolen (ST (1));
1835 if (FC_MATCH == type)
1836 ret = fc_register_match (name, pmatch);
1837 else if (FC_TARGET == type)
1838 ret = fc_register_target (name, ptarget);
1844 } /* static XS (Collectd_fc_register) */
1847 * Collectd::call_by_name (...).
1849 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1851 static XS (Collectd_call_by_name)
1856 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1857 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1862 name = SvPV_nolen (tmp);
1864 if (NULL == get_cv (name, 0)) {
1865 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1870 /* simply pass on the subroutine call without touching the stack,
1871 * thus leaving any arguments and return values in place */
1873 } /* static XS (Collectd_call_by_name) */
1876 * Interface to collectd.
1879 static int perl_init (void)
1883 if (NULL == perl_threads)
1887 c_ithread_t *t = NULL;
1889 pthread_mutex_lock (&perl_threads->mutex);
1890 t = c_ithread_create (perl_threads->head->interp);
1891 pthread_mutex_unlock (&perl_threads->mutex);
1896 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1897 aTHX, perl_threads->number_of_threads);
1898 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1899 } /* static int perl_init (void) */
1901 static int perl_read (void)
1905 if (NULL == perl_threads)
1909 c_ithread_t *t = NULL;
1911 pthread_mutex_lock (&perl_threads->mutex);
1912 t = c_ithread_create (perl_threads->head->interp);
1913 pthread_mutex_unlock (&perl_threads->mutex);
1918 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1919 aTHX, perl_threads->number_of_threads);
1920 return pplugin_call_all (aTHX_ PLUGIN_READ);
1921 } /* static int perl_read (void) */
1923 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1924 user_data_t __attribute__((unused)) *user_data)
1928 if (NULL == perl_threads)
1932 c_ithread_t *t = NULL;
1934 pthread_mutex_lock (&perl_threads->mutex);
1935 t = c_ithread_create (perl_threads->head->interp);
1936 pthread_mutex_unlock (&perl_threads->mutex);
1941 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1942 aTHX, perl_threads->number_of_threads);
1943 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1944 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1946 static void perl_log (int level, const char *msg,
1947 user_data_t __attribute__((unused)) *user_data)
1951 if (NULL == perl_threads)
1955 c_ithread_t *t = NULL;
1957 pthread_mutex_lock (&perl_threads->mutex);
1958 t = c_ithread_create (perl_threads->head->interp);
1959 pthread_mutex_unlock (&perl_threads->mutex);
1964 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1966 } /* static void perl_log (int, const char *) */
1968 static int perl_notify (const notification_t *notif,
1969 user_data_t __attribute__((unused)) *user_data)
1973 if (NULL == perl_threads)
1977 c_ithread_t *t = NULL;
1979 pthread_mutex_lock (&perl_threads->mutex);
1980 t = c_ithread_create (perl_threads->head->interp);
1981 pthread_mutex_unlock (&perl_threads->mutex);
1985 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1986 } /* static int perl_notify (const notification_t *) */
1988 static int perl_flush (int timeout, const char *identifier,
1989 user_data_t __attribute__((unused)) *user_data)
1993 if (NULL == perl_threads)
1997 c_ithread_t *t = NULL;
1999 pthread_mutex_lock (&perl_threads->mutex);
2000 t = c_ithread_create (perl_threads->head->interp);
2001 pthread_mutex_unlock (&perl_threads->mutex);
2005 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2006 } /* static int perl_flush (const int) */
2008 static int perl_shutdown (void)
2010 c_ithread_t *t = NULL;
2016 plugin_unregister_complex_config ("perl");
2018 if (NULL == perl_threads)
2022 c_ithread_t *t = NULL;
2024 pthread_mutex_lock (&perl_threads->mutex);
2025 t = c_ithread_create (perl_threads->head->interp);
2026 pthread_mutex_unlock (&perl_threads->mutex);
2031 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2032 aTHX, perl_threads->number_of_threads);
2034 plugin_unregister_log ("perl");
2035 plugin_unregister_notification ("perl");
2036 plugin_unregister_init ("perl");
2037 plugin_unregister_read ("perl");
2038 plugin_unregister_write ("perl");
2039 plugin_unregister_flush ("perl");
2041 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2043 pthread_mutex_lock (&perl_threads->mutex);
2044 t = perl_threads->tail;
2047 c_ithread_t *thr = t;
2049 /* the pointer has to be advanced before destroying
2050 * the thread as this will free the memory */
2053 c_ithread_destroy (thr);
2056 pthread_mutex_unlock (&perl_threads->mutex);
2057 pthread_mutex_destroy (&perl_threads->mutex);
2059 sfree (perl_threads);
2061 pthread_key_delete (perl_thr_key);
2065 plugin_unregister_shutdown ("perl");
2067 } /* static void perl_shutdown (void) */
2070 * Access functions for global variables.
2072 * These functions implement the "magic" used to access
2073 * the global variables from Perl.
2076 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2078 char *pv = mg->mg_ptr;
2081 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2083 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2085 char *pv = mg->mg_ptr;
2086 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2088 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2090 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2092 int *iv = (int *)mg->mg_ptr;
2093 sv_setiv (var, *iv);
2095 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2097 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2099 int *iv = (int *)mg->mg_ptr;
2100 *iv = (int)SvIV (var);
2102 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2104 static MGVTBL g_pv_vtbl = {
2105 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2106 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2110 static MGVTBL g_iv_vtbl = {
2111 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2112 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2117 /* bootstrap the Collectd module */
2118 static void xs_init (pTHX)
2122 char *file = __FILE__;
2128 /* enable usage of Perl modules using shared libraries */
2129 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2132 for (i = 0; NULL != api[i].f; ++i)
2133 newXS (api[i].name, api[i].f, file);
2135 stash = gv_stashpv ("Collectd", 1);
2137 /* export "constants" */
2138 for (i = 0; '\0' != constants[i].name[0]; ++i)
2139 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2141 /* export global variables
2142 * by adding "magic" to the SV's representing the globale variables
2143 * perl is able to automagically call the get/set function when
2144 * accessing any such variable (this is basically the same as using
2146 /* global strings */
2147 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2148 tmp = get_sv (g_strings[i].name, 1);
2149 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2150 g_strings[i].var, 0);
2153 /* global integers */
2154 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2155 tmp = get_sv (g_integers[i].name, 1);
2156 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2157 (char *)g_integers[i].var, 0);
2160 } /* static void xs_init (pTHX) */
2162 /* Initialize the global Perl interpreter. */
2163 static int init_pi (int argc, char **argv)
2167 if (NULL != perl_threads)
2170 log_info ("Initializing Perl interpreter...");
2175 for (i = 0; i < argc; ++i)
2176 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2178 #endif /* COLLECT_DEBUG */
2180 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2181 log_err ("init_pi: pthread_key_create failed");
2183 /* this must not happen - cowardly giving up if it does */
2188 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2189 * triggers a "value computed is not used" warning by gcc. */
2192 PERL_SYS_INIT3 (&argc, &argv, &environ);
2194 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2195 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2197 pthread_mutex_init (&perl_threads->mutex, NULL);
2198 /* locking the mutex should not be necessary at this point
2199 * but let's just do it for the sake of completeness */
2200 pthread_mutex_lock (&perl_threads->mutex);
2202 perl_threads->head = c_ithread_create (NULL);
2203 perl_threads->tail = perl_threads->head;
2205 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2206 log_err ("init_pi: Not enough memory.");
2210 aTHX = perl_threads->head->interp;
2211 pthread_mutex_unlock (&perl_threads->mutex);
2213 perl_construct (aTHX);
2215 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2217 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2218 SV *err = get_sv ("@", 1);
2219 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2222 perl_destruct (perl_threads->head->interp);
2223 perl_free (perl_threads->head->interp);
2224 sfree (perl_threads);
2226 pthread_key_delete (perl_thr_key);
2230 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2231 sv_setpv (get_sv ("0", 0), "collectd");
2235 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2236 plugin_register_notification ("perl", perl_notify,
2237 /* user_data = */ NULL);
2238 plugin_register_init ("perl", perl_init);
2240 plugin_register_read ("perl", perl_read);
2242 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2243 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2244 plugin_register_shutdown ("perl", perl_shutdown);
2246 } /* static int init_pi (const char **, const int) */
2249 * LoadPlugin "<Plugin>"
2251 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2253 char module_name[DATA_MAX_NAME_LEN];
2257 if ((0 != ci->children_num) || (1 != ci->values_num)
2258 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2259 log_err ("LoadPlugin expects a single string argument.");
2263 value = ci->values[0].value.string;
2265 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2266 log_err ("Invalid module name %s", value);
2270 if (0 != init_pi (perl_argc, perl_argv))
2273 assert (NULL != perl_threads);
2274 assert (NULL != perl_threads->head);
2276 aTHX = perl_threads->head->interp;
2278 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2279 load_module (PERL_LOADMOD_NOIMPORT,
2280 newSVpv (module_name, strlen (module_name)), Nullsv);
2282 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2287 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2291 if ((0 != ci->children_num) || (1 != ci->values_num)
2292 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2293 log_err ("BaseName expects a single string argument.");
2297 value = ci->values[0].value.string;
2299 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2300 sstrncpy (base_name, value, sizeof (base_name));
2302 } /* static int perl_config_basename (oconfig_item_it *) */
2305 * EnableDebugger "<Package>"|""
2307 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2311 if ((0 != ci->children_num) || (1 != ci->values_num)
2312 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2313 log_err ("EnableDebugger expects a single string argument.");
2317 if (NULL != perl_threads) {
2318 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2322 value = ci->values[0].value.string;
2324 perl_argv = (char **)realloc (perl_argv,
2325 (++perl_argc + 1) * sizeof (char *));
2327 if (NULL == perl_argv) {
2328 log_err ("perl_config: Not enough memory.");
2332 if ('\0' == value[0]) {
2333 perl_argv[perl_argc - 1] = "-d";
2336 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2337 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2338 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2341 perl_argv[perl_argc] = NULL;
2343 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2346 * IncludeDir "<Dir>"
2348 static int perl_config_includedir (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 ("IncludeDir expects a single string argument.");
2358 value = ci->values[0].value.string;
2361 perl_argv = (char **)realloc (perl_argv,
2362 (++perl_argc + 1) * sizeof (char *));
2364 if (NULL == perl_argv) {
2365 log_err ("perl_config: Not enough memory.");
2369 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2370 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2371 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2373 perl_argv[perl_argc] = NULL;
2376 /* prepend the directory to @INC */
2377 av_unshift (GvAVn (PL_incgv), 1);
2378 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2381 } /* static int perl_config_includedir (oconfig_item_it *) */
2386 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2396 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2397 log_err ("LoadPlugin expects a single string argument.");
2401 plugin = ci->values[0].value.string;
2404 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2408 log_err ("Unable to convert configuration to a Perl hash value.");
2409 config = (HV *)&PL_sv_undef;
2417 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2418 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2422 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2437 } /* static int perl_config_plugin (oconfig_item_it *) */
2439 static int perl_config (oconfig_item_t *ci)
2446 for (i = 0; i < ci->children_num; ++i) {
2447 oconfig_item_t *c = ci->children + i;
2448 int current_status = 0;
2450 if (NULL != perl_threads)
2451 aTHX = PERL_GET_CONTEXT;
2453 if (0 == strcasecmp (c->key, "LoadPlugin"))
2454 current_status = perl_config_loadplugin (aTHX_ c);
2455 else if (0 == strcasecmp (c->key, "BaseName"))
2456 current_status = perl_config_basename (aTHX_ c);
2457 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2458 current_status = perl_config_enabledebugger (aTHX_ c);
2459 else if (0 == strcasecmp (c->key, "IncludeDir"))
2460 current_status = perl_config_includedir (aTHX_ c);
2461 else if (0 == strcasecmp (c->key, "Plugin"))
2462 current_status = perl_config_plugin (aTHX_ c);
2465 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2469 /* fatal error - it's up to perl_config_* to clean up */
2470 if (0 > current_status) {
2471 log_err ("Configuration failed with a fatal error - "
2472 "plugin disabled!");
2473 return current_status;
2476 status += current_status;
2479 } /* static int perl_config (oconfig_item_t *) */
2481 void module_register (void)
2484 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2486 /* default options for the Perl interpreter */
2488 perl_argv[1] = "-MCollectd";
2489 perl_argv[2] = "-e";
2491 perl_argv[4] = NULL;
2493 plugin_register_complex_config ("perl", perl_config);
2495 } /* void module_register (void) */
2497 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */