2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 Sebastian Harl
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Sebastian Harl <sh at tokkee.org>
28 * This plugin embeds a Perl interpreter into collectd and provides an
29 * interface for collectd plugins written in perl.
32 /* do not automatically get the thread specific perl interpreter */
33 #define PERL_NO_GET_CONTEXT
35 #define DONT_POISON_SPRINTF_YET 1
37 #undef DONT_POISON_SPRINTF_YET
39 #include "configfile.h"
48 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
49 # pragma GCC poison sprintf
54 /* Some versions of Perl define their own version of DEBUG... :-/ */
59 /* ... while we want the definition found in plugin.h. */
63 #include "filter_chain.h"
67 #if !defined(USE_ITHREADS)
68 # error "Perl does not support ithreads!"
69 #endif /* !defined(USE_ITHREADS) */
71 /* clear the Perl sub's stack frame
72 * (this should only be used inside an XSUB) */
73 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
77 #define PLUGIN_WRITE 2
78 #define PLUGIN_SHUTDOWN 3
80 #define PLUGIN_NOTIF 5
81 #define PLUGIN_FLUSH 6
83 #define PLUGIN_TYPES 7
85 #define PLUGIN_CONFIG 254
86 #define PLUGIN_DATASET 255
93 #define FC_CB_CREATE 0
94 #define FC_CB_DESTROY 1
99 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
100 #define log_info(...) INFO ("perl: " __VA_ARGS__)
101 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
102 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
104 /* this is defined in DynaLoader.a */
105 void boot_DynaLoader (PerlInterpreter *, CV *);
107 static XS (Collectd_plugin_register_ds);
108 static XS (Collectd_plugin_unregister_ds);
109 static XS (Collectd_plugin_dispatch_values);
110 static XS (Collectd_plugin_get_interval);
111 static XS (Collectd__plugin_write);
112 static XS (Collectd__plugin_flush);
113 static XS (Collectd_plugin_dispatch_notification);
114 static XS (Collectd_plugin_log);
115 static XS (Collectd__fc_register);
116 static XS (Collectd_call_by_name);
122 typedef struct c_ithread_s {
123 /* the thread's Perl interpreter */
124 PerlInterpreter *interp;
126 /* double linked list of threads */
127 struct c_ithread_s *prev;
128 struct c_ithread_s *next;
136 /* some usage stats */
137 int number_of_threads;
138 #endif /* COLLECT_DEBUG */
140 pthread_mutex_t mutex;
143 /* name / user_data for Perl matches / targets */
149 #define PFC_USER_DATA_FREE(data) \
151 sfree ((data)->name); \
152 if (NULL != (data)->user_data) \
153 sv_free ((data)->user_data); \
160 extern char **environ;
166 /* if perl_threads != NULL perl_threads->head must
167 * point to the "base" thread */
168 static c_ithread_list_t *perl_threads = NULL;
170 /* the key used to store each pthread's ithread */
171 static pthread_key_t perl_thr_key;
173 static int perl_argc = 0;
174 static char **perl_argv = NULL;
176 static char base_name[DATA_MAX_NAME_LEN] = "";
183 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
184 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
185 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
186 { "Collectd::plugin_get_interval", Collectd_plugin_get_interval },
187 { "Collectd::_plugin_write", Collectd__plugin_write },
188 { "Collectd::_plugin_flush", Collectd__plugin_flush },
189 { "Collectd::plugin_dispatch_notification",
190 Collectd_plugin_dispatch_notification },
191 { "Collectd::plugin_log", Collectd_plugin_log },
192 { "Collectd::_fc_register", Collectd__fc_register },
193 { "Collectd::call_by_name", Collectd_call_by_name },
202 { "Collectd::TYPE_INIT", PLUGIN_INIT },
203 { "Collectd::TYPE_READ", PLUGIN_READ },
204 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
205 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
206 { "Collectd::TYPE_LOG", PLUGIN_LOG },
207 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
208 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
209 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
210 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
211 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
212 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
213 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
214 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
215 { "Collectd::LOG_ERR", LOG_ERR },
216 { "Collectd::LOG_WARNING", LOG_WARNING },
217 { "Collectd::LOG_NOTICE", LOG_NOTICE },
218 { "Collectd::LOG_INFO", LOG_INFO },
219 { "Collectd::LOG_DEBUG", LOG_DEBUG },
220 { "Collectd::FC_MATCH", FC_MATCH },
221 { "Collectd::FC_TARGET", FC_TARGET },
222 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
223 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
224 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
225 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
226 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
227 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
228 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
229 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
230 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
231 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
232 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
241 { "Collectd::hostname_g", hostname_g },
246 * Helper functions for data type conversion.
261 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
265 if ((NULL == hash) || (NULL == ds))
268 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
269 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
272 log_err ("hv2data_source: No DS name given.");
276 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
277 ds->type = SvIV (*tmp);
279 if ((DS_TYPE_COUNTER != ds->type)
280 && (DS_TYPE_GAUGE != ds->type)
281 && (DS_TYPE_DERIVE != ds->type)
282 && (DS_TYPE_ABSOLUTE != ds->type)) {
283 log_err ("hv2data_source: Invalid DS type.");
288 ds->type = DS_TYPE_COUNTER;
291 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
292 ds->min = SvNV (*tmp);
296 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
297 ds->max = SvNV (*tmp);
301 } /* static int hv2data_source (HV *, data_source_t *) */
303 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
305 const data_set_t *ds;
309 if ((NULL == name) || (NULL == array) || (NULL == value))
312 if (av_len (array) < len - 1)
313 len = av_len (array) + 1;
318 ds = plugin_get_ds (name);
320 log_err ("av2value: Unknown dataset \"%s\"", name);
324 if (ds->ds_num < len) {
325 log_warn ("av2value: Value length exceeds data set length.");
329 for (i = 0; i < len; ++i) {
330 SV **tmp = av_fetch (array, i, 0);
333 if (DS_TYPE_COUNTER == ds->ds[i].type)
334 value[i].counter = SvIV (*tmp);
335 else if (DS_TYPE_GAUGE == ds->ds[i].type)
336 value[i].gauge = SvNV (*tmp);
337 else if (DS_TYPE_DERIVE == ds->ds[i].type)
338 value[i].derive = SvIV (*tmp);
339 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
340 value[i].absolute = SvIV (*tmp);
347 } /* static int av2value (char *, AV *, value_t *, int) */
352 * values => [ @values ],
356 * plugin_instance => $pinstance,
357 * type_instance => $tinstance,
360 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
364 if ((NULL == hash) || (NULL == vl))
367 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
368 log_err ("hv2value_list: No type given.");
372 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
374 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
375 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
376 log_err ("hv2value_list: No valid values given.");
381 AV *array = (AV *)SvRV (*tmp);
382 int len = av_len (array) + 1;
387 vl->values = (value_t *)smalloc (len * sizeof (value_t));
388 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
391 if (-1 == vl->values_len) {
397 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
399 double t = SvNV (*tmp);
400 vl->time = DOUBLE_TO_CDTIME_T (t);
403 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
405 double t = SvNV (*tmp);
406 vl->interval = DOUBLE_TO_CDTIME_T (t);
409 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
410 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
412 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
414 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
415 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
417 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
418 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
419 sizeof (vl->plugin_instance));
421 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
422 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
423 sizeof (vl->type_instance));
425 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
427 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
431 if ((NULL == array) || (NULL == name) || (NULL == ds))
434 len = av_len (array);
437 log_err ("av2data_set: Invalid data set.");
441 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
442 ds->ds_num = len + 1;
444 for (i = 0; i <= len; ++i) {
445 SV **elem = av_fetch (array, i, 0);
448 log_err ("av2data_set: Failed to fetch data source %i.", i);
452 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
453 log_err ("av2data_set: Invalid data source.");
457 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
460 log_debug ("av2data_set: "
461 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
462 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
465 sstrncpy (ds->type, name, sizeof (ds->type));
467 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
472 * severity => $severity,
478 * plugin_instance => $instance,
479 * type_instance => $type_instance,
480 * meta => [ { name => <name>, value => <value> }, ... ]
483 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
485 notification_meta_t **m = meta;
487 int len = av_len (array);
490 for (i = 0; i <= len; ++i) {
491 SV **tmp = av_fetch (array, i, 0);
497 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
498 log_warn ("av2notification_meta: Skipping invalid "
499 "meta information.");
503 hash = (HV *)SvRV (*tmp);
505 *m = (notification_meta_t *)smalloc (sizeof (**m));
507 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
508 log_warn ("av2notification_meta: Skipping invalid "
509 "meta information.");
513 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
515 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
516 log_warn ("av2notification_meta: Skipping invalid "
517 "meta information.");
524 (*m)->nm_value.nm_double = SvNVX (*tmp);
525 (*m)->type = NM_TYPE_DOUBLE;
527 else if (SvUOK (*tmp)) {
528 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
529 (*m)->type = NM_TYPE_UNSIGNED_INT;
531 else if (SvIOK (*tmp)) {
532 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
533 (*m)->type = NM_TYPE_SIGNED_INT;
536 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
537 (*m)->type = NM_TYPE_STRING;
544 } /* static int av2notification_meta (AV *, notification_meta_t *) */
546 static int hv2notification (pTHX_ HV *hash, notification_t *n)
550 if ((NULL == hash) || (NULL == n))
553 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
554 n->severity = SvIV (*tmp);
556 n->severity = NOTIF_FAILURE;
558 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
560 double t = SvNV (*tmp);
561 n->time = DOUBLE_TO_CDTIME_T (t);
566 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
567 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
569 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
570 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
572 sstrncpy (n->host, hostname_g, sizeof (n->host));
574 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
575 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
577 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
578 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
579 sizeof (n->plugin_instance));
581 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
582 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
584 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
585 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
586 sizeof (n->type_instance));
589 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
590 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
591 log_warn ("hv2notification: Ignoring invalid meta information.");
595 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
596 plugin_notification_meta_free (n->meta);
603 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
605 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
609 if ((NULL == ds) || (NULL == array))
612 av_extend (array, ds->ds_num);
614 for (i = 0; i < ds->ds_num; ++i) {
615 HV *source = newHV ();
617 if (NULL == hv_store (source, "name", 4,
618 newSVpv (ds->ds[i].name, 0), 0))
621 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
624 if (! isnan (ds->ds[i].min))
625 if (NULL == hv_store (source, "min", 3,
626 newSVnv (ds->ds[i].min), 0))
629 if (! isnan (ds->ds[i].max))
630 if (NULL == hv_store (source, "max", 3,
631 newSVnv (ds->ds[i].max), 0))
634 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
638 } /* static int data_set2av (data_set_t *, AV *) */
640 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
647 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
650 len = vl->values_len;
652 if (ds->ds_num < len) {
653 log_warn ("value2av: Value length exceeds data set length.");
658 av_extend (values, len - 1);
660 for (i = 0; i < len; ++i) {
663 if (DS_TYPE_COUNTER == ds->ds[i].type)
664 val = newSViv (vl->values[i].counter);
665 else if (DS_TYPE_GAUGE == ds->ds[i].type)
666 val = newSVnv (vl->values[i].gauge);
667 else if (DS_TYPE_DERIVE == ds->ds[i].type)
668 val = newSViv (vl->values[i].derive);
669 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
670 val = newSViv (vl->values[i].absolute);
672 if (NULL == av_store (values, i, val)) {
678 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
683 double t = CDTIME_T_TO_DOUBLE (vl->time);
684 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
689 double t = CDTIME_T_TO_DOUBLE (vl->interval);
690 if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0))
694 if ('\0' != vl->host[0])
695 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
698 if ('\0' != vl->plugin[0])
699 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
702 if ('\0' != vl->plugin_instance[0])
703 if (NULL == hv_store (hash, "plugin_instance", 15,
704 newSVpv (vl->plugin_instance, 0), 0))
707 if ('\0' != vl->type[0])
708 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
711 if ('\0' != vl->type_instance[0])
712 if (NULL == hv_store (hash, "type_instance", 13,
713 newSVpv (vl->type_instance, 0), 0))
716 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
718 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
728 av_extend (array, meta_num);
730 for (i = 0; NULL != meta; meta = meta->next, ++i) {
734 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
737 if (NM_TYPE_STRING == meta->type)
738 value = newSVpv (meta->nm_value.nm_string, 0);
739 else if (NM_TYPE_SIGNED_INT == meta->type)
740 value = newSViv (meta->nm_value.nm_signed_int);
741 else if (NM_TYPE_UNSIGNED_INT == meta->type)
742 value = newSVuv (meta->nm_value.nm_unsigned_int);
743 else if (NM_TYPE_DOUBLE == meta->type)
744 value = newSVnv (meta->nm_value.nm_double);
745 else if (NM_TYPE_BOOLEAN == meta->type)
746 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
750 if (NULL == hv_store (m, "value", 5, value, 0)) {
755 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
762 } /* static int notification_meta2av (notification_meta_t *, AV *) */
764 static int notification2hv (pTHX_ notification_t *n, HV *hash)
766 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
771 double t = CDTIME_T_TO_DOUBLE (n->time);
772 if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0))
776 if ('\0' != *n->message)
777 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
780 if ('\0' != *n->host)
781 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
784 if ('\0' != *n->plugin)
785 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
788 if ('\0' != *n->plugin_instance)
789 if (NULL == hv_store (hash, "plugin_instance", 15,
790 newSVpv (n->plugin_instance, 0), 0))
793 if ('\0' != *n->type)
794 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
797 if ('\0' != *n->type_instance)
798 if (NULL == hv_store (hash, "type_instance", 13,
799 newSVpv (n->type_instance, 0), 0))
802 if (NULL != n->meta) {
804 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
805 || (NULL == hv_store (hash, "meta", 4,
806 newRV_noinc ((SV *)meta), 0))) {
813 } /* static int notification2hv (notification_t *, HV *) */
815 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
822 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
826 if (0 < ci->values_num)
827 av_extend (values, ci->values_num);
829 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
835 for (i = 0; i < ci->values_num; ++i) {
838 switch (ci->values[i].type) {
839 case OCONFIG_TYPE_STRING:
840 value = newSVpv (ci->values[i].value.string, 0);
842 case OCONFIG_TYPE_NUMBER:
843 value = newSVnv ((NV)ci->values[i].value.number);
845 case OCONFIG_TYPE_BOOLEAN:
846 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
849 log_err ("oconfig_item2hv: Invalid value type %i.",
851 value = &PL_sv_undef;
854 if (NULL == av_store (values, i, value)) {
860 /* ignoring 'parent' member which is uninteresting in this case */
863 if (0 < ci->children_num)
864 av_extend (children, ci->children_num);
866 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
872 for (i = 0; i < ci->children_num; ++i) {
873 HV *child = newHV ();
875 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
881 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
888 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
891 * Internal functions.
894 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
896 if (base_name[0] == '\0')
897 status = ssnprintf (buf, buf_len, "%s", module);
899 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
900 if ((status < 0) || ((unsigned int)status >= buf_len))
903 } /* char *get_module_name */
906 * Add a plugin's data set definition.
908 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
914 if ((NULL == name) || (NULL == dataset))
917 if (0 != av2data_set (aTHX_ dataset, name, &ds))
920 ret = plugin_register_data_set (&ds);
924 } /* static int pplugin_register_data_set (char *, SV *) */
927 * Remove a plugin's data set definition.
929 static int pplugin_unregister_data_set (char *name)
933 return plugin_unregister_data_set (name);
934 } /* static int pplugin_unregister_data_set (char *) */
937 * Submit the values to the write functions.
939 static int pplugin_dispatch_values (pTHX_ HV *values)
941 value_list_t vl = VALUE_LIST_INIT;
948 if (0 != hv2value_list (aTHX_ values, &vl))
951 ret = plugin_dispatch_values (&vl);
955 } /* static int pplugin_dispatch_values (char *, HV *) */
958 * Submit the values to a single write function.
960 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
963 value_list_t vl = VALUE_LIST_INIT;
970 if (0 != hv2value_list (aTHX_ values, &vl))
973 if ((NULL != data_set)
974 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
977 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
979 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
980 NULL == plugin ? "<any>" : plugin, ret);
982 if (NULL != data_set)
986 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
989 * Dispatch a notification.
991 static int pplugin_dispatch_notification (pTHX_ HV *notif)
1000 memset (&n, 0, sizeof (n));
1002 if (0 != hv2notification (aTHX_ notif, &n))
1005 ret = plugin_dispatch_notification (&n);
1006 plugin_notification_meta_free (n.meta);
1008 } /* static int pplugin_dispatch_notification (HV *) */
1011 * Call all working functions of the given type.
1013 static int pplugin_call_all (pTHX_ int type, ...)
1022 if ((type < 0) || (type >= PLUGIN_TYPES))
1025 va_start (ap, type);
1032 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1034 if (PLUGIN_WRITE == type) {
1036 * $_[0] = $plugin_type;
1051 * values => [ $v1, ... ],
1053 * host => $hostname,
1054 * plugin => $plugin,
1056 * plugin_instance => $instance,
1057 * type_instance => $type_instance
1066 ds = va_arg (ap, data_set_t *);
1067 vl = va_arg (ap, value_list_t *);
1069 if (-1 == data_set2av (aTHX_ ds, pds)) {
1072 pds = (AV *)&PL_sv_undef;
1076 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1079 pvl = (HV *)&PL_sv_undef;
1083 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1084 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1085 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1087 else if (PLUGIN_LOG == type) {
1093 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1094 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1096 else if (PLUGIN_NOTIF == type) {
1100 * severity => $severity,
1104 * plugin => $plugin,
1106 * plugin_instance => $instance,
1107 * type_instance => $type_instance
1111 HV *notif = newHV ();
1113 n = va_arg (ap, notification_t *);
1115 if (-1 == notification2hv (aTHX_ n, notif)) {
1118 notif = (HV *)&PL_sv_undef;
1122 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1124 else if (PLUGIN_FLUSH == type) {
1129 * $_[1] = $identifier;
1131 timeout = va_arg (ap, cdtime_t);
1133 XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout))));
1134 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1139 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1154 } /* static int pplugin_call_all (int, ...) */
1157 * collectd's perl interpreter based thread implementation.
1159 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1162 /* must be called with perl_threads->mutex locked */
1163 static void c_ithread_destroy (c_ithread_t *ithread)
1165 dTHXa (ithread->interp);
1167 assert (NULL != perl_threads);
1169 PERL_SET_CONTEXT (aTHX);
1170 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1175 --perl_threads->number_of_threads;
1176 #endif /* COLLECT_DEBUG */
1178 perl_destruct (aTHX);
1181 if (NULL == ithread->prev)
1182 perl_threads->head = ithread->next;
1184 ithread->prev->next = ithread->next;
1186 if (NULL == ithread->next)
1187 perl_threads->tail = ithread->prev;
1189 ithread->next->prev = ithread->prev;
1193 } /* static void c_ithread_destroy (c_ithread_t *) */
1195 static void c_ithread_destructor (void *arg)
1197 c_ithread_t *ithread = (c_ithread_t *)arg;
1198 c_ithread_t *t = NULL;
1200 if (NULL == perl_threads)
1203 pthread_mutex_lock (&perl_threads->mutex);
1205 for (t = perl_threads->head; NULL != t; t = t->next)
1209 /* the ithread no longer exists */
1213 c_ithread_destroy (ithread);
1215 pthread_mutex_unlock (&perl_threads->mutex);
1217 } /* static void c_ithread_destructor (void *) */
1219 /* must be called with perl_threads->mutex locked */
1220 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1222 c_ithread_t *t = NULL;
1225 assert (NULL != perl_threads);
1227 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1228 memset (t, 0, sizeof (c_ithread_t));
1230 t->interp = (NULL == base)
1232 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1236 if ((NULL != base) && (NULL != PL_endav)) {
1237 av_clear (PL_endav);
1238 av_undef (PL_endav);
1243 ++perl_threads->number_of_threads;
1244 #endif /* COLLECT_DEBUG */
1248 if (NULL == perl_threads->tail) {
1249 perl_threads->head = t;
1253 perl_threads->tail->next = t;
1254 t->prev = perl_threads->tail;
1257 perl_threads->tail = t;
1259 pthread_setspecific (perl_thr_key, (const void *)t);
1261 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1264 * Filter chains implementation.
1267 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1274 notification_meta_t **meta = NULL;
1279 if ((type < 0) || (type >= FC_TYPES))
1282 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1285 va_start (ap, data);
1292 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1293 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1294 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1296 if (FC_CB_CREATE == cb_type) {
1299 * $_[1] = $user_data;
1302 HV *config = newHV ();
1304 ci = va_arg (ap, oconfig_item_t *);
1306 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1309 config = (HV *)&PL_sv_undef;
1313 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1315 else if (FC_CB_DESTROY == cb_type) {
1317 * $_[1] = $user_data;
1320 /* nothing to be done - the user data pointer
1321 * is pushed onto the stack later */
1323 else if (FC_CB_EXEC == cb_type) {
1328 * $_[3] = $user_data;
1336 ds = va_arg (ap, data_set_t *);
1337 vl = va_arg (ap, value_list_t *);
1338 meta = va_arg (ap, notification_meta_t **);
1340 if (0 != data_set2av (aTHX_ ds, pds)) {
1343 pds = (AV *)&PL_sv_undef;
1347 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1350 pvl = (HV *)&PL_sv_undef;
1357 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1360 pmeta = (AV *)&PL_sv_undef;
1365 pmeta = (AV *)&PL_sv_undef;
1368 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1369 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1370 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1373 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1377 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1379 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1380 assert (pmeta != NULL);
1382 plugin_notification_meta_free (*meta);
1383 av2notification_meta (aTHX_ pmeta, meta);
1390 /* the exec callbacks return a status, while
1391 * the others return a boolean value */
1392 if (FC_CB_EXEC == cb_type)
1394 else if (! SvTRUE (tmp))
1404 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1406 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1408 pfc_user_data_t *data;
1414 if (NULL == perl_threads)
1418 c_ithread_t *t = NULL;
1420 pthread_mutex_lock (&perl_threads->mutex);
1421 t = c_ithread_create (perl_threads->head->interp);
1422 pthread_mutex_unlock (&perl_threads->mutex);
1427 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1428 aTHX, perl_threads->number_of_threads);
1430 if ((1 != ci->values_num)
1431 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1432 log_warn ("A \"%s\" block expects a single string argument.",
1433 (FC_MATCH == type) ? "Match" : "Target");
1437 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1438 data->name = sstrdup (ci->values[0].value.string);
1439 data->user_data = newSV (0);
1441 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1444 PFC_USER_DATA_FREE (data);
1448 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1450 static int fc_destroy (int type, void **user_data)
1452 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1458 if ((NULL == perl_threads) || (NULL == data))
1462 c_ithread_t *t = NULL;
1464 pthread_mutex_lock (&perl_threads->mutex);
1465 t = c_ithread_create (perl_threads->head->interp);
1466 pthread_mutex_unlock (&perl_threads->mutex);
1471 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1472 aTHX, perl_threads->number_of_threads);
1474 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1476 PFC_USER_DATA_FREE (data);
1479 } /* static int fc_destroy (int, void **) */
1481 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1482 notification_meta_t **meta, void **user_data)
1484 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1488 if (NULL == perl_threads)
1491 assert (NULL != data);
1494 c_ithread_t *t = NULL;
1496 pthread_mutex_lock (&perl_threads->mutex);
1497 t = c_ithread_create (perl_threads->head->interp);
1498 pthread_mutex_unlock (&perl_threads->mutex);
1503 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1504 aTHX, perl_threads->number_of_threads);
1506 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1507 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1508 notification_meta_t **, void **) */
1510 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1512 return fc_create (FC_MATCH, ci, user_data);
1513 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1515 static int pmatch_destroy (void **user_data)
1517 return fc_destroy (FC_MATCH, user_data);
1518 } /* static int pmatch_destroy (void **) */
1520 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1521 notification_meta_t **meta, void **user_data)
1523 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1524 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1525 notification_meta_t **, void **) */
1527 static match_proc_t pmatch = {
1528 pmatch_create, pmatch_destroy, pmatch_match
1531 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1533 return fc_create (FC_TARGET, ci, user_data);
1534 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1536 static int ptarget_destroy (void **user_data)
1538 return fc_destroy (FC_TARGET, user_data);
1539 } /* static int ptarget_destroy (void **) */
1541 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1542 notification_meta_t **meta, void **user_data)
1544 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1545 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1546 notification_meta_t **, void **) */
1548 static target_proc_t ptarget = {
1549 ptarget_create, ptarget_destroy, ptarget_invoke
1553 * Exported Perl API.
1557 * Collectd::plugin_register_data_set (type, dataset).
1560 * type of the dataset
1563 * dataset to be registered
1565 static XS (Collectd_plugin_register_ds)
1572 log_warn ("Using plugin_register() to register new data-sets is "
1573 "deprecated - add new entries to a custom types.db instead.");
1576 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1580 log_debug ("Collectd::plugin_register_data_set: "
1581 "type = \"%s\", dataset = \"%s\"",
1582 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1586 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1587 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1591 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1599 } /* static XS (Collectd_plugin_register_ds) */
1602 * Collectd::plugin_unregister_data_set (type).
1605 * type of the dataset
1607 static XS (Collectd_plugin_unregister_ds)
1612 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1616 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1617 SvPV_nolen (ST (0)));
1619 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1623 } /* static XS (Collectd_plugin_register_ds) */
1626 * Collectd::plugin_dispatch_values (name, values).
1629 * name of the plugin
1632 * value list to submit
1634 static XS (Collectd_plugin_dispatch_values)
1643 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1647 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1648 SvPV_nolen (ST (/* stack index = */ 0)));
1650 values = ST (/* stack index = */ 0);
1652 /* Make sure the argument is a hash reference. */
1653 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1654 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1661 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1667 } /* static XS (Collectd_plugin_dispatch_values) */
1670 * Collectd::plugin_get_interval ().
1672 static XS (Collectd_plugin_get_interval)
1676 /* make sure we don't get any unused variable warnings for 'items';
1677 * don't abort, though */
1679 log_err ("Usage: Collectd::plugin_get_interval()");
1681 XSRETURN_NV ((NV) CDTIME_T_TO_DOUBLE (plugin_get_interval ()));
1682 } /* static XS (Collectd_plugin_get_interval) */
1684 /* Collectd::plugin_write (plugin, ds, vl).
1687 * name of the plugin to call, may be 'undef'
1690 * data-set that describes the submitted values, may be 'undef'
1693 * value-list to be written
1695 static XS (Collectd__plugin_write)
1706 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1710 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1711 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1712 SvPV_nolen (ST (2)));
1714 if (! SvOK (ST (0)))
1717 plugin = SvPV_nolen (ST (0));
1720 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1721 ds_array = (AV *)SvRV (ds);
1722 else if (! SvOK (ds))
1725 log_err ("Collectd::plugin_write: Invalid data-set.");
1730 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1731 log_err ("Collectd::plugin_write: Invalid value-list.");
1735 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1741 } /* static XS (Collectd__plugin_write) */
1744 * Collectd::_plugin_flush (plugin, timeout, identifier).
1747 * name of the plugin to flush
1750 * timeout to use when flushing the data
1753 * data-set identifier to flush
1755 static XS (Collectd__plugin_flush)
1757 char *plugin = NULL;
1764 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1769 plugin = SvPV_nolen (ST (0));
1772 timeout = (int)SvIV (ST (1));
1775 id = SvPV_nolen (ST (2));
1777 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1778 "id = \"%s\"", plugin, timeout, id);
1780 if (0 == plugin_flush (plugin, timeout, id))
1784 } /* static XS (Collectd__plugin_flush) */
1787 * Collectd::plugin_dispatch_notification (notif).
1790 * notification to dispatch
1792 static XS (Collectd_plugin_dispatch_notification)
1801 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1805 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1806 SvPV_nolen (ST (0)));
1810 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1811 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1815 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1821 } /* static XS (Collectd_plugin_dispatch_notification) */
1824 * Collectd::plugin_log (level, message).
1827 * log level (LOG_DEBUG, ... LOG_ERR)
1832 static XS (Collectd_plugin_log)
1837 log_err ("Usage: Collectd::plugin_log(level, message)");
1841 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1843 } /* static XS (Collectd_plugin_log) */
1846 * Collectd::_fc_register (type, name)
1854 static XS (Collectd__fc_register)
1864 log_err ("Usage: Collectd::_fc_register(type, name)");
1868 type = SvIV (ST (0));
1869 name = SvPV_nolen (ST (1));
1871 if (FC_MATCH == type)
1872 ret = fc_register_match (name, pmatch);
1873 else if (FC_TARGET == type)
1874 ret = fc_register_target (name, ptarget);
1880 } /* static XS (Collectd_fc_register) */
1883 * Collectd::call_by_name (...).
1885 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1887 static XS (Collectd_call_by_name)
1892 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1893 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1898 name = SvPV_nolen (tmp);
1900 if (NULL == get_cv (name, 0)) {
1901 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1906 /* simply pass on the subroutine call without touching the stack,
1907 * thus leaving any arguments and return values in place */
1909 } /* static XS (Collectd_call_by_name) */
1912 * Interface to collectd.
1915 static int perl_init (void)
1919 if (NULL == perl_threads)
1923 c_ithread_t *t = NULL;
1925 pthread_mutex_lock (&perl_threads->mutex);
1926 t = c_ithread_create (perl_threads->head->interp);
1927 pthread_mutex_unlock (&perl_threads->mutex);
1932 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1933 aTHX, perl_threads->number_of_threads);
1934 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1935 } /* static int perl_init (void) */
1937 static int perl_read (void)
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 /* Assert that we're not running as the base thread. Otherwise, we might
1955 * run into concurrency issues with c_ithread_create(). See
1956 * https://github.com/collectd/collectd/issues/9 for details. */
1957 assert (aTHX != perl_threads->head->interp);
1959 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1960 aTHX, perl_threads->number_of_threads);
1961 return pplugin_call_all (aTHX_ PLUGIN_READ);
1962 } /* static int perl_read (void) */
1964 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1965 user_data_t __attribute__((unused)) *user_data)
1970 if (NULL == perl_threads)
1974 c_ithread_t *t = NULL;
1976 pthread_mutex_lock (&perl_threads->mutex);
1977 t = c_ithread_create (perl_threads->head->interp);
1978 pthread_mutex_unlock (&perl_threads->mutex);
1983 /* Lock the base thread if this is not called from one of the read threads
1984 * to avoid race conditions with c_ithread_create(). See
1985 * https://github.com/collectd/collectd/issues/9 for details. */
1986 if (aTHX == perl_threads->head->interp)
1987 pthread_mutex_lock (&perl_threads->mutex);
1989 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1990 aTHX, perl_threads->number_of_threads);
1991 status = pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1993 if (aTHX == perl_threads->head->interp)
1994 pthread_mutex_unlock (&perl_threads->mutex);
1997 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1999 static void perl_log (int level, const char *msg,
2000 user_data_t __attribute__((unused)) *user_data)
2004 if (NULL == perl_threads)
2008 c_ithread_t *t = NULL;
2010 pthread_mutex_lock (&perl_threads->mutex);
2011 t = c_ithread_create (perl_threads->head->interp);
2012 pthread_mutex_unlock (&perl_threads->mutex);
2017 /* Lock the base thread if this is not called from one of the read threads
2018 * to avoid race conditions with c_ithread_create(). See
2019 * https://github.com/collectd/collectd/issues/9 for details. */
2020 if (aTHX == perl_threads->head->interp)
2021 pthread_mutex_lock (&perl_threads->mutex);
2023 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
2025 if (aTHX == perl_threads->head->interp)
2026 pthread_mutex_unlock (&perl_threads->mutex);
2029 } /* static void perl_log (int, const char *) */
2031 static int perl_notify (const notification_t *notif,
2032 user_data_t __attribute__((unused)) *user_data)
2036 if (NULL == perl_threads)
2040 c_ithread_t *t = NULL;
2042 pthread_mutex_lock (&perl_threads->mutex);
2043 t = c_ithread_create (perl_threads->head->interp);
2044 pthread_mutex_unlock (&perl_threads->mutex);
2048 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
2049 } /* static int perl_notify (const notification_t *) */
2051 static int perl_flush (cdtime_t timeout, const char *identifier,
2052 user_data_t __attribute__((unused)) *user_data)
2056 if (NULL == perl_threads)
2060 c_ithread_t *t = NULL;
2062 pthread_mutex_lock (&perl_threads->mutex);
2063 t = c_ithread_create (perl_threads->head->interp);
2064 pthread_mutex_unlock (&perl_threads->mutex);
2068 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2069 } /* static int perl_flush (const int) */
2071 static int perl_shutdown (void)
2073 c_ithread_t *t = NULL;
2079 plugin_unregister_complex_config ("perl");
2081 if (NULL == perl_threads)
2085 c_ithread_t *t = NULL;
2087 pthread_mutex_lock (&perl_threads->mutex);
2088 t = c_ithread_create (perl_threads->head->interp);
2089 pthread_mutex_unlock (&perl_threads->mutex);
2094 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2095 aTHX, perl_threads->number_of_threads);
2097 plugin_unregister_log ("perl");
2098 plugin_unregister_notification ("perl");
2099 plugin_unregister_init ("perl");
2100 plugin_unregister_read ("perl");
2101 plugin_unregister_write ("perl");
2102 plugin_unregister_flush ("perl");
2104 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2106 pthread_mutex_lock (&perl_threads->mutex);
2107 t = perl_threads->tail;
2110 c_ithread_t *thr = t;
2112 /* the pointer has to be advanced before destroying
2113 * the thread as this will free the memory */
2116 c_ithread_destroy (thr);
2119 pthread_mutex_unlock (&perl_threads->mutex);
2120 pthread_mutex_destroy (&perl_threads->mutex);
2122 sfree (perl_threads);
2124 pthread_key_delete (perl_thr_key);
2128 plugin_unregister_shutdown ("perl");
2130 } /* static void perl_shutdown (void) */
2133 * Access functions for global variables.
2135 * These functions implement the "magic" used to access
2136 * the global variables from Perl.
2139 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2141 char *pv = mg->mg_ptr;
2144 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2146 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2148 char *pv = mg->mg_ptr;
2149 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2151 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2153 static int g_interval_get (pTHX_ SV *var, MAGIC *mg)
2155 log_warn ("Accessing $interval_g is deprecated (and might not "
2156 "give the desired results) - plugin_get_interval() should "
2157 "be used instead.");
2158 sv_setnv (var, CDTIME_T_TO_DOUBLE (interval_g));
2160 } /* static int g_interval_get (pTHX_ SV *, MAGIC *) */
2162 static int g_interval_set (pTHX_ SV *var, MAGIC *mg)
2164 double nv = (double)SvNV (var);
2165 log_warn ("Accessing $interval_g is deprecated (and might not "
2166 "give the desired results) - plugin_get_interval() should "
2167 "be used instead.");
2168 interval_g = DOUBLE_TO_CDTIME_T (nv);
2170 } /* static int g_interval_set (pTHX_ SV *, MAGIC *) */
2172 static MGVTBL g_pv_vtbl = {
2173 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2174 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2178 static MGVTBL g_interval_vtbl = {
2179 g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL
2180 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2185 /* bootstrap the Collectd module */
2186 static void xs_init (pTHX)
2190 char *file = __FILE__;
2196 /* enable usage of Perl modules using shared libraries */
2197 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2200 for (i = 0; NULL != api[i].f; ++i)
2201 newXS (api[i].name, api[i].f, file);
2203 stash = gv_stashpv ("Collectd", 1);
2205 /* export "constants" */
2206 for (i = 0; '\0' != constants[i].name[0]; ++i)
2207 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2209 /* export global variables
2210 * by adding "magic" to the SV's representing the globale variables
2211 * perl is able to automagically call the get/set function when
2212 * accessing any such variable (this is basically the same as using
2214 /* global strings */
2215 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2216 tmp = get_sv (g_strings[i].name, 1);
2217 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2218 g_strings[i].var, 0);
2221 tmp = get_sv ("Collectd::interval_g", /* create = */ 1);
2222 sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext,
2223 /* vtbl = */ &g_interval_vtbl,
2224 /* name = */ NULL, /* namelen = */ 0);
2227 } /* static void xs_init (pTHX) */
2229 /* Initialize the global Perl interpreter. */
2230 static int init_pi (int argc, char **argv)
2234 if (NULL != perl_threads)
2237 log_info ("Initializing Perl interpreter...");
2242 for (i = 0; i < argc; ++i)
2243 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2245 #endif /* COLLECT_DEBUG */
2247 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2248 log_err ("init_pi: pthread_key_create failed");
2250 /* this must not happen - cowardly giving up if it does */
2255 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2256 * triggers a "value computed is not used" warning by gcc. */
2259 PERL_SYS_INIT3 (&argc, &argv, &environ);
2261 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2262 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2264 pthread_mutex_init (&perl_threads->mutex, NULL);
2265 /* locking the mutex should not be necessary at this point
2266 * but let's just do it for the sake of completeness */
2267 pthread_mutex_lock (&perl_threads->mutex);
2269 perl_threads->head = c_ithread_create (NULL);
2270 perl_threads->tail = perl_threads->head;
2272 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2273 log_err ("init_pi: Not enough memory.");
2277 aTHX = perl_threads->head->interp;
2278 pthread_mutex_unlock (&perl_threads->mutex);
2280 perl_construct (aTHX);
2282 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2284 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2285 SV *err = get_sv ("@", 1);
2286 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2289 perl_destruct (perl_threads->head->interp);
2290 perl_free (perl_threads->head->interp);
2291 sfree (perl_threads);
2293 pthread_key_delete (perl_thr_key);
2297 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2298 sv_setpv (get_sv ("0", 0), "collectd");
2302 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2303 plugin_register_notification ("perl", perl_notify,
2304 /* user_data = */ NULL);
2305 plugin_register_init ("perl", perl_init);
2307 plugin_register_read ("perl", perl_read);
2309 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2310 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2311 plugin_register_shutdown ("perl", perl_shutdown);
2313 } /* static int init_pi (const char **, const int) */
2316 * LoadPlugin "<Plugin>"
2318 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2320 char module_name[DATA_MAX_NAME_LEN];
2324 if ((0 != ci->children_num) || (1 != ci->values_num)
2325 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2326 log_err ("LoadPlugin expects a single string argument.");
2330 value = ci->values[0].value.string;
2332 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2333 log_err ("Invalid module name %s", value);
2337 if (0 != init_pi (perl_argc, perl_argv))
2340 assert (NULL != perl_threads);
2341 assert (NULL != perl_threads->head);
2343 aTHX = perl_threads->head->interp;
2345 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2346 load_module (PERL_LOADMOD_NOIMPORT,
2347 newSVpv (module_name, strlen (module_name)), Nullsv);
2349 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2354 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2358 if ((0 != ci->children_num) || (1 != ci->values_num)
2359 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2360 log_err ("BaseName expects a single string argument.");
2364 value = ci->values[0].value.string;
2366 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2367 sstrncpy (base_name, value, sizeof (base_name));
2369 } /* static int perl_config_basename (oconfig_item_it *) */
2372 * EnableDebugger "<Package>"|""
2374 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2378 if ((0 != ci->children_num) || (1 != ci->values_num)
2379 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2380 log_err ("EnableDebugger expects a single string argument.");
2384 if (NULL != perl_threads) {
2385 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2389 value = ci->values[0].value.string;
2391 perl_argv = (char **)realloc (perl_argv,
2392 (++perl_argc + 1) * sizeof (char *));
2394 if (NULL == perl_argv) {
2395 log_err ("perl_config: Not enough memory.");
2399 if ('\0' == value[0]) {
2400 perl_argv[perl_argc - 1] = "-d";
2403 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2404 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2405 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2408 perl_argv[perl_argc] = NULL;
2410 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2413 * IncludeDir "<Dir>"
2415 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2419 if ((0 != ci->children_num) || (1 != ci->values_num)
2420 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2421 log_err ("IncludeDir expects a single string argument.");
2425 value = ci->values[0].value.string;
2428 perl_argv = (char **)realloc (perl_argv,
2429 (++perl_argc + 1) * sizeof (char *));
2431 if (NULL == perl_argv) {
2432 log_err ("perl_config: Not enough memory.");
2436 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2437 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2438 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2440 perl_argv[perl_argc] = NULL;
2443 /* prepend the directory to @INC */
2444 av_unshift (GvAVn (PL_incgv), 1);
2445 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2448 } /* static int perl_config_includedir (oconfig_item_it *) */
2453 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2463 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2464 log_err ("LoadPlugin expects a single string argument.");
2468 plugin = ci->values[0].value.string;
2471 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2475 log_err ("Unable to convert configuration to a Perl hash value.");
2476 config = (HV *)&PL_sv_undef;
2484 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2485 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2489 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2504 } /* static int perl_config_plugin (oconfig_item_it *) */
2506 static int perl_config (oconfig_item_t *ci)
2513 for (i = 0; i < ci->children_num; ++i) {
2514 oconfig_item_t *c = ci->children + i;
2515 int current_status = 0;
2517 if (NULL != perl_threads)
2518 aTHX = PERL_GET_CONTEXT;
2520 if (0 == strcasecmp (c->key, "LoadPlugin"))
2521 current_status = perl_config_loadplugin (aTHX_ c);
2522 else if (0 == strcasecmp (c->key, "BaseName"))
2523 current_status = perl_config_basename (aTHX_ c);
2524 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2525 current_status = perl_config_enabledebugger (aTHX_ c);
2526 else if (0 == strcasecmp (c->key, "IncludeDir"))
2527 current_status = perl_config_includedir (aTHX_ c);
2528 else if (0 == strcasecmp (c->key, "Plugin"))
2529 current_status = perl_config_plugin (aTHX_ c);
2532 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2536 /* fatal error - it's up to perl_config_* to clean up */
2537 if (0 > current_status) {
2538 log_err ("Configuration failed with a fatal error - "
2539 "plugin disabled!");
2540 return current_status;
2543 status += current_status;
2546 } /* static int perl_config (oconfig_item_t *) */
2548 void module_register (void)
2551 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2553 /* default options for the Perl interpreter */
2555 perl_argv[1] = "-MCollectd";
2556 perl_argv[2] = "-e";
2558 perl_argv[4] = NULL;
2560 plugin_register_complex_config ("perl", perl_config);
2562 } /* void module_register (void) */
2564 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */