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)))
396 vl->time = (time_t)SvIV (*tmp);
398 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
399 vl->interval = SvIV (*tmp);
401 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
402 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
404 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
406 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
407 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
409 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
410 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
411 sizeof (vl->plugin_instance));
413 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
414 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
415 sizeof (vl->type_instance));
417 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
419 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
423 if ((NULL == array) || (NULL == name) || (NULL == ds))
426 len = av_len (array);
429 log_err ("av2data_set: Invalid data set.");
433 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
434 ds->ds_num = len + 1;
436 for (i = 0; i <= len; ++i) {
437 SV **elem = av_fetch (array, i, 0);
440 log_err ("av2data_set: Failed to fetch data source %i.", i);
444 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
445 log_err ("av2data_set: Invalid data source.");
449 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
452 log_debug ("av2data_set: "
453 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
454 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
457 sstrncpy (ds->type, name, sizeof (ds->type));
459 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
464 * severity => $severity,
470 * plugin_instance => $instance,
471 * type_instance => $type_instance,
472 * meta => [ { name => <name>, value => <value> }, ... ]
475 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
477 notification_meta_t **m = meta;
479 int len = av_len (array);
482 for (i = 0; i <= len; ++i) {
483 SV **tmp = av_fetch (array, i, 0);
489 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
490 log_warn ("av2notification_meta: Skipping invalid "
491 "meta information.");
495 hash = (HV *)SvRV (*tmp);
497 *m = (notification_meta_t *)smalloc (sizeof (**m));
499 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
500 log_warn ("av2notification_meta: Skipping invalid "
501 "meta information.");
505 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
507 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
508 log_warn ("av2notification_meta: Skipping invalid "
509 "meta information.");
516 (*m)->nm_value.nm_double = SvNVX (*tmp);
517 (*m)->type = NM_TYPE_DOUBLE;
519 else if (SvUOK (*tmp)) {
520 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
521 (*m)->type = NM_TYPE_UNSIGNED_INT;
523 else if (SvIOK (*tmp)) {
524 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
525 (*m)->type = NM_TYPE_SIGNED_INT;
528 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
529 (*m)->type = NM_TYPE_STRING;
536 } /* static int av2notification_meta (AV *, notification_meta_t *) */
538 static int hv2notification (pTHX_ HV *hash, notification_t *n)
542 if ((NULL == hash) || (NULL == n))
545 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
546 n->severity = SvIV (*tmp);
548 n->severity = NOTIF_FAILURE;
550 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
551 n->time = (time_t)SvIV (*tmp);
553 n->time = time (NULL);
555 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
556 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
558 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
559 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
561 sstrncpy (n->host, hostname_g, sizeof (n->host));
563 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
564 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
566 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
567 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
568 sizeof (n->plugin_instance));
570 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
571 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
573 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
574 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
575 sizeof (n->type_instance));
578 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
579 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
580 log_warn ("hv2notification: Ignoring invalid meta information.");
584 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
585 plugin_notification_meta_free (n->meta);
592 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
594 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
598 if ((NULL == ds) || (NULL == array))
601 av_extend (array, ds->ds_num);
603 for (i = 0; i < ds->ds_num; ++i) {
604 HV *source = newHV ();
606 if (NULL == hv_store (source, "name", 4,
607 newSVpv (ds->ds[i].name, 0), 0))
610 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
613 if (! isnan (ds->ds[i].min))
614 if (NULL == hv_store (source, "min", 3,
615 newSVnv (ds->ds[i].min), 0))
618 if (! isnan (ds->ds[i].max))
619 if (NULL == hv_store (source, "max", 3,
620 newSVnv (ds->ds[i].max), 0))
623 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
627 } /* static int data_set2av (data_set_t *, AV *) */
629 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
636 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
639 len = vl->values_len;
641 if (ds->ds_num < len) {
642 log_warn ("value2av: Value length exceeds data set length.");
647 av_extend (values, len - 1);
649 for (i = 0; i < len; ++i) {
652 if (DS_TYPE_COUNTER == ds->ds[i].type)
653 val = newSViv (vl->values[i].counter);
654 else if (DS_TYPE_GAUGE == ds->ds[i].type)
655 val = newSVnv (vl->values[i].gauge);
656 else if (DS_TYPE_DERIVE == ds->ds[i].type)
657 val = newSViv (vl->values[i].derive);
658 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
659 val = newSViv (vl->values[i].absolute);
661 if (NULL == av_store (values, i, val)) {
667 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
671 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
674 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
677 if ('\0' != vl->host[0])
678 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
681 if ('\0' != vl->plugin[0])
682 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
685 if ('\0' != vl->plugin_instance[0])
686 if (NULL == hv_store (hash, "plugin_instance", 15,
687 newSVpv (vl->plugin_instance, 0), 0))
690 if ('\0' != vl->type[0])
691 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
694 if ('\0' != vl->type_instance[0])
695 if (NULL == hv_store (hash, "type_instance", 13,
696 newSVpv (vl->type_instance, 0), 0))
699 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
701 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
711 av_extend (array, meta_num);
713 for (i = 0; NULL != meta; meta = meta->next, ++i) {
717 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
720 if (NM_TYPE_STRING == meta->type)
721 value = newSVpv (meta->nm_value.nm_string, 0);
722 else if (NM_TYPE_SIGNED_INT == meta->type)
723 value = newSViv (meta->nm_value.nm_signed_int);
724 else if (NM_TYPE_UNSIGNED_INT == meta->type)
725 value = newSVuv (meta->nm_value.nm_unsigned_int);
726 else if (NM_TYPE_DOUBLE == meta->type)
727 value = newSVnv (meta->nm_value.nm_double);
728 else if (NM_TYPE_BOOLEAN == meta->type)
729 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
733 if (NULL == hv_store (m, "value", 5, value, 0)) {
738 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
745 } /* static int notification_meta2av (notification_meta_t *, AV *) */
747 static int notification2hv (pTHX_ notification_t *n, HV *hash)
749 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
753 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
756 if ('\0' != *n->message)
757 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
760 if ('\0' != *n->host)
761 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
764 if ('\0' != *n->plugin)
765 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
768 if ('\0' != *n->plugin_instance)
769 if (NULL == hv_store (hash, "plugin_instance", 15,
770 newSVpv (n->plugin_instance, 0), 0))
773 if ('\0' != *n->type)
774 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
777 if ('\0' != *n->type_instance)
778 if (NULL == hv_store (hash, "type_instance", 13,
779 newSVpv (n->type_instance, 0), 0))
782 if (NULL != n->meta) {
784 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
785 || (NULL == hv_store (hash, "meta", 4,
786 newRV_noinc ((SV *)meta), 0))) {
793 } /* static int notification2hv (notification_t *, HV *) */
795 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
802 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
806 if (0 < ci->values_num)
807 av_extend (values, ci->values_num);
809 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
815 for (i = 0; i < ci->values_num; ++i) {
818 switch (ci->values[i].type) {
819 case OCONFIG_TYPE_STRING:
820 value = newSVpv (ci->values[i].value.string, 0);
822 case OCONFIG_TYPE_NUMBER:
823 value = newSVnv ((NV)ci->values[i].value.number);
825 case OCONFIG_TYPE_BOOLEAN:
826 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
829 log_err ("oconfig_item2hv: Invalid value type %i.",
831 value = &PL_sv_undef;
834 if (NULL == av_store (values, i, value)) {
840 /* ignoring 'parent' member which is uninteresting in this case */
843 if (0 < ci->children_num)
844 av_extend (children, ci->children_num);
846 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
852 for (i = 0; i < ci->children_num; ++i) {
853 HV *child = newHV ();
855 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
861 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
868 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
871 * Internal functions.
874 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
876 if (base_name[0] == '\0')
877 status = ssnprintf (buf, buf_len, "%s", module);
879 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
880 if ((status < 0) || ((unsigned int)status >= buf_len))
883 } /* char *get_module_name */
886 * Add a plugin's data set definition.
888 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
894 if ((NULL == name) || (NULL == dataset))
897 if (0 != av2data_set (aTHX_ dataset, name, &ds))
900 ret = plugin_register_data_set (&ds);
904 } /* static int pplugin_register_data_set (char *, SV *) */
907 * Remove a plugin's data set definition.
909 static int pplugin_unregister_data_set (char *name)
913 return plugin_unregister_data_set (name);
914 } /* static int pplugin_unregister_data_set (char *) */
917 * Submit the values to the write functions.
919 static int pplugin_dispatch_values (pTHX_ HV *values)
921 value_list_t vl = VALUE_LIST_INIT;
928 if (0 != hv2value_list (aTHX_ values, &vl))
931 ret = plugin_dispatch_values (&vl);
935 } /* static int pplugin_dispatch_values (char *, HV *) */
938 * Submit the values to a single write function.
940 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
943 value_list_t vl = VALUE_LIST_INIT;
950 if (0 != hv2value_list (aTHX_ values, &vl))
953 if ((NULL != data_set)
954 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
957 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
959 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
960 NULL == plugin ? "<any>" : plugin, ret);
962 if (NULL != data_set)
966 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
969 * Dispatch a notification.
971 static int pplugin_dispatch_notification (pTHX_ HV *notif)
980 memset (&n, 0, sizeof (n));
982 if (0 != hv2notification (aTHX_ notif, &n))
985 ret = plugin_dispatch_notification (&n);
986 plugin_notification_meta_free (n.meta);
988 } /* static int pplugin_dispatch_notification (HV *) */
991 * Call all working functions of the given type.
993 static int pplugin_call_all (pTHX_ int type, ...)
1002 if ((type < 0) || (type >= PLUGIN_TYPES))
1005 va_start (ap, type);
1012 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1014 if (PLUGIN_WRITE == type) {
1016 * $_[0] = $plugin_type;
1031 * values => [ $v1, ... ],
1033 * host => $hostname,
1034 * plugin => $plugin,
1036 * plugin_instance => $instance,
1037 * type_instance => $type_instance
1046 ds = va_arg (ap, data_set_t *);
1047 vl = va_arg (ap, value_list_t *);
1049 if (-1 == data_set2av (aTHX_ ds, pds)) {
1052 pds = (AV *)&PL_sv_undef;
1056 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1059 pvl = (HV *)&PL_sv_undef;
1063 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1064 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1065 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1067 else if (PLUGIN_LOG == type) {
1073 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1074 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1076 else if (PLUGIN_NOTIF == type) {
1080 * severity => $severity,
1084 * plugin => $plugin,
1086 * plugin_instance => $instance,
1087 * type_instance => $type_instance
1091 HV *notif = newHV ();
1093 n = va_arg (ap, notification_t *);
1095 if (-1 == notification2hv (aTHX_ n, notif)) {
1098 notif = (HV *)&PL_sv_undef;
1102 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1104 else if (PLUGIN_FLUSH == type) {
1107 * $_[1] = $identifier;
1109 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1110 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1115 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1130 } /* static int pplugin_call_all (int, ...) */
1133 * collectd's perl interpreter based thread implementation.
1135 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1138 /* must be called with perl_threads->mutex locked */
1139 static void c_ithread_destroy (c_ithread_t *ithread)
1141 dTHXa (ithread->interp);
1143 assert (NULL != perl_threads);
1145 PERL_SET_CONTEXT (aTHX);
1146 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1151 --perl_threads->number_of_threads;
1152 #endif /* COLLECT_DEBUG */
1154 perl_destruct (aTHX);
1157 if (NULL == ithread->prev)
1158 perl_threads->head = ithread->next;
1160 ithread->prev->next = ithread->next;
1162 if (NULL == ithread->next)
1163 perl_threads->tail = ithread->prev;
1165 ithread->next->prev = ithread->prev;
1169 } /* static void c_ithread_destroy (c_ithread_t *) */
1171 static void c_ithread_destructor (void *arg)
1173 c_ithread_t *ithread = (c_ithread_t *)arg;
1174 c_ithread_t *t = NULL;
1176 if (NULL == perl_threads)
1179 pthread_mutex_lock (&perl_threads->mutex);
1181 for (t = perl_threads->head; NULL != t; t = t->next)
1185 /* the ithread no longer exists */
1189 c_ithread_destroy (ithread);
1191 pthread_mutex_unlock (&perl_threads->mutex);
1193 } /* static void c_ithread_destructor (void *) */
1195 /* must be called with perl_threads->mutex locked */
1196 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1198 c_ithread_t *t = NULL;
1201 assert (NULL != perl_threads);
1203 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1204 memset (t, 0, sizeof (c_ithread_t));
1206 t->interp = (NULL == base)
1208 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1212 if ((NULL != base) && (NULL != PL_endav)) {
1213 av_clear (PL_endav);
1214 av_undef (PL_endav);
1219 ++perl_threads->number_of_threads;
1220 #endif /* COLLECT_DEBUG */
1224 if (NULL == perl_threads->tail) {
1225 perl_threads->head = t;
1229 perl_threads->tail->next = t;
1230 t->prev = perl_threads->tail;
1233 perl_threads->tail = t;
1235 pthread_setspecific (perl_thr_key, (const void *)t);
1237 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1240 * Filter chains implementation.
1243 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1250 notification_meta_t **meta = NULL;
1255 if ((type < 0) || (type >= FC_TYPES))
1258 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1261 va_start (ap, data);
1268 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1269 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1270 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1272 if (FC_CB_CREATE == cb_type) {
1275 * $_[1] = $user_data;
1278 HV *config = newHV ();
1280 ci = va_arg (ap, oconfig_item_t *);
1282 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1285 config = (HV *)&PL_sv_undef;
1289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1291 else if (FC_CB_DESTROY == cb_type) {
1293 * $_[1] = $user_data;
1296 /* nothing to be done - the user data pointer
1297 * is pushed onto the stack later */
1299 else if (FC_CB_EXEC == cb_type) {
1304 * $_[3] = $user_data;
1312 ds = va_arg (ap, data_set_t *);
1313 vl = va_arg (ap, value_list_t *);
1314 meta = va_arg (ap, notification_meta_t **);
1316 if (0 != data_set2av (aTHX_ ds, pds)) {
1319 pds = (AV *)&PL_sv_undef;
1323 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1326 pvl = (HV *)&PL_sv_undef;
1333 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1336 pmeta = (AV *)&PL_sv_undef;
1341 pmeta = (AV *)&PL_sv_undef;
1344 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1345 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1346 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1349 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1353 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1355 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1356 assert (pmeta != NULL);
1358 plugin_notification_meta_free (*meta);
1359 av2notification_meta (aTHX_ pmeta, meta);
1366 /* the exec callbacks return a status, while
1367 * the others return a boolean value */
1368 if (FC_CB_EXEC == cb_type)
1370 else if (! SvTRUE (tmp))
1380 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1382 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1384 pfc_user_data_t *data;
1390 if (NULL == perl_threads)
1394 c_ithread_t *t = NULL;
1396 pthread_mutex_lock (&perl_threads->mutex);
1397 t = c_ithread_create (perl_threads->head->interp);
1398 pthread_mutex_unlock (&perl_threads->mutex);
1403 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1404 aTHX, perl_threads->number_of_threads);
1406 if ((1 != ci->values_num)
1407 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1408 log_warn ("A \"%s\" block expects a single string argument.",
1409 (FC_MATCH == type) ? "Match" : "Target");
1413 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1414 data->name = sstrdup (ci->values[0].value.string);
1415 data->user_data = newSV (0);
1417 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1420 PFC_USER_DATA_FREE (data);
1424 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1426 static int fc_destroy (int type, void **user_data)
1428 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1434 if ((NULL == perl_threads) || (NULL == data))
1438 c_ithread_t *t = NULL;
1440 pthread_mutex_lock (&perl_threads->mutex);
1441 t = c_ithread_create (perl_threads->head->interp);
1442 pthread_mutex_unlock (&perl_threads->mutex);
1447 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1448 aTHX, perl_threads->number_of_threads);
1450 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1452 PFC_USER_DATA_FREE (data);
1455 } /* static int fc_destroy (int, void **) */
1457 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1458 notification_meta_t **meta, void **user_data)
1460 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1464 if (NULL == perl_threads)
1467 assert (NULL != data);
1470 c_ithread_t *t = NULL;
1472 pthread_mutex_lock (&perl_threads->mutex);
1473 t = c_ithread_create (perl_threads->head->interp);
1474 pthread_mutex_unlock (&perl_threads->mutex);
1479 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1480 aTHX, perl_threads->number_of_threads);
1482 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1483 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1484 notification_meta_t **, void **) */
1486 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1488 return fc_create (FC_MATCH, ci, user_data);
1489 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1491 static int pmatch_destroy (void **user_data)
1493 return fc_destroy (FC_MATCH, user_data);
1494 } /* static int pmatch_destroy (void **) */
1496 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1497 notification_meta_t **meta, void **user_data)
1499 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1500 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1501 notification_meta_t **, void **) */
1503 static match_proc_t pmatch = {
1504 pmatch_create, pmatch_destroy, pmatch_match
1507 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1509 return fc_create (FC_TARGET, ci, user_data);
1510 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1512 static int ptarget_destroy (void **user_data)
1514 return fc_destroy (FC_TARGET, user_data);
1515 } /* static int ptarget_destroy (void **) */
1517 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1518 notification_meta_t **meta, void **user_data)
1520 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1521 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1522 notification_meta_t **, void **) */
1524 static target_proc_t ptarget = {
1525 ptarget_create, ptarget_destroy, ptarget_invoke
1529 * Exported Perl API.
1533 * Collectd::plugin_register_data_set (type, dataset).
1536 * type of the dataset
1539 * dataset to be registered
1541 static XS (Collectd_plugin_register_ds)
1548 log_warn ("Using plugin_register() to register new data-sets is "
1549 "deprecated - add new entries to a custom types.db instead.");
1552 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1556 log_debug ("Collectd::plugin_register_data_set: "
1557 "type = \"%s\", dataset = \"%s\"",
1558 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1562 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1563 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1567 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1575 } /* static XS (Collectd_plugin_register_ds) */
1578 * Collectd::plugin_unregister_data_set (type).
1581 * type of the dataset
1583 static XS (Collectd_plugin_unregister_ds)
1588 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1592 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1593 SvPV_nolen (ST (0)));
1595 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1599 } /* static XS (Collectd_plugin_register_ds) */
1602 * Collectd::plugin_dispatch_values (name, values).
1605 * name of the plugin
1608 * value list to submit
1610 static XS (Collectd_plugin_dispatch_values)
1620 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1621 "is deprecated - pass the type through values->{type}.");
1624 else if (1 != items) {
1625 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1629 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1630 SvPV_nolen (ST (values_idx)));
1632 values = ST (values_idx);
1634 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1635 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1639 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1642 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1643 newSVsv (ST (0)), 0))) {
1644 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1648 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1654 } /* static XS (Collectd_plugin_dispatch_values) */
1656 /* Collectd::plugin_write (plugin, ds, vl).
1659 * name of the plugin to call, may be 'undef'
1662 * data-set that describes the submitted values, may be 'undef'
1665 * value-list to be written
1667 static XS (Collectd__plugin_write)
1678 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1682 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1683 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1684 SvPV_nolen (ST (2)));
1686 if (! SvOK (ST (0)))
1689 plugin = SvPV_nolen (ST (0));
1692 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1693 ds_array = (AV *)SvRV (ds);
1694 else if (! SvOK (ds))
1697 log_err ("Collectd::plugin_write: Invalid data-set.");
1702 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1703 log_err ("Collectd::plugin_write: Invalid value-list.");
1707 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1713 } /* static XS (Collectd__plugin_write) */
1716 * Collectd::_plugin_flush (plugin, timeout, identifier).
1719 * name of the plugin to flush
1722 * timeout to use when flushing the data
1725 * data-set identifier to flush
1727 static XS (Collectd__plugin_flush)
1729 char *plugin = NULL;
1736 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1741 plugin = SvPV_nolen (ST (0));
1744 timeout = (int)SvIV (ST (1));
1747 id = SvPV_nolen (ST (2));
1749 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1750 "id = \"%s\"", plugin, timeout, id);
1752 if (0 == plugin_flush (plugin, timeout, id))
1756 } /* static XS (Collectd__plugin_flush) */
1759 * Collectd::plugin_dispatch_notification (notif).
1762 * notification to dispatch
1764 static XS (Collectd_plugin_dispatch_notification)
1773 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1777 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1778 SvPV_nolen (ST (0)));
1782 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1783 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1787 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1793 } /* static XS (Collectd_plugin_dispatch_notification) */
1796 * Collectd::plugin_log (level, message).
1799 * log level (LOG_DEBUG, ... LOG_ERR)
1804 static XS (Collectd_plugin_log)
1809 log_err ("Usage: Collectd::plugin_log(level, message)");
1813 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1815 } /* static XS (Collectd_plugin_log) */
1818 * Collectd::_fc_register (type, name)
1826 static XS (Collectd__fc_register)
1836 log_err ("Usage: Collectd::_fc_register(type, name)");
1840 type = SvIV (ST (0));
1841 name = SvPV_nolen (ST (1));
1843 if (FC_MATCH == type)
1844 ret = fc_register_match (name, pmatch);
1845 else if (FC_TARGET == type)
1846 ret = fc_register_target (name, ptarget);
1852 } /* static XS (Collectd_fc_register) */
1855 * Collectd::call_by_name (...).
1857 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1859 static XS (Collectd_call_by_name)
1864 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1865 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1870 name = SvPV_nolen (tmp);
1872 if (NULL == get_cv (name, 0)) {
1873 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1878 /* simply pass on the subroutine call without touching the stack,
1879 * thus leaving any arguments and return values in place */
1881 } /* static XS (Collectd_call_by_name) */
1884 * Interface to collectd.
1887 static int perl_init (void)
1891 if (NULL == perl_threads)
1895 c_ithread_t *t = NULL;
1897 pthread_mutex_lock (&perl_threads->mutex);
1898 t = c_ithread_create (perl_threads->head->interp);
1899 pthread_mutex_unlock (&perl_threads->mutex);
1904 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1905 aTHX, perl_threads->number_of_threads);
1906 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1907 } /* static int perl_init (void) */
1909 static int perl_read (void)
1913 if (NULL == perl_threads)
1917 c_ithread_t *t = NULL;
1919 pthread_mutex_lock (&perl_threads->mutex);
1920 t = c_ithread_create (perl_threads->head->interp);
1921 pthread_mutex_unlock (&perl_threads->mutex);
1926 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1927 aTHX, perl_threads->number_of_threads);
1928 return pplugin_call_all (aTHX_ PLUGIN_READ);
1929 } /* static int perl_read (void) */
1931 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1932 user_data_t __attribute__((unused)) *user_data)
1936 if (NULL == perl_threads)
1940 c_ithread_t *t = NULL;
1942 pthread_mutex_lock (&perl_threads->mutex);
1943 t = c_ithread_create (perl_threads->head->interp);
1944 pthread_mutex_unlock (&perl_threads->mutex);
1949 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1950 aTHX, perl_threads->number_of_threads);
1951 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1952 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1954 static void perl_log (int level, const char *msg,
1955 user_data_t __attribute__((unused)) *user_data)
1959 if (NULL == perl_threads)
1963 c_ithread_t *t = NULL;
1965 pthread_mutex_lock (&perl_threads->mutex);
1966 t = c_ithread_create (perl_threads->head->interp);
1967 pthread_mutex_unlock (&perl_threads->mutex);
1972 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1974 } /* static void perl_log (int, const char *) */
1976 static int perl_notify (const notification_t *notif,
1977 user_data_t __attribute__((unused)) *user_data)
1981 if (NULL == perl_threads)
1985 c_ithread_t *t = NULL;
1987 pthread_mutex_lock (&perl_threads->mutex);
1988 t = c_ithread_create (perl_threads->head->interp);
1989 pthread_mutex_unlock (&perl_threads->mutex);
1993 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1994 } /* static int perl_notify (const notification_t *) */
1996 static int perl_flush (int timeout, const char *identifier,
1997 user_data_t __attribute__((unused)) *user_data)
2001 if (NULL == perl_threads)
2005 c_ithread_t *t = NULL;
2007 pthread_mutex_lock (&perl_threads->mutex);
2008 t = c_ithread_create (perl_threads->head->interp);
2009 pthread_mutex_unlock (&perl_threads->mutex);
2013 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2014 } /* static int perl_flush (const int) */
2016 static int perl_shutdown (void)
2018 c_ithread_t *t = NULL;
2024 plugin_unregister_complex_config ("perl");
2026 if (NULL == perl_threads)
2030 c_ithread_t *t = NULL;
2032 pthread_mutex_lock (&perl_threads->mutex);
2033 t = c_ithread_create (perl_threads->head->interp);
2034 pthread_mutex_unlock (&perl_threads->mutex);
2039 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2040 aTHX, perl_threads->number_of_threads);
2042 plugin_unregister_log ("perl");
2043 plugin_unregister_notification ("perl");
2044 plugin_unregister_init ("perl");
2045 plugin_unregister_read ("perl");
2046 plugin_unregister_write ("perl");
2047 plugin_unregister_flush ("perl");
2049 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2051 pthread_mutex_lock (&perl_threads->mutex);
2052 t = perl_threads->tail;
2055 c_ithread_t *thr = t;
2057 /* the pointer has to be advanced before destroying
2058 * the thread as this will free the memory */
2061 c_ithread_destroy (thr);
2064 pthread_mutex_unlock (&perl_threads->mutex);
2065 pthread_mutex_destroy (&perl_threads->mutex);
2067 sfree (perl_threads);
2069 pthread_key_delete (perl_thr_key);
2073 plugin_unregister_shutdown ("perl");
2075 } /* static void perl_shutdown (void) */
2078 * Access functions for global variables.
2080 * These functions implement the "magic" used to access
2081 * the global variables from Perl.
2084 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2086 char *pv = mg->mg_ptr;
2089 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2091 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2093 char *pv = mg->mg_ptr;
2094 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2096 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2098 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2100 int *iv = (int *)mg->mg_ptr;
2101 sv_setiv (var, *iv);
2103 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2105 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2107 int *iv = (int *)mg->mg_ptr;
2108 *iv = (int)SvIV (var);
2110 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2112 static MGVTBL g_pv_vtbl = {
2113 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2114 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2118 static MGVTBL g_iv_vtbl = {
2119 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2120 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2125 /* bootstrap the Collectd module */
2126 static void xs_init (pTHX)
2130 char *file = __FILE__;
2136 /* enable usage of Perl modules using shared libraries */
2137 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2140 for (i = 0; NULL != api[i].f; ++i)
2141 newXS (api[i].name, api[i].f, file);
2143 stash = gv_stashpv ("Collectd", 1);
2145 /* export "constants" */
2146 for (i = 0; '\0' != constants[i].name[0]; ++i)
2147 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2149 /* export global variables
2150 * by adding "magic" to the SV's representing the globale variables
2151 * perl is able to automagically call the get/set function when
2152 * accessing any such variable (this is basically the same as using
2154 /* global strings */
2155 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2156 tmp = get_sv (g_strings[i].name, 1);
2157 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2158 g_strings[i].var, 0);
2161 /* global integers */
2162 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2163 tmp = get_sv (g_integers[i].name, 1);
2164 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2165 (char *)g_integers[i].var, 0);
2168 } /* static void xs_init (pTHX) */
2170 /* Initialize the global Perl interpreter. */
2171 static int init_pi (int argc, char **argv)
2175 if (NULL != perl_threads)
2178 log_info ("Initializing Perl interpreter...");
2183 for (i = 0; i < argc; ++i)
2184 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2186 #endif /* COLLECT_DEBUG */
2188 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2189 log_err ("init_pi: pthread_key_create failed");
2191 /* this must not happen - cowardly giving up if it does */
2196 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2197 * triggers a "value computed is not used" warning by gcc. */
2200 PERL_SYS_INIT3 (&argc, &argv, &environ);
2202 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2203 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2205 pthread_mutex_init (&perl_threads->mutex, NULL);
2206 /* locking the mutex should not be necessary at this point
2207 * but let's just do it for the sake of completeness */
2208 pthread_mutex_lock (&perl_threads->mutex);
2210 perl_threads->head = c_ithread_create (NULL);
2211 perl_threads->tail = perl_threads->head;
2213 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2214 log_err ("init_pi: Not enough memory.");
2218 aTHX = perl_threads->head->interp;
2219 pthread_mutex_unlock (&perl_threads->mutex);
2221 perl_construct (aTHX);
2223 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2225 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2226 SV *err = get_sv ("@", 1);
2227 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2230 perl_destruct (perl_threads->head->interp);
2231 perl_free (perl_threads->head->interp);
2232 sfree (perl_threads);
2234 pthread_key_delete (perl_thr_key);
2238 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2239 sv_setpv (get_sv ("0", 0), "collectd");
2243 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2244 plugin_register_notification ("perl", perl_notify,
2245 /* user_data = */ NULL);
2246 plugin_register_init ("perl", perl_init);
2248 plugin_register_read ("perl", perl_read);
2250 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2251 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2252 plugin_register_shutdown ("perl", perl_shutdown);
2254 } /* static int init_pi (const char **, const int) */
2257 * LoadPlugin "<Plugin>"
2259 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2261 char module_name[DATA_MAX_NAME_LEN];
2265 if ((0 != ci->children_num) || (1 != ci->values_num)
2266 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2267 log_err ("LoadPlugin expects a single string argument.");
2271 value = ci->values[0].value.string;
2273 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2274 log_err ("Invalid module name %s", value);
2278 if (0 != init_pi (perl_argc, perl_argv))
2281 assert (NULL != perl_threads);
2282 assert (NULL != perl_threads->head);
2284 aTHX = perl_threads->head->interp;
2286 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2287 load_module (PERL_LOADMOD_NOIMPORT,
2288 newSVpv (module_name, strlen (module_name)), Nullsv);
2290 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2295 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2299 if ((0 != ci->children_num) || (1 != ci->values_num)
2300 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2301 log_err ("BaseName expects a single string argument.");
2305 value = ci->values[0].value.string;
2307 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2308 sstrncpy (base_name, value, sizeof (base_name));
2310 } /* static int perl_config_basename (oconfig_item_it *) */
2313 * EnableDebugger "<Package>"|""
2315 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2319 if ((0 != ci->children_num) || (1 != ci->values_num)
2320 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2321 log_err ("EnableDebugger expects a single string argument.");
2325 if (NULL != perl_threads) {
2326 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2330 value = ci->values[0].value.string;
2332 perl_argv = (char **)realloc (perl_argv,
2333 (++perl_argc + 1) * sizeof (char *));
2335 if (NULL == perl_argv) {
2336 log_err ("perl_config: Not enough memory.");
2340 if ('\0' == value[0]) {
2341 perl_argv[perl_argc - 1] = "-d";
2344 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2345 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2346 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2349 perl_argv[perl_argc] = NULL;
2351 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2354 * IncludeDir "<Dir>"
2356 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2360 if ((0 != ci->children_num) || (1 != ci->values_num)
2361 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2362 log_err ("IncludeDir expects a single string argument.");
2366 value = ci->values[0].value.string;
2369 perl_argv = (char **)realloc (perl_argv,
2370 (++perl_argc + 1) * sizeof (char *));
2372 if (NULL == perl_argv) {
2373 log_err ("perl_config: Not enough memory.");
2377 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2378 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2379 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2381 perl_argv[perl_argc] = NULL;
2384 /* prepend the directory to @INC */
2385 av_unshift (GvAVn (PL_incgv), 1);
2386 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2389 } /* static int perl_config_includedir (oconfig_item_it *) */
2394 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2404 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2405 log_err ("LoadPlugin expects a single string argument.");
2409 plugin = ci->values[0].value.string;
2412 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2416 log_err ("Unable to convert configuration to a Perl hash value.");
2417 config = (HV *)&PL_sv_undef;
2425 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2426 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2430 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2445 } /* static int perl_config_plugin (oconfig_item_it *) */
2447 static int perl_config (oconfig_item_t *ci)
2454 for (i = 0; i < ci->children_num; ++i) {
2455 oconfig_item_t *c = ci->children + i;
2456 int current_status = 0;
2458 if (NULL != perl_threads)
2459 aTHX = PERL_GET_CONTEXT;
2461 if (0 == strcasecmp (c->key, "LoadPlugin"))
2462 current_status = perl_config_loadplugin (aTHX_ c);
2463 else if (0 == strcasecmp (c->key, "BaseName"))
2464 current_status = perl_config_basename (aTHX_ c);
2465 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2466 current_status = perl_config_enabledebugger (aTHX_ c);
2467 else if (0 == strcasecmp (c->key, "IncludeDir"))
2468 current_status = perl_config_includedir (aTHX_ c);
2469 else if (0 == strcasecmp (c->key, "Plugin"))
2470 current_status = perl_config_plugin (aTHX_ c);
2473 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2477 /* fatal error - it's up to perl_config_* to clean up */
2478 if (0 > current_status) {
2479 log_err ("Configuration failed with a fatal error - "
2480 "plugin disabled!");
2481 return current_status;
2484 status += current_status;
2487 } /* static int perl_config (oconfig_item_t *) */
2489 void module_register (void)
2492 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2494 /* default options for the Perl interpreter */
2496 perl_argv[1] = "-MCollectd";
2497 perl_argv[2] = "-e";
2499 perl_argv[4] = NULL;
2501 plugin_register_complex_config ("perl", perl_config);
2503 } /* void module_register (void) */
2505 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */