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. */
56 #if !defined(USE_ITHREADS)
57 # error "Perl does not support ithreads!"
58 #endif /* !defined(USE_ITHREADS) */
60 /* clear the Perl sub's stack frame
61 * (this should only be used inside an XSUB) */
62 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
66 #define PLUGIN_WRITE 2
67 #define PLUGIN_SHUTDOWN 3
69 #define PLUGIN_NOTIF 5
70 #define PLUGIN_FLUSH 6
72 #define PLUGIN_TYPES 7
74 #define PLUGIN_CONFIG 254
75 #define PLUGIN_DATASET 255
77 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
78 #define log_info(...) INFO ("perl: " __VA_ARGS__)
79 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
80 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
82 /* this is defined in DynaLoader.a */
83 void boot_DynaLoader (PerlInterpreter *, CV *);
85 static XS (Collectd_plugin_register_ds);
86 static XS (Collectd_plugin_unregister_ds);
87 static XS (Collectd_plugin_dispatch_values);
88 static XS (Collectd__plugin_flush);
89 static XS (Collectd_plugin_dispatch_notification);
90 static XS (Collectd_plugin_log);
91 static XS (Collectd_call_by_name);
97 typedef struct c_ithread_s {
98 /* the thread's Perl interpreter */
99 PerlInterpreter *interp;
101 /* double linked list of threads */
102 struct c_ithread_s *prev;
103 struct c_ithread_s *next;
111 /* some usage stats */
112 int number_of_threads;
113 #endif /* COLLECT_DEBUG */
115 pthread_mutex_t mutex;
122 /* if perl_threads != NULL perl_threads->head must
123 * point to the "base" thread */
124 static c_ithread_list_t *perl_threads = NULL;
126 /* the key used to store each pthread's ithread */
127 static pthread_key_t perl_thr_key;
129 static int perl_argc = 0;
130 static char **perl_argv = NULL;
132 static char base_name[DATA_MAX_NAME_LEN] = "";
139 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
140 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
141 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
142 { "Collectd::_plugin_flush", Collectd__plugin_flush },
143 { "Collectd::plugin_dispatch_notification",
144 Collectd_plugin_dispatch_notification },
145 { "Collectd::plugin_log", Collectd_plugin_log },
146 { "Collectd::call_by_name", Collectd_call_by_name },
155 { "Collectd::TYPE_INIT", PLUGIN_INIT },
156 { "Collectd::TYPE_READ", PLUGIN_READ },
157 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
158 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
159 { "Collectd::TYPE_LOG", PLUGIN_LOG },
160 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
161 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
162 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
163 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
164 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
165 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
166 { "Collectd::LOG_ERR", LOG_ERR },
167 { "Collectd::LOG_WARNING", LOG_WARNING },
168 { "Collectd::LOG_NOTICE", LOG_NOTICE },
169 { "Collectd::LOG_INFO", LOG_INFO },
170 { "Collectd::LOG_DEBUG", LOG_DEBUG },
171 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
172 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
173 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
182 { "Collectd::hostname_g", hostname_g },
191 { "Collectd::interval_g", &interval_g },
196 * Helper functions for data type conversion.
211 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
215 if ((NULL == hash) || (NULL == ds))
218 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
219 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
222 log_err ("hv2data_source: No DS name given.");
226 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
227 ds->type = SvIV (*tmp);
229 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
230 log_err ("hv2data_source: Invalid DS type.");
235 ds->type = DS_TYPE_COUNTER;
238 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
239 ds->min = SvNV (*tmp);
243 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
244 ds->max = SvNV (*tmp);
248 } /* static int hv2data_source (HV *, data_source_t *) */
250 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
252 const data_set_t *ds;
256 if ((NULL == name) || (NULL == array) || (NULL == value))
259 if (av_len (array) < len - 1)
260 len = av_len (array) + 1;
265 ds = plugin_get_ds (name);
267 log_err ("av2value: Unknown dataset \"%s\"", name);
271 if (ds->ds_num < len) {
272 log_warn ("av2value: Value length exceeds data set length.");
276 for (i = 0; i < len; ++i) {
277 SV **tmp = av_fetch (array, i, 0);
280 if (DS_TYPE_COUNTER == ds->ds[i].type)
281 value[i].counter = SvIV (*tmp);
283 value[i].gauge = SvNV (*tmp);
290 } /* static int av2value (char *, AV *, value_t *, int) */
295 * values => [ @values ],
299 * plugin_instance => $pinstance,
300 * type_instance => $tinstance,
303 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
307 if ((NULL == hash) || (NULL == vl))
310 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
311 log_err ("hv2value_list: No type given.");
315 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
317 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
318 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
319 log_err ("hv2value_list: No valid values given.");
324 AV *array = (AV *)SvRV (*tmp);
325 int len = av_len (array) + 1;
330 vl->values = (value_t *)smalloc (len * sizeof (value_t));
331 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
334 if (-1 == vl->values_len) {
340 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0))) {
341 vl->time = (time_t)SvIV (*tmp);
344 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0))) {
345 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
348 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
351 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
352 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
354 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
355 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
356 sizeof (vl->plugin_instance));
358 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
359 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
360 sizeof (vl->type_instance));
362 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
364 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
368 if ((NULL == ds) || (NULL == array))
371 av_extend (array, ds->ds_num);
373 for (i = 0; i < ds->ds_num; ++i) {
374 HV *source = newHV ();
376 if (NULL == hv_store (source, "name", 4,
377 newSVpv (ds->ds[i].name, 0), 0))
380 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
383 if (! isnan (ds->ds[i].min))
384 if (NULL == hv_store (source, "min", 3,
385 newSVnv (ds->ds[i].min), 0))
388 if (! isnan (ds->ds[i].max))
389 if (NULL == hv_store (source, "max", 3,
390 newSVnv (ds->ds[i].max), 0))
393 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
397 } /* static int data_set2av (data_set_t *, AV *) */
399 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
406 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
409 len = vl->values_len;
411 if (ds->ds_num < len) {
412 log_warn ("value2av: Value length exceeds data set length.");
417 av_extend (values, len - 1);
419 for (i = 0; i < len; ++i) {
422 if (DS_TYPE_COUNTER == ds->ds[i].type)
423 val = newSViv (vl->values[i].counter);
425 val = newSVnv (vl->values[i].gauge);
427 if (NULL == av_store (values, i, val)) {
433 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
437 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
440 if ('\0' != vl->host[0])
441 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
444 if ('\0' != vl->plugin[0])
445 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
448 if ('\0' != vl->plugin_instance[0])
449 if (NULL == hv_store (hash, "plugin_instance", 15,
450 newSVpv (vl->plugin_instance, 0), 0))
453 if ('\0' != vl->type[0])
454 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
457 if ('\0' != vl->type_instance[0])
458 if (NULL == hv_store (hash, "type_instance", 13,
459 newSVpv (vl->type_instance, 0), 0))
462 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
464 static int notification2hv (pTHX_ notification_t *n, HV *hash)
466 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
470 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
473 if ('\0' != *n->message)
474 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
477 if ('\0' != *n->host)
478 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
481 if ('\0' != *n->plugin)
482 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
485 if ('\0' != *n->plugin_instance)
486 if (NULL == hv_store (hash, "plugin_instance", 15,
487 newSVpv (n->plugin_instance, 0), 0))
490 if ('\0' != *n->type)
491 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
494 if ('\0' != *n->type_instance)
495 if (NULL == hv_store (hash, "type_instance", 13,
496 newSVpv (n->type_instance, 0), 0))
499 } /* static int notification2hv (notification_t *, HV *) */
501 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
508 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
512 if (0 < ci->values_num)
513 av_extend (values, ci->values_num);
515 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
521 for (i = 0; i < ci->values_num; ++i) {
524 switch (ci->values[i].type) {
525 case OCONFIG_TYPE_STRING:
526 value = newSVpv (ci->values[i].value.string, 0);
528 case OCONFIG_TYPE_NUMBER:
529 value = newSVnv ((NV)ci->values[i].value.number);
531 case OCONFIG_TYPE_BOOLEAN:
532 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
535 log_err ("oconfig_item2hv: Invalid value type %i.",
537 value = &PL_sv_undef;
540 if (NULL == av_store (values, i, value)) {
546 /* ignoring 'parent' member which is uninteresting in this case */
549 if (0 < ci->children_num)
550 av_extend (children, ci->children_num);
552 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
558 for (i = 0; i < ci->children_num; ++i) {
559 HV *child = newHV ();
561 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
567 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
574 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
577 * Internal functions.
580 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
582 if (base_name[0] == '\0')
583 status = ssnprintf (buf, buf_len, "%s", module);
585 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
586 if ((status < 0) || ((unsigned int)status >= buf_len))
589 } /* char *get_module_name */
592 * Add a plugin's data set definition.
594 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
600 data_source_t *ds = NULL;
601 data_set_t *set = NULL;
603 if ((NULL == name) || (NULL == dataset))
606 len = av_len (dataset);
611 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
612 set = (data_set_t *)smalloc (sizeof (data_set_t));
614 for (i = 0; i <= len; ++i) {
615 SV **elem = av_fetch (dataset, i, 0);
620 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
621 log_err ("pplugin_register_data_set: Invalid data source.");
625 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds[i]))
628 log_debug ("pplugin_register_data_set: "
629 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
630 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
633 sstrncpy (set->type, name, sizeof (set->type));
635 set->ds_num = len + 1;
638 ret = plugin_register_data_set (set);
643 } /* static int pplugin_register_data_set (char *, SV *) */
646 * Remove a plugin's data set definition.
648 static int pplugin_unregister_data_set (char *name)
652 return plugin_unregister_data_set (name);
653 } /* static int pplugin_unregister_data_set (char *) */
656 * Submit the values to the write functions.
658 static int pplugin_dispatch_values (pTHX_ HV *values)
660 value_list_t vl = VALUE_LIST_INIT;
667 if (0 != hv2value_list (aTHX_ values, &vl))
670 ret = plugin_dispatch_values (&vl);
674 } /* static int pplugin_dispatch_values (char *, HV *) */
677 * Dispatch a notification.
681 * severity => $severity,
687 * plugin_instance => $instance,
688 * type_instance => $type_instance
691 static int pplugin_dispatch_notification (pTHX_ HV *notif)
700 memset (&n, 0, sizeof (n));
702 if (NULL != (tmp = hv_fetch (notif, "severity", 8, 0)))
703 n.severity = SvIV (*tmp);
705 n.severity = NOTIF_FAILURE;
707 if (NULL != (tmp = hv_fetch (notif, "time", 4, 0)))
708 n.time = (time_t)SvIV (*tmp);
710 n.time = time (NULL);
712 if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
713 sstrncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
715 if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
716 sstrncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
718 sstrncpy (n.host, hostname_g, sizeof (n.host));
720 if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
721 sstrncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
723 if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
724 sstrncpy (n.plugin_instance, SvPV_nolen (*tmp),
725 sizeof (n.plugin_instance));
727 if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
728 sstrncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
730 if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
731 sstrncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
732 return plugin_dispatch_notification (&n);
733 } /* static int pplugin_dispatch_notification (HV *) */
736 * Call all working functions of the given type.
738 static int pplugin_call_all (pTHX_ int type, ...)
747 if ((type < 0) || (type >= PLUGIN_TYPES))
757 XPUSHs (sv_2mortal (newSViv ((IV)type)));
759 if (PLUGIN_WRITE == type) {
761 * $_[0] = $plugin_type;
776 * values => [ $v1, ... ],
781 * plugin_instance => $instance,
782 * type_instance => $type_instance
791 ds = va_arg (ap, data_set_t *);
792 vl = va_arg (ap, value_list_t *);
794 if (-1 == data_set2av (aTHX_ ds, pds)) {
801 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
808 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
809 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
810 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
812 else if (PLUGIN_LOG == type) {
818 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
819 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
821 else if (PLUGIN_NOTIF == type) {
825 * severity => $severity,
831 * plugin_instance => $instance,
832 * type_instance => $type_instance
836 HV *notif = newHV ();
838 n = va_arg (ap, notification_t *);
840 if (-1 == notification2hv (aTHX_ n, notif)) {
847 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
849 else if (PLUGIN_FLUSH == type) {
852 * $_[1] = $identifier;
854 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
855 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
860 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
875 } /* static int pplugin_call_all (int, ...) */
882 * Collectd::plugin_register_data_set (type, dataset).
885 * type of the dataset
888 * dataset to be registered
890 static XS (Collectd_plugin_register_ds)
898 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
902 log_debug ("Collectd::plugin_register_data_set: "
903 "type = \"%s\", dataset = \"%s\"",
904 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
908 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
909 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
913 log_err ("Collectd::plugin_register_data_set: Invalid data.");
921 } /* static XS (Collectd_plugin_register_ds) */
924 * Collectd::plugin_unregister_data_set (type).
927 * type of the dataset
929 static XS (Collectd_plugin_unregister_ds)
934 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
938 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
939 SvPV_nolen (ST (0)));
941 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
945 } /* static XS (Collectd_plugin_register_ds) */
948 * Collectd::plugin_dispatch_values (name, values).
954 * value list to submit
956 static XS (Collectd_plugin_dispatch_values)
966 log_warn ("Collectd::plugin_dispatch_values with two arguments "
967 "is deprecated - pass the type through values->{type}.");
970 else if (1 != items) {
971 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
975 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
976 SvPV_nolen (ST (values_idx)));
978 values = ST (values_idx);
980 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
981 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
985 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
988 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
989 newSVsv (ST (0)), 0))) {
990 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
994 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1000 } /* static XS (Collectd_plugin_dispatch_values) */
1003 * Collectd::_plugin_flush (plugin, timeout, identifier).
1006 * name of the plugin to flush
1009 * timeout to use when flushing the data
1012 * data-set identifier to flush
1014 static XS (Collectd__plugin_flush)
1016 char *plugin = NULL;
1023 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1028 plugin = SvPV_nolen (ST (0));
1031 timeout = (int)SvIV (ST (1));
1034 id = SvPV_nolen (ST (2));
1036 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1037 "id = \"%s\"", plugin, timeout, id);
1039 if (0 == plugin_flush (plugin, timeout, id))
1043 } /* static XS (Collectd__plugin_flush) */
1046 * Collectd::plugin_dispatch_notification (notif).
1049 * notification to dispatch
1051 static XS (Collectd_plugin_dispatch_notification)
1060 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1064 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1065 SvPV_nolen (ST (0)));
1069 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1070 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1074 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1080 } /* static XS (Collectd_plugin_dispatch_notification) */
1083 * Collectd::plugin_log (level, message).
1086 * log level (LOG_DEBUG, ... LOG_ERR)
1091 static XS (Collectd_plugin_log)
1096 log_err ("Usage: Collectd::plugin_log(level, message)");
1100 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1102 } /* static XS (Collectd_plugin_log) */
1105 * Collectd::call_by_name (...).
1107 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1109 static XS (Collectd_call_by_name)
1114 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1115 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1120 name = SvPV_nolen (tmp);
1122 if (NULL == get_cv (name, 0)) {
1123 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1128 /* simply pass on the subroutine call without touching the stack,
1129 * thus leaving any arguments and return values in place */
1131 } /* static XS (Collectd_call_by_name) */
1134 * collectd's perl interpreter based thread implementation.
1136 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1139 /* must be called with perl_threads->mutex locked */
1140 static void c_ithread_destroy (c_ithread_t *ithread)
1142 dTHXa (ithread->interp);
1144 assert (NULL != perl_threads);
1146 PERL_SET_CONTEXT (aTHX);
1147 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1152 --perl_threads->number_of_threads;
1153 #endif /* COLLECT_DEBUG */
1155 perl_destruct (aTHX);
1158 if (NULL == ithread->prev)
1159 perl_threads->head = ithread->next;
1161 ithread->prev->next = ithread->next;
1163 if (NULL == ithread->next)
1164 perl_threads->tail = ithread->prev;
1166 ithread->next->prev = ithread->prev;
1170 } /* static void c_ithread_destroy (c_ithread_t *) */
1172 static void c_ithread_destructor (void *arg)
1174 c_ithread_t *ithread = (c_ithread_t *)arg;
1175 c_ithread_t *t = NULL;
1177 if (NULL == perl_threads)
1180 pthread_mutex_lock (&perl_threads->mutex);
1182 for (t = perl_threads->head; NULL != t; t = t->next)
1186 /* the ithread no longer exists */
1190 c_ithread_destroy (ithread);
1192 pthread_mutex_unlock (&perl_threads->mutex);
1194 } /* static void c_ithread_destructor (void *) */
1196 /* must be called with perl_threads->mutex locked */
1197 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1199 c_ithread_t *t = NULL;
1202 assert (NULL != perl_threads);
1204 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1205 memset (t, 0, sizeof (c_ithread_t));
1207 t->interp = (NULL == base)
1209 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1213 if ((NULL != base) && (NULL != PL_endav)) {
1214 av_clear (PL_endav);
1215 av_undef (PL_endav);
1220 ++perl_threads->number_of_threads;
1221 #endif /* COLLECT_DEBUG */
1225 if (NULL == perl_threads->tail) {
1226 perl_threads->head = t;
1230 perl_threads->tail->next = t;
1231 t->prev = perl_threads->tail;
1234 perl_threads->tail = t;
1236 pthread_setspecific (perl_thr_key, (const void *)t);
1238 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1241 * Interface to collectd.
1244 static int perl_init (void)
1248 if (NULL == perl_threads)
1252 c_ithread_t *t = NULL;
1254 pthread_mutex_lock (&perl_threads->mutex);
1255 t = c_ithread_create (perl_threads->head->interp);
1256 pthread_mutex_unlock (&perl_threads->mutex);
1261 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1262 aTHX, perl_threads->number_of_threads);
1263 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1264 } /* static int perl_init (void) */
1266 static int perl_read (void)
1270 if (NULL == perl_threads)
1274 c_ithread_t *t = NULL;
1276 pthread_mutex_lock (&perl_threads->mutex);
1277 t = c_ithread_create (perl_threads->head->interp);
1278 pthread_mutex_unlock (&perl_threads->mutex);
1283 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1284 aTHX, perl_threads->number_of_threads);
1285 return pplugin_call_all (aTHX_ PLUGIN_READ);
1286 } /* static int perl_read (void) */
1288 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1292 if (NULL == perl_threads)
1296 c_ithread_t *t = NULL;
1298 pthread_mutex_lock (&perl_threads->mutex);
1299 t = c_ithread_create (perl_threads->head->interp);
1300 pthread_mutex_unlock (&perl_threads->mutex);
1305 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1306 aTHX, perl_threads->number_of_threads);
1307 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1308 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1310 static void perl_log (int level, const char *msg)
1314 if (NULL == perl_threads)
1318 c_ithread_t *t = NULL;
1320 pthread_mutex_lock (&perl_threads->mutex);
1321 t = c_ithread_create (perl_threads->head->interp);
1322 pthread_mutex_unlock (&perl_threads->mutex);
1327 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1329 } /* static void perl_log (int, const char *) */
1331 static int perl_notify (const notification_t *notif)
1335 if (NULL == perl_threads)
1339 c_ithread_t *t = NULL;
1341 pthread_mutex_lock (&perl_threads->mutex);
1342 t = c_ithread_create (perl_threads->head->interp);
1343 pthread_mutex_unlock (&perl_threads->mutex);
1347 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1348 } /* static int perl_notify (const notification_t *) */
1350 static int perl_flush (int timeout, const char *identifier)
1354 if (NULL == perl_threads)
1358 c_ithread_t *t = NULL;
1360 pthread_mutex_lock (&perl_threads->mutex);
1361 t = c_ithread_create (perl_threads->head->interp);
1362 pthread_mutex_unlock (&perl_threads->mutex);
1366 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
1367 } /* static int perl_flush (const int) */
1369 static int perl_shutdown (void)
1371 c_ithread_t *t = NULL;
1377 plugin_unregister_complex_config ("perl");
1379 if (NULL == perl_threads)
1383 c_ithread_t *t = NULL;
1385 pthread_mutex_lock (&perl_threads->mutex);
1386 t = c_ithread_create (perl_threads->head->interp);
1387 pthread_mutex_unlock (&perl_threads->mutex);
1392 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1393 aTHX, perl_threads->number_of_threads);
1395 plugin_unregister_log ("perl");
1396 plugin_unregister_notification ("perl");
1397 plugin_unregister_init ("perl");
1398 plugin_unregister_read ("perl");
1399 plugin_unregister_write ("perl");
1400 plugin_unregister_flush ("perl");
1402 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1404 pthread_mutex_lock (&perl_threads->mutex);
1405 t = perl_threads->tail;
1408 c_ithread_t *thr = t;
1410 /* the pointer has to be advanced before destroying
1411 * the thread as this will free the memory */
1414 c_ithread_destroy (thr);
1417 pthread_mutex_unlock (&perl_threads->mutex);
1418 pthread_mutex_destroy (&perl_threads->mutex);
1420 sfree (perl_threads);
1422 pthread_key_delete (perl_thr_key);
1426 plugin_unregister_shutdown ("perl");
1428 } /* static void perl_shutdown (void) */
1431 * Access functions for global variables.
1433 * These functions implement the "magic" used to access
1434 * the global variables from Perl.
1437 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1439 char *pv = mg->mg_ptr;
1442 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1444 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1446 char *pv = mg->mg_ptr;
1447 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1449 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1451 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1453 int *iv = (int *)mg->mg_ptr;
1454 sv_setiv (var, *iv);
1456 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1458 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1460 int *iv = (int *)mg->mg_ptr;
1461 *iv = (int)SvIV (var);
1463 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1465 static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL };
1466 static MGVTBL g_iv_vtbl = { g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL };
1468 /* bootstrap the Collectd module */
1469 static void xs_init (pTHX)
1473 char *file = __FILE__;
1479 /* enable usage of Perl modules using shared libraries */
1480 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1483 for (i = 0; NULL != api[i].f; ++i)
1484 newXS (api[i].name, api[i].f, file);
1486 stash = gv_stashpv ("Collectd", 1);
1488 /* export "constants" */
1489 for (i = 0; '\0' != constants[i].name[0]; ++i)
1490 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1492 /* export global variables
1493 * by adding "magic" to the SV's representing the globale variables
1494 * perl is able to automagically call the get/set function when
1495 * accessing any such variable (this is basically the same as using
1497 /* global strings */
1498 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1499 tmp = get_sv (g_strings[i].name, 1);
1500 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1501 g_strings[i].var, 0);
1504 /* global integers */
1505 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1506 tmp = get_sv (g_integers[i].name, 1);
1507 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1508 (char *)g_integers[i].var, 0);
1511 } /* static void xs_init (pTHX) */
1513 /* Initialize the global Perl interpreter. */
1514 static int init_pi (int argc, char **argv)
1518 if (NULL != perl_threads)
1521 log_info ("Initializing Perl interpreter...");
1526 for (i = 0; i < argc; ++i)
1527 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1529 #endif /* COLLECT_DEBUG */
1531 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1532 log_err ("init_pi: pthread_key_create failed");
1534 /* this must not happen - cowardly giving up if it does */
1539 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1540 * triggers a "value computed is not used" warning by gcc. */
1543 PERL_SYS_INIT3 (&argc, &argv, &environ);
1545 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1546 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1548 pthread_mutex_init (&perl_threads->mutex, NULL);
1549 /* locking the mutex should not be necessary at this point
1550 * but let's just do it for the sake of completeness */
1551 pthread_mutex_lock (&perl_threads->mutex);
1553 perl_threads->head = c_ithread_create (NULL);
1554 perl_threads->tail = perl_threads->head;
1556 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1557 log_err ("init_pi: Not enough memory.");
1561 aTHX = perl_threads->head->interp;
1562 pthread_mutex_unlock (&perl_threads->mutex);
1564 perl_construct (aTHX);
1566 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1568 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1569 SV *err = get_sv ("@", 1);
1570 log_err ("init_pi: Unable to bootstrap Collectd: %s",
1573 perl_destruct (perl_threads->head->interp);
1574 perl_free (perl_threads->head->interp);
1575 sfree (perl_threads);
1577 pthread_key_delete (perl_thr_key);
1581 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1582 sv_setpv (get_sv ("0", 0), "collectd");
1586 plugin_register_log ("perl", perl_log);
1587 plugin_register_notification ("perl", perl_notify);
1588 plugin_register_init ("perl", perl_init);
1590 plugin_register_read ("perl", perl_read);
1592 plugin_register_write ("perl", perl_write);
1593 plugin_register_flush ("perl", perl_flush);
1594 plugin_register_shutdown ("perl", perl_shutdown);
1596 } /* static int init_pi (const char **, const int) */
1599 * LoadPlugin "<Plugin>"
1601 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1603 char module_name[DATA_MAX_NAME_LEN];
1607 if ((0 != ci->children_num) || (1 != ci->values_num)
1608 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1609 log_err ("LoadPlugin expects a single string argument.");
1613 value = ci->values[0].value.string;
1615 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1616 log_err ("Invalid module name %s", value);
1620 if (0 != init_pi (perl_argc, perl_argv))
1623 assert (NULL != perl_threads);
1624 assert (NULL != perl_threads->head);
1626 aTHX = perl_threads->head->interp;
1628 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1629 load_module (PERL_LOADMOD_NOIMPORT,
1630 newSVpv (module_name, strlen (module_name)), Nullsv);
1632 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1637 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1641 if ((0 != ci->children_num) || (1 != ci->values_num)
1642 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1643 log_err ("BaseName expects a single string argument.");
1647 value = ci->values[0].value.string;
1649 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1650 sstrncpy (base_name, value, sizeof (base_name));
1652 } /* static int perl_config_basename (oconfig_item_it *) */
1655 * EnableDebugger "<Package>"|""
1657 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1661 if ((0 != ci->children_num) || (1 != ci->values_num)
1662 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1663 log_err ("EnableDebugger expects a single string argument.");
1667 if (NULL != perl_threads) {
1668 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1672 value = ci->values[0].value.string;
1674 perl_argv = (char **)realloc (perl_argv,
1675 (++perl_argc + 1) * sizeof (char *));
1677 if (NULL == perl_argv) {
1678 log_err ("perl_config: Not enough memory.");
1682 if ('\0' == value[0]) {
1683 perl_argv[perl_argc - 1] = "-d";
1686 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1687 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1688 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1691 perl_argv[perl_argc] = NULL;
1693 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1696 * IncludeDir "<Dir>"
1698 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1702 if ((0 != ci->children_num) || (1 != ci->values_num)
1703 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1704 log_err ("IncludeDir expects a single string argument.");
1708 value = ci->values[0].value.string;
1711 perl_argv = (char **)realloc (perl_argv,
1712 (++perl_argc + 1) * sizeof (char *));
1714 if (NULL == perl_argv) {
1715 log_err ("perl_config: Not enough memory.");
1719 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1720 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1721 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1723 perl_argv[perl_argc] = NULL;
1726 /* prepend the directory to @INC */
1727 av_unshift (GvAVn (PL_incgv), 1);
1728 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1731 } /* static int perl_config_includedir (oconfig_item_it *) */
1736 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
1746 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1747 log_err ("LoadPlugin expects a single string argument.");
1751 plugin = ci->values[0].value.string;
1754 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1758 log_err ("Unable to convert configuration to a Perl hash value.");
1767 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
1768 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1772 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
1787 } /* static int perl_config_plugin (oconfig_item_it *) */
1789 static int perl_config (oconfig_item_t *ci)
1796 for (i = 0; i < ci->children_num; ++i) {
1797 oconfig_item_t *c = ci->children + i;
1798 int current_status = 0;
1800 if (NULL != perl_threads)
1801 aTHX = PERL_GET_CONTEXT;
1803 if (0 == strcasecmp (c->key, "LoadPlugin"))
1804 current_status = perl_config_loadplugin (aTHX_ c);
1805 else if (0 == strcasecmp (c->key, "BaseName"))
1806 current_status = perl_config_basename (aTHX_ c);
1807 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1808 current_status = perl_config_enabledebugger (aTHX_ c);
1809 else if (0 == strcasecmp (c->key, "IncludeDir"))
1810 current_status = perl_config_includedir (aTHX_ c);
1811 else if (0 == strcasecmp (c->key, "Plugin"))
1812 current_status = perl_config_plugin (aTHX_ c);
1815 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1819 /* fatal error - it's up to perl_config_* to clean up */
1820 if (0 > current_status) {
1821 log_err ("Configuration failed with a fatal error - "
1822 "plugin disabled!");
1823 return current_status;
1826 status += current_status;
1829 } /* static int perl_config (oconfig_item_t *) */
1831 void module_register (void)
1834 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1836 /* default options for the Perl interpreter */
1838 perl_argv[1] = "-MCollectd";
1839 perl_argv[2] = "-e";
1841 perl_argv[4] = NULL;
1843 plugin_register_complex_config ("perl", perl_config);
1845 } /* void module_register (void) */
1847 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */