2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 Sebastian Harl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Sebastian Harl <sh at tokkee.org>
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
30 #define DONT_POISON_SPRINTF_YET 1
32 #undef DONT_POISON_SPRINTF_YET
34 #include "configfile.h"
39 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
40 # pragma GCC poison sprintf
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
50 /* ... while we want the definition found in plugin.h. */
54 #include "filter_chain.h"
58 #if !defined(USE_ITHREADS)
59 # error "Perl does not support ithreads!"
60 #endif /* !defined(USE_ITHREADS) */
62 /* clear the Perl sub's stack frame
63 * (this should only be used inside an XSUB) */
64 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
68 #define PLUGIN_WRITE 2
69 #define PLUGIN_SHUTDOWN 3
71 #define PLUGIN_NOTIF 5
72 #define PLUGIN_FLUSH 6
74 #define PLUGIN_TYPES 7
76 #define PLUGIN_CONFIG 254
77 #define PLUGIN_DATASET 255
84 #define FC_CB_CREATE 0
85 #define FC_CB_DESTROY 1
90 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
91 #define log_info(...) INFO ("perl: " __VA_ARGS__)
92 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
93 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
95 /* this is defined in DynaLoader.a */
96 void boot_DynaLoader (PerlInterpreter *, CV *);
98 static XS (Collectd_plugin_register_ds);
99 static XS (Collectd_plugin_unregister_ds);
100 static XS (Collectd_plugin_dispatch_values);
101 static XS (Collectd__plugin_write);
102 static XS (Collectd__plugin_flush);
103 static XS (Collectd_plugin_dispatch_notification);
104 static XS (Collectd_plugin_log);
105 static XS (Collectd__fc_register);
106 static XS (Collectd_call_by_name);
112 typedef struct c_ithread_s {
113 /* the thread's Perl interpreter */
114 PerlInterpreter *interp;
116 /* double linked list of threads */
117 struct c_ithread_s *prev;
118 struct c_ithread_s *next;
126 /* some usage stats */
127 int number_of_threads;
128 #endif /* COLLECT_DEBUG */
130 pthread_mutex_t mutex;
133 /* name / user_data for Perl matches / targets */
139 #define PFC_USER_DATA_FREE(data) \
141 sfree ((data)->name); \
142 if (NULL != (data)->user_data) \
143 sv_free ((data)->user_data); \
150 extern char **environ;
156 /* if perl_threads != NULL perl_threads->head must
157 * point to the "base" thread */
158 static c_ithread_list_t *perl_threads = NULL;
160 /* the key used to store each pthread's ithread */
161 static pthread_key_t perl_thr_key;
163 static int perl_argc = 0;
164 static char **perl_argv = NULL;
166 static char base_name[DATA_MAX_NAME_LEN] = "";
173 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
174 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
175 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
176 { "Collectd::_plugin_write", Collectd__plugin_write },
177 { "Collectd::_plugin_flush", Collectd__plugin_flush },
178 { "Collectd::plugin_dispatch_notification",
179 Collectd_plugin_dispatch_notification },
180 { "Collectd::plugin_log", Collectd_plugin_log },
181 { "Collectd::_fc_register", Collectd__fc_register },
182 { "Collectd::call_by_name", Collectd_call_by_name },
191 { "Collectd::TYPE_INIT", PLUGIN_INIT },
192 { "Collectd::TYPE_READ", PLUGIN_READ },
193 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
194 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
195 { "Collectd::TYPE_LOG", PLUGIN_LOG },
196 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
197 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
198 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
199 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
200 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
201 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
202 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
203 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
204 { "Collectd::LOG_ERR", LOG_ERR },
205 { "Collectd::LOG_WARNING", LOG_WARNING },
206 { "Collectd::LOG_NOTICE", LOG_NOTICE },
207 { "Collectd::LOG_INFO", LOG_INFO },
208 { "Collectd::LOG_DEBUG", LOG_DEBUG },
209 { "Collectd::FC_MATCH", FC_MATCH },
210 { "Collectd::FC_TARGET", FC_TARGET },
211 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
212 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
213 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
214 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
215 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
216 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
217 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
218 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
219 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
220 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
221 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
230 { "Collectd::hostname_g", hostname_g },
239 { "Collectd::interval_g", &interval_g },
244 * Helper functions for data type conversion.
259 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
263 if ((NULL == hash) || (NULL == ds))
266 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
267 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
270 log_err ("hv2data_source: No DS name given.");
274 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
275 ds->type = SvIV (*tmp);
277 if ((DS_TYPE_COUNTER != ds->type)
278 && (DS_TYPE_GAUGE != ds->type)
279 && (DS_TYPE_DERIVE != ds->type)
280 && (DS_TYPE_ABSOLUTE != ds->type)) {
281 log_err ("hv2data_source: Invalid DS type.");
286 ds->type = DS_TYPE_COUNTER;
289 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
290 ds->min = SvNV (*tmp);
294 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
295 ds->max = SvNV (*tmp);
299 } /* static int hv2data_source (HV *, data_source_t *) */
301 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
303 const data_set_t *ds;
307 if ((NULL == name) || (NULL == array) || (NULL == value))
310 if (av_len (array) < len - 1)
311 len = av_len (array) + 1;
316 ds = plugin_get_ds (name);
318 log_err ("av2value: Unknown dataset \"%s\"", name);
322 if (ds->ds_num < len) {
323 log_warn ("av2value: Value length exceeds data set length.");
327 for (i = 0; i < len; ++i) {
328 SV **tmp = av_fetch (array, i, 0);
331 if (DS_TYPE_COUNTER == ds->ds[i].type)
332 value[i].counter = SvIV (*tmp);
333 else if (DS_TYPE_GAUGE == ds->ds[i].type)
334 value[i].gauge = SvNV (*tmp);
335 else if (DS_TYPE_DERIVE == ds->ds[i].type)
336 value[i].derive = SvIV (*tmp);
337 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
338 value[i].absolute = SvIV (*tmp);
345 } /* static int av2value (char *, AV *, value_t *, int) */
350 * values => [ @values ],
354 * plugin_instance => $pinstance,
355 * type_instance => $tinstance,
358 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
362 if ((NULL == hash) || (NULL == vl))
365 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
366 log_err ("hv2value_list: No type given.");
370 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
372 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
373 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
374 log_err ("hv2value_list: No valid values given.");
379 AV *array = (AV *)SvRV (*tmp);
380 int len = av_len (array) + 1;
385 vl->values = (value_t *)smalloc (len * sizeof (value_t));
386 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
389 if (-1 == vl->values_len) {
395 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
397 double t = SvNV (*tmp);
398 vl->time = DOUBLE_TO_CDTIME_T (t);
401 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
402 vl->interval = SvIV (*tmp);
404 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
405 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
407 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
409 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
410 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
412 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
413 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
414 sizeof (vl->plugin_instance));
416 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
417 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
418 sizeof (vl->type_instance));
420 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
422 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
426 if ((NULL == array) || (NULL == name) || (NULL == ds))
429 len = av_len (array);
432 log_err ("av2data_set: Invalid data set.");
436 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
437 ds->ds_num = len + 1;
439 for (i = 0; i <= len; ++i) {
440 SV **elem = av_fetch (array, i, 0);
443 log_err ("av2data_set: Failed to fetch data source %i.", i);
447 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
448 log_err ("av2data_set: Invalid data source.");
452 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
455 log_debug ("av2data_set: "
456 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
457 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
460 sstrncpy (ds->type, name, sizeof (ds->type));
462 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
467 * severity => $severity,
473 * plugin_instance => $instance,
474 * type_instance => $type_instance,
475 * meta => [ { name => <name>, value => <value> }, ... ]
478 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
480 notification_meta_t **m = meta;
482 int len = av_len (array);
485 for (i = 0; i <= len; ++i) {
486 SV **tmp = av_fetch (array, i, 0);
492 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
493 log_warn ("av2notification_meta: Skipping invalid "
494 "meta information.");
498 hash = (HV *)SvRV (*tmp);
500 *m = (notification_meta_t *)smalloc (sizeof (**m));
502 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
503 log_warn ("av2notification_meta: Skipping invalid "
504 "meta information.");
508 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
510 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
511 log_warn ("av2notification_meta: Skipping invalid "
512 "meta information.");
519 (*m)->nm_value.nm_double = SvNVX (*tmp);
520 (*m)->type = NM_TYPE_DOUBLE;
522 else if (SvUOK (*tmp)) {
523 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
524 (*m)->type = NM_TYPE_UNSIGNED_INT;
526 else if (SvIOK (*tmp)) {
527 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
528 (*m)->type = NM_TYPE_SIGNED_INT;
531 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
532 (*m)->type = NM_TYPE_STRING;
539 } /* static int av2notification_meta (AV *, notification_meta_t *) */
541 static int hv2notification (pTHX_ HV *hash, notification_t *n)
545 if ((NULL == hash) || (NULL == n))
548 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
549 n->severity = SvIV (*tmp);
551 n->severity = NOTIF_FAILURE;
553 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
555 double t = SvNV (*tmp);
556 n->time = DOUBLE_TO_CDTIME_T (t);
561 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
562 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
564 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
565 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
567 sstrncpy (n->host, hostname_g, sizeof (n->host));
569 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
570 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
572 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
573 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
574 sizeof (n->plugin_instance));
576 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
577 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
579 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
580 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
581 sizeof (n->type_instance));
584 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
585 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
586 log_warn ("hv2notification: Ignoring invalid meta information.");
590 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
591 plugin_notification_meta_free (n->meta);
598 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
600 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
604 if ((NULL == ds) || (NULL == array))
607 av_extend (array, ds->ds_num);
609 for (i = 0; i < ds->ds_num; ++i) {
610 HV *source = newHV ();
612 if (NULL == hv_store (source, "name", 4,
613 newSVpv (ds->ds[i].name, 0), 0))
616 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
619 if (! isnan (ds->ds[i].min))
620 if (NULL == hv_store (source, "min", 3,
621 newSVnv (ds->ds[i].min), 0))
624 if (! isnan (ds->ds[i].max))
625 if (NULL == hv_store (source, "max", 3,
626 newSVnv (ds->ds[i].max), 0))
629 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
633 } /* static int data_set2av (data_set_t *, AV *) */
635 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
642 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
645 len = vl->values_len;
647 if (ds->ds_num < len) {
648 log_warn ("value2av: Value length exceeds data set length.");
653 av_extend (values, len - 1);
655 for (i = 0; i < len; ++i) {
658 if (DS_TYPE_COUNTER == ds->ds[i].type)
659 val = newSViv (vl->values[i].counter);
660 else if (DS_TYPE_GAUGE == ds->ds[i].type)
661 val = newSVnv (vl->values[i].gauge);
662 else if (DS_TYPE_DERIVE == ds->ds[i].type)
663 val = newSViv (vl->values[i].derive);
664 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
665 val = newSViv (vl->values[i].absolute);
667 if (NULL == av_store (values, i, val)) {
673 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
678 double t = CDTIME_T_TO_DOUBLE (vl->time);
679 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
683 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
686 if ('\0' != vl->host[0])
687 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
690 if ('\0' != vl->plugin[0])
691 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
694 if ('\0' != vl->plugin_instance[0])
695 if (NULL == hv_store (hash, "plugin_instance", 15,
696 newSVpv (vl->plugin_instance, 0), 0))
699 if ('\0' != vl->type[0])
700 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
703 if ('\0' != vl->type_instance[0])
704 if (NULL == hv_store (hash, "type_instance", 13,
705 newSVpv (vl->type_instance, 0), 0))
708 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
710 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
720 av_extend (array, meta_num);
722 for (i = 0; NULL != meta; meta = meta->next, ++i) {
726 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
729 if (NM_TYPE_STRING == meta->type)
730 value = newSVpv (meta->nm_value.nm_string, 0);
731 else if (NM_TYPE_SIGNED_INT == meta->type)
732 value = newSViv (meta->nm_value.nm_signed_int);
733 else if (NM_TYPE_UNSIGNED_INT == meta->type)
734 value = newSVuv (meta->nm_value.nm_unsigned_int);
735 else if (NM_TYPE_DOUBLE == meta->type)
736 value = newSVnv (meta->nm_value.nm_double);
737 else if (NM_TYPE_BOOLEAN == meta->type)
738 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
742 if (NULL == hv_store (m, "value", 5, value, 0)) {
747 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
754 } /* static int notification_meta2av (notification_meta_t *, AV *) */
756 static int notification2hv (pTHX_ notification_t *n, HV *hash)
758 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
763 double t = CDTIME_T_TO_DOUBLE (n->time);
764 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
768 if ('\0' != *n->message)
769 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
772 if ('\0' != *n->host)
773 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
776 if ('\0' != *n->plugin)
777 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
780 if ('\0' != *n->plugin_instance)
781 if (NULL == hv_store (hash, "plugin_instance", 15,
782 newSVpv (n->plugin_instance, 0), 0))
785 if ('\0' != *n->type)
786 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
789 if ('\0' != *n->type_instance)
790 if (NULL == hv_store (hash, "type_instance", 13,
791 newSVpv (n->type_instance, 0), 0))
794 if (NULL != n->meta) {
796 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
797 || (NULL == hv_store (hash, "meta", 4,
798 newRV_noinc ((SV *)meta), 0))) {
805 } /* static int notification2hv (notification_t *, HV *) */
807 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
814 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
818 if (0 < ci->values_num)
819 av_extend (values, ci->values_num);
821 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
827 for (i = 0; i < ci->values_num; ++i) {
830 switch (ci->values[i].type) {
831 case OCONFIG_TYPE_STRING:
832 value = newSVpv (ci->values[i].value.string, 0);
834 case OCONFIG_TYPE_NUMBER:
835 value = newSVnv ((NV)ci->values[i].value.number);
837 case OCONFIG_TYPE_BOOLEAN:
838 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
841 log_err ("oconfig_item2hv: Invalid value type %i.",
843 value = &PL_sv_undef;
846 if (NULL == av_store (values, i, value)) {
852 /* ignoring 'parent' member which is uninteresting in this case */
855 if (0 < ci->children_num)
856 av_extend (children, ci->children_num);
858 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
864 for (i = 0; i < ci->children_num; ++i) {
865 HV *child = newHV ();
867 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
873 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
880 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
883 * Internal functions.
886 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
888 if (base_name[0] == '\0')
889 status = ssnprintf (buf, buf_len, "%s", module);
891 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
892 if ((status < 0) || ((unsigned int)status >= buf_len))
895 } /* char *get_module_name */
898 * Add a plugin's data set definition.
900 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
906 if ((NULL == name) || (NULL == dataset))
909 if (0 != av2data_set (aTHX_ dataset, name, &ds))
912 ret = plugin_register_data_set (&ds);
916 } /* static int pplugin_register_data_set (char *, SV *) */
919 * Remove a plugin's data set definition.
921 static int pplugin_unregister_data_set (char *name)
925 return plugin_unregister_data_set (name);
926 } /* static int pplugin_unregister_data_set (char *) */
929 * Submit the values to the write functions.
931 static int pplugin_dispatch_values (pTHX_ HV *values)
933 value_list_t vl = VALUE_LIST_INIT;
940 if (0 != hv2value_list (aTHX_ values, &vl))
943 ret = plugin_dispatch_values (&vl);
947 } /* static int pplugin_dispatch_values (char *, HV *) */
950 * Submit the values to a single write function.
952 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
955 value_list_t vl = VALUE_LIST_INIT;
962 if (0 != hv2value_list (aTHX_ values, &vl))
965 if ((NULL != data_set)
966 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
969 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
971 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
972 NULL == plugin ? "<any>" : plugin, ret);
974 if (NULL != data_set)
978 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
981 * Dispatch a notification.
983 static int pplugin_dispatch_notification (pTHX_ HV *notif)
992 memset (&n, 0, sizeof (n));
994 if (0 != hv2notification (aTHX_ notif, &n))
997 ret = plugin_dispatch_notification (&n);
998 plugin_notification_meta_free (n.meta);
1000 } /* static int pplugin_dispatch_notification (HV *) */
1003 * Call all working functions of the given type.
1005 static int pplugin_call_all (pTHX_ int type, ...)
1014 if ((type < 0) || (type >= PLUGIN_TYPES))
1017 va_start (ap, type);
1024 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1026 if (PLUGIN_WRITE == type) {
1028 * $_[0] = $plugin_type;
1043 * values => [ $v1, ... ],
1045 * host => $hostname,
1046 * plugin => $plugin,
1048 * plugin_instance => $instance,
1049 * type_instance => $type_instance
1058 ds = va_arg (ap, data_set_t *);
1059 vl = va_arg (ap, value_list_t *);
1061 if (-1 == data_set2av (aTHX_ ds, pds)) {
1064 pds = (AV *)&PL_sv_undef;
1068 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1071 pvl = (HV *)&PL_sv_undef;
1075 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1076 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1077 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1079 else if (PLUGIN_LOG == type) {
1085 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1086 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1088 else if (PLUGIN_NOTIF == type) {
1092 * severity => $severity,
1096 * plugin => $plugin,
1098 * plugin_instance => $instance,
1099 * type_instance => $type_instance
1103 HV *notif = newHV ();
1105 n = va_arg (ap, notification_t *);
1107 if (-1 == notification2hv (aTHX_ n, notif)) {
1110 notif = (HV *)&PL_sv_undef;
1114 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1116 else if (PLUGIN_FLUSH == type) {
1121 * $_[1] = $identifier;
1123 timeout = va_arg (ap, cdtime_t);
1125 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1126 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1131 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1146 } /* static int pplugin_call_all (int, ...) */
1149 * collectd's perl interpreter based thread implementation.
1151 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1154 /* must be called with perl_threads->mutex locked */
1155 static void c_ithread_destroy (c_ithread_t *ithread)
1157 dTHXa (ithread->interp);
1159 assert (NULL != perl_threads);
1161 PERL_SET_CONTEXT (aTHX);
1162 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1167 --perl_threads->number_of_threads;
1168 #endif /* COLLECT_DEBUG */
1170 perl_destruct (aTHX);
1173 if (NULL == ithread->prev)
1174 perl_threads->head = ithread->next;
1176 ithread->prev->next = ithread->next;
1178 if (NULL == ithread->next)
1179 perl_threads->tail = ithread->prev;
1181 ithread->next->prev = ithread->prev;
1185 } /* static void c_ithread_destroy (c_ithread_t *) */
1187 static void c_ithread_destructor (void *arg)
1189 c_ithread_t *ithread = (c_ithread_t *)arg;
1190 c_ithread_t *t = NULL;
1192 if (NULL == perl_threads)
1195 pthread_mutex_lock (&perl_threads->mutex);
1197 for (t = perl_threads->head; NULL != t; t = t->next)
1201 /* the ithread no longer exists */
1205 c_ithread_destroy (ithread);
1207 pthread_mutex_unlock (&perl_threads->mutex);
1209 } /* static void c_ithread_destructor (void *) */
1211 /* must be called with perl_threads->mutex locked */
1212 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1214 c_ithread_t *t = NULL;
1217 assert (NULL != perl_threads);
1219 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1220 memset (t, 0, sizeof (c_ithread_t));
1222 t->interp = (NULL == base)
1224 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1228 if ((NULL != base) && (NULL != PL_endav)) {
1229 av_clear (PL_endav);
1230 av_undef (PL_endav);
1235 ++perl_threads->number_of_threads;
1236 #endif /* COLLECT_DEBUG */
1240 if (NULL == perl_threads->tail) {
1241 perl_threads->head = t;
1245 perl_threads->tail->next = t;
1246 t->prev = perl_threads->tail;
1249 perl_threads->tail = t;
1251 pthread_setspecific (perl_thr_key, (const void *)t);
1253 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1256 * Filter chains implementation.
1259 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1266 notification_meta_t **meta = NULL;
1271 if ((type < 0) || (type >= FC_TYPES))
1274 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1277 va_start (ap, data);
1284 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1285 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1286 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1288 if (FC_CB_CREATE == cb_type) {
1291 * $_[1] = $user_data;
1294 HV *config = newHV ();
1296 ci = va_arg (ap, oconfig_item_t *);
1298 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1301 config = (HV *)&PL_sv_undef;
1305 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1307 else if (FC_CB_DESTROY == cb_type) {
1309 * $_[1] = $user_data;
1312 /* nothing to be done - the user data pointer
1313 * is pushed onto the stack later */
1315 else if (FC_CB_EXEC == cb_type) {
1320 * $_[3] = $user_data;
1328 ds = va_arg (ap, data_set_t *);
1329 vl = va_arg (ap, value_list_t *);
1330 meta = va_arg (ap, notification_meta_t **);
1332 if (0 != data_set2av (aTHX_ ds, pds)) {
1335 pds = (AV *)&PL_sv_undef;
1339 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1342 pvl = (HV *)&PL_sv_undef;
1349 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1352 pmeta = (AV *)&PL_sv_undef;
1357 pmeta = (AV *)&PL_sv_undef;
1360 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1361 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1362 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1365 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1369 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1371 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1372 assert (pmeta != NULL);
1374 plugin_notification_meta_free (*meta);
1375 av2notification_meta (aTHX_ pmeta, meta);
1382 /* the exec callbacks return a status, while
1383 * the others return a boolean value */
1384 if (FC_CB_EXEC == cb_type)
1386 else if (! SvTRUE (tmp))
1396 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1398 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1400 pfc_user_data_t *data;
1406 if (NULL == perl_threads)
1410 c_ithread_t *t = NULL;
1412 pthread_mutex_lock (&perl_threads->mutex);
1413 t = c_ithread_create (perl_threads->head->interp);
1414 pthread_mutex_unlock (&perl_threads->mutex);
1419 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1420 aTHX, perl_threads->number_of_threads);
1422 if ((1 != ci->values_num)
1423 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1424 log_warn ("A \"%s\" block expects a single string argument.",
1425 (FC_MATCH == type) ? "Match" : "Target");
1429 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1430 data->name = sstrdup (ci->values[0].value.string);
1431 data->user_data = newSV (0);
1433 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1436 PFC_USER_DATA_FREE (data);
1440 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1442 static int fc_destroy (int type, void **user_data)
1444 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1450 if ((NULL == perl_threads) || (NULL == data))
1454 c_ithread_t *t = NULL;
1456 pthread_mutex_lock (&perl_threads->mutex);
1457 t = c_ithread_create (perl_threads->head->interp);
1458 pthread_mutex_unlock (&perl_threads->mutex);
1463 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1464 aTHX, perl_threads->number_of_threads);
1466 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1468 PFC_USER_DATA_FREE (data);
1471 } /* static int fc_destroy (int, void **) */
1473 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1474 notification_meta_t **meta, void **user_data)
1476 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1480 if (NULL == perl_threads)
1483 assert (NULL != data);
1486 c_ithread_t *t = NULL;
1488 pthread_mutex_lock (&perl_threads->mutex);
1489 t = c_ithread_create (perl_threads->head->interp);
1490 pthread_mutex_unlock (&perl_threads->mutex);
1495 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1496 aTHX, perl_threads->number_of_threads);
1498 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1499 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1500 notification_meta_t **, void **) */
1502 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1504 return fc_create (FC_MATCH, ci, user_data);
1505 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1507 static int pmatch_destroy (void **user_data)
1509 return fc_destroy (FC_MATCH, user_data);
1510 } /* static int pmatch_destroy (void **) */
1512 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1513 notification_meta_t **meta, void **user_data)
1515 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1516 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1517 notification_meta_t **, void **) */
1519 static match_proc_t pmatch = {
1520 pmatch_create, pmatch_destroy, pmatch_match
1523 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1525 return fc_create (FC_TARGET, ci, user_data);
1526 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1528 static int ptarget_destroy (void **user_data)
1530 return fc_destroy (FC_TARGET, user_data);
1531 } /* static int ptarget_destroy (void **) */
1533 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1534 notification_meta_t **meta, void **user_data)
1536 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1537 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1538 notification_meta_t **, void **) */
1540 static target_proc_t ptarget = {
1541 ptarget_create, ptarget_destroy, ptarget_invoke
1545 * Exported Perl API.
1549 * Collectd::plugin_register_data_set (type, dataset).
1552 * type of the dataset
1555 * dataset to be registered
1557 static XS (Collectd_plugin_register_ds)
1564 log_warn ("Using plugin_register() to register new data-sets is "
1565 "deprecated - add new entries to a custom types.db instead.");
1568 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1572 log_debug ("Collectd::plugin_register_data_set: "
1573 "type = \"%s\", dataset = \"%s\"",
1574 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1578 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1579 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1583 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1591 } /* static XS (Collectd_plugin_register_ds) */
1594 * Collectd::plugin_unregister_data_set (type).
1597 * type of the dataset
1599 static XS (Collectd_plugin_unregister_ds)
1604 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1608 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1609 SvPV_nolen (ST (0)));
1611 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1615 } /* static XS (Collectd_plugin_register_ds) */
1618 * Collectd::plugin_dispatch_values (name, values).
1621 * name of the plugin
1624 * value list to submit
1626 static XS (Collectd_plugin_dispatch_values)
1635 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1639 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1640 SvPV_nolen (ST (/* stack index = */ 0)));
1642 values = ST (/* stack index = */ 0);
1644 /* Make sure the argument is a hash reference. */
1645 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1646 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1653 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1659 } /* static XS (Collectd_plugin_dispatch_values) */
1661 /* Collectd::plugin_write (plugin, ds, vl).
1664 * name of the plugin to call, may be 'undef'
1667 * data-set that describes the submitted values, may be 'undef'
1670 * value-list to be written
1672 static XS (Collectd__plugin_write)
1683 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1687 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1688 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1689 SvPV_nolen (ST (2)));
1691 if (! SvOK (ST (0)))
1694 plugin = SvPV_nolen (ST (0));
1697 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1698 ds_array = (AV *)SvRV (ds);
1699 else if (! SvOK (ds))
1702 log_err ("Collectd::plugin_write: Invalid data-set.");
1707 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1708 log_err ("Collectd::plugin_write: Invalid value-list.");
1712 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1718 } /* static XS (Collectd__plugin_write) */
1721 * Collectd::_plugin_flush (plugin, timeout, identifier).
1724 * name of the plugin to flush
1727 * timeout to use when flushing the data
1730 * data-set identifier to flush
1732 static XS (Collectd__plugin_flush)
1734 char *plugin = NULL;
1741 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1746 plugin = SvPV_nolen (ST (0));
1749 timeout = (int)SvIV (ST (1));
1752 id = SvPV_nolen (ST (2));
1754 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1755 "id = \"%s\"", plugin, timeout, id);
1757 if (0 == plugin_flush (plugin, timeout, id))
1761 } /* static XS (Collectd__plugin_flush) */
1764 * Collectd::plugin_dispatch_notification (notif).
1767 * notification to dispatch
1769 static XS (Collectd_plugin_dispatch_notification)
1778 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1782 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1783 SvPV_nolen (ST (0)));
1787 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1788 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1792 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1798 } /* static XS (Collectd_plugin_dispatch_notification) */
1801 * Collectd::plugin_log (level, message).
1804 * log level (LOG_DEBUG, ... LOG_ERR)
1809 static XS (Collectd_plugin_log)
1814 log_err ("Usage: Collectd::plugin_log(level, message)");
1818 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1820 } /* static XS (Collectd_plugin_log) */
1823 * Collectd::_fc_register (type, name)
1831 static XS (Collectd__fc_register)
1841 log_err ("Usage: Collectd::_fc_register(type, name)");
1845 type = SvIV (ST (0));
1846 name = SvPV_nolen (ST (1));
1848 if (FC_MATCH == type)
1849 ret = fc_register_match (name, pmatch);
1850 else if (FC_TARGET == type)
1851 ret = fc_register_target (name, ptarget);
1857 } /* static XS (Collectd_fc_register) */
1860 * Collectd::call_by_name (...).
1862 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1864 static XS (Collectd_call_by_name)
1869 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1870 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1875 name = SvPV_nolen (tmp);
1877 if (NULL == get_cv (name, 0)) {
1878 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1883 /* simply pass on the subroutine call without touching the stack,
1884 * thus leaving any arguments and return values in place */
1886 } /* static XS (Collectd_call_by_name) */
1889 * Interface to collectd.
1892 static int perl_init (void)
1896 if (NULL == perl_threads)
1900 c_ithread_t *t = NULL;
1902 pthread_mutex_lock (&perl_threads->mutex);
1903 t = c_ithread_create (perl_threads->head->interp);
1904 pthread_mutex_unlock (&perl_threads->mutex);
1909 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1910 aTHX, perl_threads->number_of_threads);
1911 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1912 } /* static int perl_init (void) */
1914 static int perl_read (void)
1918 if (NULL == perl_threads)
1922 c_ithread_t *t = NULL;
1924 pthread_mutex_lock (&perl_threads->mutex);
1925 t = c_ithread_create (perl_threads->head->interp);
1926 pthread_mutex_unlock (&perl_threads->mutex);
1931 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1932 aTHX, perl_threads->number_of_threads);
1933 return pplugin_call_all (aTHX_ PLUGIN_READ);
1934 } /* static int perl_read (void) */
1936 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1937 user_data_t __attribute__((unused)) *user_data)
1941 if (NULL == perl_threads)
1945 c_ithread_t *t = NULL;
1947 pthread_mutex_lock (&perl_threads->mutex);
1948 t = c_ithread_create (perl_threads->head->interp);
1949 pthread_mutex_unlock (&perl_threads->mutex);
1954 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1955 aTHX, perl_threads->number_of_threads);
1956 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1957 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1959 static void perl_log (int level, const char *msg,
1960 user_data_t __attribute__((unused)) *user_data)
1964 if (NULL == perl_threads)
1968 c_ithread_t *t = NULL;
1970 pthread_mutex_lock (&perl_threads->mutex);
1971 t = c_ithread_create (perl_threads->head->interp);
1972 pthread_mutex_unlock (&perl_threads->mutex);
1977 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1979 } /* static void perl_log (int, const char *) */
1981 static int perl_notify (const notification_t *notif,
1982 user_data_t __attribute__((unused)) *user_data)
1986 if (NULL == perl_threads)
1990 c_ithread_t *t = NULL;
1992 pthread_mutex_lock (&perl_threads->mutex);
1993 t = c_ithread_create (perl_threads->head->interp);
1994 pthread_mutex_unlock (&perl_threads->mutex);
1998 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1999 } /* static int perl_notify (const notification_t *) */
2001 static int perl_flush (cdtime_t timeout, const char *identifier,
2002 user_data_t __attribute__((unused)) *user_data)
2006 if (NULL == perl_threads)
2010 c_ithread_t *t = NULL;
2012 pthread_mutex_lock (&perl_threads->mutex);
2013 t = c_ithread_create (perl_threads->head->interp);
2014 pthread_mutex_unlock (&perl_threads->mutex);
2018 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2019 } /* static int perl_flush (const int) */
2021 static int perl_shutdown (void)
2023 c_ithread_t *t = NULL;
2029 plugin_unregister_complex_config ("perl");
2031 if (NULL == perl_threads)
2035 c_ithread_t *t = NULL;
2037 pthread_mutex_lock (&perl_threads->mutex);
2038 t = c_ithread_create (perl_threads->head->interp);
2039 pthread_mutex_unlock (&perl_threads->mutex);
2044 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2045 aTHX, perl_threads->number_of_threads);
2047 plugin_unregister_log ("perl");
2048 plugin_unregister_notification ("perl");
2049 plugin_unregister_init ("perl");
2050 plugin_unregister_read ("perl");
2051 plugin_unregister_write ("perl");
2052 plugin_unregister_flush ("perl");
2054 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2056 pthread_mutex_lock (&perl_threads->mutex);
2057 t = perl_threads->tail;
2060 c_ithread_t *thr = t;
2062 /* the pointer has to be advanced before destroying
2063 * the thread as this will free the memory */
2066 c_ithread_destroy (thr);
2069 pthread_mutex_unlock (&perl_threads->mutex);
2070 pthread_mutex_destroy (&perl_threads->mutex);
2072 sfree (perl_threads);
2074 pthread_key_delete (perl_thr_key);
2078 plugin_unregister_shutdown ("perl");
2080 } /* static void perl_shutdown (void) */
2083 * Access functions for global variables.
2085 * These functions implement the "magic" used to access
2086 * the global variables from Perl.
2089 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2091 char *pv = mg->mg_ptr;
2094 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2096 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2098 char *pv = mg->mg_ptr;
2099 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2101 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2103 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2105 int *iv = (int *)mg->mg_ptr;
2106 sv_setiv (var, *iv);
2108 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2110 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2112 int *iv = (int *)mg->mg_ptr;
2113 *iv = (int)SvIV (var);
2115 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2117 static MGVTBL g_pv_vtbl = {
2118 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2119 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2123 static MGVTBL g_iv_vtbl = {
2124 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2125 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2130 /* bootstrap the Collectd module */
2131 static void xs_init (pTHX)
2135 char *file = __FILE__;
2141 /* enable usage of Perl modules using shared libraries */
2142 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2145 for (i = 0; NULL != api[i].f; ++i)
2146 newXS (api[i].name, api[i].f, file);
2148 stash = gv_stashpv ("Collectd", 1);
2150 /* export "constants" */
2151 for (i = 0; '\0' != constants[i].name[0]; ++i)
2152 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2154 /* export global variables
2155 * by adding "magic" to the SV's representing the globale variables
2156 * perl is able to automagically call the get/set function when
2157 * accessing any such variable (this is basically the same as using
2159 /* global strings */
2160 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2161 tmp = get_sv (g_strings[i].name, 1);
2162 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2163 g_strings[i].var, 0);
2166 /* global integers */
2167 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2168 tmp = get_sv (g_integers[i].name, 1);
2169 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2170 (char *)g_integers[i].var, 0);
2173 } /* static void xs_init (pTHX) */
2175 /* Initialize the global Perl interpreter. */
2176 static int init_pi (int argc, char **argv)
2180 if (NULL != perl_threads)
2183 log_info ("Initializing Perl interpreter...");
2188 for (i = 0; i < argc; ++i)
2189 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2191 #endif /* COLLECT_DEBUG */
2193 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2194 log_err ("init_pi: pthread_key_create failed");
2196 /* this must not happen - cowardly giving up if it does */
2201 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2202 * triggers a "value computed is not used" warning by gcc. */
2205 PERL_SYS_INIT3 (&argc, &argv, &environ);
2207 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2208 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2210 pthread_mutex_init (&perl_threads->mutex, NULL);
2211 /* locking the mutex should not be necessary at this point
2212 * but let's just do it for the sake of completeness */
2213 pthread_mutex_lock (&perl_threads->mutex);
2215 perl_threads->head = c_ithread_create (NULL);
2216 perl_threads->tail = perl_threads->head;
2218 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2219 log_err ("init_pi: Not enough memory.");
2223 aTHX = perl_threads->head->interp;
2224 pthread_mutex_unlock (&perl_threads->mutex);
2226 perl_construct (aTHX);
2228 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2230 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2231 SV *err = get_sv ("@", 1);
2232 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2235 perl_destruct (perl_threads->head->interp);
2236 perl_free (perl_threads->head->interp);
2237 sfree (perl_threads);
2239 pthread_key_delete (perl_thr_key);
2243 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2244 sv_setpv (get_sv ("0", 0), "collectd");
2248 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2249 plugin_register_notification ("perl", perl_notify,
2250 /* user_data = */ NULL);
2251 plugin_register_init ("perl", perl_init);
2253 plugin_register_read ("perl", perl_read);
2255 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2256 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2257 plugin_register_shutdown ("perl", perl_shutdown);
2259 } /* static int init_pi (const char **, const int) */
2262 * LoadPlugin "<Plugin>"
2264 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2266 char module_name[DATA_MAX_NAME_LEN];
2270 if ((0 != ci->children_num) || (1 != ci->values_num)
2271 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2272 log_err ("LoadPlugin expects a single string argument.");
2276 value = ci->values[0].value.string;
2278 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2279 log_err ("Invalid module name %s", value);
2283 if (0 != init_pi (perl_argc, perl_argv))
2286 assert (NULL != perl_threads);
2287 assert (NULL != perl_threads->head);
2289 aTHX = perl_threads->head->interp;
2291 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2292 load_module (PERL_LOADMOD_NOIMPORT,
2293 newSVpv (module_name, strlen (module_name)), Nullsv);
2295 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2300 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2304 if ((0 != ci->children_num) || (1 != ci->values_num)
2305 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2306 log_err ("BaseName expects a single string argument.");
2310 value = ci->values[0].value.string;
2312 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2313 sstrncpy (base_name, value, sizeof (base_name));
2315 } /* static int perl_config_basename (oconfig_item_it *) */
2318 * EnableDebugger "<Package>"|""
2320 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2324 if ((0 != ci->children_num) || (1 != ci->values_num)
2325 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2326 log_err ("EnableDebugger expects a single string argument.");
2330 if (NULL != perl_threads) {
2331 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2335 value = ci->values[0].value.string;
2337 perl_argv = (char **)realloc (perl_argv,
2338 (++perl_argc + 1) * sizeof (char *));
2340 if (NULL == perl_argv) {
2341 log_err ("perl_config: Not enough memory.");
2345 if ('\0' == value[0]) {
2346 perl_argv[perl_argc - 1] = "-d";
2349 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2350 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2351 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2354 perl_argv[perl_argc] = NULL;
2356 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2359 * IncludeDir "<Dir>"
2361 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2365 if ((0 != ci->children_num) || (1 != ci->values_num)
2366 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2367 log_err ("IncludeDir expects a single string argument.");
2371 value = ci->values[0].value.string;
2374 perl_argv = (char **)realloc (perl_argv,
2375 (++perl_argc + 1) * sizeof (char *));
2377 if (NULL == perl_argv) {
2378 log_err ("perl_config: Not enough memory.");
2382 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2383 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2384 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2386 perl_argv[perl_argc] = NULL;
2389 /* prepend the directory to @INC */
2390 av_unshift (GvAVn (PL_incgv), 1);
2391 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2394 } /* static int perl_config_includedir (oconfig_item_it *) */
2399 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2409 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2410 log_err ("LoadPlugin expects a single string argument.");
2414 plugin = ci->values[0].value.string;
2417 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2421 log_err ("Unable to convert configuration to a Perl hash value.");
2422 config = (HV *)&PL_sv_undef;
2430 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2431 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2435 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2450 } /* static int perl_config_plugin (oconfig_item_it *) */
2452 static int perl_config (oconfig_item_t *ci)
2459 for (i = 0; i < ci->children_num; ++i) {
2460 oconfig_item_t *c = ci->children + i;
2461 int current_status = 0;
2463 if (NULL != perl_threads)
2464 aTHX = PERL_GET_CONTEXT;
2466 if (0 == strcasecmp (c->key, "LoadPlugin"))
2467 current_status = perl_config_loadplugin (aTHX_ c);
2468 else if (0 == strcasecmp (c->key, "BaseName"))
2469 current_status = perl_config_basename (aTHX_ c);
2470 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2471 current_status = perl_config_enabledebugger (aTHX_ c);
2472 else if (0 == strcasecmp (c->key, "IncludeDir"))
2473 current_status = perl_config_includedir (aTHX_ c);
2474 else if (0 == strcasecmp (c->key, "Plugin"))
2475 current_status = perl_config_plugin (aTHX_ c);
2478 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2482 /* fatal error - it's up to perl_config_* to clean up */
2483 if (0 > current_status) {
2484 log_err ("Configuration failed with a fatal error - "
2485 "plugin disabled!");
2486 return current_status;
2489 status += current_status;
2492 } /* static int perl_config (oconfig_item_t *) */
2494 void module_register (void)
2497 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2499 /* default options for the Perl interpreter */
2501 perl_argv[1] = "-MCollectd";
2502 perl_argv[2] = "-e";
2504 perl_argv[4] = NULL;
2506 plugin_register_complex_config ("perl", perl_config);
2508 } /* void module_register (void) */
2510 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */