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_write);
89 static XS (Collectd__plugin_flush);
90 static XS (Collectd_plugin_dispatch_notification);
91 static XS (Collectd_plugin_log);
92 static XS (Collectd_call_by_name);
98 typedef struct c_ithread_s {
99 /* the thread's Perl interpreter */
100 PerlInterpreter *interp;
102 /* double linked list of threads */
103 struct c_ithread_s *prev;
104 struct c_ithread_s *next;
112 /* some usage stats */
113 int number_of_threads;
114 #endif /* COLLECT_DEBUG */
116 pthread_mutex_t mutex;
123 /* if perl_threads != NULL perl_threads->head must
124 * point to the "base" thread */
125 static c_ithread_list_t *perl_threads = NULL;
127 /* the key used to store each pthread's ithread */
128 static pthread_key_t perl_thr_key;
130 static int perl_argc = 0;
131 static char **perl_argv = NULL;
133 static char base_name[DATA_MAX_NAME_LEN] = "";
140 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
141 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
142 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
143 { "Collectd::_plugin_write", Collectd__plugin_write },
144 { "Collectd::_plugin_flush", Collectd__plugin_flush },
145 { "Collectd::plugin_dispatch_notification",
146 Collectd_plugin_dispatch_notification },
147 { "Collectd::plugin_log", Collectd_plugin_log },
148 { "Collectd::call_by_name", Collectd_call_by_name },
157 { "Collectd::TYPE_INIT", PLUGIN_INIT },
158 { "Collectd::TYPE_READ", PLUGIN_READ },
159 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
160 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
161 { "Collectd::TYPE_LOG", PLUGIN_LOG },
162 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
163 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
164 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
165 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
166 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
167 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
168 { "Collectd::LOG_ERR", LOG_ERR },
169 { "Collectd::LOG_WARNING", LOG_WARNING },
170 { "Collectd::LOG_NOTICE", LOG_NOTICE },
171 { "Collectd::LOG_INFO", LOG_INFO },
172 { "Collectd::LOG_DEBUG", LOG_DEBUG },
173 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
174 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
175 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
184 { "Collectd::hostname_g", hostname_g },
193 { "Collectd::interval_g", &interval_g },
198 * Helper functions for data type conversion.
213 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
217 if ((NULL == hash) || (NULL == ds))
220 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
221 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
224 log_err ("hv2data_source: No DS name given.");
228 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
229 ds->type = SvIV (*tmp);
231 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
232 log_err ("hv2data_source: Invalid DS type.");
237 ds->type = DS_TYPE_COUNTER;
240 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
241 ds->min = SvNV (*tmp);
245 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
246 ds->max = SvNV (*tmp);
250 } /* static int hv2data_source (HV *, data_source_t *) */
252 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
254 const data_set_t *ds;
258 if ((NULL == name) || (NULL == array) || (NULL == value))
261 if (av_len (array) < len - 1)
262 len = av_len (array) + 1;
267 ds = plugin_get_ds (name);
269 log_err ("av2value: Unknown dataset \"%s\"", name);
273 if (ds->ds_num < len) {
274 log_warn ("av2value: Value length exceeds data set length.");
278 for (i = 0; i < len; ++i) {
279 SV **tmp = av_fetch (array, i, 0);
282 if (DS_TYPE_COUNTER == ds->ds[i].type)
283 value[i].counter = SvIV (*tmp);
285 value[i].gauge = SvNV (*tmp);
292 } /* static int av2value (char *, AV *, value_t *, int) */
297 * values => [ @values ],
301 * plugin_instance => $pinstance,
302 * type_instance => $tinstance,
305 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
309 if ((NULL == hash) || (NULL == vl))
312 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
313 log_err ("hv2value_list: No type given.");
317 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
319 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
320 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
321 log_err ("hv2value_list: No valid values given.");
326 AV *array = (AV *)SvRV (*tmp);
327 int len = av_len (array) + 1;
332 vl->values = (value_t *)smalloc (len * sizeof (value_t));
333 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
336 if (-1 == vl->values_len) {
342 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0))) {
343 vl->time = (time_t)SvIV (*tmp);
346 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0))) {
347 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
350 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
353 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
354 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
356 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
357 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
358 sizeof (vl->plugin_instance));
360 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
361 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
362 sizeof (vl->type_instance));
364 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
366 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
370 if ((NULL == array) || (NULL == name) || (NULL == ds))
373 len = av_len (array);
376 log_err ("av2data_set: Invalid data set.");
380 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
381 ds->ds_num = len + 1;
383 for (i = 0; i <= len; ++i) {
384 SV **elem = av_fetch (array, i, 0);
387 log_err ("av2data_set: Failed to fetch data source %i.", i);
391 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
392 log_err ("av2data_set: Invalid data source.");
396 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
399 log_debug ("av2data_set: "
400 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
401 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
404 sstrncpy (ds->type, name, sizeof (ds->type));
406 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
411 * severity => $severity,
417 * plugin_instance => $instance,
418 * type_instance => $type_instance
421 static int hv2notification (pTHX_ HV *hash, notification_t *n)
425 if ((NULL == hash) || (NULL == n))
428 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
429 n->severity = SvIV (*tmp);
431 n->severity = NOTIF_FAILURE;
433 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
434 n->time = (time_t)SvIV (*tmp);
436 n->time = time (NULL);
438 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
439 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
441 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
442 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
444 sstrncpy (n->host, hostname_g, sizeof (n->host));
446 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
447 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
449 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
450 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
451 sizeof (n->plugin_instance));
453 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
454 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
456 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
457 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
458 sizeof (n->type_instance));
460 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
462 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
466 if ((NULL == ds) || (NULL == array))
469 av_extend (array, ds->ds_num);
471 for (i = 0; i < ds->ds_num; ++i) {
472 HV *source = newHV ();
474 if (NULL == hv_store (source, "name", 4,
475 newSVpv (ds->ds[i].name, 0), 0))
478 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
481 if (! isnan (ds->ds[i].min))
482 if (NULL == hv_store (source, "min", 3,
483 newSVnv (ds->ds[i].min), 0))
486 if (! isnan (ds->ds[i].max))
487 if (NULL == hv_store (source, "max", 3,
488 newSVnv (ds->ds[i].max), 0))
491 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
495 } /* static int data_set2av (data_set_t *, AV *) */
497 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
504 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
507 len = vl->values_len;
509 if (ds->ds_num < len) {
510 log_warn ("value2av: Value length exceeds data set length.");
515 av_extend (values, len - 1);
517 for (i = 0; i < len; ++i) {
520 if (DS_TYPE_COUNTER == ds->ds[i].type)
521 val = newSViv (vl->values[i].counter);
523 val = newSVnv (vl->values[i].gauge);
525 if (NULL == av_store (values, i, val)) {
531 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
535 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
538 if ('\0' != vl->host[0])
539 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
542 if ('\0' != vl->plugin[0])
543 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
546 if ('\0' != vl->plugin_instance[0])
547 if (NULL == hv_store (hash, "plugin_instance", 15,
548 newSVpv (vl->plugin_instance, 0), 0))
551 if ('\0' != vl->type[0])
552 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
555 if ('\0' != vl->type_instance[0])
556 if (NULL == hv_store (hash, "type_instance", 13,
557 newSVpv (vl->type_instance, 0), 0))
560 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
562 static int notification2hv (pTHX_ notification_t *n, HV *hash)
564 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
568 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
571 if ('\0' != *n->message)
572 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
575 if ('\0' != *n->host)
576 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
579 if ('\0' != *n->plugin)
580 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
583 if ('\0' != *n->plugin_instance)
584 if (NULL == hv_store (hash, "plugin_instance", 15,
585 newSVpv (n->plugin_instance, 0), 0))
588 if ('\0' != *n->type)
589 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
592 if ('\0' != *n->type_instance)
593 if (NULL == hv_store (hash, "type_instance", 13,
594 newSVpv (n->type_instance, 0), 0))
597 } /* static int notification2hv (notification_t *, HV *) */
599 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
606 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
610 if (0 < ci->values_num)
611 av_extend (values, ci->values_num);
613 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
619 for (i = 0; i < ci->values_num; ++i) {
622 switch (ci->values[i].type) {
623 case OCONFIG_TYPE_STRING:
624 value = newSVpv (ci->values[i].value.string, 0);
626 case OCONFIG_TYPE_NUMBER:
627 value = newSVnv ((NV)ci->values[i].value.number);
629 case OCONFIG_TYPE_BOOLEAN:
630 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
633 log_err ("oconfig_item2hv: Invalid value type %i.",
635 value = &PL_sv_undef;
638 if (NULL == av_store (values, i, value)) {
644 /* ignoring 'parent' member which is uninteresting in this case */
647 if (0 < ci->children_num)
648 av_extend (children, ci->children_num);
650 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
656 for (i = 0; i < ci->children_num; ++i) {
657 HV *child = newHV ();
659 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
665 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
672 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
675 * Internal functions.
678 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
680 if (base_name[0] == '\0')
681 status = ssnprintf (buf, buf_len, "%s", module);
683 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
684 if ((status < 0) || ((unsigned int)status >= buf_len))
687 } /* char *get_module_name */
690 * Add a plugin's data set definition.
692 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
698 if ((NULL == name) || (NULL == dataset))
701 if (0 != av2data_set (aTHX_ dataset, name, &ds))
704 ret = plugin_register_data_set (&ds);
708 } /* static int pplugin_register_data_set (char *, SV *) */
711 * Remove a plugin's data set definition.
713 static int pplugin_unregister_data_set (char *name)
717 return plugin_unregister_data_set (name);
718 } /* static int pplugin_unregister_data_set (char *) */
721 * Submit the values to the write functions.
723 static int pplugin_dispatch_values (pTHX_ HV *values)
725 value_list_t vl = VALUE_LIST_INIT;
732 if (0 != hv2value_list (aTHX_ values, &vl))
735 ret = plugin_dispatch_values (&vl);
739 } /* static int pplugin_dispatch_values (char *, HV *) */
742 * Submit the values to a single write function.
744 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
747 value_list_t vl = VALUE_LIST_INIT;
754 if (0 != hv2value_list (aTHX_ values, &vl))
757 if ((NULL != data_set)
758 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
761 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
763 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
764 NULL == plugin ? "<any>" : plugin, ret);
766 if (NULL != data_set)
770 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
773 * Dispatch a notification.
775 static int pplugin_dispatch_notification (pTHX_ HV *notif)
782 memset (&n, 0, sizeof (n));
784 if (0 != hv2notification (aTHX_ notif, &n))
787 return plugin_dispatch_notification (&n);
788 } /* static int pplugin_dispatch_notification (HV *) */
791 * Call all working functions of the given type.
793 static int pplugin_call_all (pTHX_ int type, ...)
802 if ((type < 0) || (type >= PLUGIN_TYPES))
812 XPUSHs (sv_2mortal (newSViv ((IV)type)));
814 if (PLUGIN_WRITE == type) {
816 * $_[0] = $plugin_type;
831 * values => [ $v1, ... ],
836 * plugin_instance => $instance,
837 * type_instance => $type_instance
846 ds = va_arg (ap, data_set_t *);
847 vl = va_arg (ap, value_list_t *);
849 if (-1 == data_set2av (aTHX_ ds, pds)) {
856 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
863 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
864 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
865 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
867 else if (PLUGIN_LOG == type) {
873 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
874 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
876 else if (PLUGIN_NOTIF == type) {
880 * severity => $severity,
886 * plugin_instance => $instance,
887 * type_instance => $type_instance
891 HV *notif = newHV ();
893 n = va_arg (ap, notification_t *);
895 if (-1 == notification2hv (aTHX_ n, notif)) {
902 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
904 else if (PLUGIN_FLUSH == type) {
907 * $_[1] = $identifier;
909 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
910 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
915 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
930 } /* static int pplugin_call_all (int, ...) */
937 * Collectd::plugin_register_data_set (type, dataset).
940 * type of the dataset
943 * dataset to be registered
945 static XS (Collectd_plugin_register_ds)
953 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
957 log_debug ("Collectd::plugin_register_data_set: "
958 "type = \"%s\", dataset = \"%s\"",
959 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
963 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
964 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
968 log_err ("Collectd::plugin_register_data_set: Invalid data.");
976 } /* static XS (Collectd_plugin_register_ds) */
979 * Collectd::plugin_unregister_data_set (type).
982 * type of the dataset
984 static XS (Collectd_plugin_unregister_ds)
989 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
993 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
994 SvPV_nolen (ST (0)));
996 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1000 } /* static XS (Collectd_plugin_register_ds) */
1003 * Collectd::plugin_dispatch_values (name, values).
1006 * name of the plugin
1009 * value list to submit
1011 static XS (Collectd_plugin_dispatch_values)
1021 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1022 "is deprecated - pass the type through values->{type}.");
1025 else if (1 != items) {
1026 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1030 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1031 SvPV_nolen (ST (values_idx)));
1033 values = ST (values_idx);
1035 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1036 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1040 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1043 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1044 newSVsv (ST (0)), 0))) {
1045 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1049 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1055 } /* static XS (Collectd_plugin_dispatch_values) */
1057 /* Collectd::plugin_write (plugin, ds, vl).
1060 * name of the plugin to call, may be 'undef'
1063 * data-set that describes the submitted values, may be 'undef'
1066 * value-list to be written
1068 static XS (Collectd__plugin_write)
1079 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1083 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1084 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1085 SvPV_nolen (ST (2)));
1087 if (! SvOK (ST (0)))
1090 plugin = SvPV_nolen (ST (0));
1093 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1094 ds_array = (AV *)SvRV (ds);
1095 else if (! SvOK (ds))
1098 log_err ("Collectd::plugin_write: Invalid data-set.");
1103 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1104 log_err ("Collectd::plugin_write: Invalid value-list.");
1108 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1114 } /* static XS (Collectd__plugin_write) */
1117 * Collectd::_plugin_flush (plugin, timeout, identifier).
1120 * name of the plugin to flush
1123 * timeout to use when flushing the data
1126 * data-set identifier to flush
1128 static XS (Collectd__plugin_flush)
1130 char *plugin = NULL;
1137 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1142 plugin = SvPV_nolen (ST (0));
1145 timeout = (int)SvIV (ST (1));
1148 id = SvPV_nolen (ST (2));
1150 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1151 "id = \"%s\"", plugin, timeout, id);
1153 if (0 == plugin_flush (plugin, timeout, id))
1157 } /* static XS (Collectd__plugin_flush) */
1160 * Collectd::plugin_dispatch_notification (notif).
1163 * notification to dispatch
1165 static XS (Collectd_plugin_dispatch_notification)
1174 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1178 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1179 SvPV_nolen (ST (0)));
1183 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1184 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1188 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1194 } /* static XS (Collectd_plugin_dispatch_notification) */
1197 * Collectd::plugin_log (level, message).
1200 * log level (LOG_DEBUG, ... LOG_ERR)
1205 static XS (Collectd_plugin_log)
1210 log_err ("Usage: Collectd::plugin_log(level, message)");
1214 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1216 } /* static XS (Collectd_plugin_log) */
1219 * Collectd::call_by_name (...).
1221 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1223 static XS (Collectd_call_by_name)
1228 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1229 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1234 name = SvPV_nolen (tmp);
1236 if (NULL == get_cv (name, 0)) {
1237 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1242 /* simply pass on the subroutine call without touching the stack,
1243 * thus leaving any arguments and return values in place */
1245 } /* static XS (Collectd_call_by_name) */
1248 * collectd's perl interpreter based thread implementation.
1250 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1253 /* must be called with perl_threads->mutex locked */
1254 static void c_ithread_destroy (c_ithread_t *ithread)
1256 dTHXa (ithread->interp);
1258 assert (NULL != perl_threads);
1260 PERL_SET_CONTEXT (aTHX);
1261 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1266 --perl_threads->number_of_threads;
1267 #endif /* COLLECT_DEBUG */
1269 perl_destruct (aTHX);
1272 if (NULL == ithread->prev)
1273 perl_threads->head = ithread->next;
1275 ithread->prev->next = ithread->next;
1277 if (NULL == ithread->next)
1278 perl_threads->tail = ithread->prev;
1280 ithread->next->prev = ithread->prev;
1284 } /* static void c_ithread_destroy (c_ithread_t *) */
1286 static void c_ithread_destructor (void *arg)
1288 c_ithread_t *ithread = (c_ithread_t *)arg;
1289 c_ithread_t *t = NULL;
1291 if (NULL == perl_threads)
1294 pthread_mutex_lock (&perl_threads->mutex);
1296 for (t = perl_threads->head; NULL != t; t = t->next)
1300 /* the ithread no longer exists */
1304 c_ithread_destroy (ithread);
1306 pthread_mutex_unlock (&perl_threads->mutex);
1308 } /* static void c_ithread_destructor (void *) */
1310 /* must be called with perl_threads->mutex locked */
1311 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1313 c_ithread_t *t = NULL;
1316 assert (NULL != perl_threads);
1318 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1319 memset (t, 0, sizeof (c_ithread_t));
1321 t->interp = (NULL == base)
1323 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1327 if ((NULL != base) && (NULL != PL_endav)) {
1328 av_clear (PL_endav);
1329 av_undef (PL_endav);
1334 ++perl_threads->number_of_threads;
1335 #endif /* COLLECT_DEBUG */
1339 if (NULL == perl_threads->tail) {
1340 perl_threads->head = t;
1344 perl_threads->tail->next = t;
1345 t->prev = perl_threads->tail;
1348 perl_threads->tail = t;
1350 pthread_setspecific (perl_thr_key, (const void *)t);
1352 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1355 * Interface to collectd.
1358 static int perl_init (void)
1362 if (NULL == perl_threads)
1366 c_ithread_t *t = NULL;
1368 pthread_mutex_lock (&perl_threads->mutex);
1369 t = c_ithread_create (perl_threads->head->interp);
1370 pthread_mutex_unlock (&perl_threads->mutex);
1375 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1376 aTHX, perl_threads->number_of_threads);
1377 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1378 } /* static int perl_init (void) */
1380 static int perl_read (void)
1384 if (NULL == perl_threads)
1388 c_ithread_t *t = NULL;
1390 pthread_mutex_lock (&perl_threads->mutex);
1391 t = c_ithread_create (perl_threads->head->interp);
1392 pthread_mutex_unlock (&perl_threads->mutex);
1397 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1398 aTHX, perl_threads->number_of_threads);
1399 return pplugin_call_all (aTHX_ PLUGIN_READ);
1400 } /* static int perl_read (void) */
1402 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1406 if (NULL == perl_threads)
1410 c_ithread_t *t = NULL;
1412 pthread_mutex_lock (&perl_threads->mutex);
1413 t = c_ithread_create (perl_threads->head->interp);
1414 pthread_mutex_unlock (&perl_threads->mutex);
1419 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1420 aTHX, perl_threads->number_of_threads);
1421 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1422 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1424 static void perl_log (int level, const char *msg)
1428 if (NULL == perl_threads)
1432 c_ithread_t *t = NULL;
1434 pthread_mutex_lock (&perl_threads->mutex);
1435 t = c_ithread_create (perl_threads->head->interp);
1436 pthread_mutex_unlock (&perl_threads->mutex);
1441 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1443 } /* static void perl_log (int, const char *) */
1445 static int perl_notify (const notification_t *notif)
1449 if (NULL == perl_threads)
1453 c_ithread_t *t = NULL;
1455 pthread_mutex_lock (&perl_threads->mutex);
1456 t = c_ithread_create (perl_threads->head->interp);
1457 pthread_mutex_unlock (&perl_threads->mutex);
1461 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1462 } /* static int perl_notify (const notification_t *) */
1464 static int perl_flush (int timeout, const char *identifier)
1468 if (NULL == perl_threads)
1472 c_ithread_t *t = NULL;
1474 pthread_mutex_lock (&perl_threads->mutex);
1475 t = c_ithread_create (perl_threads->head->interp);
1476 pthread_mutex_unlock (&perl_threads->mutex);
1480 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
1481 } /* static int perl_flush (const int) */
1483 static int perl_shutdown (void)
1485 c_ithread_t *t = NULL;
1491 plugin_unregister_complex_config ("perl");
1493 if (NULL == perl_threads)
1497 c_ithread_t *t = NULL;
1499 pthread_mutex_lock (&perl_threads->mutex);
1500 t = c_ithread_create (perl_threads->head->interp);
1501 pthread_mutex_unlock (&perl_threads->mutex);
1506 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
1507 aTHX, perl_threads->number_of_threads);
1509 plugin_unregister_log ("perl");
1510 plugin_unregister_notification ("perl");
1511 plugin_unregister_init ("perl");
1512 plugin_unregister_read ("perl");
1513 plugin_unregister_write ("perl");
1514 plugin_unregister_flush ("perl");
1516 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
1518 pthread_mutex_lock (&perl_threads->mutex);
1519 t = perl_threads->tail;
1522 c_ithread_t *thr = t;
1524 /* the pointer has to be advanced before destroying
1525 * the thread as this will free the memory */
1528 c_ithread_destroy (thr);
1531 pthread_mutex_unlock (&perl_threads->mutex);
1532 pthread_mutex_destroy (&perl_threads->mutex);
1534 sfree (perl_threads);
1536 pthread_key_delete (perl_thr_key);
1540 plugin_unregister_shutdown ("perl");
1542 } /* static void perl_shutdown (void) */
1545 * Access functions for global variables.
1547 * These functions implement the "magic" used to access
1548 * the global variables from Perl.
1551 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
1553 char *pv = mg->mg_ptr;
1556 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
1558 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
1560 char *pv = mg->mg_ptr;
1561 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
1563 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
1565 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
1567 int *iv = (int *)mg->mg_ptr;
1568 sv_setiv (var, *iv);
1570 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
1572 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
1574 int *iv = (int *)mg->mg_ptr;
1575 *iv = (int)SvIV (var);
1577 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
1579 static MGVTBL g_pv_vtbl = {
1580 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
1581 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
1585 static MGVTBL g_iv_vtbl = {
1586 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
1587 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
1592 /* bootstrap the Collectd module */
1593 static void xs_init (pTHX)
1597 char *file = __FILE__;
1603 /* enable usage of Perl modules using shared libraries */
1604 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1607 for (i = 0; NULL != api[i].f; ++i)
1608 newXS (api[i].name, api[i].f, file);
1610 stash = gv_stashpv ("Collectd", 1);
1612 /* export "constants" */
1613 for (i = 0; '\0' != constants[i].name[0]; ++i)
1614 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
1616 /* export global variables
1617 * by adding "magic" to the SV's representing the globale variables
1618 * perl is able to automagically call the get/set function when
1619 * accessing any such variable (this is basically the same as using
1621 /* global strings */
1622 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
1623 tmp = get_sv (g_strings[i].name, 1);
1624 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
1625 g_strings[i].var, 0);
1628 /* global integers */
1629 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
1630 tmp = get_sv (g_integers[i].name, 1);
1631 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
1632 (char *)g_integers[i].var, 0);
1635 } /* static void xs_init (pTHX) */
1637 /* Initialize the global Perl interpreter. */
1638 static int init_pi (int argc, char **argv)
1642 if (NULL != perl_threads)
1645 log_info ("Initializing Perl interpreter...");
1650 for (i = 0; i < argc; ++i)
1651 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
1653 #endif /* COLLECT_DEBUG */
1655 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
1656 log_err ("init_pi: pthread_key_create failed");
1658 /* this must not happen - cowardly giving up if it does */
1663 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
1664 * triggers a "value computed is not used" warning by gcc. */
1667 PERL_SYS_INIT3 (&argc, &argv, &environ);
1669 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
1670 memset (perl_threads, 0, sizeof (c_ithread_list_t));
1672 pthread_mutex_init (&perl_threads->mutex, NULL);
1673 /* locking the mutex should not be necessary at this point
1674 * but let's just do it for the sake of completeness */
1675 pthread_mutex_lock (&perl_threads->mutex);
1677 perl_threads->head = c_ithread_create (NULL);
1678 perl_threads->tail = perl_threads->head;
1680 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
1681 log_err ("init_pi: Not enough memory.");
1685 aTHX = perl_threads->head->interp;
1686 pthread_mutex_unlock (&perl_threads->mutex);
1688 perl_construct (aTHX);
1690 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1692 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
1693 SV *err = get_sv ("@", 1);
1694 log_err ("init_pi: Unable to bootstrap Collectd: %s",
1697 perl_destruct (perl_threads->head->interp);
1698 perl_free (perl_threads->head->interp);
1699 sfree (perl_threads);
1701 pthread_key_delete (perl_thr_key);
1705 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
1706 sv_setpv (get_sv ("0", 0), "collectd");
1710 plugin_register_log ("perl", perl_log);
1711 plugin_register_notification ("perl", perl_notify);
1712 plugin_register_init ("perl", perl_init);
1714 plugin_register_read ("perl", perl_read);
1716 plugin_register_write ("perl", perl_write);
1717 plugin_register_flush ("perl", perl_flush);
1718 plugin_register_shutdown ("perl", perl_shutdown);
1720 } /* static int init_pi (const char **, const int) */
1723 * LoadPlugin "<Plugin>"
1725 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
1727 char module_name[DATA_MAX_NAME_LEN];
1731 if ((0 != ci->children_num) || (1 != ci->values_num)
1732 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1733 log_err ("LoadPlugin expects a single string argument.");
1737 value = ci->values[0].value.string;
1739 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
1740 log_err ("Invalid module name %s", value);
1744 if (0 != init_pi (perl_argc, perl_argv))
1747 assert (NULL != perl_threads);
1748 assert (NULL != perl_threads->head);
1750 aTHX = perl_threads->head->interp;
1752 log_debug ("perl_config: loading perl plugin \"%s\"", value);
1753 load_module (PERL_LOADMOD_NOIMPORT,
1754 newSVpv (module_name, strlen (module_name)), Nullsv);
1756 } /* static int perl_config_loadplugin (oconfig_item_it *) */
1761 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
1765 if ((0 != ci->children_num) || (1 != ci->values_num)
1766 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1767 log_err ("BaseName expects a single string argument.");
1771 value = ci->values[0].value.string;
1773 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
1774 sstrncpy (base_name, value, sizeof (base_name));
1776 } /* static int perl_config_basename (oconfig_item_it *) */
1779 * EnableDebugger "<Package>"|""
1781 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
1785 if ((0 != ci->children_num) || (1 != ci->values_num)
1786 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1787 log_err ("EnableDebugger expects a single string argument.");
1791 if (NULL != perl_threads) {
1792 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
1796 value = ci->values[0].value.string;
1798 perl_argv = (char **)realloc (perl_argv,
1799 (++perl_argc + 1) * sizeof (char *));
1801 if (NULL == perl_argv) {
1802 log_err ("perl_config: Not enough memory.");
1806 if ('\0' == value[0]) {
1807 perl_argv[perl_argc - 1] = "-d";
1810 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
1811 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
1812 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
1815 perl_argv[perl_argc] = NULL;
1817 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
1820 * IncludeDir "<Dir>"
1822 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
1826 if ((0 != ci->children_num) || (1 != ci->values_num)
1827 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1828 log_err ("IncludeDir expects a single string argument.");
1832 value = ci->values[0].value.string;
1835 perl_argv = (char **)realloc (perl_argv,
1836 (++perl_argc + 1) * sizeof (char *));
1838 if (NULL == perl_argv) {
1839 log_err ("perl_config: Not enough memory.");
1843 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
1844 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
1845 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
1847 perl_argv[perl_argc] = NULL;
1850 /* prepend the directory to @INC */
1851 av_unshift (GvAVn (PL_incgv), 1);
1852 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
1855 } /* static int perl_config_includedir (oconfig_item_it *) */
1860 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
1870 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1871 log_err ("LoadPlugin expects a single string argument.");
1875 plugin = ci->values[0].value.string;
1878 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1882 log_err ("Unable to convert configuration to a Perl hash value.");
1891 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
1892 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1896 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
1911 } /* static int perl_config_plugin (oconfig_item_it *) */
1913 static int perl_config (oconfig_item_t *ci)
1920 for (i = 0; i < ci->children_num; ++i) {
1921 oconfig_item_t *c = ci->children + i;
1922 int current_status = 0;
1924 if (NULL != perl_threads)
1925 aTHX = PERL_GET_CONTEXT;
1927 if (0 == strcasecmp (c->key, "LoadPlugin"))
1928 current_status = perl_config_loadplugin (aTHX_ c);
1929 else if (0 == strcasecmp (c->key, "BaseName"))
1930 current_status = perl_config_basename (aTHX_ c);
1931 else if (0 == strcasecmp (c->key, "EnableDebugger"))
1932 current_status = perl_config_enabledebugger (aTHX_ c);
1933 else if (0 == strcasecmp (c->key, "IncludeDir"))
1934 current_status = perl_config_includedir (aTHX_ c);
1935 else if (0 == strcasecmp (c->key, "Plugin"))
1936 current_status = perl_config_plugin (aTHX_ c);
1939 log_warn ("Ignoring unknown config key \"%s\".", c->key);
1943 /* fatal error - it's up to perl_config_* to clean up */
1944 if (0 > current_status) {
1945 log_err ("Configuration failed with a fatal error - "
1946 "plugin disabled!");
1947 return current_status;
1950 status += current_status;
1953 } /* static int perl_config (oconfig_item_t *) */
1955 void module_register (void)
1958 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
1960 /* default options for the Perl interpreter */
1962 perl_argv[1] = "-MCollectd";
1963 perl_argv[2] = "-e";
1965 perl_argv[4] = NULL;
1967 plugin_register_complex_config ("perl", perl_config);
1969 } /* void module_register (void) */
1971 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */