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 },
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)
273 && (DS_TYPE_GAUGE != ds->type)
274 && (DS_TYPE_DERIVE != ds->type)
275 && (DS_TYPE_ABSOLUTE != ds->type)) {
276 log_err ("hv2data_source: Invalid DS type.");
281 ds->type = DS_TYPE_COUNTER;
284 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
285 ds->min = SvNV (*tmp);
289 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
290 ds->max = SvNV (*tmp);
294 } /* static int hv2data_source (HV *, data_source_t *) */
296 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
298 const data_set_t *ds;
302 if ((NULL == name) || (NULL == array) || (NULL == value))
305 if (av_len (array) < len - 1)
306 len = av_len (array) + 1;
311 ds = plugin_get_ds (name);
313 log_err ("av2value: Unknown dataset \"%s\"", name);
317 if (ds->ds_num < len) {
318 log_warn ("av2value: Value length exceeds data set length.");
322 for (i = 0; i < len; ++i) {
323 SV **tmp = av_fetch (array, i, 0);
326 if (DS_TYPE_COUNTER == ds->ds[i].type)
327 value[i].counter = SvIV (*tmp);
328 else if (DS_TYPE_GAUGE == ds->ds[i].type)
329 value[i].gauge = SvNV (*tmp);
330 else if (DS_TYPE_DERIVE == ds->ds[i].type)
331 value[i].derive = SvIV (*tmp);
332 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
333 value[i].absolute = SvIV (*tmp);
340 } /* static int av2value (char *, AV *, value_t *, int) */
345 * values => [ @values ],
349 * plugin_instance => $pinstance,
350 * type_instance => $tinstance,
353 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
357 if ((NULL == hash) || (NULL == vl))
360 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
361 log_err ("hv2value_list: No type given.");
365 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
367 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
368 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
369 log_err ("hv2value_list: No valid values given.");
374 AV *array = (AV *)SvRV (*tmp);
375 int len = av_len (array) + 1;
380 vl->values = (value_t *)smalloc (len * sizeof (value_t));
381 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
384 if (-1 == vl->values_len) {
390 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
392 double t = SvNV (*tmp);
393 vl->time = DOUBLE_TO_CDTIME_T (t);
396 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
398 double t = SvNV (*tmp);
399 vl->interval = DOUBLE_TO_CDTIME_T (t);
402 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
403 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
405 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
407 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
408 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
410 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
411 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
412 sizeof (vl->plugin_instance));
414 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
415 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
416 sizeof (vl->type_instance));
418 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
420 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
424 if ((NULL == array) || (NULL == name) || (NULL == ds))
427 len = av_len (array);
430 log_err ("av2data_set: Invalid data set.");
434 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
435 ds->ds_num = len + 1;
437 for (i = 0; i <= len; ++i) {
438 SV **elem = av_fetch (array, i, 0);
441 log_err ("av2data_set: Failed to fetch data source %i.", i);
445 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
446 log_err ("av2data_set: Invalid data source.");
450 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
453 log_debug ("av2data_set: "
454 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
455 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
458 sstrncpy (ds->type, name, sizeof (ds->type));
460 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
465 * severity => $severity,
471 * plugin_instance => $instance,
472 * type_instance => $type_instance,
473 * meta => [ { name => <name>, value => <value> }, ... ]
476 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
478 notification_meta_t **m = meta;
480 int len = av_len (array);
483 for (i = 0; i <= len; ++i) {
484 SV **tmp = av_fetch (array, i, 0);
490 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
491 log_warn ("av2notification_meta: Skipping invalid "
492 "meta information.");
496 hash = (HV *)SvRV (*tmp);
498 *m = (notification_meta_t *)smalloc (sizeof (**m));
500 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
501 log_warn ("av2notification_meta: Skipping invalid "
502 "meta information.");
506 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
508 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
509 log_warn ("av2notification_meta: Skipping invalid "
510 "meta information.");
517 (*m)->nm_value.nm_double = SvNVX (*tmp);
518 (*m)->type = NM_TYPE_DOUBLE;
520 else if (SvUOK (*tmp)) {
521 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
522 (*m)->type = NM_TYPE_UNSIGNED_INT;
524 else if (SvIOK (*tmp)) {
525 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
526 (*m)->type = NM_TYPE_SIGNED_INT;
529 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
530 (*m)->type = NM_TYPE_STRING;
537 } /* static int av2notification_meta (AV *, notification_meta_t *) */
539 static int hv2notification (pTHX_ HV *hash, notification_t *n)
543 if ((NULL == hash) || (NULL == n))
546 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
547 n->severity = SvIV (*tmp);
549 n->severity = NOTIF_FAILURE;
551 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
553 double t = SvNV (*tmp);
554 n->time = DOUBLE_TO_CDTIME_T (t);
559 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
560 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
562 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
563 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
565 sstrncpy (n->host, hostname_g, sizeof (n->host));
567 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
568 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
570 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
571 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
572 sizeof (n->plugin_instance));
574 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
575 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
577 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
578 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
579 sizeof (n->type_instance));
582 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
583 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
584 log_warn ("hv2notification: Ignoring invalid meta information.");
588 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
589 plugin_notification_meta_free (n->meta);
596 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
598 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
602 if ((NULL == ds) || (NULL == array))
605 av_extend (array, ds->ds_num);
607 for (i = 0; i < ds->ds_num; ++i) {
608 HV *source = newHV ();
610 if (NULL == hv_store (source, "name", 4,
611 newSVpv (ds->ds[i].name, 0), 0))
614 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
617 if (! isnan (ds->ds[i].min))
618 if (NULL == hv_store (source, "min", 3,
619 newSVnv (ds->ds[i].min), 0))
622 if (! isnan (ds->ds[i].max))
623 if (NULL == hv_store (source, "max", 3,
624 newSVnv (ds->ds[i].max), 0))
627 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
631 } /* static int data_set2av (data_set_t *, AV *) */
633 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
640 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
643 len = vl->values_len;
645 if (ds->ds_num < len) {
646 log_warn ("value2av: Value length exceeds data set length.");
651 av_extend (values, len - 1);
653 for (i = 0; i < len; ++i) {
656 if (DS_TYPE_COUNTER == ds->ds[i].type)
657 val = newSViv (vl->values[i].counter);
658 else if (DS_TYPE_GAUGE == ds->ds[i].type)
659 val = newSVnv (vl->values[i].gauge);
660 else if (DS_TYPE_DERIVE == ds->ds[i].type)
661 val = newSViv (vl->values[i].derive);
662 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
663 val = newSViv (vl->values[i].absolute);
665 if (NULL == av_store (values, i, val)) {
671 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
676 double t = CDTIME_T_TO_DOUBLE (vl->time);
677 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
682 double t = CDTIME_T_TO_DOUBLE (vl->interval);
683 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
687 if ('\0' != vl->host[0])
688 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
691 if ('\0' != vl->plugin[0])
692 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
695 if ('\0' != vl->plugin_instance[0])
696 if (NULL == hv_store (hash, "plugin_instance", 15,
697 newSVpv (vl->plugin_instance, 0), 0))
700 if ('\0' != vl->type[0])
701 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
704 if ('\0' != vl->type_instance[0])
705 if (NULL == hv_store (hash, "type_instance", 13,
706 newSVpv (vl->type_instance, 0), 0))
709 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
711 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
721 av_extend (array, meta_num);
723 for (i = 0; NULL != meta; meta = meta->next, ++i) {
727 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
730 if (NM_TYPE_STRING == meta->type)
731 value = newSVpv (meta->nm_value.nm_string, 0);
732 else if (NM_TYPE_SIGNED_INT == meta->type)
733 value = newSViv (meta->nm_value.nm_signed_int);
734 else if (NM_TYPE_UNSIGNED_INT == meta->type)
735 value = newSVuv (meta->nm_value.nm_unsigned_int);
736 else if (NM_TYPE_DOUBLE == meta->type)
737 value = newSVnv (meta->nm_value.nm_double);
738 else if (NM_TYPE_BOOLEAN == meta->type)
739 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
743 if (NULL == hv_store (m, "value", 5, value, 0)) {
748 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
755 } /* static int notification_meta2av (notification_meta_t *, AV *) */
757 static int notification2hv (pTHX_ notification_t *n, HV *hash)
759 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
764 double t = CDTIME_T_TO_DOUBLE (n->time);
765 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
769 if ('\0' != *n->message)
770 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
773 if ('\0' != *n->host)
774 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
777 if ('\0' != *n->plugin)
778 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
781 if ('\0' != *n->plugin_instance)
782 if (NULL == hv_store (hash, "plugin_instance", 15,
783 newSVpv (n->plugin_instance, 0), 0))
786 if ('\0' != *n->type)
787 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
790 if ('\0' != *n->type_instance)
791 if (NULL == hv_store (hash, "type_instance", 13,
792 newSVpv (n->type_instance, 0), 0))
795 if (NULL != n->meta) {
797 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
798 || (NULL == hv_store (hash, "meta", 4,
799 newRV_noinc ((SV *)meta), 0))) {
806 } /* static int notification2hv (notification_t *, HV *) */
808 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
815 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
819 if (0 < ci->values_num)
820 av_extend (values, ci->values_num);
822 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
828 for (i = 0; i < ci->values_num; ++i) {
831 switch (ci->values[i].type) {
832 case OCONFIG_TYPE_STRING:
833 value = newSVpv (ci->values[i].value.string, 0);
835 case OCONFIG_TYPE_NUMBER:
836 value = newSVnv ((NV)ci->values[i].value.number);
838 case OCONFIG_TYPE_BOOLEAN:
839 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
842 log_err ("oconfig_item2hv: Invalid value type %i.",
844 value = &PL_sv_undef;
847 if (NULL == av_store (values, i, value)) {
853 /* ignoring 'parent' member which is uninteresting in this case */
856 if (0 < ci->children_num)
857 av_extend (children, ci->children_num);
859 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
865 for (i = 0; i < ci->children_num; ++i) {
866 HV *child = newHV ();
868 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
874 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
881 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
884 * Internal functions.
887 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
889 if (base_name[0] == '\0')
890 status = ssnprintf (buf, buf_len, "%s", module);
892 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
893 if ((status < 0) || ((unsigned int)status >= buf_len))
896 } /* char *get_module_name */
899 * Add a plugin's data set definition.
901 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
907 if ((NULL == name) || (NULL == dataset))
910 if (0 != av2data_set (aTHX_ dataset, name, &ds))
913 ret = plugin_register_data_set (&ds);
917 } /* static int pplugin_register_data_set (char *, SV *) */
920 * Remove a plugin's data set definition.
922 static int pplugin_unregister_data_set (char *name)
926 return plugin_unregister_data_set (name);
927 } /* static int pplugin_unregister_data_set (char *) */
930 * Submit the values to the write functions.
932 static int pplugin_dispatch_values (pTHX_ HV *values)
934 value_list_t vl = VALUE_LIST_INIT;
941 if (0 != hv2value_list (aTHX_ values, &vl))
944 ret = plugin_dispatch_values (&vl);
948 } /* static int pplugin_dispatch_values (char *, HV *) */
951 * Submit the values to a single write function.
953 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
956 value_list_t vl = VALUE_LIST_INIT;
963 if (0 != hv2value_list (aTHX_ values, &vl))
966 if ((NULL != data_set)
967 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
970 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
972 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
973 NULL == plugin ? "<any>" : plugin, ret);
975 if (NULL != data_set)
979 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
982 * Dispatch a notification.
984 static int pplugin_dispatch_notification (pTHX_ HV *notif)
993 memset (&n, 0, sizeof (n));
995 if (0 != hv2notification (aTHX_ notif, &n))
998 ret = plugin_dispatch_notification (&n);
999 plugin_notification_meta_free (n.meta);
1001 } /* static int pplugin_dispatch_notification (HV *) */
1004 * Call all working functions of the given type.
1006 static int pplugin_call_all (pTHX_ int type, ...)
1015 if ((type < 0) || (type >= PLUGIN_TYPES))
1018 va_start (ap, type);
1025 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1027 if (PLUGIN_WRITE == type) {
1029 * $_[0] = $plugin_type;
1044 * values => [ $v1, ... ],
1046 * host => $hostname,
1047 * plugin => $plugin,
1049 * plugin_instance => $instance,
1050 * type_instance => $type_instance
1059 ds = va_arg (ap, data_set_t *);
1060 vl = va_arg (ap, value_list_t *);
1062 if (-1 == data_set2av (aTHX_ ds, pds)) {
1065 pds = (AV *)&PL_sv_undef;
1069 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1072 pvl = (HV *)&PL_sv_undef;
1076 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1077 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1078 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1080 else if (PLUGIN_LOG == type) {
1086 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1087 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1089 else if (PLUGIN_NOTIF == type) {
1093 * severity => $severity,
1097 * plugin => $plugin,
1099 * plugin_instance => $instance,
1100 * type_instance => $type_instance
1104 HV *notif = newHV ();
1106 n = va_arg (ap, notification_t *);
1108 if (-1 == notification2hv (aTHX_ n, notif)) {
1111 notif = (HV *)&PL_sv_undef;
1115 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1117 else if (PLUGIN_FLUSH == type) {
1122 * $_[1] = $identifier;
1124 timeout = va_arg (ap, cdtime_t);
1126 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1127 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1132 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1147 } /* static int pplugin_call_all (int, ...) */
1150 * collectd's perl interpreter based thread implementation.
1152 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1155 /* must be called with perl_threads->mutex locked */
1156 static void c_ithread_destroy (c_ithread_t *ithread)
1158 dTHXa (ithread->interp);
1160 assert (NULL != perl_threads);
1162 PERL_SET_CONTEXT (aTHX);
1163 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1168 --perl_threads->number_of_threads;
1169 #endif /* COLLECT_DEBUG */
1171 perl_destruct (aTHX);
1174 if (NULL == ithread->prev)
1175 perl_threads->head = ithread->next;
1177 ithread->prev->next = ithread->next;
1179 if (NULL == ithread->next)
1180 perl_threads->tail = ithread->prev;
1182 ithread->next->prev = ithread->prev;
1186 } /* static void c_ithread_destroy (c_ithread_t *) */
1188 static void c_ithread_destructor (void *arg)
1190 c_ithread_t *ithread = (c_ithread_t *)arg;
1191 c_ithread_t *t = NULL;
1193 if (NULL == perl_threads)
1196 pthread_mutex_lock (&perl_threads->mutex);
1198 for (t = perl_threads->head; NULL != t; t = t->next)
1202 /* the ithread no longer exists */
1206 c_ithread_destroy (ithread);
1208 pthread_mutex_unlock (&perl_threads->mutex);
1210 } /* static void c_ithread_destructor (void *) */
1212 /* must be called with perl_threads->mutex locked */
1213 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1215 c_ithread_t *t = NULL;
1218 assert (NULL != perl_threads);
1220 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1221 memset (t, 0, sizeof (c_ithread_t));
1223 t->interp = (NULL == base)
1225 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1229 if ((NULL != base) && (NULL != PL_endav)) {
1230 av_clear (PL_endav);
1231 av_undef (PL_endav);
1236 ++perl_threads->number_of_threads;
1237 #endif /* COLLECT_DEBUG */
1241 if (NULL == perl_threads->tail) {
1242 perl_threads->head = t;
1246 perl_threads->tail->next = t;
1247 t->prev = perl_threads->tail;
1250 perl_threads->tail = t;
1252 pthread_setspecific (perl_thr_key, (const void *)t);
1254 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1257 * Filter chains implementation.
1260 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1267 notification_meta_t **meta = NULL;
1272 if ((type < 0) || (type >= FC_TYPES))
1275 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1278 va_start (ap, data);
1285 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1286 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1287 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1289 if (FC_CB_CREATE == cb_type) {
1292 * $_[1] = $user_data;
1295 HV *config = newHV ();
1297 ci = va_arg (ap, oconfig_item_t *);
1299 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1302 config = (HV *)&PL_sv_undef;
1306 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1308 else if (FC_CB_DESTROY == cb_type) {
1310 * $_[1] = $user_data;
1313 /* nothing to be done - the user data pointer
1314 * is pushed onto the stack later */
1316 else if (FC_CB_EXEC == cb_type) {
1321 * $_[3] = $user_data;
1329 ds = va_arg (ap, data_set_t *);
1330 vl = va_arg (ap, value_list_t *);
1331 meta = va_arg (ap, notification_meta_t **);
1333 if (0 != data_set2av (aTHX_ ds, pds)) {
1336 pds = (AV *)&PL_sv_undef;
1340 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1343 pvl = (HV *)&PL_sv_undef;
1350 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1353 pmeta = (AV *)&PL_sv_undef;
1358 pmeta = (AV *)&PL_sv_undef;
1361 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1362 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1363 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1366 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1370 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1372 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1373 assert (pmeta != NULL);
1375 plugin_notification_meta_free (*meta);
1376 av2notification_meta (aTHX_ pmeta, meta);
1383 /* the exec callbacks return a status, while
1384 * the others return a boolean value */
1385 if (FC_CB_EXEC == cb_type)
1387 else if (! SvTRUE (tmp))
1397 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1399 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1401 pfc_user_data_t *data;
1407 if (NULL == perl_threads)
1411 c_ithread_t *t = NULL;
1413 pthread_mutex_lock (&perl_threads->mutex);
1414 t = c_ithread_create (perl_threads->head->interp);
1415 pthread_mutex_unlock (&perl_threads->mutex);
1420 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1421 aTHX, perl_threads->number_of_threads);
1423 if ((1 != ci->values_num)
1424 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1425 log_warn ("A \"%s\" block expects a single string argument.",
1426 (FC_MATCH == type) ? "Match" : "Target");
1430 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1431 data->name = sstrdup (ci->values[0].value.string);
1432 data->user_data = newSV (0);
1434 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1437 PFC_USER_DATA_FREE (data);
1441 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1443 static int fc_destroy (int type, void **user_data)
1445 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1451 if ((NULL == perl_threads) || (NULL == data))
1455 c_ithread_t *t = NULL;
1457 pthread_mutex_lock (&perl_threads->mutex);
1458 t = c_ithread_create (perl_threads->head->interp);
1459 pthread_mutex_unlock (&perl_threads->mutex);
1464 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1465 aTHX, perl_threads->number_of_threads);
1467 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1469 PFC_USER_DATA_FREE (data);
1472 } /* static int fc_destroy (int, void **) */
1474 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1475 notification_meta_t **meta, void **user_data)
1477 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1481 if (NULL == perl_threads)
1484 assert (NULL != data);
1487 c_ithread_t *t = NULL;
1489 pthread_mutex_lock (&perl_threads->mutex);
1490 t = c_ithread_create (perl_threads->head->interp);
1491 pthread_mutex_unlock (&perl_threads->mutex);
1496 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1497 aTHX, perl_threads->number_of_threads);
1499 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1500 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1501 notification_meta_t **, void **) */
1503 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1505 return fc_create (FC_MATCH, ci, user_data);
1506 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1508 static int pmatch_destroy (void **user_data)
1510 return fc_destroy (FC_MATCH, user_data);
1511 } /* static int pmatch_destroy (void **) */
1513 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1514 notification_meta_t **meta, void **user_data)
1516 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1517 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1518 notification_meta_t **, void **) */
1520 static match_proc_t pmatch = {
1521 pmatch_create, pmatch_destroy, pmatch_match
1524 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1526 return fc_create (FC_TARGET, ci, user_data);
1527 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1529 static int ptarget_destroy (void **user_data)
1531 return fc_destroy (FC_TARGET, user_data);
1532 } /* static int ptarget_destroy (void **) */
1534 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1535 notification_meta_t **meta, void **user_data)
1537 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1538 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1539 notification_meta_t **, void **) */
1541 static target_proc_t ptarget = {
1542 ptarget_create, ptarget_destroy, ptarget_invoke
1546 * Exported Perl API.
1550 * Collectd::plugin_register_data_set (type, dataset).
1553 * type of the dataset
1556 * dataset to be registered
1558 static XS (Collectd_plugin_register_ds)
1565 log_warn ("Using plugin_register() to register new data-sets is "
1566 "deprecated - add new entries to a custom types.db instead.");
1569 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1573 log_debug ("Collectd::plugin_register_data_set: "
1574 "type = \"%s\", dataset = \"%s\"",
1575 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1579 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1580 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1584 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1592 } /* static XS (Collectd_plugin_register_ds) */
1595 * Collectd::plugin_unregister_data_set (type).
1598 * type of the dataset
1600 static XS (Collectd_plugin_unregister_ds)
1605 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1609 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1610 SvPV_nolen (ST (0)));
1612 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1616 } /* static XS (Collectd_plugin_register_ds) */
1619 * Collectd::plugin_dispatch_values (name, values).
1622 * name of the plugin
1625 * value list to submit
1627 static XS (Collectd_plugin_dispatch_values)
1636 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1640 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1641 SvPV_nolen (ST (/* stack index = */ 0)));
1643 values = ST (/* stack index = */ 0);
1645 /* Make sure the argument is a hash reference. */
1646 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1647 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1654 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1660 } /* static XS (Collectd_plugin_dispatch_values) */
1662 /* Collectd::plugin_write (plugin, ds, vl).
1665 * name of the plugin to call, may be 'undef'
1668 * data-set that describes the submitted values, may be 'undef'
1671 * value-list to be written
1673 static XS (Collectd__plugin_write)
1684 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1688 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1689 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1690 SvPV_nolen (ST (2)));
1692 if (! SvOK (ST (0)))
1695 plugin = SvPV_nolen (ST (0));
1698 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1699 ds_array = (AV *)SvRV (ds);
1700 else if (! SvOK (ds))
1703 log_err ("Collectd::plugin_write: Invalid data-set.");
1708 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1709 log_err ("Collectd::plugin_write: Invalid value-list.");
1713 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1719 } /* static XS (Collectd__plugin_write) */
1722 * Collectd::_plugin_flush (plugin, timeout, identifier).
1725 * name of the plugin to flush
1728 * timeout to use when flushing the data
1731 * data-set identifier to flush
1733 static XS (Collectd__plugin_flush)
1735 char *plugin = NULL;
1742 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1747 plugin = SvPV_nolen (ST (0));
1750 timeout = (int)SvIV (ST (1));
1753 id = SvPV_nolen (ST (2));
1755 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1756 "id = \"%s\"", plugin, timeout, id);
1758 if (0 == plugin_flush (plugin, timeout, id))
1762 } /* static XS (Collectd__plugin_flush) */
1765 * Collectd::plugin_dispatch_notification (notif).
1768 * notification to dispatch
1770 static XS (Collectd_plugin_dispatch_notification)
1779 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1783 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1784 SvPV_nolen (ST (0)));
1788 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1789 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1793 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1799 } /* static XS (Collectd_plugin_dispatch_notification) */
1802 * Collectd::plugin_log (level, message).
1805 * log level (LOG_DEBUG, ... LOG_ERR)
1810 static XS (Collectd_plugin_log)
1815 log_err ("Usage: Collectd::plugin_log(level, message)");
1819 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1821 } /* static XS (Collectd_plugin_log) */
1824 * Collectd::_fc_register (type, name)
1832 static XS (Collectd__fc_register)
1842 log_err ("Usage: Collectd::_fc_register(type, name)");
1846 type = SvIV (ST (0));
1847 name = SvPV_nolen (ST (1));
1849 if (FC_MATCH == type)
1850 ret = fc_register_match (name, pmatch);
1851 else if (FC_TARGET == type)
1852 ret = fc_register_target (name, ptarget);
1858 } /* static XS (Collectd_fc_register) */
1861 * Collectd::call_by_name (...).
1863 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1865 static XS (Collectd_call_by_name)
1870 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1871 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1876 name = SvPV_nolen (tmp);
1878 if (NULL == get_cv (name, 0)) {
1879 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1884 /* simply pass on the subroutine call without touching the stack,
1885 * thus leaving any arguments and return values in place */
1887 } /* static XS (Collectd_call_by_name) */
1890 * Interface to collectd.
1893 static int perl_init (void)
1897 if (NULL == perl_threads)
1901 c_ithread_t *t = NULL;
1903 pthread_mutex_lock (&perl_threads->mutex);
1904 t = c_ithread_create (perl_threads->head->interp);
1905 pthread_mutex_unlock (&perl_threads->mutex);
1910 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1911 aTHX, perl_threads->number_of_threads);
1912 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1913 } /* static int perl_init (void) */
1915 static int perl_read (void)
1919 if (NULL == perl_threads)
1923 c_ithread_t *t = NULL;
1925 pthread_mutex_lock (&perl_threads->mutex);
1926 t = c_ithread_create (perl_threads->head->interp);
1927 pthread_mutex_unlock (&perl_threads->mutex);
1932 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1933 aTHX, perl_threads->number_of_threads);
1934 return pplugin_call_all (aTHX_ PLUGIN_READ);
1935 } /* static int perl_read (void) */
1937 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1938 user_data_t __attribute__((unused)) *user_data)
1942 if (NULL == perl_threads)
1946 c_ithread_t *t = NULL;
1948 pthread_mutex_lock (&perl_threads->mutex);
1949 t = c_ithread_create (perl_threads->head->interp);
1950 pthread_mutex_unlock (&perl_threads->mutex);
1955 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1956 aTHX, perl_threads->number_of_threads);
1957 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1958 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1960 static void perl_log (int level, const char *msg,
1961 user_data_t __attribute__((unused)) *user_data)
1965 if (NULL == perl_threads)
1969 c_ithread_t *t = NULL;
1971 pthread_mutex_lock (&perl_threads->mutex);
1972 t = c_ithread_create (perl_threads->head->interp);
1973 pthread_mutex_unlock (&perl_threads->mutex);
1978 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1980 } /* static void perl_log (int, const char *) */
1982 static int perl_notify (const notification_t *notif,
1983 user_data_t __attribute__((unused)) *user_data)
1987 if (NULL == perl_threads)
1991 c_ithread_t *t = NULL;
1993 pthread_mutex_lock (&perl_threads->mutex);
1994 t = c_ithread_create (perl_threads->head->interp);
1995 pthread_mutex_unlock (&perl_threads->mutex);
1999 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2000 } /* static int perl_notify (const notification_t *) */
2002 static int perl_flush (cdtime_t timeout, const char *identifier,
2003 user_data_t __attribute__((unused)) *user_data)
2007 if (NULL == perl_threads)
2011 c_ithread_t *t = NULL;
2013 pthread_mutex_lock (&perl_threads->mutex);
2014 t = c_ithread_create (perl_threads->head->interp);
2015 pthread_mutex_unlock (&perl_threads->mutex);
2019 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2020 } /* static int perl_flush (const int) */
2022 static int perl_shutdown (void)
2024 c_ithread_t *t = NULL;
2030 plugin_unregister_complex_config ("perl");
2032 if (NULL == perl_threads)
2036 c_ithread_t *t = NULL;
2038 pthread_mutex_lock (&perl_threads->mutex);
2039 t = c_ithread_create (perl_threads->head->interp);
2040 pthread_mutex_unlock (&perl_threads->mutex);
2045 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2046 aTHX, perl_threads->number_of_threads);
2048 plugin_unregister_log ("perl");
2049 plugin_unregister_notification ("perl");
2050 plugin_unregister_init ("perl");
2051 plugin_unregister_read ("perl");
2052 plugin_unregister_write ("perl");
2053 plugin_unregister_flush ("perl");
2055 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2057 pthread_mutex_lock (&perl_threads->mutex);
2058 t = perl_threads->tail;
2061 c_ithread_t *thr = t;
2063 /* the pointer has to be advanced before destroying
2064 * the thread as this will free the memory */
2067 c_ithread_destroy (thr);
2070 pthread_mutex_unlock (&perl_threads->mutex);
2071 pthread_mutex_destroy (&perl_threads->mutex);
2073 sfree (perl_threads);
2075 pthread_key_delete (perl_thr_key);
2079 plugin_unregister_shutdown ("perl");
2081 } /* static void perl_shutdown (void) */
2084 * Access functions for global variables.
2086 * These functions implement the "magic" used to access
2087 * the global variables from Perl.
2090 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2092 char *pv = mg->mg_ptr;
2095 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2097 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2099 char *pv = mg->mg_ptr;
2100 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2102 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2104 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2106 cdtime_t *interval = (cdtime_t *)mg->mg_ptr;
2109 nv = CDTIME_T_TO_DOUBLE (*interval);
2113 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2115 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2117 cdtime_t *interval = (cdtime_t *)mg->mg_ptr;
2120 nv = (double)SvNV (var);
2122 *interval = DOUBLE_TO_CDTIME_T (nv);
2124 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2126 static MGVTBL g_pv_vtbl = {
2127 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2128 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2132 static MGVTBL g_interval_vtbl = {
2133 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2134 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2139 /* bootstrap the Collectd module */
2140 static void xs_init (pTHX)
2144 char *file = __FILE__;
2150 /* enable usage of Perl modules using shared libraries */
2151 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2154 for (i = 0; NULL != api[i].f; ++i)
2155 newXS (api[i].name, api[i].f, file);
2157 stash = gv_stashpv ("Collectd", 1);
2159 /* export "constants" */
2160 for (i = 0; '\0' != constants[i].name[0]; ++i)
2161 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2163 /* export global variables
2164 * by adding "magic" to the SV's representing the globale variables
2165 * perl is able to automagically call the get/set function when
2166 * accessing any such variable (this is basically the same as using
2168 /* global strings */
2169 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2170 tmp = get_sv (g_strings[i].name, 1);
2171 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2172 g_strings[i].var, 0);
2175 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2176 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2177 /* vtbl = */ &g_interval_vtbl,
2178 /* name = */ (char *) &interval_g, /* namelen = */ 0);
2181 } /* static void xs_init (pTHX) */
2183 /* Initialize the global Perl interpreter. */
2184 static int init_pi (int argc, char **argv)
2188 if (NULL != perl_threads)
2191 log_info ("Initializing Perl interpreter...");
2196 for (i = 0; i < argc; ++i)
2197 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2199 #endif /* COLLECT_DEBUG */
2201 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2202 log_err ("init_pi: pthread_key_create failed");
2204 /* this must not happen - cowardly giving up if it does */
2209 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2210 * triggers a "value computed is not used" warning by gcc. */
2213 PERL_SYS_INIT3 (&argc, &argv, &environ);
2215 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2216 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2218 pthread_mutex_init (&perl_threads->mutex, NULL);
2219 /* locking the mutex should not be necessary at this point
2220 * but let's just do it for the sake of completeness */
2221 pthread_mutex_lock (&perl_threads->mutex);
2223 perl_threads->head = c_ithread_create (NULL);
2224 perl_threads->tail = perl_threads->head;
2226 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2227 log_err ("init_pi: Not enough memory.");
2231 aTHX = perl_threads->head->interp;
2232 pthread_mutex_unlock (&perl_threads->mutex);
2234 perl_construct (aTHX);
2236 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2238 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2239 SV *err = get_sv ("@", 1);
2240 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2243 perl_destruct (perl_threads->head->interp);
2244 perl_free (perl_threads->head->interp);
2245 sfree (perl_threads);
2247 pthread_key_delete (perl_thr_key);
2251 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2252 sv_setpv (get_sv ("0", 0), "collectd");
2256 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2257 plugin_register_notification ("perl", perl_notify,
2258 /* user_data = */ NULL);
2259 plugin_register_init ("perl", perl_init);
2261 plugin_register_read ("perl", perl_read);
2263 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2264 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2265 plugin_register_shutdown ("perl", perl_shutdown);
2267 } /* static int init_pi (const char **, const int) */
2270 * LoadPlugin "<Plugin>"
2272 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2274 char module_name[DATA_MAX_NAME_LEN];
2278 if ((0 != ci->children_num) || (1 != ci->values_num)
2279 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2280 log_err ("LoadPlugin expects a single string argument.");
2284 value = ci->values[0].value.string;
2286 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2287 log_err ("Invalid module name %s", value);
2291 if (0 != init_pi (perl_argc, perl_argv))
2294 assert (NULL != perl_threads);
2295 assert (NULL != perl_threads->head);
2297 aTHX = perl_threads->head->interp;
2299 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2300 load_module (PERL_LOADMOD_NOIMPORT,
2301 newSVpv (module_name, strlen (module_name)), Nullsv);
2303 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2308 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2312 if ((0 != ci->children_num) || (1 != ci->values_num)
2313 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2314 log_err ("BaseName expects a single string argument.");
2318 value = ci->values[0].value.string;
2320 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2321 sstrncpy (base_name, value, sizeof (base_name));
2323 } /* static int perl_config_basename (oconfig_item_it *) */
2326 * EnableDebugger "<Package>"|""
2328 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2332 if ((0 != ci->children_num) || (1 != ci->values_num)
2333 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2334 log_err ("EnableDebugger expects a single string argument.");
2338 if (NULL != perl_threads) {
2339 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2343 value = ci->values[0].value.string;
2345 perl_argv = (char **)realloc (perl_argv,
2346 (++perl_argc + 1) * sizeof (char *));
2348 if (NULL == perl_argv) {
2349 log_err ("perl_config: Not enough memory.");
2353 if ('\0' == value[0]) {
2354 perl_argv[perl_argc - 1] = "-d";
2357 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2358 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2359 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2362 perl_argv[perl_argc] = NULL;
2364 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2367 * IncludeDir "<Dir>"
2369 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2373 if ((0 != ci->children_num) || (1 != ci->values_num)
2374 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2375 log_err ("IncludeDir expects a single string argument.");
2379 value = ci->values[0].value.string;
2382 perl_argv = (char **)realloc (perl_argv,
2383 (++perl_argc + 1) * sizeof (char *));
2385 if (NULL == perl_argv) {
2386 log_err ("perl_config: Not enough memory.");
2390 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2391 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2392 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2394 perl_argv[perl_argc] = NULL;
2397 /* prepend the directory to @INC */
2398 av_unshift (GvAVn (PL_incgv), 1);
2399 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2402 } /* static int perl_config_includedir (oconfig_item_it *) */
2407 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2417 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2418 log_err ("LoadPlugin expects a single string argument.");
2422 plugin = ci->values[0].value.string;
2425 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2429 log_err ("Unable to convert configuration to a Perl hash value.");
2430 config = (HV *)&PL_sv_undef;
2438 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2439 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2443 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2458 } /* static int perl_config_plugin (oconfig_item_it *) */
2460 static int perl_config (oconfig_item_t *ci)
2467 for (i = 0; i < ci->children_num; ++i) {
2468 oconfig_item_t *c = ci->children + i;
2469 int current_status = 0;
2471 if (NULL != perl_threads)
2472 aTHX = PERL_GET_CONTEXT;
2474 if (0 == strcasecmp (c->key, "LoadPlugin"))
2475 current_status = perl_config_loadplugin (aTHX_ c);
2476 else if (0 == strcasecmp (c->key, "BaseName"))
2477 current_status = perl_config_basename (aTHX_ c);
2478 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2479 current_status = perl_config_enabledebugger (aTHX_ c);
2480 else if (0 == strcasecmp (c->key, "IncludeDir"))
2481 current_status = perl_config_includedir (aTHX_ c);
2482 else if (0 == strcasecmp (c->key, "Plugin"))
2483 current_status = perl_config_plugin (aTHX_ c);
2486 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2490 /* fatal error - it's up to perl_config_* to clean up */
2491 if (0 > current_status) {
2492 log_err ("Configuration failed with a fatal error - "
2493 "plugin disabled!");
2494 return current_status;
2497 status += current_status;
2500 } /* static int perl_config (oconfig_item_t *) */
2502 void module_register (void)
2505 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2507 /* default options for the Perl interpreter */
2509 perl_argv[1] = "-MCollectd";
2510 perl_argv[2] = "-e";
2512 perl_argv[4] = NULL;
2514 plugin_register_complex_config ("perl", perl_config);
2516 } /* void module_register (void) */
2518 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */