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"
28 #include "common.h"
29 #include "plugin.h"
31 #include "configfile.h"
33 #include <EXTERN.h>
34 #include <perl.h>
36 #include <XSUB.h>
38 #define PLUGIN_INIT 0
39 #define PLUGIN_READ 1
40 #define PLUGIN_WRITE 2
41 #define PLUGIN_SHUTDOWN 3
42 #define PLUGIN_LOG 4
44 #define PLUGIN_TYPES 5
46 #define PLUGIN_DATASET 255
48 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
49 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
50 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
53 /* this is defined in DynaLoader.a */
54 void boot_DynaLoader (PerlInterpreter *, CV *);
56 static XS (Collectd_plugin_register);
57 static XS (Collectd_plugin_unregister);
58 static XS (Collectd_plugin_dispatch_values);
59 static XS (Collectd_plugin_log);
62 /*
63 * private data types
64 */
66 typedef struct {
67 int len;
68 int *values;
69 } ds_types_t;
71 typedef struct {
72 int wait_time;
73 int wait_left;
75 SV *sub;
76 } pplugin_t;
79 /*
80 * private variables
81 */
83 /* valid configuration file keys */
84 static const char *config_keys[] =
85 {
86 "LoadPlugin",
87 "BaseName",
88 NULL
89 };
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92 static PerlInterpreter *perl = NULL;
94 static char base_name[DATA_MAX_NAME_LEN] = "Collectd::Plugin";
96 static char *plugin_types[] = { "init", "read", "write", "shutdown" };
97 static HV *plugins[PLUGIN_TYPES];
98 static HV *data_sets;
100 static struct {
101 char name[64];
102 XS ((*f));
103 } api[] =
104 {
105 { "Collectd::plugin_register", Collectd_plugin_register },
106 { "Collectd::plugin_unregister", Collectd_plugin_unregister },
107 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
108 { "Collectd::plugin_log", Collectd_plugin_log },
109 { "", NULL }
110 };
113 /*
114 * Helper functions for data type conversion.
115 */
117 /*
118 * data source:
119 * [
120 * {
121 * name => $ds_name,
122 * type => $ds_type,
123 * min => $ds_min,
124 * max => $ds_max
125 * },
126 * ...
127 * ]
128 */
129 static int hv2data_source (HV *hash, data_source_t *ds)
130 {
131 SV **tmp = NULL;
133 if ((NULL == hash) || (NULL == ds))
134 return -1;
136 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "name", 4, 0))) {
137 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
138 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
139 }
140 else {
141 log_err ("hv2data_source: No DS name given.");
142 return -1;
143 }
145 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "type", 4, 0))) {
146 ds->type = SvIV (*tmp);
148 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
149 log_err ("hv2data_source: Invalid DS type.");
150 return -1;
151 }
152 }
153 else {
154 ds->type = DS_TYPE_COUNTER;
155 }
157 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "min", 3, 0)))
158 ds->min = SvNV (*tmp);
159 else
160 ds->min = NAN;
162 if (NULL != (tmp = Perl_hv_fetch (perl, hash, "max", 3, 0)))
163 ds->max = SvNV (*tmp);
164 else
165 ds->max = NAN;
166 return 0;
167 } /* static data_source_t *hv2data_source (HV *) */
169 static int av2value (char *name, AV *array, value_t *value, int len)
170 {
171 SV **tmp = NULL;
173 ds_types_t *ds = NULL;
175 int i = 0;
177 if ((NULL == name) || (NULL == array) || (NULL == value))
178 return -1;
180 if (Perl_av_len (perl, array) < len - 1)
181 len = Perl_av_len (perl, array) + 1;
183 if (0 >= len)
184 return -1;
186 tmp = Perl_hv_fetch (perl, data_sets, name, strlen (name), 0);
187 if (NULL == tmp) {
188 log_err ("av2value: No dataset for \"%s\".", name);
189 return -1;
190 }
191 ds = (ds_types_t *)SvIV ((SV *)SvRV (*tmp));
193 if (ds->len < len) {
194 log_warn ("av2value: Value length exceeds data set length.");
195 len = ds->len;
196 }
198 for (i = 0; i < len; ++i) {
199 SV **tmp = Perl_av_fetch (perl, array, i, 0);
201 if (NULL != tmp) {
202 if (DS_TYPE_COUNTER == ds->values[i])
203 value[i].counter = SvIV (*tmp);
204 else
205 value[i].gauge = SvNV (*tmp);
206 }
207 else {
208 return -1;
209 }
210 }
211 return len;
212 } /* static int av2value (char *, AV *, value_t *, int) */
214 static int data_set2av (data_set_t *ds, AV *array)
215 {
216 int i = 0;
218 if ((NULL == ds) || (NULL == array))
219 return -1;
221 Perl_av_extend (perl, array, ds->ds_num);
223 for (i = 0; i < ds->ds_num; ++i) {
224 HV *source = Perl_newHV (perl);
226 if (NULL == Perl_hv_store (perl, source, "name", 4,
227 Perl_newSVpv (perl, ds->ds[i].name, 0), 0))
228 return -1;
230 if (NULL == Perl_hv_store (perl, source, "type", 4,
231 Perl_newSViv (perl, ds->ds[i].type), 0))
232 return -1;
234 if (! isnan (ds->ds[i].min))
235 if (NULL == Perl_hv_store (perl, source, "min", 3,
236 Perl_newSVnv (perl, ds->ds[i].min), 0))
237 return -1;
239 if (! isnan (ds->ds[i].max))
240 if (NULL == Perl_hv_store (perl, source, "max", 3,
241 Perl_newSVnv (perl, ds->ds[i].max), 0))
242 return -1;
244 if (NULL == Perl_av_store (perl, array, i,
245 Perl_newRV_noinc (perl, (SV *)source)))
246 return -1;
247 }
248 return 0;
249 } /* static int data_set2av (data_set_t *, AV *) */
251 static int value_list2hv (value_list_t *vl, data_set_t *ds, HV *hash)
252 {
253 AV *values = NULL;
255 int i = 0;
256 int len = 0;
258 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
259 return -1;
261 len = vl->values_len;
263 if (ds->ds_num < len) {
264 log_warn ("value2av: Value length exceeds data set length.");
265 len = ds->ds_num;
266 }
268 values = Perl_newAV (perl);
269 Perl_av_extend (perl, values, len - 1);
271 for (i = 0; i < len; ++i) {
272 SV *val = NULL;
274 if (DS_TYPE_COUNTER == ds->ds[i].type)
275 val = Perl_newSViv (perl, vl->values[i].counter);
276 else
277 val = Perl_newSVnv (perl, vl->values[i].gauge);
279 if (NULL == Perl_av_store (perl, values, i, val)) {
280 Perl_av_undef (perl, values);
281 return -1;
282 }
283 }
285 if (NULL == Perl_hv_store (perl, hash, "values", 6,
286 Perl_newRV_noinc (perl, (SV *)values), 0))
287 return -1;
289 if (0 != vl->time)
290 if (NULL == Perl_hv_store (perl, hash, "time", 4,
291 Perl_newSViv (perl, vl->time), 0))
292 return -1;
294 if ('\0' != vl->host[0])
295 if (NULL == Perl_hv_store (perl, hash, "host", 4,
296 Perl_newSVpv (perl, vl->host, 0), 0))
297 return -1;
299 if ('\0' != vl->plugin[0])
300 if (NULL == Perl_hv_store (perl, hash, "plugin", 6,
301 Perl_newSVpv (perl, vl->plugin, 0), 0))
302 return -1;
304 if ('\0' != vl->plugin_instance[0])
305 if (NULL == Perl_hv_store (perl, hash, "plugin_instance", 15,
306 Perl_newSVpv (perl, vl->plugin_instance, 0), 0))
307 return -1;
309 if ('\0' != vl->type_instance[0])
310 if (NULL == Perl_hv_store (perl, hash, "type_instance", 13,
311 Perl_newSVpv (perl, vl->type_instance, 0), 0))
312 return -1;
313 return 0;
314 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
317 /*
318 * Internal functions.
319 */
321 /*
322 * Add a new plugin with the given name.
323 */
324 static int pplugin_register (int type, const char *name, SV *sub)
325 {
326 pplugin_t *p = NULL;
328 if ((type < 0) || (type >= PLUGIN_TYPES))
329 return -1;
331 if (NULL == name)
332 return -1;
334 p = (pplugin_t *)smalloc (sizeof (pplugin_t));
335 /* this happens during parsing of config file,
336 * thus interval_g is not set correctly */
337 p->wait_time = 10;
338 p->wait_left = 0;
339 p->sub = Perl_newSVsv (perl, sub);
341 if (NULL == Perl_hv_store (perl, plugins[type], name, strlen (name),
342 Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, p), 0)) {
343 log_debug ("pplugin_register: Failed to add plugin \"%s\" (\"%s\")",
344 name, SvPV_nolen (sub));
345 Perl_sv_free (perl, p->sub);
346 sfree (p);
347 return -1;
348 }
349 return 0;
350 } /* static int pplugin_register (int, char *, SV *) */
352 /*
353 * Removes the plugin with the given name and frees any ressources.
354 */
355 static int pplugin_unregister (int type, char *name)
356 {
357 SV *tmp = NULL;
359 if ((type < 0) || (type >= PLUGIN_TYPES))
360 return -1;
362 if (NULL == name)
363 return -1;
365 /* freeing the allocated memory of the element itself (pplugin_t *) causes
366 * a segfault during perl_destruct () thus I assume perl somehow takes
367 * care of this... */
369 tmp = Perl_hv_delete (perl, plugins[type], name, strlen (name), 0);
370 if (NULL != tmp) {
371 pplugin_t *p = (pplugin_t *)SvIV ((SV *)SvRV (tmp));
372 Perl_sv_free (perl, p->sub);
373 }
374 return 0;
375 } /* static int pplugin_unregister (char *) */
377 /*
378 * Add a plugin's data set definition.
379 */
380 static int pplugin_register_data_set (char *name, AV *dataset)
381 {
382 int len = -1;
383 int i = 0;
385 data_source_t *ds = NULL;
386 data_set_t *set = NULL;
388 ds_types_t *types = NULL;
390 if ((NULL == name) || (NULL == dataset))
391 return -1;
393 len = Perl_av_len (perl, dataset);
395 if (-1 == len)
396 return -1;
398 ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
399 set = (data_set_t *)smalloc (sizeof (data_set_t));
401 types = (ds_types_t *)smalloc (sizeof (ds_types_t));
402 types->len = len + 1;
403 types->values = (int *)smalloc ((types->len) * sizeof (int));
405 for (i = 0; i <= len; ++i) {
406 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
408 if (NULL == elem)
409 return -1;
411 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
412 log_err ("pplugin_register_data_set: Invalid data source.");
413 return -1;
414 }
416 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
417 return -1;
419 types->values[i] = ds[i].type;
420 log_debug ("pplugin_register_data_set: "
421 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
422 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
423 }
425 if (NULL == Perl_hv_store (perl, data_sets, name, strlen (name),
426 Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, types), 0))
427 return -1;
429 strncpy (set->type, name, DATA_MAX_NAME_LEN);
430 set->type[DATA_MAX_NAME_LEN - 1] = '\0';
432 set->ds_num = len + 1;
433 set->ds = ds;
434 return plugin_register_data_set (set);
435 } /* static int pplugin_register_data_set (char *, SV *) */
437 /*
438 * Remove a plugin's data set definition.
439 */
440 static int pplugin_unregister_data_set (char *name)
441 {
442 SV *tmp = NULL;
444 if (NULL == name)
445 return 0;
447 /* freeing the allocated memory of the element itself (ds_types_t *)
448 * causes a segfault during perl_destruct () thus I assume perl somehow
449 * takes care of this... */
451 tmp = Perl_hv_delete (perl, data_sets, name, strlen (name), 0);
452 if (NULL != tmp) {
453 ds_types_t *ds = (ds_types_t *)SvIV ((SV *)SvRV (tmp));
454 sfree (ds->values);
455 }
456 return plugin_unregister_data_set (name);
457 } /* static int pplugin_unregister_data_set (char *) */
459 /*
460 * Submit the values to the write functions.
461 *
462 * value list:
463 * {
464 * values => [ @values ],
465 * time => $time,
466 * host => $host,
467 * plugin => $plugin,
468 * plugin_instance => $pinstance,
469 * type_instance => $tinstance,
470 * }
471 */
472 static int pplugin_dispatch_values (char *name, HV *values)
473 {
474 value_list_t list = VALUE_LIST_INIT;
475 value_t *val = NULL;
477 SV **tmp = NULL;
479 int ret = 0;
481 if ((NULL == name) || (NULL == values))
482 return -1;
484 if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
485 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
486 log_err ("pplugin_dispatch_values: No valid values given.");
487 return -1;
488 }
490 {
491 AV *array = (AV *)SvRV (*tmp);
492 int len = Perl_av_len (perl, array) + 1;
494 val = (value_t *)smalloc (len * sizeof (value_t));
496 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
497 list.values = val;
499 if (-1 == list.values_len) {
500 sfree (val);
501 return -1;
502 }
503 }
505 if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
506 list.time = (time_t)SvIV (*tmp);
507 }
508 else {
509 list.time = time (NULL);
510 }
512 if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
513 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
514 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
515 }
516 else {
517 strcpy (list.host, hostname_g);
518 }
520 if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
521 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
522 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
523 }
525 if (NULL != (tmp = Perl_hv_fetch (perl, values,
526 "plugin_instance", 15, 0))) {
527 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
528 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
529 }
531 if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
532 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
533 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
534 }
536 ret = plugin_dispatch_values (name, &list);
538 sfree (val);
539 return ret;
540 } /* static int pplugin_dispatch_values (char *, HV *) */
542 /*
543 * Call a plugin's working function.
544 */
545 static int pplugin_call (int type, char *name, SV *sub, va_list ap)
546 {
547 int retvals = 0;
548 I32 xflags = G_NOARGS;
550 int ret = 0;
552 dSP;
554 if ((type < 0) || (type >= PLUGIN_TYPES))
555 return -1;
557 ENTER;
558 SAVETMPS;
560 PUSHMARK (SP);
562 if (PLUGIN_WRITE == type) {
563 /*
564 * $_[0] = $plugin_type;
565 *
566 * $_[1] =
567 * [
568 * {
569 * name => $ds_name,
570 * type => $ds_type,
571 * min => $ds_min,
572 * max => $ds_max
573 * },
574 * ...
575 * ];
576 *
577 * $_[2] =
578 * {
579 * values => [ $v1, ... ],
580 * time => $time,
581 * host => $hostname,
582 * plugin => $plugin,
583 * plugin_instance => $instance,
584 * type_instance => $type_instance
585 * };
586 */
587 data_set_t *ds;
588 value_list_t *vl;
590 AV *pds = Perl_newAV (perl);
591 HV *pvl = Perl_newHV (perl);
593 ds = va_arg (ap, data_set_t *);
594 vl = va_arg (ap, value_list_t *);
596 if (-1 == data_set2av (ds, pds))
597 return -1;
599 if (-1 == value_list2hv (vl, ds, pvl))
600 return -1;
602 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
603 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
604 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
606 xflags = 0;
607 }
608 else if (PLUGIN_LOG == type) {
609 /*
610 * $_[0] = $level;
611 *
612 * $_[1] = $message;
613 */
614 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
615 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
617 xflags = 0;
618 }
620 PUTBACK;
622 /* prevent an endless loop */
623 if (PLUGIN_LOG != type)
624 log_debug ("pplugin_call: executing %s::%s->%s()",
625 base_name, name, plugin_types[type]);
627 retvals = Perl_call_sv (perl, sub, G_SCALAR | xflags);
629 SPAGAIN;
630 if (1 > retvals) {
631 if (PLUGIN_LOG != type)
632 log_warn ("pplugin_call: "
633 "%s::%s->%s() returned void - assuming true",
634 base_name, name, plugin_types[type]);
635 }
636 else {
637 SV *tmp = POPs;
638 if (! SvTRUE (tmp))
639 ret = -1;
640 }
642 PUTBACK;
643 FREETMPS;
644 LEAVE;
645 return ret;
646 } /* static int pplugin_call (int, char *, SV *, va_list) */
648 /*
649 * Call all working functions of the given type.
650 */
651 static int pplugin_call_all (int type, ...)
652 {
653 SV *tmp = NULL;
655 char *plugin;
656 I32 len;
658 if ((type < 0) || (type >= PLUGIN_TYPES))
659 return -1;
661 if (0 == Perl_hv_iterinit (perl, plugins[type]))
662 return 0;
664 while (NULL != (tmp = Perl_hv_iternextsv (perl, plugins[type],
665 &plugin, &len))) {
666 pplugin_t *p;
667 va_list ap;
669 int status;
671 va_start (ap, type);
673 p = (pplugin_t *)SvIV ((SV *)SvRV (tmp));
675 if (p->wait_left > 0)
676 p->wait_left -= interval_g;
678 if (p->wait_left > 0)
679 continue;
681 if (0 == (status = pplugin_call (type, plugin, p->sub, ap))) {
682 p->wait_left = 0;
683 p->wait_time = interval_g;
684 }
685 else if (PLUGIN_READ == type) {
686 p->wait_left = p->wait_time;
687 p->wait_time <<= 1;
689 if (p->wait_time > 86400)
690 p->wait_time = 86400;
692 log_warn ("%s::%s->read() failed. Will suspend it for %i seconds.",
693 base_name, plugin, p->wait_left);
694 }
695 else if (PLUGIN_INIT == type) {
696 int i = 0;
698 log_err ("%s::%s->init() failed. Plugin will be disabled.",
699 base_name, plugin, status);
701 for (i = 0; i < PLUGIN_TYPES; ++i)
702 pplugin_unregister (i, plugin);
703 }
704 else if (PLUGIN_LOG != type) {
705 log_warn ("%s::%s->%s() failed with status %i.",
706 base_name, plugin, plugin_types[type], status);
707 }
709 va_end (ap);
710 }
711 return 0;
712 } /* static int pplugin_call_all (int, ...) */
715 /*
716 * Exported Perl API.
717 */
719 /*
720 * Collectd::plugin_register (type, name, data).
721 *
722 * type:
723 * init, read, write, shutdown, data set
724 *
725 * name:
726 * name of the plugin
727 *
728 * data:
729 * reference to the plugin's subroutine that does the work or the data set
730 * definition
731 */
732 static XS (Collectd_plugin_register)
733 {
734 int type = 0;
735 SV *data = NULL;
737 int ret = 0;
739 dXSARGS;
741 if (3 != items) {
742 log_err ("Usage: Collectd::plugin_register(type, name, data)");
743 XSRETURN_EMPTY;
744 }
746 log_debug ("Collectd::plugin_register: "
747 "type = \"%i\", name = \"%s\", \"%s\"",
748 (int)SvIV (ST (0)), SvPV_nolen (ST (1)), SvPV_nolen (ST (2)));
750 type = (int)SvIV (ST (0));
751 data = ST (2);
753 if ((type >= 0) && (type < PLUGIN_TYPES)
754 && SvROK (data) && (SVt_PVCV == SvTYPE (SvRV (data)))) {
755 ret = pplugin_register (type, SvPV_nolen (ST (1)), data);
756 }
757 else if ((type == PLUGIN_DATASET)
758 && SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
759 ret = pplugin_register_data_set (SvPV_nolen (ST (1)),
760 (AV *)SvRV (data));
761 }
762 else {
763 log_err ("Collectd::plugin_register: Invalid data.");
764 XSRETURN_EMPTY;
765 }
767 if (0 == ret)
768 XSRETURN_YES;
769 else
770 XSRETURN_EMPTY;
771 } /* static XS (Collectd_plugin_register) */
773 /*
774 * Collectd::plugin_unregister (type, name).
775 *
776 * type:
777 * init, read, write, shutdown, data set
778 *
779 * name:
780 * name of the plugin
781 */
782 static XS (Collectd_plugin_unregister)
783 {
784 int type = 0;
785 int ret = 0;
787 dXSARGS;
789 if (2 != items) {
790 log_err ("Usage: Collectd::plugin_unregister(type, name)");
791 XSRETURN_EMPTY;
792 }
794 log_debug ("Collectd::plugin_unregister: type = \"%i\", name = \"%s\"",
795 (int)SvIV (ST (0)), SvPV_nolen (ST (1)));
797 type = (int)SvIV (ST (0));
799 if ((type >= 0) && (type < PLUGIN_TYPES)) {
800 ret = pplugin_unregister (type, SvPV_nolen (ST (1)));
801 }
802 else if (type == PLUGIN_DATASET) {
803 ret = pplugin_unregister_data_set (SvPV_nolen (ST (1)));
804 }
805 else {
806 log_err ("Collectd::plugin_unregister: Invalid type.");
807 XSRETURN_EMPTY;
808 }
810 if (0 == ret)
811 XSRETURN_YES;
812 else
813 XSRETURN_EMPTY;
814 } /* static XS (Collectd_plugin_unregister) */
816 /*
817 * Collectd::plugin_dispatch_values (name, values).
818 *
819 * name:
820 * name of the plugin
821 *
822 * values:
823 * value list to submit
824 */
825 static XS (Collectd_plugin_dispatch_values)
826 {
827 SV *values = NULL;
829 int ret = 0;
831 dXSARGS;
833 if (2 != items) {
834 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
835 XSRETURN_EMPTY;
836 }
838 log_debug ("Collectd::plugin_dispatch_values: "
839 "name = \"%s\", values=\"%s\"",
840 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
842 values = ST (1);
844 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
845 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
846 XSRETURN_EMPTY;
847 }
849 if ((NULL == ST (0)) || (NULL == values))
850 XSRETURN_EMPTY;
852 ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
854 if (0 == ret)
855 XSRETURN_YES;
856 else
857 XSRETURN_EMPTY;
858 } /* static XS (Collectd_plugin_dispatch_values) */
860 /*
861 * Collectd::plugin_log (level, message).
862 *
863 * level:
864 * log level (LOG_DEBUG, ... LOG_ERR)
865 *
866 * message:
867 * log message
868 */
869 static XS (Collectd_plugin_log)
870 {
871 dXSARGS;
873 if (2 != items) {
874 log_err ("Usage: Collectd::plugin_log(level, message)");
875 XSRETURN_EMPTY;
876 }
878 log_debug ("Collectd::plugin_log: level = %i, message = \"%s\"",
879 SvIV (ST (0)), SvPV_nolen (ST (1)));
880 plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
881 XSRETURN_YES;
882 } /* static XS (Collectd_plugin_log) */
884 /*
885 * Collectd::bootstrap ().
886 */
887 static XS (boot_Collectd)
888 {
889 HV *stash = NULL;
890 char *file = __FILE__;
892 struct {
893 char name[64];
894 SV *value;
895 } consts[] =
896 {
897 { "Collectd::TYPE_INIT", Perl_newSViv (perl, PLUGIN_INIT) },
898 { "Collectd::TYPE_READ", Perl_newSViv (perl, PLUGIN_READ) },
899 { "Collectd::TYPE_WRITE", Perl_newSViv (perl, PLUGIN_WRITE) },
900 { "Collectd::TYPE_SHUTDOWN", Perl_newSViv (perl, PLUGIN_SHUTDOWN) },
901 { "Collectd::TYPE_LOG", Perl_newSViv (perl, PLUGIN_LOG) },
902 { "Collectd::TYPE_DATASET", Perl_newSViv (perl, PLUGIN_DATASET) },
903 { "Collectd::DS_TYPE_COUNTER", Perl_newSViv (perl, DS_TYPE_COUNTER) },
904 { "Collectd::DS_TYPE_GAUGE", Perl_newSViv (perl, DS_TYPE_GAUGE) },
905 { "Collectd::LOG_ERR", Perl_newSViv (perl, LOG_ERR) },
906 { "Collectd::LOG_WARNING", Perl_newSViv (perl, LOG_WARNING) },
907 { "Collectd::LOG_NOTICE", Perl_newSViv (perl, LOG_NOTICE) },
908 { "Collectd::LOG_INFO", Perl_newSViv (perl, LOG_INFO) },
909 { "Collectd::LOG_DEBUG", Perl_newSViv (perl, LOG_DEBUG) },
910 { "", NULL }
911 };
913 int i = 0;
915 dXSARGS;
917 if ((1 > items) || (2 < items)) {
918 log_err ("Usage: Collectd::bootstrap(name[, version])");
919 XSRETURN_EMPTY;
920 }
922 XS_VERSION_BOOTCHECK;
924 /* register API */
925 for (i = 0; NULL != api[i].f; ++i)
926 Perl_newXS (perl, api[i].name, api[i].f, file);
928 stash = Perl_gv_stashpv (perl, "Collectd", 1);
930 /* export "constants" */
931 for (i = 0; NULL != consts[i].value; ++i)
932 Perl_newCONSTSUB (perl, stash, consts[i].name, consts[i].value);
933 XSRETURN_YES;
934 } /* static XS (boot_Collectd) */
937 /*
938 * Interface to collectd.
939 */
941 static int perl_config (const char *key, const char *value)
942 {
943 log_debug ("perl_config: key = \"%s\", value=\"%s\"", key, value);
945 if (0 == strcasecmp (key, "LoadPlugin")) {
946 log_debug ("perl_config: loading perl plugin \"%s\"", value);
948 Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
949 Perl_newSVpvf (perl, "%s::%s", base_name, value),
950 Nullsv);
951 }
952 else if (0 == strcasecmp (key, "BaseName")) {
953 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
954 strncpy (base_name, value, DATA_MAX_NAME_LEN);
955 base_name[DATA_MAX_NAME_LEN - 1] = '\0';
956 }
957 else {
958 return -1;
959 }
960 return 0;
961 } /* static int perl_config (char *, char *) */
963 static int perl_init (void)
964 {
965 PERL_SET_CONTEXT (perl);
966 return pplugin_call_all (PLUGIN_INIT);
967 } /* static int perl_init (void) */
969 static int perl_read (void)
970 {
971 PERL_SET_CONTEXT (perl);
972 return pplugin_call_all (PLUGIN_READ);
973 } /* static int perl_read (void) */
975 static int perl_write (const data_set_t *ds, const value_list_t *vl)
976 {
977 PERL_SET_CONTEXT (perl);
978 return pplugin_call_all (PLUGIN_WRITE, ds, vl);
979 } /* static int perl_write (const data_set_t *, const value_list_t *) */
981 static void perl_log (int level, const char *msg)
982 {
983 PERL_SET_CONTEXT (perl);
984 pplugin_call_all (PLUGIN_LOG, level, msg);
985 return;
986 } /* static void perl_log (int, const char *) */
988 static int perl_shutdown (void)
989 {
990 int i = 0;
991 int ret = 0;
993 PERL_SET_CONTEXT (perl);
994 ret = pplugin_call_all (PLUGIN_SHUTDOWN);
996 for (i = 0; i < PLUGIN_TYPES; ++i) {
997 if (0 < Perl_hv_iterinit (perl, plugins[i])) {
998 char *k = NULL;
999 I32 l = 0;
1001 while (NULL != Perl_hv_iternextsv (perl, plugins[i], &k, &l)) {
1002 pplugin_unregister (i, k);
1003 }
1004 }
1006 Perl_hv_undef (perl, plugins[i]);
1007 }
1009 if (0 < Perl_hv_iterinit (perl, data_sets)) {
1010 char *k = NULL;
1011 I32 l = 0;
1013 while (NULL != Perl_hv_iternextsv (perl, data_sets, &k, &l)) {
1014 pplugin_unregister_data_set (k);
1015 }
1016 }
1018 Perl_hv_undef (perl, data_sets);
1020 #if COLLECT_DEBUG
1021 Perl_sv_report_used (perl);
1022 #endif /* COLLECT_DEBUG */
1024 perl_destruct (perl);
1025 perl_free (perl);
1027 PERL_SYS_TERM ();
1028 return ret;
1029 } /* static void perl_shutdown (void) */
1031 static void xs_init (pTHX)
1032 {
1033 char *file = __FILE__;
1035 dXSUB_SYS;
1037 /* build the Collectd module into the perl interpreter */
1038 Perl_newXS (perl, "Collectd::bootstrap", boot_Collectd, file);
1040 /* enable usage of Perl modules using shared libraries */
1041 Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1042 return;
1043 } /* static void xs_init (pTHX) */
1045 /*
1046 * Create the perl interpreter and register it with collectd.
1047 */
1048 void module_register (void)
1049 {
1050 char *embed_argv[] = { "", "-e", "bootstrap Collectd \""VERSION"\"", NULL };
1051 int embed_argc = 3;
1053 int i = 0;
1055 log_debug ("module_register: Registering perl plugin...");
1057 PERL_SYS_INIT3 (&argc, &argv, &environ);
1059 if (NULL == (perl = perl_alloc ())) {
1060 log_err ("module_register: Not enough memory.");
1061 exit (3);
1062 }
1063 perl_construct (perl);
1065 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1067 if (0 != perl_parse (perl, xs_init, embed_argc, embed_argv, NULL)) {
1068 log_err ("module_register: Unable to bootstrap Collectd.");
1069 exit (1);
1070 }
1071 perl_run (perl);
1073 for (i = 0; i < PLUGIN_TYPES; ++i)
1074 plugins[i] = Perl_newHV (perl);
1076 data_sets = Perl_newHV (perl);
1078 plugin_register_log ("perl", perl_log);
1079 plugin_register_config ("perl", perl_config, config_keys, config_keys_num);
1080 plugin_register_init ("perl", perl_init);
1081 plugin_register_read ("perl", perl_read);
1082 plugin_register_write ("perl", perl_write);
1083 plugin_register_shutdown ("perl", perl_shutdown);
1084 return;
1085 } /* void module_register (void) */
1087 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */