Code

configure, perl plugin: Check for ithreads support.
[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"
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 #if !defined(USE_ITHREADS)
46 # error "Perl does not support ithreads!"
47 #endif /* !defined(USE_ITHREADS) */
49 #define PLUGIN_INIT     0
50 #define PLUGIN_READ     1
51 #define PLUGIN_WRITE    2
52 #define PLUGIN_SHUTDOWN 3
53 #define PLUGIN_LOG      4
55 #define PLUGIN_TYPES    5
57 #define PLUGIN_DATASET  255
59 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
60 #define log_info(...) INFO ("perl: " __VA_ARGS__)
61 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
62 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
64 /* this is defined in DynaLoader.a */
65 void boot_DynaLoader (PerlInterpreter *, CV *);
67 static XS (Collectd_plugin_register_ds);
68 static XS (Collectd_plugin_unregister_ds);
69 static XS (Collectd_plugin_dispatch_values);
70 static XS (Collectd_plugin_log);
72 /*
73  * private variables
74  */
76 static PerlInterpreter *perl = NULL;
78 static int  perl_argc   = 0;
79 static char **perl_argv = NULL;
81 static char base_name[DATA_MAX_NAME_LEN] = "";
83 static struct {
84         char name[64];
85         XS ((*f));
86 } api[] =
87 {
88         { "Collectd::plugin_register_data_set",   Collectd_plugin_register_ds },
89         { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
90         { "Collectd::plugin_dispatch_values",     Collectd_plugin_dispatch_values },
91         { "Collectd::plugin_log",                 Collectd_plugin_log },
92         { "", NULL }
93 };
95 struct {
96         char name[64];
97         int  value;
98 } constants[] =
99 {
100         { "Collectd::TYPE_INIT",       PLUGIN_INIT },
101         { "Collectd::TYPE_READ",       PLUGIN_READ },
102         { "Collectd::TYPE_WRITE",      PLUGIN_WRITE },
103         { "Collectd::TYPE_SHUTDOWN",   PLUGIN_SHUTDOWN },
104         { "Collectd::TYPE_LOG",        PLUGIN_LOG },
105         { "Collectd::TYPE_DATASET",    PLUGIN_DATASET },
106         { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
107         { "Collectd::DS_TYPE_GAUGE",   DS_TYPE_GAUGE },
108         { "Collectd::LOG_ERR",         LOG_ERR },
109         { "Collectd::LOG_WARNING",     LOG_WARNING },
110         { "Collectd::LOG_NOTICE",      LOG_NOTICE },
111         { "Collectd::LOG_INFO",        LOG_INFO },
112         { "Collectd::LOG_DEBUG",       LOG_DEBUG },
113         { "", 0 }
114 };
116 /*
117  * Helper functions for data type conversion.
118  */
120 /*
121  * data source:
122  * [
123  *   {
124  *     name => $ds_name,
125  *     type => $ds_type,
126  *     min  => $ds_min,
127  *     max  => $ds_max
128  *   },
129  *   ...
130  * ]
131  */
132 static int hv2data_source (HV *hash, data_source_t *ds)
134         SV **tmp = NULL;
136         if ((NULL == hash) || (NULL == ds))
137                 return -1;
139         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "name", 4, 0))) {
140                 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
141                 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
142         }
143         else {
144                 log_err ("hv2data_source: No DS name given.");
145                 return -1;
146         }
148         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "type", 4, 0))) {
149                 ds->type = SvIV (*tmp);
151                 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
152                         log_err ("hv2data_source: Invalid DS type.");
153                         return -1;
154                 }
155         }
156         else {
157                 ds->type = DS_TYPE_COUNTER;
158         }
160         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "min", 3, 0)))
161                 ds->min = SvNV (*tmp);
162         else
163                 ds->min = NAN;
165         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "max", 3, 0)))
166                 ds->max = SvNV (*tmp);
167         else
168                 ds->max = NAN;
169         return 0;
170 } /* static data_source_t *hv2data_source (HV *) */
172 static int av2value (char *name, AV *array, value_t *value, int len)
174         const data_set_t *ds;
176         int i = 0;
178         if ((NULL == name) || (NULL == array) || (NULL == value))
179                 return -1;
181         if (Perl_av_len (perl, array) < len - 1)
182                 len = Perl_av_len (perl, array) + 1;
184         if (0 >= len)
185                 return -1;
187         ds = plugin_get_ds (name);
188         if (NULL == ds) {
189                 log_err ("av2value: Unknown dataset \"%s\"", name);
190                 return -1;
191         }
193         if (ds->ds_num < len) {
194                 log_warn ("av2value: Value length exceeds data set length.");
195                 len = ds->ds_num;
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->ds[i].type)
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 *) */
316 /*
317  * Internal functions.
318  */
320 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
321         int status = 0;
322         if (base_name[0] == '\0')
323                 status = snprintf (buf, buf_len, "%s", module);
324         else
325                 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
326         if ((status < 0) || (status >= buf_len))
327                 return (NULL);
328         buf[buf_len - 1] = '\0';
329         return (buf);
330 } /* char *get_module_name */
332 /*
333  * Add a plugin's data set definition.
334  */
335 static int pplugin_register_data_set (char *name, AV *dataset)
337         int len = -1;
338         int i   = 0;
340         data_source_t *ds  = NULL;
341         data_set_t    *set = NULL;
343         if ((NULL == name) || (NULL == dataset))
344                 return -1;
346         len = Perl_av_len (perl, dataset);
348         if (-1 == len)
349                 return -1;
351         ds  = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
352         set = (data_set_t *)smalloc (sizeof (data_set_t));
354         for (i = 0; i <= len; ++i) {
355                 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
357                 if (NULL == elem)
358                         return -1;
360                 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
361                         log_err ("pplugin_register_data_set: Invalid data source.");
362                         return -1;
363                 }
365                 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
366                         return -1;
368                 log_debug ("pplugin_register_data_set: "
369                                 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
370                                 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
371         }
373         strncpy (set->type, name, DATA_MAX_NAME_LEN);
374         set->type[DATA_MAX_NAME_LEN - 1] = '\0';
376         set->ds_num = len + 1;
377         set->ds = ds;
378         return plugin_register_data_set (set);
379 } /* static int pplugin_register_data_set (char *, SV *) */
381 /*
382  * Remove a plugin's data set definition.
383  */
384 static int pplugin_unregister_data_set (char *name)
386         if (NULL == name)
387                 return 0;
388         return plugin_unregister_data_set (name);
389 } /* static int pplugin_unregister_data_set (char *) */
391 /*
392  * Submit the values to the write functions.
393  *
394  * value list:
395  * {
396  *   values => [ @values ],
397  *   time   => $time,
398  *   host   => $host,
399  *   plugin => $plugin,
400  *   plugin_instance => $pinstance,
401  *   type_instance   => $tinstance,
402  * }
403  */
404 static int pplugin_dispatch_values (char *name, HV *values)
406         value_list_t list = VALUE_LIST_INIT;
407         value_t      *val = NULL;
409         SV **tmp = NULL;
411         int ret = 0;
413         if ((NULL == name) || (NULL == values))
414                 return -1;
416         if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
417                         || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
418                 log_err ("pplugin_dispatch_values: No valid values given.");
419                 return -1;
420         }
422         {
423                 AV  *array = (AV *)SvRV (*tmp);
424                 int len    = Perl_av_len (perl, array) + 1;
426                 if (len <= 0)
427                         return -1;
429                 val = (value_t *)smalloc (len * sizeof (value_t));
431                 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
432                 list.values = val;
434                 if (-1 == list.values_len) {
435                         sfree (val);
436                         return -1;
437                 }
438         }
440         if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
441                 list.time = (time_t)SvIV (*tmp);
442         }
443         else {
444                 list.time = time (NULL);
445         }
447         if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
448                 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
449                 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
450         }
451         else {
452                 strcpy (list.host, hostname_g);
453         }
455         if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
456                 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
457                 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
458         }
460         if (NULL != (tmp = Perl_hv_fetch (perl, values,
461                         "plugin_instance", 15, 0))) {
462                 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
463                 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
464         }
466         if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
467                 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
468                 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
469         }
471         ret = plugin_dispatch_values (name, &list);
473         sfree (val);
474         return ret;
475 } /* static int pplugin_dispatch_values (char *, HV *) */
477 /*
478  * Call all working functions of the given type.
479  */
480 static int pplugin_call_all (int type, ...)
482         int retvals = 0;
484         va_list ap;
485         int ret = 0;
487         dSP;
489         if ((type < 0) || (type >= PLUGIN_TYPES))
490                 return -1;
492         va_start (ap, type);
494         ENTER;
495         SAVETMPS;
497         PUSHMARK (SP);
499         XPUSHs (sv_2mortal (Perl_newSViv (perl, (IV)type)));
501         if (PLUGIN_WRITE == type) {
502                 /*
503                  * $_[0] = $plugin_type;
504                  *
505                  * $_[1] =
506                  * [
507                  *   {
508                  *     name => $ds_name,
509                  *     type => $ds_type,
510                  *     min  => $ds_min,
511                  *     max  => $ds_max
512                  *   },
513                  *   ...
514                  * ];
515                  *
516                  * $_[2] =
517                  * {
518                  *   values => [ $v1, ... ],
519                  *   time   => $time,
520                  *   host   => $hostname,
521                  *   plugin => $plugin,
522                  *   plugin_instance => $instance,
523                  *   type_instance   => $type_instance
524                  * };
525                  */
526                 data_set_t   *ds;
527                 value_list_t *vl;
529                 AV *pds = Perl_newAV (perl);
530                 HV *pvl = Perl_newHV (perl);
532                 ds = va_arg (ap, data_set_t *);
533                 vl = va_arg (ap, value_list_t *);
535                 if (-1 == data_set2av (ds, pds))
536                         return -1;
538                 if (-1 == value_list2hv (vl, ds, pvl))
539                         return -1;
541                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
542                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
543                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
544         }
545         else if (PLUGIN_LOG == type) {
546                 /*
547                  * $_[0] = $level;
548                  *
549                  * $_[1] = $message;
550                  */
551                 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
552                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
553         }
555         PUTBACK;
557         retvals = Perl_call_pv (perl, "Collectd::plugin_call_all", G_SCALAR);
559         SPAGAIN;
560         if (0 < retvals) {
561                 SV *tmp = POPs;
562                 if (! SvTRUE (tmp))
563                         ret = -1;
564         }
566         PUTBACK;
567         FREETMPS;
568         LEAVE;
570         va_end (ap);
571         return ret;
572 } /* static int pplugin_call_all (int, ...) */
574 /*
575  * Exported Perl API.
576  */
578 /*
579  * Collectd::plugin_register_data_set (type, dataset).
580  *
581  * type:
582  *   type of the dataset
583  *
584  * dataset:
585  *   dataset to be registered
586  */
587 static XS (Collectd_plugin_register_ds)
589         SV  *data = NULL;
590         int ret   = 0;
592         dXSARGS;
594         if (2 != items) {
595                 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
596                 XSRETURN_EMPTY;
597         }
599         log_debug ("Collectd::plugin_register_data_set: "
600                         "type = \"%s\", dataset = \"%s\"",
601                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
603         data = ST (1);
605         if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
606                 ret = pplugin_register_data_set (SvPV_nolen (ST (0)),
607                                 (AV *)SvRV (data));
608         }
609         else {
610                 log_err ("Collectd::plugin_register_data_set: Invalid data.");
611                 XSRETURN_EMPTY;
612         }
614         if (0 == ret)
615                 XSRETURN_YES;
616         else
617                 XSRETURN_EMPTY;
618 } /* static XS (Collectd_plugin_register_ds) */
620 /*
621  * Collectd::plugin_unregister_data_set (type).
622  *
623  * type:
624  *   type of the dataset
625  */
626 static XS (Collectd_plugin_unregister_ds)
628         dXSARGS;
630         if (1 != items) {
631                 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
632                 XSRETURN_EMPTY;
633         }
635         log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
636                         SvPV_nolen (ST (0)));
638         if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
639                 XSRETURN_YES;
640         else
641                 XSRETURN_EMPTY;
642 } /* static XS (Collectd_plugin_register_ds) */
644 /*
645  * Collectd::plugin_dispatch_values (name, values).
646  *
647  * name:
648  *   name of the plugin
649  *
650  * values:
651  *   value list to submit
652  */
653 static XS (Collectd_plugin_dispatch_values)
655         SV *values = NULL;
657         int ret = 0;
659         dXSARGS;
661         if (2 != items) {
662                 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
663                 XSRETURN_EMPTY;
664         }
666         log_debug ("Collectd::plugin_dispatch_values: "
667                         "name = \"%s\", values=\"%s\"",
668                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
670         values = ST (1);
672         if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
673                 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
674                 XSRETURN_EMPTY;
675         }
677         if ((NULL == ST (0)) || (NULL == values))
678                 XSRETURN_EMPTY;
680         ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
682         if (0 == ret)
683                 XSRETURN_YES;
684         else
685                 XSRETURN_EMPTY;
686 } /* static XS (Collectd_plugin_dispatch_values) */
688 /*
689  * Collectd::plugin_log (level, message).
690  *
691  * level:
692  *   log level (LOG_DEBUG, ... LOG_ERR)
693  *
694  * message:
695  *   log message
696  */
697 static XS (Collectd_plugin_log)
699         dXSARGS;
701         if (2 != items) {
702                 log_err ("Usage: Collectd::plugin_log(level, message)");
703                 XSRETURN_EMPTY;
704         }
706         plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
707         XSRETURN_YES;
708 } /* static XS (Collectd_plugin_log) */
710 /*
711  * Interface to collectd.
712  */
714 static int perl_init (void)
716         if (NULL == perl)
717                 return 0;
719         PERL_SET_CONTEXT (perl);
720         return pplugin_call_all (PLUGIN_INIT);
721 } /* static int perl_init (void) */
723 static int perl_read (void)
725         if (NULL == perl)
726                 return 0;
728         PERL_SET_CONTEXT (perl);
729         return pplugin_call_all (PLUGIN_READ);
730 } /* static int perl_read (void) */
732 static int perl_write (const data_set_t *ds, const value_list_t *vl)
734         if (NULL == perl)
735                 return 0;
737         PERL_SET_CONTEXT (perl);
738         return pplugin_call_all (PLUGIN_WRITE, ds, vl);
739 } /* static int perl_write (const data_set_t *, const value_list_t *) */
741 static void perl_log (int level, const char *msg)
743         if (NULL == perl)
744                 return;
746         PERL_SET_CONTEXT (perl);
747         pplugin_call_all (PLUGIN_LOG, level, msg);
748         return;
749 } /* static void perl_log (int, const char *) */
751 static int perl_shutdown (void)
753         int ret = 0;
755         plugin_unregister_complex_config ("perl");
757         if (NULL == perl)
758                 return 0;
760         plugin_unregister_log ("perl");
761         plugin_unregister_init ("perl");
762         plugin_unregister_read ("perl");
763         plugin_unregister_write ("perl");
765         PERL_SET_CONTEXT (perl);
766         ret = pplugin_call_all (PLUGIN_SHUTDOWN);
768 #if COLLECT_DEBUG
769         Perl_sv_report_used (perl);
770 #endif /* COLLECT_DEBUG */
772         perl_destruct (perl);
773         perl_free (perl);
774         perl = NULL;
776         PERL_SYS_TERM ();
778         plugin_unregister_shutdown ("perl");
779         return ret;
780 } /* static void perl_shutdown (void) */
782 /* bootstrap the Collectd module */
783 static void xs_init (pTHX)
785         HV   *stash = NULL;
786         char *file  = __FILE__;
788         int i = 0;
790         dXSUB_SYS;
792         /* enable usage of Perl modules using shared libraries */
793         Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
795         /* register API */
796         for (i = 0; NULL != api[i].f; ++i)
797                 Perl_newXS (perl, api[i].name, api[i].f, file);
799         stash = Perl_gv_stashpv (perl, "Collectd", 1);
801         /* export "constants" */
802         for (i = 0; '\0' != constants[i].name[0]; ++i)
803                 Perl_newCONSTSUB (perl, stash, constants[i].name,
804                                 Perl_newSViv (perl, constants[i].value));
805         return;
806 } /* static void xs_init (pTHX) */
808 /* Initialize the global Perl interpreter. */
809 static int init_pi (int argc, char **argv)
811         if (NULL != perl)
812                 return 0;
814         log_info ("Initializing Perl interpreter...");
815 #if COLLECT_DEBUG
816         {
817                 int i = 0;
819                 for (i = 0; i < argc; ++i)
820                         log_debug ("argv[%i] = \"%s\"", i, argv[i]);
821         }
822 #endif /* COLLECT_DEBUG */
824         PERL_SYS_INIT3 (&argc, &argv, &environ);
826         if (NULL == (perl = perl_alloc ())) {
827                 log_err ("module_register: Not enough memory.");
828                 exit (3);
829         }
830         perl_construct (perl);
832         PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
834         if (0 != perl_parse (perl, xs_init, argc, argv, NULL)) {
835                 log_err ("module_register: Unable to bootstrap Collectd.");
836                 exit (1);
837         }
839         /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
840         Perl_sv_setpv (perl, Perl_get_sv (perl, "0", 0), "collectd");
842         perl_run (perl);
844         plugin_register_log ("perl", perl_log);
845         plugin_register_init ("perl", perl_init);
847         plugin_register_read ("perl", perl_read);
849         plugin_register_write ("perl", perl_write);
850         plugin_register_shutdown ("perl", perl_shutdown);
851         return 0;
852 } /* static int init_pi (const char **, const int) */
854 /*
855  * LoadPlugin "<Plugin>"
856  */
857 static int perl_config_loadplugin (oconfig_item_t *ci)
859         char module_name[DATA_MAX_NAME_LEN];
861         char *value = NULL;
863         if ((0 != ci->children_num) || (1 != ci->values_num)
864                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
865                 return 1;
867         value = ci->values[0].value.string;
869         if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
870                 log_err ("Invalid module name %s", value);
871                 return (1);
872         }
874         init_pi (perl_argc, perl_argv);
876         log_debug ("perl_config: loading perl plugin \"%s\"", value);
877         Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
878                         Perl_newSVpv (perl, module_name, strlen (module_name)),
879                         Nullsv);
880         return 0;
881 } /* static int perl_config_loadplugin (oconfig_item_it *) */
883 /*
884  * BaseName "<Name>"
885  */
886 static int perl_config_basename (oconfig_item_t *ci)
888         char *value = NULL;
890         if ((0 != ci->children_num) || (1 != ci->values_num)
891                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
892                 return 1;
894         value = ci->values[0].value.string;
896         log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
897         strncpy (base_name, value, sizeof (base_name));
898         base_name[sizeof (base_name) - 1] = '\0';
899         return 0;
900 } /* static int perl_config_basename (oconfig_item_it *) */
902 /*
903  * EnableDebugger "<Package>"|""
904  */
905 static int perl_config_enabledebugger (oconfig_item_t *ci)
907         char *value = NULL;
909         if ((0 != ci->children_num) || (1 != ci->values_num)
910                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
911                 return 1;
913         value = ci->values[0].value.string;
915         perl_argv = (char **)realloc (perl_argv,
916                         (++perl_argc + 1) * sizeof (char *));
918         if (NULL == perl_argv) {
919                 log_err ("perl_config: Not enough memory.");
920                 exit (3);
921         }
923         if ('\0' == value[0]) {
924                 perl_argv[perl_argc - 1] = "-d";
925         }
926         else {
927                 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
928                 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
929                 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
930         }
932         perl_argv[perl_argc] = NULL;
933         return 0;
934 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
936 /*
937  * IncludeDir "<Dir>"
938  */
939 static int perl_config_includedir (oconfig_item_t *ci)
941         char *value = NULL;
943         if ((0 != ci->children_num) || (1 != ci->values_num)
944                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
945                 return 1;
947         value = ci->values[0].value.string;
949         if (NULL == perl) {
950                 perl_argv = (char **)realloc (perl_argv,
951                                 (++perl_argc + 1) * sizeof (char *));
953                 if (NULL == perl_argv) {
954                         log_err ("perl_config: Not enough memory.");
955                         exit (3);
956                 }
958                 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
959                 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
960                 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
962                 perl_argv[perl_argc] = NULL;
963         }
964         else {
965                 /* prepend the directory to @INC */
966                 Perl_av_unshift (perl, GvAVn (PL_incgv), 1);
967                 Perl_av_store (perl, GvAVn (PL_incgv),
968                                 0, Perl_newSVpv (perl, value, strlen (value)));
969         }
970         return 0;
971 } /* static int perl_config_includedir (oconfig_item_it *) */
973 static int perl_config (oconfig_item_t *ci)
975         int i = 0;
977         for (i = 0; i < ci->children_num; ++i) {
978                 oconfig_item_t *c = ci->children + i;
980                 if (0 == strcasecmp (c->key, "LoadPlugin"))
981                         perl_config_loadplugin (c);
982                 else if (0 == strcasecmp (c->key, "BaseName"))
983                         perl_config_basename (c);
984                 else if (0 == strcasecmp (c->key, "EnableDebugger"))
985                         perl_config_enabledebugger (c);
986                 else if (0 == strcasecmp (c->key, "IncludeDir"))
987                         perl_config_includedir (c);
988                 else
989                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
990         }
991         return 0;
992 } /* static int perl_config (oconfig_item_t *) */
994 void module_register (void)
996         perl_argc = 4;
997         perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
999         /* default options for the Perl interpreter */
1000         perl_argv[0] = "";
1001         perl_argv[1] = "-MCollectd";
1002         perl_argv[2] = "-e";
1003         perl_argv[3] = "1";
1004         perl_argv[4] = NULL;
1006         plugin_register_complex_config ("perl", perl_config);
1007         return;
1008 } /* void module_register (void) */
1010 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */