Code

perl plugin: Added the "IncludeDir" config option.
[collectd.git] / src / perl.c
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         "IncludeDir"
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] = "";
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[] =
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)
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)
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)
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)
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 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
322         int status = 0;
323         if (base_name[0] == '\0')
324                 status = snprintf (buf, buf_len, "%s", module);
325         else
326                 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
327         if ((status < 0) || (status >= buf_len))
328                 return (NULL);
329         buf[buf_len] = '\0';
330         return (buf);
331 } /* char *get_module_name */
333 /*
334  * Add a new plugin with the given name.
335  */
336 static int pplugin_register (int type, const char *name, SV *sub)
338         pplugin_t *p = NULL;
340         if ((type < 0) || (type >= PLUGIN_TYPES))
341                 return -1;
343         if (NULL == name)
344                 return -1;
346         p = (pplugin_t *)smalloc (sizeof (pplugin_t));
347         /* this happens during parsing of config file,
348          * thus interval_g is not set correctly */
349         p->wait_time = 10;
350         p->wait_left = 0;
351         p->sub = Perl_newSVsv (perl, sub);
353         if (NULL == Perl_hv_store (perl, plugins[type], name, strlen (name),
354                                 Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, p), 0)) {
355                 log_debug ("pplugin_register: Failed to add plugin \"%s\" (\"%s\")",
356                                 name, SvPV_nolen (sub));
357                 Perl_sv_free (perl, p->sub);
358                 sfree (p);
359                 return -1;
360         }
361         return 0;
362 } /* static int pplugin_register (int, char *, SV *) */
364 /*
365  * Removes the plugin with the given name and frees any ressources.
366  */
367 static int pplugin_unregister (int type, char *name)
369         SV *tmp = NULL;
371         if ((type < 0) || (type >= PLUGIN_TYPES))
372                 return -1;
374         if (NULL == name)
375                 return -1;
377         /* freeing the allocated memory of the element itself (pplugin_t *) causes
378          * a segfault during perl_destruct () thus I assume perl somehow takes
379          * care of this... */
381         tmp = Perl_hv_delete (perl, plugins[type], name, strlen (name), 0);
382         if (NULL != tmp) {
383                 pplugin_t *p = (pplugin_t *)SvIV ((SV *)SvRV (tmp));
384                 Perl_sv_free (perl, p->sub);
385         }
386         return 0;
387 } /* static int pplugin_unregister (char *) */
389 /*
390  * Add a plugin's data set definition.
391  */
392 static int pplugin_register_data_set (char *name, AV *dataset)
394         int len = -1;
395         int i   = 0;
397         data_source_t *ds  = NULL;
398         data_set_t    *set = NULL;
400         ds_types_t *types = NULL;
402         if ((NULL == name) || (NULL == dataset))
403                 return -1;
405         len = Perl_av_len (perl, dataset);
407         if (-1 == len)
408                 return -1;
410         ds  = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
411         set = (data_set_t *)smalloc (sizeof (data_set_t));
413         types = (ds_types_t *)smalloc (sizeof (ds_types_t));
414         types->len = len + 1;
415         types->values = (int *)smalloc ((types->len) * sizeof (int));
417         for (i = 0; i <= len; ++i) {
418                 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
420                 if (NULL == elem)
421                         return -1;
423                 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
424                         log_err ("pplugin_register_data_set: Invalid data source.");
425                         return -1;
426                 }
428                 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
429                         return -1;
431                 types->values[i] = ds[i].type;
432                 log_debug ("pplugin_register_data_set: "
433                                 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
434                                 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
435         }
437         if (NULL == Perl_hv_store (perl, data_sets, name, strlen (name),
438                         Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, types), 0))
439                 return -1;
441         strncpy (set->type, name, DATA_MAX_NAME_LEN);
442         set->type[DATA_MAX_NAME_LEN - 1] = '\0';
444         set->ds_num = len + 1;
445         set->ds = ds;
446         return plugin_register_data_set (set);
447 } /* static int pplugin_register_data_set (char *, SV *) */
449 /*
450  * Remove a plugin's data set definition.
451  */
452 static int pplugin_unregister_data_set (char *name)
454         SV *tmp = NULL;
456         if (NULL == name)
457                 return 0;
459         /* freeing the allocated memory of the element itself (ds_types_t *)
460          * causes a segfault during perl_destruct () thus I assume perl somehow
461          * takes care of this... */
463         tmp = Perl_hv_delete (perl, data_sets, name, strlen (name), 0);
464         if (NULL != tmp) {
465                 ds_types_t *ds = (ds_types_t *)SvIV ((SV *)SvRV (tmp));
466                 sfree (ds->values);
467         }
468         return plugin_unregister_data_set (name);
469 } /* static int pplugin_unregister_data_set (char *) */
471 /*
472  * Submit the values to the write functions.
473  *
474  * value list:
475  * {
476  *   values => [ @values ],
477  *   time   => $time,
478  *   host   => $host,
479  *   plugin => $plugin,
480  *   plugin_instance => $pinstance,
481  *   type_instance   => $tinstance,
482  * }
483  */
484 static int pplugin_dispatch_values (char *name, HV *values)
486         value_list_t list = VALUE_LIST_INIT;
487         value_t      *val = NULL;
489         SV **tmp = NULL;
491         int ret = 0;
493         if ((NULL == name) || (NULL == values))
494                 return -1;
496         if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
497                         || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
498                 log_err ("pplugin_dispatch_values: No valid values given.");
499                 return -1;
500         }
502         {
503                 AV  *array = (AV *)SvRV (*tmp);
504                 int len    = Perl_av_len (perl, array) + 1;
506                 val = (value_t *)smalloc (len * sizeof (value_t));
508                 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
509                 list.values = val;
511                 if (-1 == list.values_len) {
512                         sfree (val);
513                         return -1;
514                 }
515         }
517         if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
518                 list.time = (time_t)SvIV (*tmp);
519         }
520         else {
521                 list.time = time (NULL);
522         }
524         if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
525                 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
526                 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
527         }
528         else {
529                 strcpy (list.host, hostname_g);
530         }
532         if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
533                 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
534                 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
535         }
537         if (NULL != (tmp = Perl_hv_fetch (perl, values,
538                         "plugin_instance", 15, 0))) {
539                 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
540                 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
541         }
543         if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
544                 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
545                 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
546         }
548         ret = plugin_dispatch_values (name, &list);
550         sfree (val);
551         return ret;
552 } /* static int pplugin_dispatch_values (char *, HV *) */
554 /*
555  * Call a plugin's working function.
556  */
557 static int pplugin_call (int type, char *name, SV *sub, va_list ap)
559         int retvals = 0;
560         I32 xflags  = G_NOARGS;
562         int ret = 0;
564         dSP;
566         if ((type < 0) || (type >= PLUGIN_TYPES))
567                 return -1;
569         ENTER;
570         SAVETMPS;
572         PUSHMARK (SP);
574         if (PLUGIN_WRITE == type) {
575                 /*
576                  * $_[0] = $plugin_type;
577                  *
578                  * $_[1] =
579                  * [
580                  *   {
581                  *     name => $ds_name,
582                  *     type => $ds_type,
583                  *     min  => $ds_min,
584                  *     max  => $ds_max
585                  *   },
586                  *   ...
587                  * ];
588                  *
589                  * $_[2] =
590                  * {
591                  *   values => [ $v1, ... ],
592                  *   time   => $time,
593                  *   host   => $hostname,
594                  *   plugin => $plugin,
595                  *   plugin_instance => $instance,
596                  *   type_instance   => $type_instance
597                  * };
598                  */
599                 data_set_t   *ds;
600                 value_list_t *vl;
602                 AV *pds = Perl_newAV (perl);
603                 HV *pvl = Perl_newHV (perl);
605                 ds = va_arg (ap, data_set_t *);
606                 vl = va_arg (ap, value_list_t *);
608                 if (-1 == data_set2av (ds, pds))
609                         return -1;
611                 if (-1 == value_list2hv (vl, ds, pvl))
612                         return -1;
614                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
615                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
616                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
618                 xflags = 0;
619         }
620         else if (PLUGIN_LOG == type) {
621                 /*
622                  * $_[0] = $level;
623                  *
624                  * $_[1] = $message;
625                  */
626                 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
627                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
629                 xflags = 0;
630         }
632         PUTBACK;
634         /* prevent an endless loop */
635         if (PLUGIN_LOG != type)
636                 log_debug ("pplugin_call: executing %s::%s->%s()",
637                                 base_name, name, plugin_types[type]);
639         retvals = Perl_call_sv (perl, sub, G_SCALAR | xflags);
641         SPAGAIN;
642         if (1 > retvals) {
643                 if (PLUGIN_LOG != type)
644                         log_warn ("pplugin_call: "
645                                         "%s::%s->%s() returned void - assuming true",
646                                         base_name, name, plugin_types[type]);
647         }
648         else {
649                 SV *tmp = POPs;
650                 if (! SvTRUE (tmp))
651                         ret = -1;
652         }
654         PUTBACK;
655         FREETMPS;
656         LEAVE;
657         return ret;
658 } /* static int pplugin_call (int, char *, SV *, va_list) */
660 /*
661  * Call all working functions of the given type.
662  */
663 static int pplugin_call_all (int type, ...)
665         SV *tmp = NULL;
667         char *plugin;
668         I32  len;
670         if ((type < 0) || (type >= PLUGIN_TYPES))
671                 return -1;
673         if (0 == Perl_hv_iterinit (perl, plugins[type]))
674                 return 0;
676         while (NULL != (tmp = Perl_hv_iternextsv (perl, plugins[type],
677                         &plugin, &len))) {
678                 pplugin_t *p;
679                 va_list   ap;
681                 int status;
683                 va_start (ap, type);
685                 p = (pplugin_t *)SvIV ((SV *)SvRV (tmp));
687                 if (p->wait_left > 0)
688                         p->wait_left -= interval_g;
690                 if (p->wait_left > 0)
691                         continue;
693                 if (0 == (status = pplugin_call (type, plugin, p->sub, ap))) {
694                         p->wait_left = 0;
695                         p->wait_time = interval_g;
696                 }
697                 else if (PLUGIN_READ == type) {
698                         p->wait_left = p->wait_time;
699                         p->wait_time <<= 1;
701                         if (p->wait_time > 86400)
702                                 p->wait_time = 86400;
704                         log_warn ("%s->read() failed. Will suspend it for %i seconds.",
705                                         plugin, p->wait_left);
706                 }
707                 else if (PLUGIN_INIT == type) {
708                         int i = 0;
710                         log_err ("%s->init() failed. Plugin will be disabled.",
711                                         plugin, status);
713                         for (i = 0; i < PLUGIN_TYPES; ++i)
714                                 pplugin_unregister (i, plugin);
715                 }
716                 else if (PLUGIN_LOG != type) {
717                         log_warn ("%s->%s() failed with status %i.",
718                                         plugin, plugin_types[type], status);
719                 }
721                 va_end (ap);
722         }
723         return 0;
724 } /* static int pplugin_call_all (int, ...) */
727 /*
728  * Exported Perl API.
729  */
731 /*
732  * Collectd::plugin_register (type, name, data).
733  *
734  * type:
735  *   init, read, write, shutdown, data set
736  *
737  * name:
738  *   name of the plugin
739  *
740  * data:
741  *   reference to the plugin's subroutine that does the work or the data set
742  *   definition
743  */
744 static XS (Collectd_plugin_register)
746         int type  = 0;
747         SV  *data = NULL;
749         int ret = 0;
751         dXSARGS;
753         if (3 != items) {
754                 log_err ("Usage: Collectd::plugin_register(type, name, data)");
755                 XSRETURN_EMPTY;
756         }
758         log_debug ("Collectd::plugin_register: "
759                         "type = \"%i\", name = \"%s\", \"%s\"",
760                         (int)SvIV (ST (0)), SvPV_nolen (ST (1)), SvPV_nolen (ST (2)));
762         type = (int)SvIV (ST (0));
763         data = ST (2);
765         if ((type >= 0) && (type < PLUGIN_TYPES)
766                         && SvROK (data) && (SVt_PVCV == SvTYPE (SvRV (data)))) {
767                 ret = pplugin_register (type, SvPV_nolen (ST (1)), data);
768         }
769         else if ((type == PLUGIN_DATASET)
770                         && SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
771                 ret = pplugin_register_data_set (SvPV_nolen (ST (1)),
772                                 (AV *)SvRV (data));
773         }
774         else {
775                 log_err ("Collectd::plugin_register: Invalid data.");
776                 XSRETURN_EMPTY;
777         }
779         if (0 == ret)
780                 XSRETURN_YES;
781         else
782                 XSRETURN_EMPTY;
783 } /* static XS (Collectd_plugin_register) */
785 /*
786  * Collectd::plugin_unregister (type, name).
787  *
788  * type:
789  *   init, read, write, shutdown, data set
790  *
791  * name:
792  *   name of the plugin
793  */
794 static XS (Collectd_plugin_unregister)
796         int type = 0;
797         int ret  = 0;
799         dXSARGS;
801         if (2 != items) {
802                 log_err ("Usage: Collectd::plugin_unregister(type, name)");
803                 XSRETURN_EMPTY;
804         }
806         log_debug ("Collectd::plugin_unregister: type = \"%i\", name = \"%s\"",
807                         (int)SvIV (ST (0)), SvPV_nolen (ST (1)));
809         type = (int)SvIV (ST (0));
811         if ((type >= 0) && (type < PLUGIN_TYPES)) {
812                 ret = pplugin_unregister (type, SvPV_nolen (ST (1)));
813         }
814         else if (type == PLUGIN_DATASET) {
815                 ret = pplugin_unregister_data_set (SvPV_nolen (ST (1)));
816         }
817         else {
818                 log_err ("Collectd::plugin_unregister: Invalid type.");
819                 XSRETURN_EMPTY;
820         }
822         if (0 == ret)
823                 XSRETURN_YES;
824         else
825                 XSRETURN_EMPTY;
826 } /* static XS (Collectd_plugin_unregister) */
828 /*
829  * Collectd::plugin_dispatch_values (name, values).
830  *
831  * name:
832  *   name of the plugin
833  *
834  * values:
835  *   value list to submit
836  */
837 static XS (Collectd_plugin_dispatch_values)
839         SV *values = NULL;
841         int ret = 0;
843         dXSARGS;
845         if (2 != items) {
846                 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
847                 XSRETURN_EMPTY;
848         }
850         log_debug ("Collectd::plugin_dispatch_values: "
851                         "name = \"%s\", values=\"%s\"",
852                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
854         values = ST (1);
856         if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
857                 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
858                 XSRETURN_EMPTY;
859         }
861         if ((NULL == ST (0)) || (NULL == values))
862                 XSRETURN_EMPTY;
864         ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
866         if (0 == ret)
867                 XSRETURN_YES;
868         else
869                 XSRETURN_EMPTY;
870 } /* static XS (Collectd_plugin_dispatch_values) */
872 /*
873  * Collectd::plugin_log (level, message).
874  *
875  * level:
876  *   log level (LOG_DEBUG, ... LOG_ERR)
877  *
878  * message:
879  *   log message
880  */
881 static XS (Collectd_plugin_log)
883         dXSARGS;
885         if (2 != items) {
886                 log_err ("Usage: Collectd::plugin_log(level, message)");
887                 XSRETURN_EMPTY;
888         }
890         log_debug ("Collectd::plugin_log: level = %i, message = \"%s\"",
891                         SvIV (ST (0)), SvPV_nolen (ST (1)));
892         plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
893         XSRETURN_YES;
894 } /* static XS (Collectd_plugin_log) */
896 /*
897  * Collectd::bootstrap ().
898  */
899 static XS (boot_Collectd)
901         HV   *stash = NULL;
902         char *file  = __FILE__;
904         struct {
905                 char name[64];
906                 SV   *value;
907         } consts[] =
908         {
909                 { "Collectd::TYPE_INIT",       Perl_newSViv (perl, PLUGIN_INIT) },
910                 { "Collectd::TYPE_READ",       Perl_newSViv (perl, PLUGIN_READ) },
911                 { "Collectd::TYPE_WRITE",      Perl_newSViv (perl, PLUGIN_WRITE) },
912                 { "Collectd::TYPE_SHUTDOWN",   Perl_newSViv (perl, PLUGIN_SHUTDOWN) },
913                 { "Collectd::TYPE_LOG",        Perl_newSViv (perl, PLUGIN_LOG) },
914                 { "Collectd::TYPE_DATASET",    Perl_newSViv (perl, PLUGIN_DATASET) },
915                 { "Collectd::DS_TYPE_COUNTER", Perl_newSViv (perl, DS_TYPE_COUNTER) },
916                 { "Collectd::DS_TYPE_GAUGE",   Perl_newSViv (perl, DS_TYPE_GAUGE) },
917                 { "Collectd::LOG_ERR",         Perl_newSViv (perl, LOG_ERR) },
918                 { "Collectd::LOG_WARNING",     Perl_newSViv (perl, LOG_WARNING) },
919                 { "Collectd::LOG_NOTICE",      Perl_newSViv (perl, LOG_NOTICE) },
920                 { "Collectd::LOG_INFO",        Perl_newSViv (perl, LOG_INFO) },
921                 { "Collectd::LOG_DEBUG",       Perl_newSViv (perl, LOG_DEBUG) },
922                 { "", NULL }
923         };
925         int i = 0;
927         dXSARGS;
929         if ((1 > items) || (2 < items)) {
930                 log_err ("Usage: Collectd::bootstrap(name[, version])");
931                 XSRETURN_EMPTY;
932         }
934         XS_VERSION_BOOTCHECK;
936         /* register API */
937         for (i = 0; NULL != api[i].f; ++i)
938                 Perl_newXS (perl, api[i].name, api[i].f, file);
940         stash = Perl_gv_stashpv (perl, "Collectd", 1);
942         /* export "constants" */
943         for (i = 0; NULL != consts[i].value; ++i)
944                 Perl_newCONSTSUB (perl, stash, consts[i].name, consts[i].value);
945         XSRETURN_YES;
946 } /* static XS (boot_Collectd) */
949 /*
950  * Interface to collectd.
951  */
953 static int perl_config (const char *key, const char *value)
955         log_debug ("perl_config: key = \"%s\", value=\"%s\"", key, value);
957         if (0 == strcasecmp (key, "LoadPlugin")) {
958                 char module_name[DATA_MAX_NAME_LEN];
960                 if (get_module_name (module_name, sizeof (module_name), value)
961                                 == NULL) {
962                         log_err ("Invalid module name %s", value);
963                         return (1);
964                 } /* if (get_module_name == NULL) */
966                 log_debug ("perl_config: loading perl plugin \"%s\"", value);
967                 Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
968                                 Perl_newSVpv (perl, module_name, strlen (module_name)),
969                                 Nullsv);
970         }
971         else if (0 == strcasecmp (key, "BaseName")) {
972                 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
973                 strncpy (base_name, value, sizeof (base_name));
974                 base_name[sizeof (base_name) - 1] = '\0';
975         }
976         else if (0 == strcasecmp (key, "IncludeDir")) {
977                 Perl_av_unshift (perl, GvAVn (PL_incgv), 1);
978                 Perl_av_store (perl, GvAVn (PL_incgv),
979                                 0, Perl_newSVpv (perl, value, strlen (value)));
980         }
981         else {
982                 return -1;
983         }
984         return 0;
985 } /* static int perl_config (char *, char *) */
987 static int perl_init (void)
989         PERL_SET_CONTEXT (perl);
990         return pplugin_call_all (PLUGIN_INIT);
991 } /* static int perl_init (void) */
993 static int perl_read (void)
995         PERL_SET_CONTEXT (perl);
996         return pplugin_call_all (PLUGIN_READ);
997 } /* static int perl_read (void) */
999 static int perl_write (const data_set_t *ds, const value_list_t *vl)
1001         PERL_SET_CONTEXT (perl);
1002         return pplugin_call_all (PLUGIN_WRITE, ds, vl);
1003 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1005 static void perl_log (int level, const char *msg)
1007         PERL_SET_CONTEXT (perl);
1008         pplugin_call_all (PLUGIN_LOG, level, msg);
1009         return;
1010 } /* static void perl_log (int, const char *) */
1012 static int perl_shutdown (void)
1014         int i   = 0;
1015         int ret = 0;
1017         PERL_SET_CONTEXT (perl);
1018         ret = pplugin_call_all (PLUGIN_SHUTDOWN);
1020         for (i = 0; i < PLUGIN_TYPES; ++i) {
1021                 if (0 < Perl_hv_iterinit (perl, plugins[i])) {
1022                         char *k = NULL;
1023                         I32  l  = 0;
1025                         while (NULL != Perl_hv_iternextsv (perl, plugins[i], &k, &l)) {
1026                                 pplugin_unregister (i, k);
1027                         }
1028                 }
1030                 Perl_hv_undef (perl, plugins[i]);
1031         }
1033         if (0 < Perl_hv_iterinit (perl, data_sets)) {
1034                 char *k = NULL;
1035                 I32  l  = 0;
1037                 while (NULL != Perl_hv_iternextsv (perl, data_sets, &k, &l)) {
1038                         pplugin_unregister_data_set (k);
1039                 }
1040         }
1042         Perl_hv_undef (perl, data_sets);
1044 #if COLLECT_DEBUG
1045         Perl_sv_report_used (perl);
1046 #endif /* COLLECT_DEBUG */
1048         perl_destruct (perl);
1049         perl_free (perl);
1051         PERL_SYS_TERM ();
1052         return ret;
1053 } /* static void perl_shutdown (void) */
1055 static void xs_init (pTHX)
1057         char *file = __FILE__;
1059         dXSUB_SYS;
1061         /* build the Collectd module into the perl interpreter */
1062         Perl_newXS (perl, "Collectd::bootstrap", boot_Collectd, file);
1064         /* enable usage of Perl modules using shared libraries */
1065         Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1066         return;
1067 } /* static void xs_init (pTHX) */
1069 /*
1070  * Create the perl interpreter and register it with collectd.
1071  */
1072 void module_register (modreg_e load)
1074         char *embed_argv[] = { "", "-e", "bootstrap Collectd \""VERSION"\"", NULL };
1075         int  embed_argc    = 3;
1077         int i = 0;
1079         log_debug ("module_register: Registering perl plugin...");
1081         PERL_SYS_INIT3 (&argc, &argv, &environ);
1083         if (NULL == (perl = perl_alloc ())) {
1084                 log_err ("module_register: Not enough memory.");
1085                 exit (3);
1086         }
1087         perl_construct (perl);
1089         PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1091         if (0 != perl_parse (perl, xs_init, embed_argc, embed_argv, NULL)) {
1092                 log_err ("module_register: Unable to bootstrap Collectd.");
1093                 exit (1);
1094         }
1095         perl_run (perl);
1097         for (i = 0; i < PLUGIN_TYPES; ++i)
1098                 plugins[i] = Perl_newHV (perl);
1100         data_sets = Perl_newHV (perl);
1102         plugin_register_log ("perl", perl_log);
1103         plugin_register_config ("perl", perl_config, config_keys, config_keys_num);
1104         plugin_register_init ("perl", perl_init);
1106         if (load & MR_READ)
1107                 plugin_register_read ("perl", perl_read);
1109         plugin_register_write ("perl", perl_write);
1110         plugin_register_shutdown ("perl", perl_shutdown);
1111         return;
1112 } /* void module_register (void) */
1114 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */