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)
1531 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1535 log_debug ("Collectd::plugin_register_data_set: "
1536 "type = \"%s\", dataset = \"%s\"",
1537 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1541 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1542 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1546 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1554 } /* static XS (Collectd_plugin_register_ds) */
1557 * Collectd::plugin_unregister_data_set (type).
1560 * type of the dataset
1562 static XS (Collectd_plugin_unregister_ds)
1567 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1571 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1572 SvPV_nolen (ST (0)));
1574 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1578 } /* static XS (Collectd_plugin_register_ds) */
1581 * Collectd::plugin_dispatch_values (name, values).
1584 * name of the plugin
1587 * value list to submit
1589 static XS (Collectd_plugin_dispatch_values)
1599 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1600 "is deprecated - pass the type through values->{type}.");
1603 else if (1 != items) {
1604 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1608 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1609 SvPV_nolen (ST (values_idx)));
1611 values = ST (values_idx);
1613 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1614 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1618 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1621 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1622 newSVsv (ST (0)), 0))) {
1623 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1627 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1633 } /* static XS (Collectd_plugin_dispatch_values) */
1635 /* Collectd::plugin_write (plugin, ds, vl).
1638 * name of the plugin to call, may be 'undef'
1641 * data-set that describes the submitted values, may be 'undef'
1644 * value-list to be written
1646 static XS (Collectd__plugin_write)
1657 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1661 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1662 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1663 SvPV_nolen (ST (2)));
1665 if (! SvOK (ST (0)))
1668 plugin = SvPV_nolen (ST (0));
1671 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1672 ds_array = (AV *)SvRV (ds);
1673 else if (! SvOK (ds))
1676 log_err ("Collectd::plugin_write: Invalid data-set.");
1681 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1682 log_err ("Collectd::plugin_write: Invalid value-list.");
1686 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1692 } /* static XS (Collectd__plugin_write) */
1695 * Collectd::_plugin_flush (plugin, timeout, identifier).
1698 * name of the plugin to flush
1701 * timeout to use when flushing the data
1704 * data-set identifier to flush
1706 static XS (Collectd__plugin_flush)
1708 char *plugin = NULL;
1715 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1720 plugin = SvPV_nolen (ST (0));
1723 timeout = (int)SvIV (ST (1));
1726 id = SvPV_nolen (ST (2));
1728 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1729 "id = \"%s\"", plugin, timeout, id);
1731 if (0 == plugin_flush (plugin, timeout, id))
1735 } /* static XS (Collectd__plugin_flush) */
1738 * Collectd::plugin_dispatch_notification (notif).
1741 * notification to dispatch
1743 static XS (Collectd_plugin_dispatch_notification)
1752 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1756 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1757 SvPV_nolen (ST (0)));
1761 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1762 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1766 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1772 } /* static XS (Collectd_plugin_dispatch_notification) */
1775 * Collectd::plugin_log (level, message).
1778 * log level (LOG_DEBUG, ... LOG_ERR)
1783 static XS (Collectd_plugin_log)
1788 log_err ("Usage: Collectd::plugin_log(level, message)");
1792 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1794 } /* static XS (Collectd_plugin_log) */
1797 * Collectd::_fc_register (type, name)
1805 static XS (Collectd__fc_register)
1815 log_err ("Usage: Collectd::_fc_register(type, name)");
1819 type = SvIV (ST (0));
1820 name = SvPV_nolen (ST (1));
1822 if (FC_MATCH == type)
1823 ret = fc_register_match (name, pmatch);
1824 else if (FC_TARGET == type)
1825 ret = fc_register_target (name, ptarget);
1831 } /* static XS (Collectd_fc_register) */
1834 * Collectd::call_by_name (...).
1836 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1838 static XS (Collectd_call_by_name)
1843 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1844 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1849 name = SvPV_nolen (tmp);
1851 if (NULL == get_cv (name, 0)) {
1852 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1857 /* simply pass on the subroutine call without touching the stack,
1858 * thus leaving any arguments and return values in place */
1860 } /* static XS (Collectd_call_by_name) */
1863 * Interface to collectd.
1866 static int perl_init (void)
1870 if (NULL == perl_threads)
1874 c_ithread_t *t = NULL;
1876 pthread_mutex_lock (&perl_threads->mutex);
1877 t = c_ithread_create (perl_threads->head->interp);
1878 pthread_mutex_unlock (&perl_threads->mutex);
1883 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1884 aTHX, perl_threads->number_of_threads);
1885 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1886 } /* static int perl_init (void) */
1888 static int perl_read (void)
1892 if (NULL == perl_threads)
1896 c_ithread_t *t = NULL;
1898 pthread_mutex_lock (&perl_threads->mutex);
1899 t = c_ithread_create (perl_threads->head->interp);
1900 pthread_mutex_unlock (&perl_threads->mutex);
1905 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1906 aTHX, perl_threads->number_of_threads);
1907 return pplugin_call_all (aTHX_ PLUGIN_READ);
1908 } /* static int perl_read (void) */
1910 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1914 if (NULL == perl_threads)
1918 c_ithread_t *t = NULL;
1920 pthread_mutex_lock (&perl_threads->mutex);
1921 t = c_ithread_create (perl_threads->head->interp);
1922 pthread_mutex_unlock (&perl_threads->mutex);
1927 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1928 aTHX, perl_threads->number_of_threads);
1929 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1930 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1932 static void perl_log (int level, const char *msg)
1936 if (NULL == perl_threads)
1940 c_ithread_t *t = NULL;
1942 pthread_mutex_lock (&perl_threads->mutex);
1943 t = c_ithread_create (perl_threads->head->interp);
1944 pthread_mutex_unlock (&perl_threads->mutex);
1949 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1951 } /* static void perl_log (int, const char *) */
1953 static int perl_notify (const notification_t *notif)
1957 if (NULL == perl_threads)
1961 c_ithread_t *t = NULL;
1963 pthread_mutex_lock (&perl_threads->mutex);
1964 t = c_ithread_create (perl_threads->head->interp);
1965 pthread_mutex_unlock (&perl_threads->mutex);
1969 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1970 } /* static int perl_notify (const notification_t *) */
1972 static int perl_flush (int timeout, const char *identifier)
1976 if (NULL == perl_threads)
1980 c_ithread_t *t = NULL;
1982 pthread_mutex_lock (&perl_threads->mutex);
1983 t = c_ithread_create (perl_threads->head->interp);
1984 pthread_mutex_unlock (&perl_threads->mutex);
1988 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
1989 } /* static int perl_flush (const int) */
1991 static int perl_shutdown (void)
1993 c_ithread_t *t = NULL;
1999 plugin_unregister_complex_config ("perl");
2001 if (NULL == perl_threads)
2005 c_ithread_t *t = NULL;
2007 pthread_mutex_lock (&perl_threads->mutex);
2008 t = c_ithread_create (perl_threads->head->interp);
2009 pthread_mutex_unlock (&perl_threads->mutex);
2014 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2015 aTHX, perl_threads->number_of_threads);
2017 plugin_unregister_log ("perl");
2018 plugin_unregister_notification ("perl");
2019 plugin_unregister_init ("perl");
2020 plugin_unregister_read ("perl");
2021 plugin_unregister_write ("perl");
2022 plugin_unregister_flush ("perl");
2024 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2026 pthread_mutex_lock (&perl_threads->mutex);
2027 t = perl_threads->tail;
2030 c_ithread_t *thr = t;
2032 /* the pointer has to be advanced before destroying
2033 * the thread as this will free the memory */
2036 c_ithread_destroy (thr);
2039 pthread_mutex_unlock (&perl_threads->mutex);
2040 pthread_mutex_destroy (&perl_threads->mutex);
2042 sfree (perl_threads);
2044 pthread_key_delete (perl_thr_key);
2048 plugin_unregister_shutdown ("perl");
2050 } /* static void perl_shutdown (void) */
2053 * Access functions for global variables.
2055 * These functions implement the "magic" used to access
2056 * the global variables from Perl.
2059 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2061 char *pv = mg->mg_ptr;
2064 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2066 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2068 char *pv = mg->mg_ptr;
2069 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2071 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2073 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2075 int *iv = (int *)mg->mg_ptr;
2076 sv_setiv (var, *iv);
2078 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2080 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2082 int *iv = (int *)mg->mg_ptr;
2083 *iv = (int)SvIV (var);
2085 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2087 static MGVTBL g_pv_vtbl = {
2088 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2089 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2093 static MGVTBL g_iv_vtbl = {
2094 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2095 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2100 /* bootstrap the Collectd module */
2101 static void xs_init (pTHX)
2105 char *file = __FILE__;
2111 /* enable usage of Perl modules using shared libraries */
2112 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2115 for (i = 0; NULL != api[i].f; ++i)
2116 newXS (api[i].name, api[i].f, file);
2118 stash = gv_stashpv ("Collectd", 1);
2120 /* export "constants" */
2121 for (i = 0; '\0' != constants[i].name[0]; ++i)
2122 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2124 /* export global variables
2125 * by adding "magic" to the SV's representing the globale variables
2126 * perl is able to automagically call the get/set function when
2127 * accessing any such variable (this is basically the same as using
2129 /* global strings */
2130 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2131 tmp = get_sv (g_strings[i].name, 1);
2132 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2133 g_strings[i].var, 0);
2136 /* global integers */
2137 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2138 tmp = get_sv (g_integers[i].name, 1);
2139 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2140 (char *)g_integers[i].var, 0);
2143 } /* static void xs_init (pTHX) */
2145 /* Initialize the global Perl interpreter. */
2146 static int init_pi (int argc, char **argv)
2150 if (NULL != perl_threads)
2153 log_info ("Initializing Perl interpreter...");
2158 for (i = 0; i < argc; ++i)
2159 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2161 #endif /* COLLECT_DEBUG */
2163 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2164 log_err ("init_pi: pthread_key_create failed");
2166 /* this must not happen - cowardly giving up if it does */
2171 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2172 * triggers a "value computed is not used" warning by gcc. */
2175 PERL_SYS_INIT3 (&argc, &argv, &environ);
2177 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2178 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2180 pthread_mutex_init (&perl_threads->mutex, NULL);
2181 /* locking the mutex should not be necessary at this point
2182 * but let's just do it for the sake of completeness */
2183 pthread_mutex_lock (&perl_threads->mutex);
2185 perl_threads->head = c_ithread_create (NULL);
2186 perl_threads->tail = perl_threads->head;
2188 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2189 log_err ("init_pi: Not enough memory.");
2193 aTHX = perl_threads->head->interp;
2194 pthread_mutex_unlock (&perl_threads->mutex);
2196 perl_construct (aTHX);
2198 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2200 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2201 SV *err = get_sv ("@", 1);
2202 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2205 perl_destruct (perl_threads->head->interp);
2206 perl_free (perl_threads->head->interp);
2207 sfree (perl_threads);
2209 pthread_key_delete (perl_thr_key);
2213 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2214 sv_setpv (get_sv ("0", 0), "collectd");
2218 plugin_register_log ("perl", perl_log);
2219 plugin_register_notification ("perl", perl_notify);
2220 plugin_register_init ("perl", perl_init);
2222 plugin_register_read ("perl", perl_read);
2224 plugin_register_write ("perl", perl_write);
2225 plugin_register_flush ("perl", perl_flush);
2226 plugin_register_shutdown ("perl", perl_shutdown);
2228 } /* static int init_pi (const char **, const int) */
2231 * LoadPlugin "<Plugin>"
2233 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2235 char module_name[DATA_MAX_NAME_LEN];
2239 if ((0 != ci->children_num) || (1 != ci->values_num)
2240 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2241 log_err ("LoadPlugin expects a single string argument.");
2245 value = ci->values[0].value.string;
2247 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2248 log_err ("Invalid module name %s", value);
2252 if (0 != init_pi (perl_argc, perl_argv))
2255 assert (NULL != perl_threads);
2256 assert (NULL != perl_threads->head);
2258 aTHX = perl_threads->head->interp;
2260 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2261 load_module (PERL_LOADMOD_NOIMPORT,
2262 newSVpv (module_name, strlen (module_name)), Nullsv);
2264 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2269 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2273 if ((0 != ci->children_num) || (1 != ci->values_num)
2274 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2275 log_err ("BaseName expects a single string argument.");
2279 value = ci->values[0].value.string;
2281 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2282 sstrncpy (base_name, value, sizeof (base_name));
2284 } /* static int perl_config_basename (oconfig_item_it *) */
2287 * EnableDebugger "<Package>"|""
2289 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2293 if ((0 != ci->children_num) || (1 != ci->values_num)
2294 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2295 log_err ("EnableDebugger expects a single string argument.");
2299 if (NULL != perl_threads) {
2300 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2304 value = ci->values[0].value.string;
2306 perl_argv = (char **)realloc (perl_argv,
2307 (++perl_argc + 1) * sizeof (char *));
2309 if (NULL == perl_argv) {
2310 log_err ("perl_config: Not enough memory.");
2314 if ('\0' == value[0]) {
2315 perl_argv[perl_argc - 1] = "-d";
2318 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2319 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2320 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2323 perl_argv[perl_argc] = NULL;
2325 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2328 * IncludeDir "<Dir>"
2330 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2334 if ((0 != ci->children_num) || (1 != ci->values_num)
2335 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2336 log_err ("IncludeDir expects a single string argument.");
2340 value = ci->values[0].value.string;
2343 perl_argv = (char **)realloc (perl_argv,
2344 (++perl_argc + 1) * sizeof (char *));
2346 if (NULL == perl_argv) {
2347 log_err ("perl_config: Not enough memory.");
2351 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2352 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2353 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2355 perl_argv[perl_argc] = NULL;
2358 /* prepend the directory to @INC */
2359 av_unshift (GvAVn (PL_incgv), 1);
2360 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2363 } /* static int perl_config_includedir (oconfig_item_it *) */
2368 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2378 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2379 log_err ("LoadPlugin expects a single string argument.");
2383 plugin = ci->values[0].value.string;
2386 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2390 log_err ("Unable to convert configuration to a Perl hash value.");
2391 config = (HV *)&PL_sv_undef;
2399 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2400 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2404 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2419 } /* static int perl_config_plugin (oconfig_item_it *) */
2421 static int perl_config (oconfig_item_t *ci)
2428 for (i = 0; i < ci->children_num; ++i) {
2429 oconfig_item_t *c = ci->children + i;
2430 int current_status = 0;
2432 if (NULL != perl_threads)
2433 aTHX = PERL_GET_CONTEXT;
2435 if (0 == strcasecmp (c->key, "LoadPlugin"))
2436 current_status = perl_config_loadplugin (aTHX_ c);
2437 else if (0 == strcasecmp (c->key, "BaseName"))
2438 current_status = perl_config_basename (aTHX_ c);
2439 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2440 current_status = perl_config_enabledebugger (aTHX_ c);
2441 else if (0 == strcasecmp (c->key, "IncludeDir"))
2442 current_status = perl_config_includedir (aTHX_ c);
2443 else if (0 == strcasecmp (c->key, "Plugin"))
2444 current_status = perl_config_plugin (aTHX_ c);
2447 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2451 /* fatal error - it's up to perl_config_* to clean up */
2452 if (0 > current_status) {
2453 log_err ("Configuration failed with a fatal error - "
2454 "plugin disabled!");
2455 return current_status;
2458 status += current_status;
2461 } /* static int perl_config (oconfig_item_t *) */
2463 void module_register (void)
2466 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2468 /* default options for the Perl interpreter */
2470 perl_argv[1] = "-MCollectd";
2471 perl_argv[2] = "-e";
2473 perl_argv[4] = NULL;
2475 plugin_register_complex_config ("perl", perl_config);
2477 } /* void module_register (void) */
2479 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */