2 * collectd - src/perl.c
3 * Copyright (C) 2007 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.
29 #include "configfile.h"
36 /* Some versions of Perl define their own version of DEBUG... :-/ */
41 /* ... while we want the definition found in plugin.h. */
47 #define PLUGIN_WRITE 2
48 #define PLUGIN_SHUTDOWN 3
51 #define PLUGIN_TYPES 5
53 #define PLUGIN_DATASET 255
55 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
56 #define log_info(...) INFO ("perl: " __VA_ARGS__)
57 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
58 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
61 /* this is defined in DynaLoader.a */
62 void boot_DynaLoader (PerlInterpreter *, CV *);
64 static XS (Collectd_plugin_register_ds);
65 static XS (Collectd_plugin_unregister_ds);
66 static XS (Collectd_plugin_dispatch_values);
67 static XS (Collectd_plugin_log);
84 /* valid configuration file keys */
85 static const char *config_keys[] =
91 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
93 static PerlInterpreter *perl = NULL;
95 static int perl_argc = 0;
96 static char **perl_argv = NULL;
98 static char base_name[DATA_MAX_NAME_LEN] = "";
100 static HV *data_sets;
107 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
108 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
109 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
110 { "Collectd::plugin_log", Collectd_plugin_log },
116 * Helper functions for data type conversion.
131 static int hv2data_source (HV *hash, data_source_t *ds)
135 if ((NULL == hash) || (NULL == ds))
138 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "name", 4, 0))) {
139 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
140 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
143 log_err ("hv2data_source: No DS name given.");
147 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "type", 4, 0))) {
148 ds->type = SvIV (*tmp);
150 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
151 log_err ("hv2data_source: Invalid DS type.");
156 ds->type = DS_TYPE_COUNTER;
159 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "min", 3, 0)))
160 ds->min = SvNV (*tmp);
164 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "max", 3, 0)))
165 ds->max = SvNV (*tmp);
169 } /* static data_source_t *hv2data_source (HV *) */
171 static int av2value (char *name, AV *array, value_t *value, int len)
175 ds_types_t *ds = NULL;
179 if ((NULL == name) || (NULL == array) || (NULL == value))
182 if (Perl_av_len (perl, array) < len - 1)
183 len = Perl_av_len (perl, array) + 1;
188 tmp = Perl_hv_fetch (perl, data_sets, name, strlen (name), 0);
190 log_err ("av2value: No dataset for \"%s\".", name);
193 ds = (ds_types_t *)SvIV ((SV *)SvRV (*tmp));
196 log_warn ("av2value: Value length exceeds data set length.");
200 for (i = 0; i < len; ++i) {
201 SV **tmp = Perl_av_fetch (perl, array, i, 0);
204 if (DS_TYPE_COUNTER == ds->values[i])
205 value[i].counter = SvIV (*tmp);
207 value[i].gauge = SvNV (*tmp);
214 } /* static int av2value (char *, AV *, value_t *, int) */
216 static int data_set2av (data_set_t *ds, AV *array)
220 if ((NULL == ds) || (NULL == array))
223 Perl_av_extend (perl, array, ds->ds_num);
225 for (i = 0; i < ds->ds_num; ++i) {
226 HV *source = Perl_newHV (perl);
228 if (NULL == Perl_hv_store (perl, source, "name", 4,
229 Perl_newSVpv (perl, ds->ds[i].name, 0), 0))
232 if (NULL == Perl_hv_store (perl, source, "type", 4,
233 Perl_newSViv (perl, ds->ds[i].type), 0))
236 if (! isnan (ds->ds[i].min))
237 if (NULL == Perl_hv_store (perl, source, "min", 3,
238 Perl_newSVnv (perl, ds->ds[i].min), 0))
241 if (! isnan (ds->ds[i].max))
242 if (NULL == Perl_hv_store (perl, source, "max", 3,
243 Perl_newSVnv (perl, ds->ds[i].max), 0))
246 if (NULL == Perl_av_store (perl, array, i,
247 Perl_newRV_noinc (perl, (SV *)source)))
251 } /* static int data_set2av (data_set_t *, AV *) */
253 static int value_list2hv (value_list_t *vl, data_set_t *ds, HV *hash)
260 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
263 len = vl->values_len;
265 if (ds->ds_num < len) {
266 log_warn ("value2av: Value length exceeds data set length.");
270 values = Perl_newAV (perl);
271 Perl_av_extend (perl, values, len - 1);
273 for (i = 0; i < len; ++i) {
276 if (DS_TYPE_COUNTER == ds->ds[i].type)
277 val = Perl_newSViv (perl, vl->values[i].counter);
279 val = Perl_newSVnv (perl, vl->values[i].gauge);
281 if (NULL == Perl_av_store (perl, values, i, val)) {
282 Perl_av_undef (perl, values);
287 if (NULL == Perl_hv_store (perl, hash, "values", 6,
288 Perl_newRV_noinc (perl, (SV *)values), 0))
292 if (NULL == Perl_hv_store (perl, hash, "time", 4,
293 Perl_newSViv (perl, vl->time), 0))
296 if ('\0' != vl->host[0])
297 if (NULL == Perl_hv_store (perl, hash, "host", 4,
298 Perl_newSVpv (perl, vl->host, 0), 0))
301 if ('\0' != vl->plugin[0])
302 if (NULL == Perl_hv_store (perl, hash, "plugin", 6,
303 Perl_newSVpv (perl, vl->plugin, 0), 0))
306 if ('\0' != vl->plugin_instance[0])
307 if (NULL == Perl_hv_store (perl, hash, "plugin_instance", 15,
308 Perl_newSVpv (perl, vl->plugin_instance, 0), 0))
311 if ('\0' != vl->type_instance[0])
312 if (NULL == Perl_hv_store (perl, hash, "type_instance", 13,
313 Perl_newSVpv (perl, vl->type_instance, 0), 0))
316 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
320 * Internal functions.
323 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
325 if (base_name[0] == '\0')
326 status = snprintf (buf, buf_len, "%s", module);
328 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
329 if ((status < 0) || (status >= buf_len))
331 buf[buf_len - 1] = '\0';
333 } /* char *get_module_name */
336 * Add a plugin's data set definition.
338 static int pplugin_register_data_set (char *name, AV *dataset)
343 data_source_t *ds = NULL;
344 data_set_t *set = NULL;
346 ds_types_t *types = NULL;
348 if ((NULL == name) || (NULL == dataset))
351 len = Perl_av_len (perl, dataset);
356 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
357 set = (data_set_t *)smalloc (sizeof (data_set_t));
359 types = (ds_types_t *)smalloc (sizeof (ds_types_t));
360 types->len = len + 1;
361 types->values = (int *)smalloc ((types->len) * sizeof (int));
363 for (i = 0; i <= len; ++i) {
364 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
369 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
370 log_err ("pplugin_register_data_set: Invalid data source.");
374 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
377 types->values[i] = ds[i].type;
378 log_debug ("pplugin_register_data_set: "
379 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
380 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
383 if (NULL == Perl_hv_store (perl, data_sets, name, strlen (name),
384 Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, types), 0))
387 strncpy (set->type, name, DATA_MAX_NAME_LEN);
388 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
390 set->ds_num = len + 1;
392 return plugin_register_data_set (set);
393 } /* static int pplugin_register_data_set (char *, SV *) */
396 * Remove a plugin's data set definition.
398 static int pplugin_unregister_data_set (char *name)
405 /* freeing the allocated memory of the element itself (ds_types_t *)
406 * causes a segfault during perl_destruct () thus I assume perl somehow
407 * takes care of this... */
409 tmp = Perl_hv_delete (perl, data_sets, name, strlen (name), 0);
411 ds_types_t *ds = (ds_types_t *)SvIV ((SV *)SvRV (tmp));
414 return plugin_unregister_data_set (name);
415 } /* static int pplugin_unregister_data_set (char *) */
418 * Submit the values to the write functions.
422 * values => [ @values ],
426 * plugin_instance => $pinstance,
427 * type_instance => $tinstance,
430 static int pplugin_dispatch_values (char *name, HV *values)
432 value_list_t list = VALUE_LIST_INIT;
439 if ((NULL == name) || (NULL == values))
442 if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
443 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
444 log_err ("pplugin_dispatch_values: No valid values given.");
449 AV *array = (AV *)SvRV (*tmp);
450 int len = Perl_av_len (perl, array) + 1;
452 val = (value_t *)smalloc (len * sizeof (value_t));
454 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
457 if (-1 == list.values_len) {
463 if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
464 list.time = (time_t)SvIV (*tmp);
467 list.time = time (NULL);
470 if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
471 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
472 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
475 strcpy (list.host, hostname_g);
478 if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
479 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
480 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
483 if (NULL != (tmp = Perl_hv_fetch (perl, values,
484 "plugin_instance", 15, 0))) {
485 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
486 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
489 if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
490 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
491 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
494 ret = plugin_dispatch_values (name, &list);
498 } /* static int pplugin_dispatch_values (char *, HV *) */
501 * Call all working functions of the given type.
503 static int pplugin_call_all (int type, ...)
512 if ((type < 0) || (type >= PLUGIN_TYPES))
522 XPUSHs (sv_2mortal (Perl_newSViv (perl, (IV)type)));
524 if (PLUGIN_WRITE == type) {
526 * $_[0] = $plugin_type;
541 * values => [ $v1, ... ],
545 * plugin_instance => $instance,
546 * type_instance => $type_instance
552 AV *pds = Perl_newAV (perl);
553 HV *pvl = Perl_newHV (perl);
555 ds = va_arg (ap, data_set_t *);
556 vl = va_arg (ap, value_list_t *);
558 if (-1 == data_set2av (ds, pds))
561 if (-1 == value_list2hv (vl, ds, pvl))
564 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
565 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
566 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
568 else if (PLUGIN_LOG == type) {
574 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
575 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
580 retvals = Perl_call_pv (perl, "Collectd::plugin_call_all", G_SCALAR);
595 } /* static int pplugin_call_all (int, ...) */
603 * Collectd::plugin_register_data_set (type, dataset).
606 * type of the dataset
609 * dataset to be registered
611 static XS (Collectd_plugin_register_ds)
619 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
623 log_debug ("Collectd::plugin_register_data_set: "
624 "type = \"%s\", dataset = \"%s\"",
625 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
629 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
630 ret = pplugin_register_data_set (SvPV_nolen (ST (0)),
634 log_err ("Collectd::plugin_register_data_set: Invalid data.");
642 } /* static XS (Collectd_plugin_register_ds) */
645 * Collectd::plugin_unregister_data_set (type).
648 * type of the dataset
650 static XS (Collectd_plugin_unregister_ds)
655 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
659 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
660 SvPV_nolen (ST (0)));
662 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
666 } /* static XS (Collectd_plugin_register_ds) */
669 * Collectd::plugin_dispatch_values (name, values).
675 * value list to submit
677 static XS (Collectd_plugin_dispatch_values)
686 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
690 log_debug ("Collectd::plugin_dispatch_values: "
691 "name = \"%s\", values=\"%s\"",
692 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
696 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
697 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
701 if ((NULL == ST (0)) || (NULL == values))
704 ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
710 } /* static XS (Collectd_plugin_dispatch_values) */
713 * Collectd::plugin_log (level, message).
716 * log level (LOG_DEBUG, ... LOG_ERR)
721 static XS (Collectd_plugin_log)
726 log_err ("Usage: Collectd::plugin_log(level, message)");
730 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
732 } /* static XS (Collectd_plugin_log) */
735 * Collectd::bootstrap ().
737 static XS (boot_Collectd)
740 char *file = __FILE__;
747 { "Collectd::TYPE_INIT", Perl_newSViv (perl, PLUGIN_INIT) },
748 { "Collectd::TYPE_READ", Perl_newSViv (perl, PLUGIN_READ) },
749 { "Collectd::TYPE_WRITE", Perl_newSViv (perl, PLUGIN_WRITE) },
750 { "Collectd::TYPE_SHUTDOWN", Perl_newSViv (perl, PLUGIN_SHUTDOWN) },
751 { "Collectd::TYPE_LOG", Perl_newSViv (perl, PLUGIN_LOG) },
752 { "Collectd::TYPE_DATASET", Perl_newSViv (perl, PLUGIN_DATASET) },
753 { "Collectd::DS_TYPE_COUNTER", Perl_newSViv (perl, DS_TYPE_COUNTER) },
754 { "Collectd::DS_TYPE_GAUGE", Perl_newSViv (perl, DS_TYPE_GAUGE) },
755 { "Collectd::LOG_ERR", Perl_newSViv (perl, LOG_ERR) },
756 { "Collectd::LOG_WARNING", Perl_newSViv (perl, LOG_WARNING) },
757 { "Collectd::LOG_NOTICE", Perl_newSViv (perl, LOG_NOTICE) },
758 { "Collectd::LOG_INFO", Perl_newSViv (perl, LOG_INFO) },
759 { "Collectd::LOG_DEBUG", Perl_newSViv (perl, LOG_DEBUG) },
767 if ((1 > items) || (2 < items)) {
768 log_err ("Usage: Collectd::bootstrap(name[, version])");
772 XS_VERSION_BOOTCHECK;
775 for (i = 0; NULL != api[i].f; ++i)
776 Perl_newXS (perl, api[i].name, api[i].f, file);
778 stash = Perl_gv_stashpv (perl, "Collectd", 1);
780 /* export "constants" */
781 for (i = 0; NULL != consts[i].value; ++i)
782 Perl_newCONSTSUB (perl, stash, consts[i].name, consts[i].value);
784 } /* static XS (boot_Collectd) */
788 * Interface to collectd.
791 static int perl_init (void)
796 PERL_SET_CONTEXT (perl);
797 return pplugin_call_all (PLUGIN_INIT);
798 } /* static int perl_init (void) */
800 static int perl_read (void)
805 PERL_SET_CONTEXT (perl);
806 return pplugin_call_all (PLUGIN_READ);
807 } /* static int perl_read (void) */
809 static int perl_write (const data_set_t *ds, const value_list_t *vl)
814 PERL_SET_CONTEXT (perl);
815 return pplugin_call_all (PLUGIN_WRITE, ds, vl);
816 } /* static int perl_write (const data_set_t *, const value_list_t *) */
818 static void perl_log (int level, const char *msg)
823 PERL_SET_CONTEXT (perl);
824 pplugin_call_all (PLUGIN_LOG, level, msg);
826 } /* static void perl_log (int, const char *) */
828 static int perl_shutdown (void)
832 plugin_unregister_config ("perl");
837 plugin_unregister_log ("perl");
838 plugin_unregister_init ("perl");
839 plugin_unregister_read ("perl");
840 plugin_unregister_write ("perl");
842 PERL_SET_CONTEXT (perl);
843 ret = pplugin_call_all (PLUGIN_SHUTDOWN);
845 if (0 < Perl_hv_iterinit (perl, data_sets)) {
849 while (NULL != Perl_hv_iternextsv (perl, data_sets, &k, &l)) {
850 pplugin_unregister_data_set (k);
854 Perl_hv_undef (perl, data_sets);
857 Perl_sv_report_used (perl);
858 #endif /* COLLECT_DEBUG */
860 perl_destruct (perl);
866 plugin_unregister_shutdown ("perl");
868 } /* static void perl_shutdown (void) */
870 static void xs_init (pTHX)
872 char *file = __FILE__;
876 /* build the Collectd module into the perl interpreter */
877 Perl_newXS (perl, "Collectd::bootstrap", boot_Collectd, file);
879 /* enable usage of Perl modules using shared libraries */
880 Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
882 } /* static void xs_init (pTHX) */
884 /* Initialize the global Perl interpreter. */
885 static int init_pi (int argc, char **argv)
892 log_info ("Initializing Perl interpreter...");
894 for (i = 0; i < argc; ++i)
895 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
896 #endif /* COLLECT_DEBUG */
898 PERL_SYS_INIT3 (&argc, &argv, &environ);
900 if (NULL == (perl = perl_alloc ())) {
901 log_err ("module_register: Not enough memory.");
904 perl_construct (perl);
906 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
908 if (0 != perl_parse (perl, xs_init, argc, argv, NULL)) {
909 log_err ("module_register: Unable to bootstrap Collectd.");
914 data_sets = Perl_newHV (perl);
916 plugin_register_log ("perl", perl_log);
917 plugin_register_init ("perl", perl_init);
919 plugin_register_read ("perl", perl_read);
921 plugin_register_write ("perl", perl_write);
922 plugin_register_shutdown ("perl", perl_shutdown);
924 } /* static int init_pi (const char **, const int) */
926 static int perl_config (const char *key, const char *value)
928 log_debug ("perl_config: key = \"%s\", value=\"%s\"", key, value);
930 if (0 == strcasecmp (key, "LoadPlugin")) {
931 char module_name[DATA_MAX_NAME_LEN];
933 if (get_module_name (module_name, sizeof (module_name), value)
935 log_err ("Invalid module name %s", value);
937 } /* if (get_module_name == NULL) */
939 init_pi (perl_argc, perl_argv);
941 log_debug ("perl_config: loading perl plugin \"%s\"", value);
942 Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
943 Perl_newSVpv (perl, module_name, strlen (module_name)),
946 else if (0 == strcasecmp (key, "BaseName")) {
947 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
948 strncpy (base_name, value, sizeof (base_name));
949 base_name[sizeof (base_name) - 1] = '\0';
951 else if (0 == strcasecmp (key, "IncludeDir")) {
952 perl_argv = (char **)realloc (perl_argv,
953 (++perl_argc + 1) * sizeof (char *));
955 if (NULL == perl_argv) {
956 log_err ("perl_config: Not enough memory.");
960 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
961 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
962 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
964 perl_argv[perl_argc] = NULL;
970 } /* static int perl_config (char *, char *) */
972 void module_register (void)
975 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
977 /* default options for the Perl interpreter */
979 perl_argv[1] = "-MCollectd";
984 plugin_register_config ("perl", perl_config, config_keys, config_keys_num);
986 } /* void module_register (void) */
988 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */