2 * collectd - src/perl.c
3 * Copyright (C) 2007, 2008 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); \
151 /* if perl_threads != NULL perl_threads->head must
152 * point to the "base" thread */
153 static c_ithread_list_t *perl_threads = NULL;
155 /* the key used to store each pthread's ithread */
156 static pthread_key_t perl_thr_key;
158 static int perl_argc = 0;
159 static char **perl_argv = NULL;
161 static char base_name[DATA_MAX_NAME_LEN] = "";
168 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
169 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
170 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
171 { "Collectd::_plugin_write", Collectd__plugin_write },
172 { "Collectd::_plugin_flush", Collectd__plugin_flush },
173 { "Collectd::plugin_dispatch_notification",
174 Collectd_plugin_dispatch_notification },
175 { "Collectd::plugin_log", Collectd_plugin_log },
176 { "Collectd::_fc_register", Collectd__fc_register },
177 { "Collectd::call_by_name", Collectd_call_by_name },
186 { "Collectd::TYPE_INIT", PLUGIN_INIT },
187 { "Collectd::TYPE_READ", PLUGIN_READ },
188 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
189 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
190 { "Collectd::TYPE_LOG", PLUGIN_LOG },
191 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
192 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
193 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
194 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
195 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
196 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
197 { "Collectd::LOG_ERR", LOG_ERR },
198 { "Collectd::LOG_WARNING", LOG_WARNING },
199 { "Collectd::LOG_NOTICE", LOG_NOTICE },
200 { "Collectd::LOG_INFO", LOG_INFO },
201 { "Collectd::LOG_DEBUG", LOG_DEBUG },
202 { "Collectd::FC_MATCH", FC_MATCH },
203 { "Collectd::FC_TARGET", FC_TARGET },
204 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
205 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
206 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
207 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
208 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
209 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
210 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
211 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
212 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
213 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
214 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
223 { "Collectd::hostname_g", hostname_g },
232 { "Collectd::interval_g", &interval_g },
237 * Helper functions for data type conversion.
252 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
256 if ((NULL == hash) || (NULL == ds))
259 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
260 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
263 log_err ("hv2data_source: No DS name given.");
267 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
268 ds->type = SvIV (*tmp);
270 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
271 log_err ("hv2data_source: Invalid DS type.");
276 ds->type = DS_TYPE_COUNTER;
279 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
280 ds->min = SvNV (*tmp);
284 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
285 ds->max = SvNV (*tmp);
289 } /* static int hv2data_source (HV *, data_source_t *) */
291 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
293 const data_set_t *ds;
297 if ((NULL == name) || (NULL == array) || (NULL == value))
300 if (av_len (array) < len - 1)
301 len = av_len (array) + 1;
306 ds = plugin_get_ds (name);
308 log_err ("av2value: Unknown dataset \"%s\"", name);
312 if (ds->ds_num < len) {
313 log_warn ("av2value: Value length exceeds data set length.");
317 for (i = 0; i < len; ++i) {
318 SV **tmp = av_fetch (array, i, 0);
321 if (DS_TYPE_COUNTER == ds->ds[i].type)
322 value[i].counter = SvIV (*tmp);
324 value[i].gauge = SvNV (*tmp);
331 } /* static int av2value (char *, AV *, value_t *, int) */
336 * values => [ @values ],
340 * plugin_instance => $pinstance,
341 * type_instance => $tinstance,
344 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
348 if ((NULL == hash) || (NULL == vl))
351 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
352 log_err ("hv2value_list: No type given.");
356 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
358 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
359 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
360 log_err ("hv2value_list: No valid values given.");
365 AV *array = (AV *)SvRV (*tmp);
366 int len = av_len (array) + 1;
371 vl->values = (value_t *)smalloc (len * sizeof (value_t));
372 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
375 if (-1 == vl->values_len) {
381 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
382 vl->time = (time_t)SvIV (*tmp);
384 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
385 vl->interval = SvIV (*tmp);
387 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
388 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
390 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
392 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
393 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
395 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
396 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
397 sizeof (vl->plugin_instance));
399 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
400 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
401 sizeof (vl->type_instance));
403 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
405 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
409 if ((NULL == array) || (NULL == name) || (NULL == ds))
412 len = av_len (array);
415 log_err ("av2data_set: Invalid data set.");
419 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
420 ds->ds_num = len + 1;
422 for (i = 0; i <= len; ++i) {
423 SV **elem = av_fetch (array, i, 0);
426 log_err ("av2data_set: Failed to fetch data source %i.", i);
430 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
431 log_err ("av2data_set: Invalid data source.");
435 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
438 log_debug ("av2data_set: "
439 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
440 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
443 sstrncpy (ds->type, name, sizeof (ds->type));
445 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
450 * severity => $severity,
456 * plugin_instance => $instance,
457 * type_instance => $type_instance,
458 * meta => [ { name => <name>, value => <value> }, ... ]
461 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
463 notification_meta_t **m = meta;
465 int len = av_len (array);
468 for (i = 0; i <= len; ++i) {
469 SV **tmp = av_fetch (array, i, 0);
475 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
476 log_warn ("av2notification_meta: Skipping invalid "
477 "meta information.");
481 hash = (HV *)SvRV (*tmp);
483 *m = (notification_meta_t *)smalloc (sizeof (**m));
485 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
486 log_warn ("av2notification_meta: Skipping invalid "
487 "meta information.");
491 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
493 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
494 log_warn ("av2notification_meta: Skipping invalid "
495 "meta information.");
502 (*m)->nm_value.nm_double = SvNVX (*tmp);
503 (*m)->type = NM_TYPE_DOUBLE;
505 else if (SvUOK (*tmp)) {
506 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
507 (*m)->type = NM_TYPE_UNSIGNED_INT;
509 else if (SvIOK (*tmp)) {
510 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
511 (*m)->type = NM_TYPE_SIGNED_INT;
514 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
515 (*m)->type = NM_TYPE_STRING;
522 } /* static int av2notification_meta (AV *, notification_meta_t *) */
524 static int hv2notification (pTHX_ HV *hash, notification_t *n)
528 if ((NULL == hash) || (NULL == n))
531 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
532 n->severity = SvIV (*tmp);
534 n->severity = NOTIF_FAILURE;
536 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
537 n->time = (time_t)SvIV (*tmp);
539 n->time = time (NULL);
541 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
542 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
544 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
545 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
547 sstrncpy (n->host, hostname_g, sizeof (n->host));
549 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
550 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
552 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
553 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
554 sizeof (n->plugin_instance));
556 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
557 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
559 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
560 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
561 sizeof (n->type_instance));
564 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
565 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
566 log_warn ("hv2notification: Ignoring invalid meta information.");
570 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
571 plugin_notification_meta_free (n->meta);
578 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
580 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
584 if ((NULL == ds) || (NULL == array))
587 av_extend (array, ds->ds_num);
589 for (i = 0; i < ds->ds_num; ++i) {
590 HV *source = newHV ();
592 if (NULL == hv_store (source, "name", 4,
593 newSVpv (ds->ds[i].name, 0), 0))
596 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
599 if (! isnan (ds->ds[i].min))
600 if (NULL == hv_store (source, "min", 3,
601 newSVnv (ds->ds[i].min), 0))
604 if (! isnan (ds->ds[i].max))
605 if (NULL == hv_store (source, "max", 3,
606 newSVnv (ds->ds[i].max), 0))
609 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
613 } /* static int data_set2av (data_set_t *, AV *) */
615 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
622 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
625 len = vl->values_len;
627 if (ds->ds_num < len) {
628 log_warn ("value2av: Value length exceeds data set length.");
633 av_extend (values, len - 1);
635 for (i = 0; i < len; ++i) {
638 if (DS_TYPE_COUNTER == ds->ds[i].type)
639 val = newSViv (vl->values[i].counter);
641 val = newSVnv (vl->values[i].gauge);
643 if (NULL == av_store (values, i, val)) {
649 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
653 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
656 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
659 if ('\0' != vl->host[0])
660 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
663 if ('\0' != vl->plugin[0])
664 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
667 if ('\0' != vl->plugin_instance[0])
668 if (NULL == hv_store (hash, "plugin_instance", 15,
669 newSVpv (vl->plugin_instance, 0), 0))
672 if ('\0' != vl->type[0])
673 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
676 if ('\0' != vl->type_instance[0])
677 if (NULL == hv_store (hash, "type_instance", 13,
678 newSVpv (vl->type_instance, 0), 0))
681 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
683 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
693 av_extend (array, meta_num);
695 for (i = 0; NULL != meta; meta = meta->next, ++i) {
699 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
702 if (NM_TYPE_STRING == meta->type)
703 value = newSVpv (meta->nm_value.nm_string, 0);
704 else if (NM_TYPE_SIGNED_INT == meta->type)
705 value = newSViv (meta->nm_value.nm_signed_int);
706 else if (NM_TYPE_UNSIGNED_INT == meta->type)
707 value = newSVuv (meta->nm_value.nm_unsigned_int);
708 else if (NM_TYPE_DOUBLE == meta->type)
709 value = newSVnv (meta->nm_value.nm_double);
710 else if (NM_TYPE_BOOLEAN == meta->type)
711 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
715 if (NULL == hv_store (m, "value", 5, value, 0)) {
720 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
727 } /* static int notification_meta2av (notification_meta_t *, AV *) */
729 static int notification2hv (pTHX_ notification_t *n, HV *hash)
731 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
735 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
738 if ('\0' != *n->message)
739 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
742 if ('\0' != *n->host)
743 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
746 if ('\0' != *n->plugin)
747 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
750 if ('\0' != *n->plugin_instance)
751 if (NULL == hv_store (hash, "plugin_instance", 15,
752 newSVpv (n->plugin_instance, 0), 0))
755 if ('\0' != *n->type)
756 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
759 if ('\0' != *n->type_instance)
760 if (NULL == hv_store (hash, "type_instance", 13,
761 newSVpv (n->type_instance, 0), 0))
764 if (NULL != n->meta) {
766 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
767 || (NULL == hv_store (hash, "meta", 4,
768 newRV_noinc ((SV *)meta), 0))) {
775 } /* static int notification2hv (notification_t *, HV *) */
777 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
784 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
788 if (0 < ci->values_num)
789 av_extend (values, ci->values_num);
791 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
797 for (i = 0; i < ci->values_num; ++i) {
800 switch (ci->values[i].type) {
801 case OCONFIG_TYPE_STRING:
802 value = newSVpv (ci->values[i].value.string, 0);
804 case OCONFIG_TYPE_NUMBER:
805 value = newSVnv ((NV)ci->values[i].value.number);
807 case OCONFIG_TYPE_BOOLEAN:
808 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
811 log_err ("oconfig_item2hv: Invalid value type %i.",
813 value = &PL_sv_undef;
816 if (NULL == av_store (values, i, value)) {
822 /* ignoring 'parent' member which is uninteresting in this case */
825 if (0 < ci->children_num)
826 av_extend (children, ci->children_num);
828 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
834 for (i = 0; i < ci->children_num; ++i) {
835 HV *child = newHV ();
837 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
843 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
850 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
853 * Internal functions.
856 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
858 if (base_name[0] == '\0')
859 status = ssnprintf (buf, buf_len, "%s", module);
861 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
862 if ((status < 0) || ((unsigned int)status >= buf_len))
865 } /* char *get_module_name */
868 * Add a plugin's data set definition.
870 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
876 if ((NULL == name) || (NULL == dataset))
879 if (0 != av2data_set (aTHX_ dataset, name, &ds))
882 ret = plugin_register_data_set (&ds);
886 } /* static int pplugin_register_data_set (char *, SV *) */
889 * Remove a plugin's data set definition.
891 static int pplugin_unregister_data_set (char *name)
895 return plugin_unregister_data_set (name);
896 } /* static int pplugin_unregister_data_set (char *) */
899 * Submit the values to the write functions.
901 static int pplugin_dispatch_values (pTHX_ HV *values)
903 value_list_t vl = VALUE_LIST_INIT;
910 if (0 != hv2value_list (aTHX_ values, &vl))
913 ret = plugin_dispatch_values (&vl);
917 } /* static int pplugin_dispatch_values (char *, HV *) */
920 * Submit the values to a single write function.
922 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
925 value_list_t vl = VALUE_LIST_INIT;
932 if (0 != hv2value_list (aTHX_ values, &vl))
935 if ((NULL != data_set)
936 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
939 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
941 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
942 NULL == plugin ? "<any>" : plugin, ret);
944 if (NULL != data_set)
948 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
951 * Dispatch a notification.
953 static int pplugin_dispatch_notification (pTHX_ HV *notif)
962 memset (&n, 0, sizeof (n));
964 if (0 != hv2notification (aTHX_ notif, &n))
967 ret = plugin_dispatch_notification (&n);
968 plugin_notification_meta_free (n.meta);
970 } /* static int pplugin_dispatch_notification (HV *) */
973 * Call all working functions of the given type.
975 static int pplugin_call_all (pTHX_ int type, ...)
984 if ((type < 0) || (type >= PLUGIN_TYPES))
994 XPUSHs (sv_2mortal (newSViv ((IV)type)));
996 if (PLUGIN_WRITE == type) {
998 * $_[0] = $plugin_type;
1013 * values => [ $v1, ... ],
1015 * host => $hostname,
1016 * plugin => $plugin,
1018 * plugin_instance => $instance,
1019 * type_instance => $type_instance
1028 ds = va_arg (ap, data_set_t *);
1029 vl = va_arg (ap, value_list_t *);
1031 if (-1 == data_set2av (aTHX_ ds, pds)) {
1034 pds = (AV *)&PL_sv_undef;
1038 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1041 pvl = (HV *)&PL_sv_undef;
1045 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1046 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1047 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1049 else if (PLUGIN_LOG == type) {
1055 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1056 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1058 else if (PLUGIN_NOTIF == type) {
1062 * severity => $severity,
1066 * plugin => $plugin,
1068 * plugin_instance => $instance,
1069 * type_instance => $type_instance
1073 HV *notif = newHV ();
1075 n = va_arg (ap, notification_t *);
1077 if (-1 == notification2hv (aTHX_ n, notif)) {
1080 notif = (HV *)&PL_sv_undef;
1084 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1086 else if (PLUGIN_FLUSH == type) {
1089 * $_[1] = $identifier;
1091 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1092 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1097 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1112 } /* static int pplugin_call_all (int, ...) */
1115 * collectd's perl interpreter based thread implementation.
1117 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1120 /* must be called with perl_threads->mutex locked */
1121 static void c_ithread_destroy (c_ithread_t *ithread)
1123 dTHXa (ithread->interp);
1125 assert (NULL != perl_threads);
1127 PERL_SET_CONTEXT (aTHX);
1128 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1133 --perl_threads->number_of_threads;
1134 #endif /* COLLECT_DEBUG */
1136 perl_destruct (aTHX);
1139 if (NULL == ithread->prev)
1140 perl_threads->head = ithread->next;
1142 ithread->prev->next = ithread->next;
1144 if (NULL == ithread->next)
1145 perl_threads->tail = ithread->prev;
1147 ithread->next->prev = ithread->prev;
1151 } /* static void c_ithread_destroy (c_ithread_t *) */
1153 static void c_ithread_destructor (void *arg)
1155 c_ithread_t *ithread = (c_ithread_t *)arg;
1156 c_ithread_t *t = NULL;
1158 if (NULL == perl_threads)
1161 pthread_mutex_lock (&perl_threads->mutex);
1163 for (t = perl_threads->head; NULL != t; t = t->next)
1167 /* the ithread no longer exists */
1171 c_ithread_destroy (ithread);
1173 pthread_mutex_unlock (&perl_threads->mutex);
1175 } /* static void c_ithread_destructor (void *) */
1177 /* must be called with perl_threads->mutex locked */
1178 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1180 c_ithread_t *t = NULL;
1183 assert (NULL != perl_threads);
1185 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1186 memset (t, 0, sizeof (c_ithread_t));
1188 t->interp = (NULL == base)
1190 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1194 if ((NULL != base) && (NULL != PL_endav)) {
1195 av_clear (PL_endav);
1196 av_undef (PL_endav);
1201 ++perl_threads->number_of_threads;
1202 #endif /* COLLECT_DEBUG */
1206 if (NULL == perl_threads->tail) {
1207 perl_threads->head = t;
1211 perl_threads->tail->next = t;
1212 t->prev = perl_threads->tail;
1215 perl_threads->tail = t;
1217 pthread_setspecific (perl_thr_key, (const void *)t);
1219 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1222 * Filter chains implementation.
1225 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1232 notification_meta_t **meta = NULL;
1237 if ((type < 0) || (type >= FC_TYPES))
1240 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1243 va_start (ap, data);
1250 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1251 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1252 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1254 if (FC_CB_CREATE == cb_type) {
1257 * $_[1] = $user_data;
1260 HV *config = newHV ();
1262 ci = va_arg (ap, oconfig_item_t *);
1264 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1267 config = (HV *)&PL_sv_undef;
1271 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1273 else if (FC_CB_DESTROY == cb_type) {
1275 * $_[1] = $user_data;
1278 /* nothing to be done - the user data pointer
1279 * is pushed onto the stack later */
1281 else if (FC_CB_EXEC == cb_type) {
1286 * $_[3] = $user_data;
1294 ds = va_arg (ap, data_set_t *);
1295 vl = va_arg (ap, value_list_t *);
1296 meta = va_arg (ap, notification_meta_t **);
1298 if (0 != data_set2av (aTHX_ ds, pds)) {
1301 pds = (AV *)&PL_sv_undef;
1305 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1308 pvl = (HV *)&PL_sv_undef;
1315 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1318 pmeta = (AV *)&PL_sv_undef;
1323 pmeta = (AV *)&PL_sv_undef;
1326 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1327 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1328 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1331 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1335 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1337 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1338 assert (pmeta != NULL);
1340 plugin_notification_meta_free (*meta);
1341 av2notification_meta (aTHX_ pmeta, meta);
1348 /* the exec callbacks return a status, while
1349 * the others return a boolean value */
1350 if (FC_CB_EXEC == cb_type)
1352 else if (! SvTRUE (tmp))
1362 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1364 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1366 pfc_user_data_t *data;
1372 if (NULL == perl_threads)
1376 c_ithread_t *t = NULL;
1378 pthread_mutex_lock (&perl_threads->mutex);
1379 t = c_ithread_create (perl_threads->head->interp);
1380 pthread_mutex_unlock (&perl_threads->mutex);
1385 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1386 aTHX, perl_threads->number_of_threads);
1388 if ((1 != ci->values_num)
1389 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1390 log_warn ("A \"%s\" block expects a single string argument.",
1391 (FC_MATCH == type) ? "Match" : "Target");
1395 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1396 data->name = sstrdup (ci->values[0].value.string);
1397 data->user_data = newSV (0);
1399 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1402 PFC_USER_DATA_FREE (data);
1406 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1408 static int fc_destroy (int type, void **user_data)
1410 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1416 if ((NULL == perl_threads) || (NULL == data))
1420 c_ithread_t *t = NULL;
1422 pthread_mutex_lock (&perl_threads->mutex);
1423 t = c_ithread_create (perl_threads->head->interp);
1424 pthread_mutex_unlock (&perl_threads->mutex);
1429 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1430 aTHX, perl_threads->number_of_threads);
1432 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1434 PFC_USER_DATA_FREE (data);
1437 } /* static int fc_destroy (int, void **) */
1439 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1440 notification_meta_t **meta, void **user_data)
1442 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1446 if (NULL == perl_threads)
1449 assert (NULL != data);
1452 c_ithread_t *t = NULL;
1454 pthread_mutex_lock (&perl_threads->mutex);
1455 t = c_ithread_create (perl_threads->head->interp);
1456 pthread_mutex_unlock (&perl_threads->mutex);
1461 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1462 aTHX, perl_threads->number_of_threads);
1464 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1465 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1466 notification_meta_t **, void **) */
1468 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1470 return fc_create (FC_MATCH, ci, user_data);
1471 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1473 static int pmatch_destroy (void **user_data)
1475 return fc_destroy (FC_MATCH, user_data);
1476 } /* static int pmatch_destroy (void **) */
1478 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1479 notification_meta_t **meta, void **user_data)
1481 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1482 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1483 notification_meta_t **, void **) */
1485 static match_proc_t pmatch = {
1486 pmatch_create, pmatch_destroy, pmatch_match
1489 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1491 return fc_create (FC_TARGET, ci, user_data);
1492 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1494 static int ptarget_destroy (void **user_data)
1496 return fc_destroy (FC_TARGET, user_data);
1497 } /* static int ptarget_destroy (void **) */
1499 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1500 notification_meta_t **meta, void **user_data)
1502 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1503 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1504 notification_meta_t **, void **) */
1506 static target_proc_t ptarget = {
1507 ptarget_create, ptarget_destroy, ptarget_invoke
1511 * Exported Perl API.
1515 * Collectd::plugin_register_data_set (type, dataset).
1518 * type of the dataset
1521 * dataset to be registered
1523 static XS (Collectd_plugin_register_ds)
1530 log_warn ("Using plugin_register() to register new data-sets is "
1531 "deprecated - add new entries to a custom types.db instead.");
1534 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1538 log_debug ("Collectd::plugin_register_data_set: "
1539 "type = \"%s\", dataset = \"%s\"",
1540 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1544 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1545 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1549 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1557 } /* static XS (Collectd_plugin_register_ds) */
1560 * Collectd::plugin_unregister_data_set (type).
1563 * type of the dataset
1565 static XS (Collectd_plugin_unregister_ds)
1570 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1574 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1575 SvPV_nolen (ST (0)));
1577 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1581 } /* static XS (Collectd_plugin_register_ds) */
1584 * Collectd::plugin_dispatch_values (name, values).
1587 * name of the plugin
1590 * value list to submit
1592 static XS (Collectd_plugin_dispatch_values)
1602 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1603 "is deprecated - pass the type through values->{type}.");
1606 else if (1 != items) {
1607 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1611 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1612 SvPV_nolen (ST (values_idx)));
1614 values = ST (values_idx);
1616 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1617 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1621 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1624 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1625 newSVsv (ST (0)), 0))) {
1626 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1630 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1636 } /* static XS (Collectd_plugin_dispatch_values) */
1638 /* Collectd::plugin_write (plugin, ds, vl).
1641 * name of the plugin to call, may be 'undef'
1644 * data-set that describes the submitted values, may be 'undef'
1647 * value-list to be written
1649 static XS (Collectd__plugin_write)
1660 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1664 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1665 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1666 SvPV_nolen (ST (2)));
1668 if (! SvOK (ST (0)))
1671 plugin = SvPV_nolen (ST (0));
1674 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1675 ds_array = (AV *)SvRV (ds);
1676 else if (! SvOK (ds))
1679 log_err ("Collectd::plugin_write: Invalid data-set.");
1684 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1685 log_err ("Collectd::plugin_write: Invalid value-list.");
1689 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1695 } /* static XS (Collectd__plugin_write) */
1698 * Collectd::_plugin_flush (plugin, timeout, identifier).
1701 * name of the plugin to flush
1704 * timeout to use when flushing the data
1707 * data-set identifier to flush
1709 static XS (Collectd__plugin_flush)
1711 char *plugin = NULL;
1718 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1723 plugin = SvPV_nolen (ST (0));
1726 timeout = (int)SvIV (ST (1));
1729 id = SvPV_nolen (ST (2));
1731 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1732 "id = \"%s\"", plugin, timeout, id);
1734 if (0 == plugin_flush (plugin, timeout, id))
1738 } /* static XS (Collectd__plugin_flush) */
1741 * Collectd::plugin_dispatch_notification (notif).
1744 * notification to dispatch
1746 static XS (Collectd_plugin_dispatch_notification)
1755 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1759 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1760 SvPV_nolen (ST (0)));
1764 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1765 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1769 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1775 } /* static XS (Collectd_plugin_dispatch_notification) */
1778 * Collectd::plugin_log (level, message).
1781 * log level (LOG_DEBUG, ... LOG_ERR)
1786 static XS (Collectd_plugin_log)
1791 log_err ("Usage: Collectd::plugin_log(level, message)");
1795 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1797 } /* static XS (Collectd_plugin_log) */
1800 * Collectd::_fc_register (type, name)
1808 static XS (Collectd__fc_register)
1818 log_err ("Usage: Collectd::_fc_register(type, name)");
1822 type = SvIV (ST (0));
1823 name = SvPV_nolen (ST (1));
1825 if (FC_MATCH == type)
1826 ret = fc_register_match (name, pmatch);
1827 else if (FC_TARGET == type)
1828 ret = fc_register_target (name, ptarget);
1834 } /* static XS (Collectd_fc_register) */
1837 * Collectd::call_by_name (...).
1839 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1841 static XS (Collectd_call_by_name)
1846 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1847 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1852 name = SvPV_nolen (tmp);
1854 if (NULL == get_cv (name, 0)) {
1855 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1860 /* simply pass on the subroutine call without touching the stack,
1861 * thus leaving any arguments and return values in place */
1863 } /* static XS (Collectd_call_by_name) */
1866 * Interface to collectd.
1869 static int perl_init (void)
1873 if (NULL == perl_threads)
1877 c_ithread_t *t = NULL;
1879 pthread_mutex_lock (&perl_threads->mutex);
1880 t = c_ithread_create (perl_threads->head->interp);
1881 pthread_mutex_unlock (&perl_threads->mutex);
1886 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1887 aTHX, perl_threads->number_of_threads);
1888 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1889 } /* static int perl_init (void) */
1891 static int perl_read (void)
1895 if (NULL == perl_threads)
1899 c_ithread_t *t = NULL;
1901 pthread_mutex_lock (&perl_threads->mutex);
1902 t = c_ithread_create (perl_threads->head->interp);
1903 pthread_mutex_unlock (&perl_threads->mutex);
1908 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1909 aTHX, perl_threads->number_of_threads);
1910 return pplugin_call_all (aTHX_ PLUGIN_READ);
1911 } /* static int perl_read (void) */
1913 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1914 user_data_t __attribute__((unused)) *user_data)
1918 if (NULL == perl_threads)
1922 c_ithread_t *t = NULL;
1924 pthread_mutex_lock (&perl_threads->mutex);
1925 t = c_ithread_create (perl_threads->head->interp);
1926 pthread_mutex_unlock (&perl_threads->mutex);
1931 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1932 aTHX, perl_threads->number_of_threads);
1933 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1934 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1936 static void perl_log (int level, const char *msg)
1940 if (NULL == perl_threads)
1944 c_ithread_t *t = NULL;
1946 pthread_mutex_lock (&perl_threads->mutex);
1947 t = c_ithread_create (perl_threads->head->interp);
1948 pthread_mutex_unlock (&perl_threads->mutex);
1953 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1955 } /* static void perl_log (int, const char *) */
1957 static int perl_notify (const notification_t *notif)
1961 if (NULL == perl_threads)
1965 c_ithread_t *t = NULL;
1967 pthread_mutex_lock (&perl_threads->mutex);
1968 t = c_ithread_create (perl_threads->head->interp);
1969 pthread_mutex_unlock (&perl_threads->mutex);
1973 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1974 } /* static int perl_notify (const notification_t *) */
1976 static int perl_flush (int timeout, const char *identifier)
1980 if (NULL == perl_threads)
1984 c_ithread_t *t = NULL;
1986 pthread_mutex_lock (&perl_threads->mutex);
1987 t = c_ithread_create (perl_threads->head->interp);
1988 pthread_mutex_unlock (&perl_threads->mutex);
1992 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
1993 } /* static int perl_flush (const int) */
1995 static int perl_shutdown (void)
1997 c_ithread_t *t = NULL;
2003 plugin_unregister_complex_config ("perl");
2005 if (NULL == perl_threads)
2009 c_ithread_t *t = NULL;
2011 pthread_mutex_lock (&perl_threads->mutex);
2012 t = c_ithread_create (perl_threads->head->interp);
2013 pthread_mutex_unlock (&perl_threads->mutex);
2018 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2019 aTHX, perl_threads->number_of_threads);
2021 plugin_unregister_log ("perl");
2022 plugin_unregister_notification ("perl");
2023 plugin_unregister_init ("perl");
2024 plugin_unregister_read ("perl");
2025 plugin_unregister_write ("perl");
2026 plugin_unregister_flush ("perl");
2028 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2030 pthread_mutex_lock (&perl_threads->mutex);
2031 t = perl_threads->tail;
2034 c_ithread_t *thr = t;
2036 /* the pointer has to be advanced before destroying
2037 * the thread as this will free the memory */
2040 c_ithread_destroy (thr);
2043 pthread_mutex_unlock (&perl_threads->mutex);
2044 pthread_mutex_destroy (&perl_threads->mutex);
2046 sfree (perl_threads);
2048 pthread_key_delete (perl_thr_key);
2052 plugin_unregister_shutdown ("perl");
2054 } /* static void perl_shutdown (void) */
2057 * Access functions for global variables.
2059 * These functions implement the "magic" used to access
2060 * the global variables from Perl.
2063 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2065 char *pv = mg->mg_ptr;
2068 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2070 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2072 char *pv = mg->mg_ptr;
2073 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2075 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2077 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2079 int *iv = (int *)mg->mg_ptr;
2080 sv_setiv (var, *iv);
2082 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2084 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2086 int *iv = (int *)mg->mg_ptr;
2087 *iv = (int)SvIV (var);
2089 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2091 static MGVTBL g_pv_vtbl = {
2092 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2093 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2097 static MGVTBL g_iv_vtbl = {
2098 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2099 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2104 /* bootstrap the Collectd module */
2105 static void xs_init (pTHX)
2109 char *file = __FILE__;
2115 /* enable usage of Perl modules using shared libraries */
2116 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2119 for (i = 0; NULL != api[i].f; ++i)
2120 newXS (api[i].name, api[i].f, file);
2122 stash = gv_stashpv ("Collectd", 1);
2124 /* export "constants" */
2125 for (i = 0; '\0' != constants[i].name[0]; ++i)
2126 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2128 /* export global variables
2129 * by adding "magic" to the SV's representing the globale variables
2130 * perl is able to automagically call the get/set function when
2131 * accessing any such variable (this is basically the same as using
2133 /* global strings */
2134 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2135 tmp = get_sv (g_strings[i].name, 1);
2136 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2137 g_strings[i].var, 0);
2140 /* global integers */
2141 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2142 tmp = get_sv (g_integers[i].name, 1);
2143 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2144 (char *)g_integers[i].var, 0);
2147 } /* static void xs_init (pTHX) */
2149 /* Initialize the global Perl interpreter. */
2150 static int init_pi (int argc, char **argv)
2154 if (NULL != perl_threads)
2157 log_info ("Initializing Perl interpreter...");
2162 for (i = 0; i < argc; ++i)
2163 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2165 #endif /* COLLECT_DEBUG */
2167 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2168 log_err ("init_pi: pthread_key_create failed");
2170 /* this must not happen - cowardly giving up if it does */
2175 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2176 * triggers a "value computed is not used" warning by gcc. */
2179 PERL_SYS_INIT3 (&argc, &argv, &environ);
2181 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2182 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2184 pthread_mutex_init (&perl_threads->mutex, NULL);
2185 /* locking the mutex should not be necessary at this point
2186 * but let's just do it for the sake of completeness */
2187 pthread_mutex_lock (&perl_threads->mutex);
2189 perl_threads->head = c_ithread_create (NULL);
2190 perl_threads->tail = perl_threads->head;
2192 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2193 log_err ("init_pi: Not enough memory.");
2197 aTHX = perl_threads->head->interp;
2198 pthread_mutex_unlock (&perl_threads->mutex);
2200 perl_construct (aTHX);
2202 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2204 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2205 SV *err = get_sv ("@", 1);
2206 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2209 perl_destruct (perl_threads->head->interp);
2210 perl_free (perl_threads->head->interp);
2211 sfree (perl_threads);
2213 pthread_key_delete (perl_thr_key);
2217 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2218 sv_setpv (get_sv ("0", 0), "collectd");
2222 plugin_register_log ("perl", perl_log);
2223 plugin_register_notification ("perl", perl_notify);
2224 plugin_register_init ("perl", perl_init);
2226 plugin_register_read ("perl", perl_read);
2228 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2229 plugin_register_flush ("perl", perl_flush);
2230 plugin_register_shutdown ("perl", perl_shutdown);
2232 } /* static int init_pi (const char **, const int) */
2235 * LoadPlugin "<Plugin>"
2237 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2239 char module_name[DATA_MAX_NAME_LEN];
2243 if ((0 != ci->children_num) || (1 != ci->values_num)
2244 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2245 log_err ("LoadPlugin expects a single string argument.");
2249 value = ci->values[0].value.string;
2251 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2252 log_err ("Invalid module name %s", value);
2256 if (0 != init_pi (perl_argc, perl_argv))
2259 assert (NULL != perl_threads);
2260 assert (NULL != perl_threads->head);
2262 aTHX = perl_threads->head->interp;
2264 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2265 load_module (PERL_LOADMOD_NOIMPORT,
2266 newSVpv (module_name, strlen (module_name)), Nullsv);
2268 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2273 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2277 if ((0 != ci->children_num) || (1 != ci->values_num)
2278 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2279 log_err ("BaseName expects a single string argument.");
2283 value = ci->values[0].value.string;
2285 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2286 sstrncpy (base_name, value, sizeof (base_name));
2288 } /* static int perl_config_basename (oconfig_item_it *) */
2291 * EnableDebugger "<Package>"|""
2293 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2297 if ((0 != ci->children_num) || (1 != ci->values_num)
2298 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2299 log_err ("EnableDebugger expects a single string argument.");
2303 if (NULL != perl_threads) {
2304 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2308 value = ci->values[0].value.string;
2310 perl_argv = (char **)realloc (perl_argv,
2311 (++perl_argc + 1) * sizeof (char *));
2313 if (NULL == perl_argv) {
2314 log_err ("perl_config: Not enough memory.");
2318 if ('\0' == value[0]) {
2319 perl_argv[perl_argc - 1] = "-d";
2322 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2323 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2324 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2327 perl_argv[perl_argc] = NULL;
2329 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2332 * IncludeDir "<Dir>"
2334 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2338 if ((0 != ci->children_num) || (1 != ci->values_num)
2339 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2340 log_err ("IncludeDir expects a single string argument.");
2344 value = ci->values[0].value.string;
2347 perl_argv = (char **)realloc (perl_argv,
2348 (++perl_argc + 1) * sizeof (char *));
2350 if (NULL == perl_argv) {
2351 log_err ("perl_config: Not enough memory.");
2355 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2356 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2357 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2359 perl_argv[perl_argc] = NULL;
2362 /* prepend the directory to @INC */
2363 av_unshift (GvAVn (PL_incgv), 1);
2364 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2367 } /* static int perl_config_includedir (oconfig_item_it *) */
2372 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2382 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2383 log_err ("LoadPlugin expects a single string argument.");
2387 plugin = ci->values[0].value.string;
2390 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2394 log_err ("Unable to convert configuration to a Perl hash value.");
2395 config = (HV *)&PL_sv_undef;
2403 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2404 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2408 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2423 } /* static int perl_config_plugin (oconfig_item_it *) */
2425 static int perl_config (oconfig_item_t *ci)
2432 for (i = 0; i < ci->children_num; ++i) {
2433 oconfig_item_t *c = ci->children + i;
2434 int current_status = 0;
2436 if (NULL != perl_threads)
2437 aTHX = PERL_GET_CONTEXT;
2439 if (0 == strcasecmp (c->key, "LoadPlugin"))
2440 current_status = perl_config_loadplugin (aTHX_ c);
2441 else if (0 == strcasecmp (c->key, "BaseName"))
2442 current_status = perl_config_basename (aTHX_ c);
2443 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2444 current_status = perl_config_enabledebugger (aTHX_ c);
2445 else if (0 == strcasecmp (c->key, "IncludeDir"))
2446 current_status = perl_config_includedir (aTHX_ c);
2447 else if (0 == strcasecmp (c->key, "Plugin"))
2448 current_status = perl_config_plugin (aTHX_ c);
2451 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2455 /* fatal error - it's up to perl_config_* to clean up */
2456 if (0 > current_status) {
2457 log_err ("Configuration failed with a fatal error - "
2458 "plugin disabled!");
2459 return current_status;
2462 status += current_status;
2465 } /* static int perl_config (oconfig_item_t *) */
2467 void module_register (void)
2470 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2472 /* default options for the Perl interpreter */
2474 perl_argv[1] = "-MCollectd";
2475 perl_argv[2] = "-e";
2477 perl_argv[4] = NULL;
2479 plugin_register_complex_config ("perl", perl_config);
2481 } /* void module_register (void) */
2483 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */