perl plugin: Updated copyright information.
[collectd.git] / src / perl.c
1 /**
2  * collectd - src/perl.c
3  * Copyright (C) 2007, 2008  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  **/
21
22 /*
23  * This plugin embeds a Perl interpreter into collectd and provides an
24  * interface for collectd plugins written in perl.
25  */
26
27 #include "collectd.h"
28
29 #include "configfile.h"
30
31 #include <EXTERN.h>
32 #include <perl.h>
33
34 #include <XSUB.h>
35
36 /* Some versions of Perl define their own version of DEBUG... :-/ */
37 #ifdef DEBUG
38 # undef DEBUG
39 #endif /* DEBUG */
40
41 /* ... while we want the definition found in plugin.h. */
42 #include "plugin.h"
43 #include "common.h"
44
45 #define PLUGIN_INIT     0
46 #define PLUGIN_READ     1
47 #define PLUGIN_WRITE    2
48 #define PLUGIN_SHUTDOWN 3
49 #define PLUGIN_LOG      4
50
51 #define PLUGIN_TYPES    5
52
53 #define PLUGIN_DATASET  255
54
55 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
56 #define log_info(...) INFO ("perl: " __VA_ARGS__)
57 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
58 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
59
60 /* this is defined in DynaLoader.a */
61 void boot_DynaLoader (PerlInterpreter *, CV *);
62
63 static XS (Collectd_plugin_register_ds);
64 static XS (Collectd_plugin_unregister_ds);
65 static XS (Collectd_plugin_dispatch_values);
66 static XS (Collectd_plugin_log);
67
68 /*
69  * private variables
70  */
71
72 static PerlInterpreter *perl = NULL;
73
74 static int  perl_argc   = 0;
75 static char **perl_argv = NULL;
76
77 static char base_name[DATA_MAX_NAME_LEN] = "";
78
79 static struct {
80         char name[64];
81         XS ((*f));
82 } api[] =
83 {
84         { "Collectd::plugin_register_data_set",   Collectd_plugin_register_ds },
85         { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
86         { "Collectd::plugin_dispatch_values",     Collectd_plugin_dispatch_values },
87         { "Collectd::plugin_log",                 Collectd_plugin_log },
88         { "", NULL }
89 };
90
91 struct {
92         char name[64];
93         int  value;
94 } constants[] =
95 {
96         { "Collectd::TYPE_INIT",       PLUGIN_INIT },
97         { "Collectd::TYPE_READ",       PLUGIN_READ },
98         { "Collectd::TYPE_WRITE",      PLUGIN_WRITE },
99         { "Collectd::TYPE_SHUTDOWN",   PLUGIN_SHUTDOWN },
100         { "Collectd::TYPE_LOG",        PLUGIN_LOG },
101         { "Collectd::TYPE_DATASET",    PLUGIN_DATASET },
102         { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
103         { "Collectd::DS_TYPE_GAUGE",   DS_TYPE_GAUGE },
104         { "Collectd::LOG_ERR",         LOG_ERR },
105         { "Collectd::LOG_WARNING",     LOG_WARNING },
106         { "Collectd::LOG_NOTICE",      LOG_NOTICE },
107         { "Collectd::LOG_INFO",        LOG_INFO },
108         { "Collectd::LOG_DEBUG",       LOG_DEBUG },
109         { "", 0 }
110 };
111
112 /*
113  * Helper functions for data type conversion.
114  */
115
116 /*
117  * data source:
118  * [
119  *   {
120  *     name => $ds_name,
121  *     type => $ds_type,
122  *     min  => $ds_min,
123  *     max  => $ds_max
124  *   },
125  *   ...
126  * ]
127  */
128 static int hv2data_source (HV *hash, data_source_t *ds)
129 {
130         SV **tmp = NULL;
131
132         if ((NULL == hash) || (NULL == ds))
133                 return -1;
134
135         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "name", 4, 0))) {
136                 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
137                 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
138         }
139         else {
140                 log_err ("hv2data_source: No DS name given.");
141                 return -1;
142         }
143
144         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "type", 4, 0))) {
145                 ds->type = SvIV (*tmp);
146
147                 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
148                         log_err ("hv2data_source: Invalid DS type.");
149                         return -1;
150                 }
151         }
152         else {
153                 ds->type = DS_TYPE_COUNTER;
154         }
155
156         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "min", 3, 0)))
157                 ds->min = SvNV (*tmp);
158         else
159                 ds->min = NAN;
160
161         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "max", 3, 0)))
162                 ds->max = SvNV (*tmp);
163         else
164                 ds->max = NAN;
165         return 0;
166 } /* static data_source_t *hv2data_source (HV *) */
167
168 static int av2value (char *name, AV *array, value_t *value, int len)
169 {
170         const data_set_t *ds;
171
172         int i = 0;
173
174         if ((NULL == name) || (NULL == array) || (NULL == value))
175                 return -1;
176
177         if (Perl_av_len (perl, array) < len - 1)
178                 len = Perl_av_len (perl, array) + 1;
179
180         if (0 >= len)
181                 return -1;
182
183         ds = plugin_get_ds (name);
184         if (NULL == ds) {
185                 log_err ("av2value: Unknown dataset \"%s\"", name);
186                 return -1;
187         }
188
189         if (ds->ds_num < len) {
190                 log_warn ("av2value: Value length exceeds data set length.");
191                 len = ds->ds_num;
192         }
193
194         for (i = 0; i < len; ++i) {
195                 SV **tmp = Perl_av_fetch (perl, array, i, 0);
196
197                 if (NULL != tmp) {
198                         if (DS_TYPE_COUNTER == ds->ds[i].type)
199                                 value[i].counter = SvIV (*tmp);
200                         else
201                                 value[i].gauge = SvNV (*tmp);
202                 }
203                 else {
204                         return -1;
205                 }
206         }
207         return len;
208 } /* static int av2value (char *, AV *, value_t *, int) */
209
210 static int data_set2av (data_set_t *ds, AV *array)
211 {
212         int i = 0;
213
214         if ((NULL == ds) || (NULL == array))
215                 return -1;
216
217         Perl_av_extend (perl, array, ds->ds_num);
218
219         for (i = 0; i < ds->ds_num; ++i) {
220                 HV *source = Perl_newHV (perl);
221
222                 if (NULL == Perl_hv_store (perl, source, "name", 4,
223                                 Perl_newSVpv (perl, ds->ds[i].name, 0), 0))
224                         return -1;
225
226                 if (NULL == Perl_hv_store (perl, source, "type", 4,
227                                 Perl_newSViv (perl, ds->ds[i].type), 0))
228                         return -1;
229
230                 if (! isnan (ds->ds[i].min))
231                         if (NULL == Perl_hv_store (perl, source, "min", 3,
232                                         Perl_newSVnv (perl, ds->ds[i].min), 0))
233                                 return -1;
234
235                 if (! isnan (ds->ds[i].max))
236                         if (NULL == Perl_hv_store (perl, source, "max", 3,
237                                         Perl_newSVnv (perl, ds->ds[i].max), 0))
238                                 return -1;
239
240                 if (NULL == Perl_av_store (perl, array, i,
241                                 Perl_newRV_noinc (perl, (SV *)source)))
242                         return -1;
243         }
244         return 0;
245 } /* static int data_set2av (data_set_t *, AV *) */
246
247 static int value_list2hv (value_list_t *vl, data_set_t *ds, HV *hash)
248 {
249         AV *values = NULL;
250
251         int i   = 0;
252         int len = 0;
253
254         if ((NULL == vl) || (NULL == ds) || (NULL == hash))
255                 return -1;
256
257         len = vl->values_len;
258
259         if (ds->ds_num < len) {
260                 log_warn ("value2av: Value length exceeds data set length.");
261                 len = ds->ds_num;
262         }
263
264         values = Perl_newAV (perl);
265         Perl_av_extend (perl, values, len - 1);
266
267         for (i = 0; i < len; ++i) {
268                 SV *val = NULL;
269
270                 if (DS_TYPE_COUNTER == ds->ds[i].type)
271                         val = Perl_newSViv (perl, vl->values[i].counter);
272                 else
273                         val = Perl_newSVnv (perl, vl->values[i].gauge);
274
275                 if (NULL == Perl_av_store (perl, values, i, val)) {
276                         Perl_av_undef (perl, values);
277                         return -1;
278                 }
279         }
280
281         if (NULL == Perl_hv_store (perl, hash, "values", 6,
282                         Perl_newRV_noinc (perl, (SV *)values), 0))
283                 return -1;
284
285         if (0 != vl->time)
286                 if (NULL == Perl_hv_store (perl, hash, "time", 4,
287                                 Perl_newSViv (perl, vl->time), 0))
288                         return -1;
289
290         if ('\0' != vl->host[0])
291                 if (NULL == Perl_hv_store (perl, hash, "host", 4,
292                                 Perl_newSVpv (perl, vl->host, 0), 0))
293                         return -1;
294
295         if ('\0' != vl->plugin[0])
296                 if (NULL == Perl_hv_store (perl, hash, "plugin", 6,
297                                 Perl_newSVpv (perl, vl->plugin, 0), 0))
298                         return -1;
299
300         if ('\0' != vl->plugin_instance[0])
301                 if (NULL == Perl_hv_store (perl, hash, "plugin_instance", 15,
302                                 Perl_newSVpv (perl, vl->plugin_instance, 0), 0))
303                         return -1;
304
305         if ('\0' != vl->type_instance[0])
306                 if (NULL == Perl_hv_store (perl, hash, "type_instance", 13,
307                                 Perl_newSVpv (perl, vl->type_instance, 0), 0))
308                         return -1;
309         return 0;
310 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
311
312 /*
313  * Internal functions.
314  */
315
316 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
317         int status = 0;
318         if (base_name[0] == '\0')
319                 status = snprintf (buf, buf_len, "%s", module);
320         else
321                 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
322         if ((status < 0) || (status >= buf_len))
323                 return (NULL);
324         buf[buf_len - 1] = '\0';
325         return (buf);
326 } /* char *get_module_name */
327
328 /*
329  * Add a plugin's data set definition.
330  */
331 static int pplugin_register_data_set (char *name, AV *dataset)
332 {
333         int len = -1;
334         int i   = 0;
335
336         data_source_t *ds  = NULL;
337         data_set_t    *set = NULL;
338
339         if ((NULL == name) || (NULL == dataset))
340                 return -1;
341
342         len = Perl_av_len (perl, dataset);
343
344         if (-1 == len)
345                 return -1;
346
347         ds  = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
348         set = (data_set_t *)smalloc (sizeof (data_set_t));
349
350         for (i = 0; i <= len; ++i) {
351                 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
352
353                 if (NULL == elem)
354                         return -1;
355
356                 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
357                         log_err ("pplugin_register_data_set: Invalid data source.");
358                         return -1;
359                 }
360
361                 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
362                         return -1;
363
364                 log_debug ("pplugin_register_data_set: "
365                                 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
366                                 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
367         }
368
369         strncpy (set->type, name, DATA_MAX_NAME_LEN);
370         set->type[DATA_MAX_NAME_LEN - 1] = '\0';
371
372         set->ds_num = len + 1;
373         set->ds = ds;
374         return plugin_register_data_set (set);
375 } /* static int pplugin_register_data_set (char *, SV *) */
376
377 /*
378  * Remove a plugin's data set definition.
379  */
380 static int pplugin_unregister_data_set (char *name)
381 {
382         if (NULL == name)
383                 return 0;
384         return plugin_unregister_data_set (name);
385 } /* static int pplugin_unregister_data_set (char *) */
386
387 /*
388  * Submit the values to the write functions.
389  *
390  * value list:
391  * {
392  *   values => [ @values ],
393  *   time   => $time,
394  *   host   => $host,
395  *   plugin => $plugin,
396  *   plugin_instance => $pinstance,
397  *   type_instance   => $tinstance,
398  * }
399  */
400 static int pplugin_dispatch_values (char *name, HV *values)
401 {
402         value_list_t list = VALUE_LIST_INIT;
403         value_t      *val = NULL;
404
405         SV **tmp = NULL;
406
407         int ret = 0;
408
409         if ((NULL == name) || (NULL == values))
410                 return -1;
411
412         if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
413                         || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
414                 log_err ("pplugin_dispatch_values: No valid values given.");
415                 return -1;
416         }
417
418         {
419                 AV  *array = (AV *)SvRV (*tmp);
420                 int len    = Perl_av_len (perl, array) + 1;
421
422                 if (len <= 0)
423                         return -1;
424
425                 val = (value_t *)smalloc (len * sizeof (value_t));
426
427                 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
428                 list.values = val;
429
430                 if (-1 == list.values_len) {
431                         sfree (val);
432                         return -1;
433                 }
434         }
435
436         if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
437                 list.time = (time_t)SvIV (*tmp);
438         }
439         else {
440                 list.time = time (NULL);
441         }
442
443         if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
444                 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
445                 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
446         }
447         else {
448                 strcpy (list.host, hostname_g);
449         }
450
451         if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
452                 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
453                 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
454         }
455
456         if (NULL != (tmp = Perl_hv_fetch (perl, values,
457                         "plugin_instance", 15, 0))) {
458                 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
459                 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
460         }
461
462         if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
463                 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
464                 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
465         }
466
467         ret = plugin_dispatch_values (name, &list);
468
469         sfree (val);
470         return ret;
471 } /* static int pplugin_dispatch_values (char *, HV *) */
472
473 /*
474  * Call all working functions of the given type.
475  */
476 static int pplugin_call_all (int type, ...)
477 {
478         int retvals = 0;
479
480         va_list ap;
481         int ret = 0;
482
483         dSP;
484
485         if ((type < 0) || (type >= PLUGIN_TYPES))
486                 return -1;
487
488         va_start (ap, type);
489
490         ENTER;
491         SAVETMPS;
492
493         PUSHMARK (SP);
494
495         XPUSHs (sv_2mortal (Perl_newSViv (perl, (IV)type)));
496
497         if (PLUGIN_WRITE == type) {
498                 /*
499                  * $_[0] = $plugin_type;
500                  *
501                  * $_[1] =
502                  * [
503                  *   {
504                  *     name => $ds_name,
505                  *     type => $ds_type,
506                  *     min  => $ds_min,
507                  *     max  => $ds_max
508                  *   },
509                  *   ...
510                  * ];
511                  *
512                  * $_[2] =
513                  * {
514                  *   values => [ $v1, ... ],
515                  *   time   => $time,
516                  *   host   => $hostname,
517                  *   plugin => $plugin,
518                  *   plugin_instance => $instance,
519                  *   type_instance   => $type_instance
520                  * };
521                  */
522                 data_set_t   *ds;
523                 value_list_t *vl;
524
525                 AV *pds = Perl_newAV (perl);
526                 HV *pvl = Perl_newHV (perl);
527
528                 ds = va_arg (ap, data_set_t *);
529                 vl = va_arg (ap, value_list_t *);
530
531                 if (-1 == data_set2av (ds, pds))
532                         return -1;
533
534                 if (-1 == value_list2hv (vl, ds, pvl))
535                         return -1;
536
537                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
538                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
539                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
540         }
541         else if (PLUGIN_LOG == type) {
542                 /*
543                  * $_[0] = $level;
544                  *
545                  * $_[1] = $message;
546                  */
547                 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
548                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
549         }
550
551         PUTBACK;
552
553         retvals = Perl_call_pv (perl, "Collectd::plugin_call_all", G_SCALAR);
554
555         SPAGAIN;
556         if (0 < retvals) {
557                 SV *tmp = POPs;
558                 if (! SvTRUE (tmp))
559                         ret = -1;
560         }
561
562         PUTBACK;
563         FREETMPS;
564         LEAVE;
565
566         va_end (ap);
567         return ret;
568 } /* static int pplugin_call_all (int, ...) */
569
570 /*
571  * Exported Perl API.
572  */
573
574 /*
575  * Collectd::plugin_register_data_set (type, dataset).
576  *
577  * type:
578  *   type of the dataset
579  *
580  * dataset:
581  *   dataset to be registered
582  */
583 static XS (Collectd_plugin_register_ds)
584 {
585         SV  *data = NULL;
586         int ret   = 0;
587
588         dXSARGS;
589
590         if (2 != items) {
591                 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
592                 XSRETURN_EMPTY;
593         }
594
595         log_debug ("Collectd::plugin_register_data_set: "
596                         "type = \"%s\", dataset = \"%s\"",
597                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
598
599         data = ST (1);
600
601         if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
602                 ret = pplugin_register_data_set (SvPV_nolen (ST (0)),
603                                 (AV *)SvRV (data));
604         }
605         else {
606                 log_err ("Collectd::plugin_register_data_set: Invalid data.");
607                 XSRETURN_EMPTY;
608         }
609
610         if (0 == ret)
611                 XSRETURN_YES;
612         else
613                 XSRETURN_EMPTY;
614 } /* static XS (Collectd_plugin_register_ds) */
615
616 /*
617  * Collectd::plugin_unregister_data_set (type).
618  *
619  * type:
620  *   type of the dataset
621  */
622 static XS (Collectd_plugin_unregister_ds)
623 {
624         dXSARGS;
625
626         if (1 != items) {
627                 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
628                 XSRETURN_EMPTY;
629         }
630
631         log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
632                         SvPV_nolen (ST (0)));
633
634         if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
635                 XSRETURN_YES;
636         else
637                 XSRETURN_EMPTY;
638 } /* static XS (Collectd_plugin_register_ds) */
639
640 /*
641  * Collectd::plugin_dispatch_values (name, values).
642  *
643  * name:
644  *   name of the plugin
645  *
646  * values:
647  *   value list to submit
648  */
649 static XS (Collectd_plugin_dispatch_values)
650 {
651         SV *values = NULL;
652
653         int ret = 0;
654
655         dXSARGS;
656
657         if (2 != items) {
658                 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
659                 XSRETURN_EMPTY;
660         }
661
662         log_debug ("Collectd::plugin_dispatch_values: "
663                         "name = \"%s\", values=\"%s\"",
664                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
665
666         values = ST (1);
667
668         if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
669                 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
670                 XSRETURN_EMPTY;
671         }
672
673         if ((NULL == ST (0)) || (NULL == values))
674                 XSRETURN_EMPTY;
675
676         ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
677
678         if (0 == ret)
679                 XSRETURN_YES;
680         else
681                 XSRETURN_EMPTY;
682 } /* static XS (Collectd_plugin_dispatch_values) */
683
684 /*
685  * Collectd::plugin_log (level, message).
686  *
687  * level:
688  *   log level (LOG_DEBUG, ... LOG_ERR)
689  *
690  * message:
691  *   log message
692  */
693 static XS (Collectd_plugin_log)
694 {
695         dXSARGS;
696
697         if (2 != items) {
698                 log_err ("Usage: Collectd::plugin_log(level, message)");
699                 XSRETURN_EMPTY;
700         }
701
702         plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
703         XSRETURN_YES;
704 } /* static XS (Collectd_plugin_log) */
705
706 /*
707  * Interface to collectd.
708  */
709
710 static int perl_init (void)
711 {
712         if (NULL == perl)
713                 return 0;
714
715         PERL_SET_CONTEXT (perl);
716         return pplugin_call_all (PLUGIN_INIT);
717 } /* static int perl_init (void) */
718
719 static int perl_read (void)
720 {
721         if (NULL == perl)
722                 return 0;
723
724         PERL_SET_CONTEXT (perl);
725         return pplugin_call_all (PLUGIN_READ);
726 } /* static int perl_read (void) */
727
728 static int perl_write (const data_set_t *ds, const value_list_t *vl)
729 {
730         if (NULL == perl)
731                 return 0;
732
733         PERL_SET_CONTEXT (perl);
734         return pplugin_call_all (PLUGIN_WRITE, ds, vl);
735 } /* static int perl_write (const data_set_t *, const value_list_t *) */
736
737 static void perl_log (int level, const char *msg)
738 {
739         if (NULL == perl)
740                 return;
741
742         PERL_SET_CONTEXT (perl);
743         pplugin_call_all (PLUGIN_LOG, level, msg);
744         return;
745 } /* static void perl_log (int, const char *) */
746
747 static int perl_shutdown (void)
748 {
749         int ret = 0;
750
751         plugin_unregister_complex_config ("perl");
752
753         if (NULL == perl)
754                 return 0;
755
756         plugin_unregister_log ("perl");
757         plugin_unregister_init ("perl");
758         plugin_unregister_read ("perl");
759         plugin_unregister_write ("perl");
760
761         PERL_SET_CONTEXT (perl);
762         ret = pplugin_call_all (PLUGIN_SHUTDOWN);
763
764 #if COLLECT_DEBUG
765         Perl_sv_report_used (perl);
766 #endif /* COLLECT_DEBUG */
767
768         perl_destruct (perl);
769         perl_free (perl);
770         perl = NULL;
771
772         PERL_SYS_TERM ();
773
774         plugin_unregister_shutdown ("perl");
775         return ret;
776 } /* static void perl_shutdown (void) */
777
778 /* bootstrap the Collectd module */
779 static void xs_init (pTHX)
780 {
781         HV   *stash = NULL;
782         char *file  = __FILE__;
783
784         int i = 0;
785
786         dXSUB_SYS;
787
788         /* enable usage of Perl modules using shared libraries */
789         Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
790
791         /* register API */
792         for (i = 0; NULL != api[i].f; ++i)
793                 Perl_newXS (perl, api[i].name, api[i].f, file);
794
795         stash = Perl_gv_stashpv (perl, "Collectd", 1);
796
797         /* export "constants" */
798         for (i = 0; '\0' != constants[i].name[0]; ++i)
799                 Perl_newCONSTSUB (perl, stash, constants[i].name,
800                                 Perl_newSViv (perl, constants[i].value));
801         return;
802 } /* static void xs_init (pTHX) */
803
804 /* Initialize the global Perl interpreter. */
805 static int init_pi (int argc, char **argv)
806 {
807         if (NULL != perl)
808                 return 0;
809
810         log_info ("Initializing Perl interpreter...");
811 #if COLLECT_DEBUG
812         {
813                 int i = 0;
814
815                 for (i = 0; i < argc; ++i)
816                         log_debug ("argv[%i] = \"%s\"", i, argv[i]);
817         }
818 #endif /* COLLECT_DEBUG */
819
820         PERL_SYS_INIT3 (&argc, &argv, &environ);
821
822         if (NULL == (perl = perl_alloc ())) {
823                 log_err ("module_register: Not enough memory.");
824                 exit (3);
825         }
826         perl_construct (perl);
827
828         PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
829
830         if (0 != perl_parse (perl, xs_init, argc, argv, NULL)) {
831                 log_err ("module_register: Unable to bootstrap Collectd.");
832                 exit (1);
833         }
834
835         /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
836         Perl_sv_setpv (perl, Perl_get_sv (perl, "0", 0), "collectd");
837
838         perl_run (perl);
839
840         plugin_register_log ("perl", perl_log);
841         plugin_register_init ("perl", perl_init);
842
843         plugin_register_read ("perl", perl_read);
844
845         plugin_register_write ("perl", perl_write);
846         plugin_register_shutdown ("perl", perl_shutdown);
847         return 0;
848 } /* static int init_pi (const char **, const int) */
849
850 /*
851  * LoadPlugin "<Plugin>"
852  */
853 static int perl_config_loadplugin (oconfig_item_t *ci)
854 {
855         char module_name[DATA_MAX_NAME_LEN];
856
857         char *value = NULL;
858
859         if ((0 != ci->children_num) || (1 != ci->values_num)
860                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
861                 return 1;
862
863         value = ci->values[0].value.string;
864
865         if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
866                 log_err ("Invalid module name %s", value);
867                 return (1);
868         }
869
870         init_pi (perl_argc, perl_argv);
871
872         log_debug ("perl_config: loading perl plugin \"%s\"", value);
873         Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
874                         Perl_newSVpv (perl, module_name, strlen (module_name)),
875                         Nullsv);
876         return 0;
877 } /* static int perl_config_loadplugin (oconfig_item_it *) */
878
879 /*
880  * BaseName "<Name>"
881  */
882 static int perl_config_basename (oconfig_item_t *ci)
883 {
884         char *value = NULL;
885
886         if ((0 != ci->children_num) || (1 != ci->values_num)
887                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
888                 return 1;
889
890         value = ci->values[0].value.string;
891
892         log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
893         strncpy (base_name, value, sizeof (base_name));
894         base_name[sizeof (base_name) - 1] = '\0';
895         return 0;
896 } /* static int perl_config_basename (oconfig_item_it *) */
897
898 /*
899  * EnableDebugger "<Package>"|""
900  */
901 static int perl_config_enabledebugger (oconfig_item_t *ci)
902 {
903         char *value = NULL;
904
905         if ((0 != ci->children_num) || (1 != ci->values_num)
906                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
907                 return 1;
908
909         value = ci->values[0].value.string;
910
911         perl_argv = (char **)realloc (perl_argv,
912                         (++perl_argc + 1) * sizeof (char *));
913
914         if (NULL == perl_argv) {
915                 log_err ("perl_config: Not enough memory.");
916                 exit (3);
917         }
918
919         if ('\0' == value[0]) {
920                 perl_argv[perl_argc - 1] = "-d";
921         }
922         else {
923                 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
924                 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
925                 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
926         }
927
928         perl_argv[perl_argc] = NULL;
929         return 0;
930 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
931
932 /*
933  * IncludeDir "<Dir>"
934  */
935 static int perl_config_includedir (oconfig_item_t *ci)
936 {
937         char *value = NULL;
938
939         if ((0 != ci->children_num) || (1 != ci->values_num)
940                         || (OCONFIG_TYPE_STRING != ci->values[0].type))
941                 return 1;
942
943         if (NULL == aTHX) {
944                 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
945                 return 1;
946         }
947
948         value = ci->values[0].value.string;
949
950         if (NULL == perl) {
951                 perl_argv = (char **)realloc (perl_argv,
952                                 (++perl_argc + 1) * sizeof (char *));
953
954                 if (NULL == perl_argv) {
955                         log_err ("perl_config: Not enough memory.");
956                         exit (3);
957                 }
958
959                 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
960                 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
961                 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
962
963                 perl_argv[perl_argc] = NULL;
964         }
965         else {
966                 /* prepend the directory to @INC */
967                 Perl_av_unshift (perl, GvAVn (PL_incgv), 1);
968                 Perl_av_store (perl, GvAVn (PL_incgv),
969                                 0, Perl_newSVpv (perl, value, strlen (value)));
970         }
971         return 0;
972 } /* static int perl_config_includedir (oconfig_item_it *) */
973
974 static int perl_config (oconfig_item_t *ci)
975 {
976         int i = 0;
977
978         for (i = 0; i < ci->children_num; ++i) {
979                 oconfig_item_t *c = ci->children + i;
980
981                 if (0 == strcasecmp (c->key, "LoadPlugin"))
982                         perl_config_loadplugin (c);
983                 else if (0 == strcasecmp (c->key, "BaseName"))
984                         perl_config_basename (c);
985                 else if (0 == strcasecmp (c->key, "EnableDebugger"))
986                         perl_config_enabledebugger (c);
987                 else if (0 == strcasecmp (c->key, "IncludeDir"))
988                         perl_config_includedir (c);
989                 else
990                         log_warn ("Ignoring unknown config key \"%s\".", c->key);
991         }
992         return 0;
993 } /* static int perl_config (oconfig_item_t *) */
994
995 void module_register (void)
996 {
997         perl_argc = 4;
998         perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
999
1000         /* default options for the Perl interpreter */
1001         perl_argv[0] = "";
1002         perl_argv[1] = "-MCollectd";
1003         perl_argv[2] = "-e";
1004         perl_argv[3] = "1";
1005         perl_argv[4] = NULL;
1006
1007         plugin_register_complex_config ("perl", perl_config);
1008         return;
1009 } /* void module_register (void) */
1010
1011 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
1012