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_write);
106 static XS (Collectd__plugin_flush);
107 static XS (Collectd_plugin_dispatch_notification);
108 static XS (Collectd_plugin_log);
109 static XS (Collectd__fc_register);
110 static XS (Collectd_call_by_name);
116 typedef struct c_ithread_s {
117 /* the thread's Perl interpreter */
118 PerlInterpreter *interp;
120 /* double linked list of threads */
121 struct c_ithread_s *prev;
122 struct c_ithread_s *next;
130 /* some usage stats */
131 int number_of_threads;
132 #endif /* COLLECT_DEBUG */
134 pthread_mutex_t mutex;
137 /* name / user_data for Perl matches / targets */
143 #define PFC_USER_DATA_FREE(data) \
145 sfree ((data)->name); \
146 if (NULL != (data)->user_data) \
147 sv_free ((data)->user_data); \
154 extern char **environ;
160 /* if perl_threads != NULL perl_threads->head must
161 * point to the "base" thread */
162 static c_ithread_list_t *perl_threads = NULL;
164 /* the key used to store each pthread's ithread */
165 static pthread_key_t perl_thr_key;
167 static int perl_argc = 0;
168 static char **perl_argv = NULL;
170 static char base_name[DATA_MAX_NAME_LEN] = "";
177 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
178 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
179 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
180 { "Collectd::_plugin_write", Collectd__plugin_write },
181 { "Collectd::_plugin_flush", Collectd__plugin_flush },
182 { "Collectd::plugin_dispatch_notification",
183 Collectd_plugin_dispatch_notification },
184 { "Collectd::plugin_log", Collectd_plugin_log },
185 { "Collectd::_fc_register", Collectd__fc_register },
186 { "Collectd::call_by_name", Collectd_call_by_name },
195 { "Collectd::TYPE_INIT", PLUGIN_INIT },
196 { "Collectd::TYPE_READ", PLUGIN_READ },
197 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
198 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
199 { "Collectd::TYPE_LOG", PLUGIN_LOG },
200 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
201 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
202 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
203 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
204 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
205 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
206 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
207 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
208 { "Collectd::LOG_ERR", LOG_ERR },
209 { "Collectd::LOG_WARNING", LOG_WARNING },
210 { "Collectd::LOG_NOTICE", LOG_NOTICE },
211 { "Collectd::LOG_INFO", LOG_INFO },
212 { "Collectd::LOG_DEBUG", LOG_DEBUG },
213 { "Collectd::FC_MATCH", FC_MATCH },
214 { "Collectd::FC_TARGET", FC_TARGET },
215 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
216 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
217 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
218 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
219 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
220 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
221 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
222 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
223 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
224 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
225 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
234 { "Collectd::hostname_g", hostname_g },
243 { "Collectd::interval_g", &interval_g },
248 * Helper functions for data type conversion.
263 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
267 if ((NULL == hash) || (NULL == ds))
270 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
271 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
274 log_err ("hv2data_source: No DS name given.");
278 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
279 ds->type = SvIV (*tmp);
281 if ((DS_TYPE_COUNTER != ds->type)
282 && (DS_TYPE_GAUGE != ds->type)
283 && (DS_TYPE_DERIVE != ds->type)
284 && (DS_TYPE_ABSOLUTE != ds->type)) {
285 log_err ("hv2data_source: Invalid DS type.");
290 ds->type = DS_TYPE_COUNTER;
293 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
294 ds->min = SvNV (*tmp);
298 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
299 ds->max = SvNV (*tmp);
303 } /* static int hv2data_source (HV *, data_source_t *) */
305 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
307 const data_set_t *ds;
311 if ((NULL == name) || (NULL == array) || (NULL == value))
314 if (av_len (array) < len - 1)
315 len = av_len (array) + 1;
320 ds = plugin_get_ds (name);
322 log_err ("av2value: Unknown dataset \"%s\"", name);
326 if (ds->ds_num < len) {
327 log_warn ("av2value: Value length exceeds data set length.");
331 for (i = 0; i < len; ++i) {
332 SV **tmp = av_fetch (array, i, 0);
335 if (DS_TYPE_COUNTER == ds->ds[i].type)
336 value[i].counter = SvIV (*tmp);
337 else if (DS_TYPE_GAUGE == ds->ds[i].type)
338 value[i].gauge = SvNV (*tmp);
339 else if (DS_TYPE_DERIVE == ds->ds[i].type)
340 value[i].derive = SvIV (*tmp);
341 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
342 value[i].absolute = SvIV (*tmp);
349 } /* static int av2value (char *, AV *, value_t *, int) */
354 * values => [ @values ],
358 * plugin_instance => $pinstance,
359 * type_instance => $tinstance,
362 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
366 if ((NULL == hash) || (NULL == vl))
369 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
370 log_err ("hv2value_list: No type given.");
374 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
376 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
377 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
378 log_err ("hv2value_list: No valid values given.");
383 AV *array = (AV *)SvRV (*tmp);
384 int len = av_len (array) + 1;
389 vl->values = (value_t *)smalloc (len * sizeof (value_t));
390 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
393 if (-1 == vl->values_len) {
399 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
401 double t = SvNV (*tmp);
402 vl->time = DOUBLE_TO_CDTIME_T (t);
405 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
406 vl->interval = SvIV (*tmp);
408 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
409 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
411 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
413 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
414 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
416 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
417 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
418 sizeof (vl->plugin_instance));
420 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
421 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
422 sizeof (vl->type_instance));
424 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
426 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
430 if ((NULL == array) || (NULL == name) || (NULL == ds))
433 len = av_len (array);
436 log_err ("av2data_set: Invalid data set.");
440 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
441 ds->ds_num = len + 1;
443 for (i = 0; i <= len; ++i) {
444 SV **elem = av_fetch (array, i, 0);
447 log_err ("av2data_set: Failed to fetch data source %i.", i);
451 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
452 log_err ("av2data_set: Invalid data source.");
456 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
459 log_debug ("av2data_set: "
460 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
461 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
464 sstrncpy (ds->type, name, sizeof (ds->type));
466 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
471 * severity => $severity,
477 * plugin_instance => $instance,
478 * type_instance => $type_instance,
479 * meta => [ { name => <name>, value => <value> }, ... ]
482 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
484 notification_meta_t **m = meta;
486 int len = av_len (array);
489 for (i = 0; i <= len; ++i) {
490 SV **tmp = av_fetch (array, i, 0);
496 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
497 log_warn ("av2notification_meta: Skipping invalid "
498 "meta information.");
502 hash = (HV *)SvRV (*tmp);
504 *m = (notification_meta_t *)smalloc (sizeof (**m));
506 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
507 log_warn ("av2notification_meta: Skipping invalid "
508 "meta information.");
512 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
514 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
515 log_warn ("av2notification_meta: Skipping invalid "
516 "meta information.");
523 (*m)->nm_value.nm_double = SvNVX (*tmp);
524 (*m)->type = NM_TYPE_DOUBLE;
526 else if (SvUOK (*tmp)) {
527 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
528 (*m)->type = NM_TYPE_UNSIGNED_INT;
530 else if (SvIOK (*tmp)) {
531 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
532 (*m)->type = NM_TYPE_SIGNED_INT;
535 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
536 (*m)->type = NM_TYPE_STRING;
543 } /* static int av2notification_meta (AV *, notification_meta_t *) */
545 static int hv2notification (pTHX_ HV *hash, notification_t *n)
549 if ((NULL == hash) || (NULL == n))
552 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
553 n->severity = SvIV (*tmp);
555 n->severity = NOTIF_FAILURE;
557 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
559 double t = SvNV (*tmp);
560 n->time = DOUBLE_TO_CDTIME_T (t);
565 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
566 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
568 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
569 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
571 sstrncpy (n->host, hostname_g, sizeof (n->host));
573 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
574 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
576 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
577 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
578 sizeof (n->plugin_instance));
580 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
581 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
583 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
584 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
585 sizeof (n->type_instance));
588 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
589 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
590 log_warn ("hv2notification: Ignoring invalid meta information.");
594 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
595 plugin_notification_meta_free (n->meta);
602 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
604 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
608 if ((NULL == ds) || (NULL == array))
611 av_extend (array, ds->ds_num);
613 for (i = 0; i < ds->ds_num; ++i) {
614 HV *source = newHV ();
616 if (NULL == hv_store (source, "name", 4,
617 newSVpv (ds->ds[i].name, 0), 0))
620 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
623 if (! isnan (ds->ds[i].min))
624 if (NULL == hv_store (source, "min", 3,
625 newSVnv (ds->ds[i].min), 0))
628 if (! isnan (ds->ds[i].max))
629 if (NULL == hv_store (source, "max", 3,
630 newSVnv (ds->ds[i].max), 0))
633 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
637 } /* static int data_set2av (data_set_t *, AV *) */
639 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
646 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
649 len = vl->values_len;
651 if (ds->ds_num < len) {
652 log_warn ("value2av: Value length exceeds data set length.");
657 av_extend (values, len - 1);
659 for (i = 0; i < len; ++i) {
662 if (DS_TYPE_COUNTER == ds->ds[i].type)
663 val = newSViv (vl->values[i].counter);
664 else if (DS_TYPE_GAUGE == ds->ds[i].type)
665 val = newSVnv (vl->values[i].gauge);
666 else if (DS_TYPE_DERIVE == ds->ds[i].type)
667 val = newSViv (vl->values[i].derive);
668 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
669 val = newSViv (vl->values[i].absolute);
671 if (NULL == av_store (values, i, val)) {
677 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
682 double t = CDTIME_T_TO_DOUBLE (vl->time);
683 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
687 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
690 if ('\0' != vl->host[0])
691 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
694 if ('\0' != vl->plugin[0])
695 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
698 if ('\0' != vl->plugin_instance[0])
699 if (NULL == hv_store (hash, "plugin_instance", 15,
700 newSVpv (vl->plugin_instance, 0), 0))
703 if ('\0' != vl->type[0])
704 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
707 if ('\0' != vl->type_instance[0])
708 if (NULL == hv_store (hash, "type_instance", 13,
709 newSVpv (vl->type_instance, 0), 0))
712 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
714 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
724 av_extend (array, meta_num);
726 for (i = 0; NULL != meta; meta = meta->next, ++i) {
730 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
733 if (NM_TYPE_STRING == meta->type)
734 value = newSVpv (meta->nm_value.nm_string, 0);
735 else if (NM_TYPE_SIGNED_INT == meta->type)
736 value = newSViv (meta->nm_value.nm_signed_int);
737 else if (NM_TYPE_UNSIGNED_INT == meta->type)
738 value = newSVuv (meta->nm_value.nm_unsigned_int);
739 else if (NM_TYPE_DOUBLE == meta->type)
740 value = newSVnv (meta->nm_value.nm_double);
741 else if (NM_TYPE_BOOLEAN == meta->type)
742 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
746 if (NULL == hv_store (m, "value", 5, value, 0)) {
751 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
758 } /* static int notification_meta2av (notification_meta_t *, AV *) */
760 static int notification2hv (pTHX_ notification_t *n, HV *hash)
762 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
767 double t = CDTIME_T_TO_DOUBLE (n->time);
768 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
772 if ('\0' != *n->message)
773 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
776 if ('\0' != *n->host)
777 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
780 if ('\0' != *n->plugin)
781 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
784 if ('\0' != *n->plugin_instance)
785 if (NULL == hv_store (hash, "plugin_instance", 15,
786 newSVpv (n->plugin_instance, 0), 0))
789 if ('\0' != *n->type)
790 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
793 if ('\0' != *n->type_instance)
794 if (NULL == hv_store (hash, "type_instance", 13,
795 newSVpv (n->type_instance, 0), 0))
798 if (NULL != n->meta) {
800 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
801 || (NULL == hv_store (hash, "meta", 4,
802 newRV_noinc ((SV *)meta), 0))) {
809 } /* static int notification2hv (notification_t *, HV *) */
811 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
818 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
822 if (0 < ci->values_num)
823 av_extend (values, ci->values_num);
825 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
831 for (i = 0; i < ci->values_num; ++i) {
834 switch (ci->values[i].type) {
835 case OCONFIG_TYPE_STRING:
836 value = newSVpv (ci->values[i].value.string, 0);
838 case OCONFIG_TYPE_NUMBER:
839 value = newSVnv ((NV)ci->values[i].value.number);
841 case OCONFIG_TYPE_BOOLEAN:
842 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
845 log_err ("oconfig_item2hv: Invalid value type %i.",
847 value = &PL_sv_undef;
850 if (NULL == av_store (values, i, value)) {
856 /* ignoring 'parent' member which is uninteresting in this case */
859 if (0 < ci->children_num)
860 av_extend (children, ci->children_num);
862 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
868 for (i = 0; i < ci->children_num; ++i) {
869 HV *child = newHV ();
871 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
877 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
884 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
887 * Internal functions.
890 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
892 if (base_name[0] == '\0')
893 status = ssnprintf (buf, buf_len, "%s", module);
895 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
896 if ((status < 0) || ((unsigned int)status >= buf_len))
899 } /* char *get_module_name */
902 * Add a plugin's data set definition.
904 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
910 if ((NULL == name) || (NULL == dataset))
913 if (0 != av2data_set (aTHX_ dataset, name, &ds))
916 ret = plugin_register_data_set (&ds);
920 } /* static int pplugin_register_data_set (char *, SV *) */
923 * Remove a plugin's data set definition.
925 static int pplugin_unregister_data_set (char *name)
929 return plugin_unregister_data_set (name);
930 } /* static int pplugin_unregister_data_set (char *) */
933 * Submit the values to the write functions.
935 static int pplugin_dispatch_values (pTHX_ HV *values)
937 value_list_t vl = VALUE_LIST_INIT;
944 if (0 != hv2value_list (aTHX_ values, &vl))
947 ret = plugin_dispatch_values (&vl);
951 } /* static int pplugin_dispatch_values (char *, HV *) */
954 * Submit the values to a single write function.
956 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
959 value_list_t vl = VALUE_LIST_INIT;
966 if (0 != hv2value_list (aTHX_ values, &vl))
969 if ((NULL != data_set)
970 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
973 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
975 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
976 NULL == plugin ? "<any>" : plugin, ret);
978 if (NULL != data_set)
982 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
985 * Dispatch a notification.
987 static int pplugin_dispatch_notification (pTHX_ HV *notif)
996 memset (&n, 0, sizeof (n));
998 if (0 != hv2notification (aTHX_ notif, &n))
1001 ret = plugin_dispatch_notification (&n);
1002 plugin_notification_meta_free (n.meta);
1004 } /* static int pplugin_dispatch_notification (HV *) */
1007 * Call all working functions of the given type.
1009 static int pplugin_call_all (pTHX_ int type, ...)
1018 if ((type < 0) || (type >= PLUGIN_TYPES))
1021 va_start (ap, type);
1028 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1030 if (PLUGIN_WRITE == type) {
1032 * $_[0] = $plugin_type;
1047 * values => [ $v1, ... ],
1049 * host => $hostname,
1050 * plugin => $plugin,
1052 * plugin_instance => $instance,
1053 * type_instance => $type_instance
1062 ds = va_arg (ap, data_set_t *);
1063 vl = va_arg (ap, value_list_t *);
1065 if (-1 == data_set2av (aTHX_ ds, pds)) {
1068 pds = (AV *)&PL_sv_undef;
1072 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1075 pvl = (HV *)&PL_sv_undef;
1079 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1080 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1081 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1083 else if (PLUGIN_LOG == type) {
1089 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1090 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1092 else if (PLUGIN_NOTIF == type) {
1096 * severity => $severity,
1100 * plugin => $plugin,
1102 * plugin_instance => $instance,
1103 * type_instance => $type_instance
1107 HV *notif = newHV ();
1109 n = va_arg (ap, notification_t *);
1111 if (-1 == notification2hv (aTHX_ n, notif)) {
1114 notif = (HV *)&PL_sv_undef;
1118 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1120 else if (PLUGIN_FLUSH == type) {
1125 * $_[1] = $identifier;
1127 timeout = va_arg (ap, cdtime_t);
1129 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1130 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1135 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1150 } /* static int pplugin_call_all (int, ...) */
1153 * collectd's perl interpreter based thread implementation.
1155 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1158 /* must be called with perl_threads->mutex locked */
1159 static void c_ithread_destroy (c_ithread_t *ithread)
1161 dTHXa (ithread->interp);
1163 assert (NULL != perl_threads);
1165 PERL_SET_CONTEXT (aTHX);
1166 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1171 --perl_threads->number_of_threads;
1172 #endif /* COLLECT_DEBUG */
1174 perl_destruct (aTHX);
1177 if (NULL == ithread->prev)
1178 perl_threads->head = ithread->next;
1180 ithread->prev->next = ithread->next;
1182 if (NULL == ithread->next)
1183 perl_threads->tail = ithread->prev;
1185 ithread->next->prev = ithread->prev;
1189 } /* static void c_ithread_destroy (c_ithread_t *) */
1191 static void c_ithread_destructor (void *arg)
1193 c_ithread_t *ithread = (c_ithread_t *)arg;
1194 c_ithread_t *t = NULL;
1196 if (NULL == perl_threads)
1199 pthread_mutex_lock (&perl_threads->mutex);
1201 for (t = perl_threads->head; NULL != t; t = t->next)
1205 /* the ithread no longer exists */
1209 c_ithread_destroy (ithread);
1211 pthread_mutex_unlock (&perl_threads->mutex);
1213 } /* static void c_ithread_destructor (void *) */
1215 /* must be called with perl_threads->mutex locked */
1216 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1218 c_ithread_t *t = NULL;
1221 assert (NULL != perl_threads);
1223 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1224 memset (t, 0, sizeof (c_ithread_t));
1226 t->interp = (NULL == base)
1228 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1232 if ((NULL != base) && (NULL != PL_endav)) {
1233 av_clear (PL_endav);
1234 av_undef (PL_endav);
1239 ++perl_threads->number_of_threads;
1240 #endif /* COLLECT_DEBUG */
1244 if (NULL == perl_threads->tail) {
1245 perl_threads->head = t;
1249 perl_threads->tail->next = t;
1250 t->prev = perl_threads->tail;
1253 perl_threads->tail = t;
1255 pthread_setspecific (perl_thr_key, (const void *)t);
1257 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1260 * Filter chains implementation.
1263 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1270 notification_meta_t **meta = NULL;
1275 if ((type < 0) || (type >= FC_TYPES))
1278 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1281 va_start (ap, data);
1288 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1289 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1290 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1292 if (FC_CB_CREATE == cb_type) {
1295 * $_[1] = $user_data;
1298 HV *config = newHV ();
1300 ci = va_arg (ap, oconfig_item_t *);
1302 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1305 config = (HV *)&PL_sv_undef;
1309 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1311 else if (FC_CB_DESTROY == cb_type) {
1313 * $_[1] = $user_data;
1316 /* nothing to be done - the user data pointer
1317 * is pushed onto the stack later */
1319 else if (FC_CB_EXEC == cb_type) {
1324 * $_[3] = $user_data;
1332 ds = va_arg (ap, data_set_t *);
1333 vl = va_arg (ap, value_list_t *);
1334 meta = va_arg (ap, notification_meta_t **);
1336 if (0 != data_set2av (aTHX_ ds, pds)) {
1339 pds = (AV *)&PL_sv_undef;
1343 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1346 pvl = (HV *)&PL_sv_undef;
1353 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1356 pmeta = (AV *)&PL_sv_undef;
1361 pmeta = (AV *)&PL_sv_undef;
1364 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1365 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1366 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1369 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1373 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1375 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1376 assert (pmeta != NULL);
1378 plugin_notification_meta_free (*meta);
1379 av2notification_meta (aTHX_ pmeta, meta);
1386 /* the exec callbacks return a status, while
1387 * the others return a boolean value */
1388 if (FC_CB_EXEC == cb_type)
1390 else if (! SvTRUE (tmp))
1400 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1402 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1404 pfc_user_data_t *data;
1410 if (NULL == perl_threads)
1414 c_ithread_t *t = NULL;
1416 pthread_mutex_lock (&perl_threads->mutex);
1417 t = c_ithread_create (perl_threads->head->interp);
1418 pthread_mutex_unlock (&perl_threads->mutex);
1423 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1424 aTHX, perl_threads->number_of_threads);
1426 if ((1 != ci->values_num)
1427 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1428 log_warn ("A \"%s\" block expects a single string argument.",
1429 (FC_MATCH == type) ? "Match" : "Target");
1433 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1434 data->name = sstrdup (ci->values[0].value.string);
1435 data->user_data = newSV (0);
1437 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1440 PFC_USER_DATA_FREE (data);
1444 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1446 static int fc_destroy (int type, void **user_data)
1448 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1454 if ((NULL == perl_threads) || (NULL == data))
1458 c_ithread_t *t = NULL;
1460 pthread_mutex_lock (&perl_threads->mutex);
1461 t = c_ithread_create (perl_threads->head->interp);
1462 pthread_mutex_unlock (&perl_threads->mutex);
1467 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1468 aTHX, perl_threads->number_of_threads);
1470 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1472 PFC_USER_DATA_FREE (data);
1475 } /* static int fc_destroy (int, void **) */
1477 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1478 notification_meta_t **meta, void **user_data)
1480 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1484 if (NULL == perl_threads)
1487 assert (NULL != data);
1490 c_ithread_t *t = NULL;
1492 pthread_mutex_lock (&perl_threads->mutex);
1493 t = c_ithread_create (perl_threads->head->interp);
1494 pthread_mutex_unlock (&perl_threads->mutex);
1499 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1500 aTHX, perl_threads->number_of_threads);
1502 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1503 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1504 notification_meta_t **, void **) */
1506 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1508 return fc_create (FC_MATCH, ci, user_data);
1509 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1511 static int pmatch_destroy (void **user_data)
1513 return fc_destroy (FC_MATCH, user_data);
1514 } /* static int pmatch_destroy (void **) */
1516 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1517 notification_meta_t **meta, void **user_data)
1519 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1520 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1521 notification_meta_t **, void **) */
1523 static match_proc_t pmatch = {
1524 pmatch_create, pmatch_destroy, pmatch_match
1527 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1529 return fc_create (FC_TARGET, ci, user_data);
1530 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1532 static int ptarget_destroy (void **user_data)
1534 return fc_destroy (FC_TARGET, user_data);
1535 } /* static int ptarget_destroy (void **) */
1537 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1538 notification_meta_t **meta, void **user_data)
1540 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1541 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1542 notification_meta_t **, void **) */
1544 static target_proc_t ptarget = {
1545 ptarget_create, ptarget_destroy, ptarget_invoke
1549 * Exported Perl API.
1553 * Collectd::plugin_register_data_set (type, dataset).
1556 * type of the dataset
1559 * dataset to be registered
1561 static XS (Collectd_plugin_register_ds)
1568 log_warn ("Using plugin_register() to register new data-sets is "
1569 "deprecated - add new entries to a custom types.db instead.");
1572 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1576 log_debug ("Collectd::plugin_register_data_set: "
1577 "type = \"%s\", dataset = \"%s\"",
1578 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1582 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1583 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1587 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1595 } /* static XS (Collectd_plugin_register_ds) */
1598 * Collectd::plugin_unregister_data_set (type).
1601 * type of the dataset
1603 static XS (Collectd_plugin_unregister_ds)
1608 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1612 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1613 SvPV_nolen (ST (0)));
1615 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1619 } /* static XS (Collectd_plugin_register_ds) */
1622 * Collectd::plugin_dispatch_values (name, values).
1625 * name of the plugin
1628 * value list to submit
1630 static XS (Collectd_plugin_dispatch_values)
1639 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1643 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1644 SvPV_nolen (ST (/* stack index = */ 0)));
1646 values = ST (/* stack index = */ 0);
1648 /* Make sure the argument is a hash reference. */
1649 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1650 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1657 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1663 } /* static XS (Collectd_plugin_dispatch_values) */
1665 /* Collectd::plugin_write (plugin, ds, vl).
1668 * name of the plugin to call, may be 'undef'
1671 * data-set that describes the submitted values, may be 'undef'
1674 * value-list to be written
1676 static XS (Collectd__plugin_write)
1687 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1691 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1692 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1693 SvPV_nolen (ST (2)));
1695 if (! SvOK (ST (0)))
1698 plugin = SvPV_nolen (ST (0));
1701 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1702 ds_array = (AV *)SvRV (ds);
1703 else if (! SvOK (ds))
1706 log_err ("Collectd::plugin_write: Invalid data-set.");
1711 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1712 log_err ("Collectd::plugin_write: Invalid value-list.");
1716 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1722 } /* static XS (Collectd__plugin_write) */
1725 * Collectd::_plugin_flush (plugin, timeout, identifier).
1728 * name of the plugin to flush
1731 * timeout to use when flushing the data
1734 * data-set identifier to flush
1736 static XS (Collectd__plugin_flush)
1738 char *plugin = NULL;
1745 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1750 plugin = SvPV_nolen (ST (0));
1753 timeout = (int)SvIV (ST (1));
1756 id = SvPV_nolen (ST (2));
1758 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1759 "id = \"%s\"", plugin, timeout, id);
1761 if (0 == plugin_flush (plugin, timeout, id))
1765 } /* static XS (Collectd__plugin_flush) */
1768 * Collectd::plugin_dispatch_notification (notif).
1771 * notification to dispatch
1773 static XS (Collectd_plugin_dispatch_notification)
1782 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1786 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1787 SvPV_nolen (ST (0)));
1791 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1792 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1796 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1802 } /* static XS (Collectd_plugin_dispatch_notification) */
1805 * Collectd::plugin_log (level, message).
1808 * log level (LOG_DEBUG, ... LOG_ERR)
1813 static XS (Collectd_plugin_log)
1818 log_err ("Usage: Collectd::plugin_log(level, message)");
1822 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1824 } /* static XS (Collectd_plugin_log) */
1827 * Collectd::_fc_register (type, name)
1835 static XS (Collectd__fc_register)
1845 log_err ("Usage: Collectd::_fc_register(type, name)");
1849 type = SvIV (ST (0));
1850 name = SvPV_nolen (ST (1));
1852 if (FC_MATCH == type)
1853 ret = fc_register_match (name, pmatch);
1854 else if (FC_TARGET == type)
1855 ret = fc_register_target (name, ptarget);
1861 } /* static XS (Collectd_fc_register) */
1864 * Collectd::call_by_name (...).
1866 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1868 static XS (Collectd_call_by_name)
1873 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1874 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1879 name = SvPV_nolen (tmp);
1881 if (NULL == get_cv (name, 0)) {
1882 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1887 /* simply pass on the subroutine call without touching the stack,
1888 * thus leaving any arguments and return values in place */
1890 } /* static XS (Collectd_call_by_name) */
1893 * Interface to collectd.
1896 static int perl_init (void)
1900 if (NULL == perl_threads)
1904 c_ithread_t *t = NULL;
1906 pthread_mutex_lock (&perl_threads->mutex);
1907 t = c_ithread_create (perl_threads->head->interp);
1908 pthread_mutex_unlock (&perl_threads->mutex);
1913 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1914 aTHX, perl_threads->number_of_threads);
1915 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1916 } /* static int perl_init (void) */
1918 static int perl_read (void)
1922 if (NULL == perl_threads)
1926 c_ithread_t *t = NULL;
1928 pthread_mutex_lock (&perl_threads->mutex);
1929 t = c_ithread_create (perl_threads->head->interp);
1930 pthread_mutex_unlock (&perl_threads->mutex);
1935 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1936 aTHX, perl_threads->number_of_threads);
1937 return pplugin_call_all (aTHX_ PLUGIN_READ);
1938 } /* static int perl_read (void) */
1940 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1941 user_data_t __attribute__((unused)) *user_data)
1945 if (NULL == perl_threads)
1949 c_ithread_t *t = NULL;
1951 pthread_mutex_lock (&perl_threads->mutex);
1952 t = c_ithread_create (perl_threads->head->interp);
1953 pthread_mutex_unlock (&perl_threads->mutex);
1958 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1959 aTHX, perl_threads->number_of_threads);
1960 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1961 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1963 static void perl_log (int level, const char *msg,
1964 user_data_t __attribute__((unused)) *user_data)
1968 if (NULL == perl_threads)
1972 c_ithread_t *t = NULL;
1974 pthread_mutex_lock (&perl_threads->mutex);
1975 t = c_ithread_create (perl_threads->head->interp);
1976 pthread_mutex_unlock (&perl_threads->mutex);
1981 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1983 } /* static void perl_log (int, const char *) */
1985 static int perl_notify (const notification_t *notif,
1986 user_data_t __attribute__((unused)) *user_data)
1990 if (NULL == perl_threads)
1994 c_ithread_t *t = NULL;
1996 pthread_mutex_lock (&perl_threads->mutex);
1997 t = c_ithread_create (perl_threads->head->interp);
1998 pthread_mutex_unlock (&perl_threads->mutex);
2002 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2003 } /* static int perl_notify (const notification_t *) */
2005 static int perl_flush (cdtime_t timeout, const char *identifier,
2006 user_data_t __attribute__((unused)) *user_data)
2010 if (NULL == perl_threads)
2014 c_ithread_t *t = NULL;
2016 pthread_mutex_lock (&perl_threads->mutex);
2017 t = c_ithread_create (perl_threads->head->interp);
2018 pthread_mutex_unlock (&perl_threads->mutex);
2022 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2023 } /* static int perl_flush (const int) */
2025 static int perl_shutdown (void)
2027 c_ithread_t *t = NULL;
2033 plugin_unregister_complex_config ("perl");
2035 if (NULL == perl_threads)
2039 c_ithread_t *t = NULL;
2041 pthread_mutex_lock (&perl_threads->mutex);
2042 t = c_ithread_create (perl_threads->head->interp);
2043 pthread_mutex_unlock (&perl_threads->mutex);
2048 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2049 aTHX, perl_threads->number_of_threads);
2051 plugin_unregister_log ("perl");
2052 plugin_unregister_notification ("perl");
2053 plugin_unregister_init ("perl");
2054 plugin_unregister_read ("perl");
2055 plugin_unregister_write ("perl");
2056 plugin_unregister_flush ("perl");
2058 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2060 pthread_mutex_lock (&perl_threads->mutex);
2061 t = perl_threads->tail;
2064 c_ithread_t *thr = t;
2066 /* the pointer has to be advanced before destroying
2067 * the thread as this will free the memory */
2070 c_ithread_destroy (thr);
2073 pthread_mutex_unlock (&perl_threads->mutex);
2074 pthread_mutex_destroy (&perl_threads->mutex);
2076 sfree (perl_threads);
2078 pthread_key_delete (perl_thr_key);
2082 plugin_unregister_shutdown ("perl");
2084 } /* static void perl_shutdown (void) */
2087 * Access functions for global variables.
2089 * These functions implement the "magic" used to access
2090 * the global variables from Perl.
2093 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2095 char *pv = mg->mg_ptr;
2098 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2100 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2102 char *pv = mg->mg_ptr;
2103 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2105 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2107 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2109 int *iv = (int *)mg->mg_ptr;
2110 sv_setiv (var, *iv);
2112 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2114 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2116 int *iv = (int *)mg->mg_ptr;
2117 *iv = (int)SvIV (var);
2119 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2121 static MGVTBL g_pv_vtbl = {
2122 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2123 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2127 static MGVTBL g_iv_vtbl = {
2128 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2129 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2134 /* bootstrap the Collectd module */
2135 static void xs_init (pTHX)
2139 char *file = __FILE__;
2145 /* enable usage of Perl modules using shared libraries */
2146 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2149 for (i = 0; NULL != api[i].f; ++i)
2150 newXS (api[i].name, api[i].f, file);
2152 stash = gv_stashpv ("Collectd", 1);
2154 /* export "constants" */
2155 for (i = 0; '\0' != constants[i].name[0]; ++i)
2156 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2158 /* export global variables
2159 * by adding "magic" to the SV's representing the globale variables
2160 * perl is able to automagically call the get/set function when
2161 * accessing any such variable (this is basically the same as using
2163 /* global strings */
2164 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2165 tmp = get_sv (g_strings[i].name, 1);
2166 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2167 g_strings[i].var, 0);
2170 /* global integers */
2171 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2172 tmp = get_sv (g_integers[i].name, 1);
2173 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2174 (char *)g_integers[i].var, 0);
2177 } /* static void xs_init (pTHX) */
2179 /* Initialize the global Perl interpreter. */
2180 static int init_pi (int argc, char **argv)
2184 if (NULL != perl_threads)
2187 log_info ("Initializing Perl interpreter...");
2192 for (i = 0; i < argc; ++i)
2193 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2195 #endif /* COLLECT_DEBUG */
2197 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2198 log_err ("init_pi: pthread_key_create failed");
2200 /* this must not happen - cowardly giving up if it does */
2205 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2206 * triggers a "value computed is not used" warning by gcc. */
2209 PERL_SYS_INIT3 (&argc, &argv, &environ);
2211 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2212 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2214 pthread_mutex_init (&perl_threads->mutex, NULL);
2215 /* locking the mutex should not be necessary at this point
2216 * but let's just do it for the sake of completeness */
2217 pthread_mutex_lock (&perl_threads->mutex);
2219 perl_threads->head = c_ithread_create (NULL);
2220 perl_threads->tail = perl_threads->head;
2222 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2223 log_err ("init_pi: Not enough memory.");
2227 aTHX = perl_threads->head->interp;
2228 pthread_mutex_unlock (&perl_threads->mutex);
2230 perl_construct (aTHX);
2232 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2234 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2235 SV *err = get_sv ("@", 1);
2236 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2239 perl_destruct (perl_threads->head->interp);
2240 perl_free (perl_threads->head->interp);
2241 sfree (perl_threads);
2243 pthread_key_delete (perl_thr_key);
2247 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2248 sv_setpv (get_sv ("0", 0), "collectd");
2252 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2253 plugin_register_notification ("perl", perl_notify,
2254 /* user_data = */ NULL);
2255 plugin_register_init ("perl", perl_init);
2257 plugin_register_read ("perl", perl_read);
2259 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2260 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2261 plugin_register_shutdown ("perl", perl_shutdown);
2263 } /* static int init_pi (const char **, const int) */
2266 * LoadPlugin "<Plugin>"
2268 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2270 char module_name[DATA_MAX_NAME_LEN];
2274 if ((0 != ci->children_num) || (1 != ci->values_num)
2275 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2276 log_err ("LoadPlugin expects a single string argument.");
2280 value = ci->values[0].value.string;
2282 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2283 log_err ("Invalid module name %s", value);
2287 if (0 != init_pi (perl_argc, perl_argv))
2290 assert (NULL != perl_threads);
2291 assert (NULL != perl_threads->head);
2293 aTHX = perl_threads->head->interp;
2295 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2296 load_module (PERL_LOADMOD_NOIMPORT,
2297 newSVpv (module_name, strlen (module_name)), Nullsv);
2299 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2304 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2308 if ((0 != ci->children_num) || (1 != ci->values_num)
2309 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2310 log_err ("BaseName expects a single string argument.");
2314 value = ci->values[0].value.string;
2316 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2317 sstrncpy (base_name, value, sizeof (base_name));
2319 } /* static int perl_config_basename (oconfig_item_it *) */
2322 * EnableDebugger "<Package>"|""
2324 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2328 if ((0 != ci->children_num) || (1 != ci->values_num)
2329 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2330 log_err ("EnableDebugger expects a single string argument.");
2334 if (NULL != perl_threads) {
2335 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2339 value = ci->values[0].value.string;
2341 perl_argv = (char **)realloc (perl_argv,
2342 (++perl_argc + 1) * sizeof (char *));
2344 if (NULL == perl_argv) {
2345 log_err ("perl_config: Not enough memory.");
2349 if ('\0' == value[0]) {
2350 perl_argv[perl_argc - 1] = "-d";
2353 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2354 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2355 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2358 perl_argv[perl_argc] = NULL;
2360 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2363 * IncludeDir "<Dir>"
2365 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2369 if ((0 != ci->children_num) || (1 != ci->values_num)
2370 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2371 log_err ("IncludeDir expects a single string argument.");
2375 value = ci->values[0].value.string;
2378 perl_argv = (char **)realloc (perl_argv,
2379 (++perl_argc + 1) * sizeof (char *));
2381 if (NULL == perl_argv) {
2382 log_err ("perl_config: Not enough memory.");
2386 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2387 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2388 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2390 perl_argv[perl_argc] = NULL;
2393 /* prepend the directory to @INC */
2394 av_unshift (GvAVn (PL_incgv), 1);
2395 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2398 } /* static int perl_config_includedir (oconfig_item_it *) */
2403 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2413 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2414 log_err ("LoadPlugin expects a single string argument.");
2418 plugin = ci->values[0].value.string;
2421 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2425 log_err ("Unable to convert configuration to a Perl hash value.");
2426 config = (HV *)&PL_sv_undef;
2434 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2435 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2439 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2454 } /* static int perl_config_plugin (oconfig_item_it *) */
2456 static int perl_config (oconfig_item_t *ci)
2463 for (i = 0; i < ci->children_num; ++i) {
2464 oconfig_item_t *c = ci->children + i;
2465 int current_status = 0;
2467 if (NULL != perl_threads)
2468 aTHX = PERL_GET_CONTEXT;
2470 if (0 == strcasecmp (c->key, "LoadPlugin"))
2471 current_status = perl_config_loadplugin (aTHX_ c);
2472 else if (0 == strcasecmp (c->key, "BaseName"))
2473 current_status = perl_config_basename (aTHX_ c);
2474 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2475 current_status = perl_config_enabledebugger (aTHX_ c);
2476 else if (0 == strcasecmp (c->key, "IncludeDir"))
2477 current_status = perl_config_includedir (aTHX_ c);
2478 else if (0 == strcasecmp (c->key, "Plugin"))
2479 current_status = perl_config_plugin (aTHX_ c);
2482 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2486 /* fatal error - it's up to perl_config_* to clean up */
2487 if (0 > current_status) {
2488 log_err ("Configuration failed with a fatal error - "
2489 "plugin disabled!");
2490 return current_status;
2493 status += current_status;
2496 } /* static int perl_config (oconfig_item_t *) */
2498 void module_register (void)
2501 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2503 /* default options for the Perl interpreter */
2505 perl_argv[1] = "-MCollectd";
2506 perl_argv[2] = "-e";
2508 perl_argv[4] = NULL;
2510 plugin_register_complex_config ("perl", perl_config);
2512 } /* void module_register (void) */
2514 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */