3b91e5f4abc97fdb6b2fe0ce4a443737014857b2
1 /**
2 * collectd - src/perl.c
3 * Copyright (C) 2007 Sebastian Harl
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
25 */
27 #include "collectd.h"
29 #include "configfile.h"
31 #include <EXTERN.h>
32 #include <perl.h>
34 #include <XSUB.h>
36 /* Some versions of Perl define their own version of DEBUG... :-/ */
37 #ifdef DEBUG
38 # undef DEBUG
39 #endif /* DEBUG */
41 /* ... while we want the definition found in plugin.h. */
42 #include "plugin.h"
43 #include "common.h"
45 #define PLUGIN_INIT 0
46 #define PLUGIN_READ 1
47 #define PLUGIN_WRITE 2
48 #define PLUGIN_SHUTDOWN 3
49 #define PLUGIN_LOG 4
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);
70 /*
71 * private data types
72 */
74 typedef struct {
75 int len;
76 int *values;
77 } ds_types_t;
80 /*
81 * private variables
82 */
84 /* valid configuration file keys */
85 static const char *config_keys[] =
86 {
87 "LoadPlugin",
88 "BaseName",
89 "EnableDebugger",
90 "IncludeDir"
91 };
92 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
94 static PerlInterpreter *perl = NULL;
96 static int perl_argc = 0;
97 static char **perl_argv = NULL;
99 static char base_name[DATA_MAX_NAME_LEN] = "";
101 static HV *data_sets;
103 static struct {
104 char name[64];
105 XS ((*f));
106 } api[] =
107 {
108 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
109 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
110 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
111 { "Collectd::plugin_log", Collectd_plugin_log },
112 { "", NULL }
113 };
115 struct {
116 char name[64];
117 int value;
118 } constants[] =
119 {
120 { "Collectd::TYPE_INIT", PLUGIN_INIT },
121 { "Collectd::TYPE_READ", PLUGIN_READ },
122 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
123 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
124 { "Collectd::TYPE_LOG", PLUGIN_LOG },
125 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
126 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
127 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
128 { "Collectd::LOG_ERR", LOG_ERR },
129 { "Collectd::LOG_WARNING", LOG_WARNING },
130 { "Collectd::LOG_NOTICE", LOG_NOTICE },
131 { "Collectd::LOG_INFO", LOG_INFO },
132 { "Collectd::LOG_DEBUG", LOG_DEBUG },
133 { "", 0 }
134 };
137 /*
138 * Helper functions for data type conversion.
139 */
141 /*
142 * data source:
143 * [
144 * {
145 * name => $ds_name,
146 * type => $ds_type,
147 * min => $ds_min,
148 * max => $ds_max
149 * },
150 * ...
151 * ]
152 */
153 static int hv2data_source (HV *hash, data_source_t *ds)
154 {
155 SV **tmp = NULL;
157 if ((NULL == hash) || (NULL == ds))
158 return -1;
160 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "name", 4, 0))) {
161 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
162 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
163 }
164 else {
165 log_err ("hv2data_source: No DS name given.");
166 return -1;
167 }
169 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "type", 4, 0))) {
170 ds->type = SvIV (*tmp);
172 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
173 log_err ("hv2data_source: Invalid DS type.");
174 return -1;
175 }
176 }
177 else {
178 ds->type = DS_TYPE_COUNTER;
179 }
181 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "min", 3, 0)))
182 ds->min = SvNV (*tmp);
183 else
184 ds->min = NAN;
186 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "max", 3, 0)))
187 ds->max = SvNV (*tmp);
188 else
189 ds->max = NAN;
190 return 0;
191 } /* static data_source_t *hv2data_source (HV *) */
193 static int av2value (char *name, AV *array, value_t *value, int len)
194 {
195 SV **tmp = NULL;
197 ds_types_t *ds = NULL;
199 int i = 0;
201 if ((NULL == name) || (NULL == array) || (NULL == value))
202 return -1;
204 if (Perl_av_len (perl, array) < len - 1)
205 len = Perl_av_len (perl, array) + 1;
207 if (0 >= len)
208 return -1;
210 tmp = Perl_hv_fetch (perl, data_sets, name, strlen (name), 0);
211 if (NULL == tmp) {
212 log_err ("av2value: No dataset for \"%s\".", name);
213 return -1;
214 }
215 ds = (ds_types_t *)SvIV ((SV *)SvRV (*tmp));
217 if (ds->len < len) {
218 log_warn ("av2value: Value length exceeds data set length.");
219 len = ds->len;
220 }
222 for (i = 0; i < len; ++i) {
223 SV **tmp = Perl_av_fetch (perl, array, i, 0);
225 if (NULL != tmp) {
226 if (DS_TYPE_COUNTER == ds->values[i])
227 value[i].counter = SvIV (*tmp);
228 else
229 value[i].gauge = SvNV (*tmp);
230 }
231 else {
232 return -1;
233 }
234 }
235 return len;
236 } /* static int av2value (char *, AV *, value_t *, int) */
238 static int data_set2av (data_set_t *ds, AV *array)
239 {
240 int i = 0;
242 if ((NULL == ds) || (NULL == array))
243 return -1;
245 Perl_av_extend (perl, array, ds->ds_num);
247 for (i = 0; i < ds->ds_num; ++i) {
248 HV *source = Perl_newHV (perl);
250 if (NULL == Perl_hv_store (perl, source, "name", 4,
251 Perl_newSVpv (perl, ds->ds[i].name, 0), 0))
252 return -1;
254 if (NULL == Perl_hv_store (perl, source, "type", 4,
255 Perl_newSViv (perl, ds->ds[i].type), 0))
256 return -1;
258 if (! isnan (ds->ds[i].min))
259 if (NULL == Perl_hv_store (perl, source, "min", 3,
260 Perl_newSVnv (perl, ds->ds[i].min), 0))
261 return -1;
263 if (! isnan (ds->ds[i].max))
264 if (NULL == Perl_hv_store (perl, source, "max", 3,
265 Perl_newSVnv (perl, ds->ds[i].max), 0))
266 return -1;
268 if (NULL == Perl_av_store (perl, array, i,
269 Perl_newRV_noinc (perl, (SV *)source)))
270 return -1;
271 }
272 return 0;
273 } /* static int data_set2av (data_set_t *, AV *) */
275 static int value_list2hv (value_list_t *vl, data_set_t *ds, HV *hash)
276 {
277 AV *values = NULL;
279 int i = 0;
280 int len = 0;
282 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
283 return -1;
285 len = vl->values_len;
287 if (ds->ds_num < len) {
288 log_warn ("value2av: Value length exceeds data set length.");
289 len = ds->ds_num;
290 }
292 values = Perl_newAV (perl);
293 Perl_av_extend (perl, values, len - 1);
295 for (i = 0; i < len; ++i) {
296 SV *val = NULL;
298 if (DS_TYPE_COUNTER == ds->ds[i].type)
299 val = Perl_newSViv (perl, vl->values[i].counter);
300 else
301 val = Perl_newSVnv (perl, vl->values[i].gauge);
303 if (NULL == Perl_av_store (perl, values, i, val)) {
304 Perl_av_undef (perl, values);
305 return -1;
306 }
307 }
309 if (NULL == Perl_hv_store (perl, hash, "values", 6,
310 Perl_newRV_noinc (perl, (SV *)values), 0))
311 return -1;
313 if (0 != vl->time)
314 if (NULL == Perl_hv_store (perl, hash, "time", 4,
315 Perl_newSViv (perl, vl->time), 0))
316 return -1;
318 if ('\0' != vl->host[0])
319 if (NULL == Perl_hv_store (perl, hash, "host", 4,
320 Perl_newSVpv (perl, vl->host, 0), 0))
321 return -1;
323 if ('\0' != vl->plugin[0])
324 if (NULL == Perl_hv_store (perl, hash, "plugin", 6,
325 Perl_newSVpv (perl, vl->plugin, 0), 0))
326 return -1;
328 if ('\0' != vl->plugin_instance[0])
329 if (NULL == Perl_hv_store (perl, hash, "plugin_instance", 15,
330 Perl_newSVpv (perl, vl->plugin_instance, 0), 0))
331 return -1;
333 if ('\0' != vl->type_instance[0])
334 if (NULL == Perl_hv_store (perl, hash, "type_instance", 13,
335 Perl_newSVpv (perl, vl->type_instance, 0), 0))
336 return -1;
337 return 0;
338 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
341 /*
342 * Internal functions.
343 */
345 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
346 int status = 0;
347 if (base_name[0] == '\0')
348 status = snprintf (buf, buf_len, "%s", module);
349 else
350 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
351 if ((status < 0) || (status >= buf_len))
352 return (NULL);
353 buf[buf_len - 1] = '\0';
354 return (buf);
355 } /* char *get_module_name */
357 /*
358 * Add a plugin's data set definition.
359 */
360 static int pplugin_register_data_set (char *name, AV *dataset)
361 {
362 int len = -1;
363 int i = 0;
365 data_source_t *ds = NULL;
366 data_set_t *set = NULL;
368 ds_types_t *types = NULL;
370 if ((NULL == name) || (NULL == dataset))
371 return -1;
373 len = Perl_av_len (perl, dataset);
375 if (-1 == len)
376 return -1;
378 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
379 set = (data_set_t *)smalloc (sizeof (data_set_t));
381 types = (ds_types_t *)smalloc (sizeof (ds_types_t));
382 types->len = len + 1;
383 types->values = (int *)smalloc ((types->len) * sizeof (int));
385 for (i = 0; i <= len; ++i) {
386 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
388 if (NULL == elem)
389 return -1;
391 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
392 log_err ("pplugin_register_data_set: Invalid data source.");
393 return -1;
394 }
396 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
397 return -1;
399 types->values[i] = ds[i].type;
400 log_debug ("pplugin_register_data_set: "
401 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
402 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
403 }
405 if (NULL == Perl_hv_store (perl, data_sets, name, strlen (name),
406 Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, types), 0))
407 return -1;
409 strncpy (set->type, name, DATA_MAX_NAME_LEN);
410 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
412 set->ds_num = len + 1;
413 set->ds = ds;
414 return plugin_register_data_set (set);
415 } /* static int pplugin_register_data_set (char *, SV *) */
417 /*
418 * Remove a plugin's data set definition.
419 */
420 static int pplugin_unregister_data_set (char *name)
421 {
422 SV *tmp = NULL;
424 if (NULL == name)
425 return 0;
427 /* freeing the allocated memory of the element itself (ds_types_t *)
428 * causes a segfault during perl_destruct () thus I assume perl somehow
429 * takes care of this... */
431 tmp = Perl_hv_delete (perl, data_sets, name, strlen (name), 0);
432 if (NULL != tmp) {
433 ds_types_t *ds = (ds_types_t *)SvIV ((SV *)SvRV (tmp));
434 sfree (ds->values);
435 }
436 return plugin_unregister_data_set (name);
437 } /* static int pplugin_unregister_data_set (char *) */
439 /*
440 * Submit the values to the write functions.
441 *
442 * value list:
443 * {
444 * values => [ @values ],
445 * time => $time,
446 * host => $host,
447 * plugin => $plugin,
448 * plugin_instance => $pinstance,
449 * type_instance => $tinstance,
450 * }
451 */
452 static int pplugin_dispatch_values (char *name, HV *values)
453 {
454 value_list_t list = VALUE_LIST_INIT;
455 value_t *val = NULL;
457 SV **tmp = NULL;
459 int ret = 0;
461 if ((NULL == name) || (NULL == values))
462 return -1;
464 if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
465 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
466 log_err ("pplugin_dispatch_values: No valid values given.");
467 return -1;
468 }
470 {
471 AV *array = (AV *)SvRV (*tmp);
472 int len = Perl_av_len (perl, array) + 1;
474 val = (value_t *)smalloc (len * sizeof (value_t));
476 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
477 list.values = val;
479 if (-1 == list.values_len) {
480 sfree (val);
481 return -1;
482 }
483 }
485 if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
486 list.time = (time_t)SvIV (*tmp);
487 }
488 else {
489 list.time = time (NULL);
490 }
492 if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
493 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
494 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
495 }
496 else {
497 strcpy (list.host, hostname_g);
498 }
500 if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
501 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
502 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
503 }
505 if (NULL != (tmp = Perl_hv_fetch (perl, values,
506 "plugin_instance", 15, 0))) {
507 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
508 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
509 }
511 if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
512 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
513 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
514 }
516 ret = plugin_dispatch_values (name, &list);
518 sfree (val);
519 return ret;
520 } /* static int pplugin_dispatch_values (char *, HV *) */
522 /*
523 * Call all working functions of the given type.
524 */
525 static int pplugin_call_all (int type, ...)
526 {
527 int retvals = 0;
529 va_list ap;
530 int ret = 0;
532 dSP;
534 if ((type < 0) || (type >= PLUGIN_TYPES))
535 return -1;
537 va_start (ap, type);
539 ENTER;
540 SAVETMPS;
542 PUSHMARK (SP);
544 XPUSHs (sv_2mortal (Perl_newSViv (perl, (IV)type)));
546 if (PLUGIN_WRITE == type) {
547 /*
548 * $_[0] = $plugin_type;
549 *
550 * $_[1] =
551 * [
552 * {
553 * name => $ds_name,
554 * type => $ds_type,
555 * min => $ds_min,
556 * max => $ds_max
557 * },
558 * ...
559 * ];
560 *
561 * $_[2] =
562 * {
563 * values => [ $v1, ... ],
564 * time => $time,
565 * host => $hostname,
566 * plugin => $plugin,
567 * plugin_instance => $instance,
568 * type_instance => $type_instance
569 * };
570 */
571 data_set_t *ds;
572 value_list_t *vl;
574 AV *pds = Perl_newAV (perl);
575 HV *pvl = Perl_newHV (perl);
577 ds = va_arg (ap, data_set_t *);
578 vl = va_arg (ap, value_list_t *);
580 if (-1 == data_set2av (ds, pds))
581 return -1;
583 if (-1 == value_list2hv (vl, ds, pvl))
584 return -1;
586 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
587 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
588 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
589 }
590 else if (PLUGIN_LOG == type) {
591 /*
592 * $_[0] = $level;
593 *
594 * $_[1] = $message;
595 */
596 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
597 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
598 }
600 PUTBACK;
602 retvals = Perl_call_pv (perl, "Collectd::plugin_call_all", G_SCALAR);
604 SPAGAIN;
605 if (0 < retvals) {
606 SV *tmp = POPs;
607 if (! SvTRUE (tmp))
608 ret = -1;
609 }
611 PUTBACK;
612 FREETMPS;
613 LEAVE;
615 va_end (ap);
616 return ret;
617 } /* static int pplugin_call_all (int, ...) */
620 /*
621 * Exported Perl API.
622 */
624 /*
625 * Collectd::plugin_register_data_set (type, dataset).
626 *
627 * type:
628 * type of the dataset
629 *
630 * dataset:
631 * dataset to be registered
632 */
633 static XS (Collectd_plugin_register_ds)
634 {
635 SV *data = NULL;
636 int ret = 0;
638 dXSARGS;
640 if (2 != items) {
641 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
642 XSRETURN_EMPTY;
643 }
645 log_debug ("Collectd::plugin_register_data_set: "
646 "type = \"%s\", dataset = \"%s\"",
647 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
649 data = ST (1);
651 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
652 ret = pplugin_register_data_set (SvPV_nolen (ST (0)),
653 (AV *)SvRV (data));
654 }
655 else {
656 log_err ("Collectd::plugin_register_data_set: Invalid data.");
657 XSRETURN_EMPTY;
658 }
660 if (0 == ret)
661 XSRETURN_YES;
662 else
663 XSRETURN_EMPTY;
664 } /* static XS (Collectd_plugin_register_ds) */
666 /*
667 * Collectd::plugin_unregister_data_set (type).
668 *
669 * type:
670 * type of the dataset
671 */
672 static XS (Collectd_plugin_unregister_ds)
673 {
674 dXSARGS;
676 if (1 != items) {
677 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
678 XSRETURN_EMPTY;
679 }
681 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
682 SvPV_nolen (ST (0)));
684 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
685 XSRETURN_YES;
686 else
687 XSRETURN_EMPTY;
688 } /* static XS (Collectd_plugin_register_ds) */
690 /*
691 * Collectd::plugin_dispatch_values (name, values).
692 *
693 * name:
694 * name of the plugin
695 *
696 * values:
697 * value list to submit
698 */
699 static XS (Collectd_plugin_dispatch_values)
700 {
701 SV *values = NULL;
703 int ret = 0;
705 dXSARGS;
707 if (2 != items) {
708 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
709 XSRETURN_EMPTY;
710 }
712 log_debug ("Collectd::plugin_dispatch_values: "
713 "name = \"%s\", values=\"%s\"",
714 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
716 values = ST (1);
718 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
719 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
720 XSRETURN_EMPTY;
721 }
723 if ((NULL == ST (0)) || (NULL == values))
724 XSRETURN_EMPTY;
726 ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
728 if (0 == ret)
729 XSRETURN_YES;
730 else
731 XSRETURN_EMPTY;
732 } /* static XS (Collectd_plugin_dispatch_values) */
734 /*
735 * Collectd::plugin_log (level, message).
736 *
737 * level:
738 * log level (LOG_DEBUG, ... LOG_ERR)
739 *
740 * message:
741 * log message
742 */
743 static XS (Collectd_plugin_log)
744 {
745 dXSARGS;
747 if (2 != items) {
748 log_err ("Usage: Collectd::plugin_log(level, message)");
749 XSRETURN_EMPTY;
750 }
752 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
753 XSRETURN_YES;
754 } /* static XS (Collectd_plugin_log) */
757 /*
758 * Interface to collectd.
759 */
761 static int perl_init (void)
762 {
763 if (NULL == perl)
764 return 0;
766 PERL_SET_CONTEXT (perl);
767 return pplugin_call_all (PLUGIN_INIT);
768 } /* static int perl_init (void) */
770 static int perl_read (void)
771 {
772 if (NULL == perl)
773 return 0;
775 PERL_SET_CONTEXT (perl);
776 return pplugin_call_all (PLUGIN_READ);
777 } /* static int perl_read (void) */
779 static int perl_write (const data_set_t *ds, const value_list_t *vl)
780 {
781 if (NULL == perl)
782 return 0;
784 PERL_SET_CONTEXT (perl);
785 return pplugin_call_all (PLUGIN_WRITE, ds, vl);
786 } /* static int perl_write (const data_set_t *, const value_list_t *) */
788 static void perl_log (int level, const char *msg)
789 {
790 if (NULL == perl)
791 return;
793 PERL_SET_CONTEXT (perl);
794 pplugin_call_all (PLUGIN_LOG, level, msg);
795 return;
796 } /* static void perl_log (int, const char *) */
798 static int perl_shutdown (void)
799 {
800 int ret = 0;
802 plugin_unregister_config ("perl");
804 if (NULL == perl)
805 return 0;
807 plugin_unregister_log ("perl");
808 plugin_unregister_init ("perl");
809 plugin_unregister_read ("perl");
810 plugin_unregister_write ("perl");
812 PERL_SET_CONTEXT (perl);
813 ret = pplugin_call_all (PLUGIN_SHUTDOWN);
815 if (0 < Perl_hv_iterinit (perl, data_sets)) {
816 char *k = NULL;
817 I32 l = 0;
819 while (NULL != Perl_hv_iternextsv (perl, data_sets, &k, &l)) {
820 pplugin_unregister_data_set (k);
821 }
822 }
824 Perl_hv_undef (perl, data_sets);
826 #if COLLECT_DEBUG
827 Perl_sv_report_used (perl);
828 #endif /* COLLECT_DEBUG */
830 perl_destruct (perl);
831 perl_free (perl);
832 perl = NULL;
834 PERL_SYS_TERM ();
836 plugin_unregister_shutdown ("perl");
837 return ret;
838 } /* static void perl_shutdown (void) */
840 /* bootstrap the Collectd module */
841 static void xs_init (pTHX)
842 {
843 HV *stash = NULL;
844 char *file = __FILE__;
846 int i = 0;
848 dXSUB_SYS;
850 /* enable usage of Perl modules using shared libraries */
851 Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
853 /* register API */
854 for (i = 0; NULL != api[i].f; ++i)
855 Perl_newXS (perl, api[i].name, api[i].f, file);
857 stash = Perl_gv_stashpv (perl, "Collectd", 1);
859 /* export "constants" */
860 for (i = 0; '\0' != constants[i].name[0]; ++i)
861 Perl_newCONSTSUB (perl, stash, constants[i].name,
862 Perl_newSViv (perl, constants[i].value));
863 return;
864 } /* static void xs_init (pTHX) */
866 /* Initialize the global Perl interpreter. */
867 static int init_pi (int argc, char **argv)
868 {
869 if (NULL != perl)
870 return 0;
872 log_info ("Initializing Perl interpreter...");
873 #if COLLECT_DEBUG
874 {
875 int i = 0;
877 for (i = 0; i < argc; ++i)
878 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
879 }
880 #endif /* COLLECT_DEBUG */
882 PERL_SYS_INIT3 (&argc, &argv, &environ);
884 if (NULL == (perl = perl_alloc ())) {
885 log_err ("module_register: Not enough memory.");
886 exit (3);
887 }
888 perl_construct (perl);
890 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
892 if (0 != perl_parse (perl, xs_init, argc, argv, NULL)) {
893 log_err ("module_register: Unable to bootstrap Collectd.");
894 exit (1);
895 }
896 perl_run (perl);
898 data_sets = Perl_newHV (perl);
900 plugin_register_log ("perl", perl_log);
901 plugin_register_init ("perl", perl_init);
903 plugin_register_read ("perl", perl_read);
905 plugin_register_write ("perl", perl_write);
906 plugin_register_shutdown ("perl", perl_shutdown);
907 return 0;
908 } /* static int init_pi (const char **, const int) */
910 static int perl_config (const char *key, const char *value)
911 {
912 log_debug ("perl_config: key = \"%s\", value=\"%s\"", key, value);
914 if (0 == strcasecmp (key, "LoadPlugin")) {
915 char module_name[DATA_MAX_NAME_LEN];
917 if (get_module_name (module_name, sizeof (module_name), value)
918 == NULL) {
919 log_err ("Invalid module name %s", value);
920 return (1);
921 } /* if (get_module_name == NULL) */
923 init_pi (perl_argc, perl_argv);
925 log_debug ("perl_config: loading perl plugin \"%s\"", value);
926 Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
927 Perl_newSVpv (perl, module_name, strlen (module_name)),
928 Nullsv);
929 }
930 else if (0 == strcasecmp (key, "BaseName")) {
931 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
932 strncpy (base_name, value, sizeof (base_name));
933 base_name[sizeof (base_name) - 1] = '\0';
934 }
935 else if (0 == strcasecmp (key, "EnableDebugger")) {
936 perl_argv = (char **)realloc (perl_argv,
937 (++perl_argc + 1) * sizeof (char *));
939 if (NULL == perl_argv) {
940 log_err ("perl_config: Not enough memory.");
941 exit (3);
942 }
944 if ('\0' == value[0]) {
945 perl_argv[perl_argc - 1] = "-d";
946 }
947 else {
948 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
949 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
950 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
951 }
953 perl_argv[perl_argc] = NULL;
954 }
955 else if (0 == strcasecmp (key, "IncludeDir")) {
956 perl_argv = (char **)realloc (perl_argv,
957 (++perl_argc + 1) * sizeof (char *));
959 if (NULL == perl_argv) {
960 log_err ("perl_config: Not enough memory.");
961 exit (3);
962 }
964 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
965 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
966 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
968 perl_argv[perl_argc] = NULL;
969 }
970 else {
971 return -1;
972 }
973 return 0;
974 } /* static int perl_config (char *, char *) */
976 void module_register (void)
977 {
978 perl_argc = 4;
979 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
981 /* default options for the Perl interpreter */
982 perl_argv[0] = "";
983 perl_argv[1] = "-MCollectd";
984 perl_argv[2] = "-e";
985 perl_argv[3] = "1";
986 perl_argv[4] = NULL;
988 plugin_register_config ("perl", perl_config, config_keys, config_keys_num);
989 return;
990 } /* void module_register (void) */
992 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */